JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: when password required, use new api
[wfpl.git] / stylus_helpers.styl
1 // This program is in the public domain within the United States. Additionally,
2 // we waive copyright and related rights in the work worldwide through the CC0
3 // 1.0 Universal public domain dedication, which can be found at
4 // http://creativecommons.org/publicdomain/zero/1.0/
5
6 // This file contains helpers for using stylus in your project.
7 //
8 // Put something like this in your styl.styl:
9 //     @require 'inc/wfpl/stylus-helpers.styl'
10
11
12 // set units to px if it doesn't have a unit already
13 _px(i)
14         unit(i) == '' ? unit(i, px) : i
15
16 // transparently support vender-specific tags
17 border-radius()
18         -webkit-border-radius: arguments
19         border-radius: arguments
20 box-shadow()
21         -webkit-box-shadow: arguments
22         box-shadow: arguments
23 box-sizing()
24         -webkit-box-sizing: arguments
25         -moz-box-sizing: arguments
26         box-sizing: arguments
27 user-select()
28         -webkit-user-select: arguments
29         -moz-user-select: arguments
30         -ms-user-select: arguments
31         user-select: arguments
32
33 // map "whitespace:" to "white-space:"
34 whitespace()
35         white-space: arguments
36
37 // helper function
38 _pos(type, args)
39         position: unquote(type)
40         for i in (0...length(args))
41                 if not args[i] is a 'unit'
42                         {args[i]}: args[i + 1] is a 'unit' ? args[i + 1] : 0
43
44 // you can pass directions and units, or just directions. Examples:
45 //     absolute: top 20px left 0
46 //     fixed: top right
47 //     relative: top -4px
48 absolute()
49         _pos('absolute', arguments)
50 fixed()
51         _pos('fixed', arguments)
52 relative()
53         _pos('relative', arguments)
54
55 // Specify width and height on the same line
56 dimensions(w, h)
57         width: _px(w)
58         height: _px(h)
59
60 // Make children of this element inline-blocks that are spaced evenly accross.
61 //
62 // To create a minimum distance between: don't use word-spacing, it's broken in
63 // firefox. Instead, try padding on the children and negative margin on the
64 // parent.
65 space_evenly(line_height = 1.2em)
66         text-align: justify
67         & > *
68                 display: inline-block
69                 relative: top line_height
70         &:before
71                 content: ''
72                 display: block
73                 width: 100%
74                 margin-bottom: -(line_height)
75         &:after
76                 content: ''
77                 display: inline-block
78                 width: 100%
79
80 // image layout: left column has normal, right column hover versions
81 //
82 // arguments: width/height are pixel dimensions of a single sprite
83 //
84 // markup: put classes n1, n2, etc on the items.
85 //
86 // Example
87 //
88 //     html:
89 //         <nav>
90 //             <ul>
91 //                 <li class="n0">home</li>
92 //                 <li class="n1">contact</li>
93 //             </ul>
94 //         </nav>
95 //     styl:
96 //         nav li
97 //             sprites_rollover "images/nav.png" 150 35 2
98 sprites_rollover(image, width, height, count, v_offset = 0, h_offset = 0)
99         width: unit(width, px)
100         height: unit(height, px)
101         if image[1]
102                 background: url(image[0])
103                 background: url(image[1]), linear-gradient(transparent, transparent);
104         else
105                 background-image: url(image)
106         background-position: top left
107         background-repeat: no-repeat;
108         for n in (0...count)
109                 &.n{n}
110                         y = - (@height * n) - unit(v_offset, px)
111                         background-position: (0 - unit(h_offset, px)) y
112                         &:hover
113                                 background-position: (0 - unit(h_offset, px) - @width) y
114 sprite_rollover(image, width, height, v_offset = 0, h_offset = 0)
115         sprites_rollover(image, width, height, 1, v_offset, h_offset)
116
117 // see sprites_rollover
118 sprites(image, height, count, v_offset = 0, h_offset = 0)
119         height: unit(height, px)
120         if image[1]
121                 background: url(image[0])
122                 background: url(image[1]), linear-gradient(transparent, transparent);
123         else
124                 background-image: url(image)
125         background-position: top left
126         background-repeat: no-repeat;
127         for n in (0...count)
128                 &.n{n}
129                         y = - (@height * n) - unit(v_offset, px)
130                         background-position: (0 - unit(h_offset, px)) y
131 sprite(image, height, v_offset = 0, h_offset = 0)
132         sprites(image, height, 1, v_offset, h_offset)
133
134 // Styling for a variable height element with an image background where the
135 // middle repeats vertically. You must split your image into three images, and
136 // name them with "-top", "-mid" and "-bot" suffixes (right before the file
137 // extension.) then pass a filepath as the first argument to this function
138 // which does not have a suffix.
139 //
140 // Example:
141 //
142 //     image files:
143 //         i/foo-top.png  (900x20)
144 //         i/foo-mid.png  (900xwhatever)
145 //         i/foo-bot.png  (900x30)
146 //
147 //     dom layout: <div id="expando"><div><div><div>...</div></div></div></div>
148 //
149 //     Stylus:
150 //
151 //         #expando
152 //            mangin: 10px auto;
153 //            width: 900px;
154 //            background_vertical_stretch("i/foo.png", 20, 30)
155 //
156 //     Notes:
157 //
158 //         You can (optionally) pass 1-4 additional arguments which
159 //         (effectively) add padding to the inner most div. The default is to
160 //         have the height and width of the inner div match exactly the outer
161 //         one. Parameters you specify are relative to that (not the section
162 //         filled with the "-mid" image).
163 background_vertical_stretch(image, top_height, bottom_height, top_pad = 0, right_pad = top_pad, bottom_pad = top_pad, left_pad = right_pad)
164         top_height = _px(top_height)
165         bottom_height = _px(bottom_height)
166         top_pad = _px(top_pad)
167         right_pad = _px(right_pad)
168         bottom_pad = _px(bottom_pad)
169         left_pad = _px(left_pad)
170         ext = extname(image)
171         path = pathjoin(dirname(image), basename(image, ext))
172
173         // bottom image (outermost block)
174         background: transparent url(path + '-bot' + ext)
175         padding-bottom: bottom_height + 1 // +1 to prevent margin collapsing
176         > *
177                 background: transparent url(path + '-top' + ext) 0 0 no-repeat
178                 padding-top: top_height + 1 // +1 to prevent margin collapsing
179         > * > *
180                 background: transparent url(path + '-mid' + ext) 0 0 repeat-y;
181                 margin-top: -1px; // correct for above +1 from top
182                 margin-bottom: -1px; // correct for above +1 from bottom
183                 padding: 1px; // to prevent margin collapsing
184         > * > * > *
185                 // all "2px" below are to correct for 1px above padding and below
186                 padding: 1px;
187                 margin-top: 2px - top_height + top_pad
188                 margin-left: left_pad - 2px
189                 margin-right: right_pad - 2px
190                 margin-bottom: 2px - bottom_height + bottom_pad
191                 background: transparent;
192
193 li_reset()
194         margin: 0
195         padding: 0
196         list-style: none
197
198 // Example:
199 //     input
200 //         +placeholder()
201 //             color: red
202 placeholder()
203         &::-webkit-input-placeholder
204                 {block}
205         &:-moz-placeholder // firefox 4-18
206                 {block}
207         &::-moz-placeholder // firefox 19+
208                 {block}
209         &:-ms-input-placeholder // ie
210                 {block}
211
212 // Example:
213 //     div.button
214 //         noselect()
215 noselect()
216         -webkit-touch-callout: none
217         -webkit-user-select: none
218         -khtml-user-select: none
219         -moz-user-select: none
220         -ms-user-select: none
221         user-select: none