JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
stylus: space_evenly: don't require units on arg
[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 // generate (return) a "calc(Fpx + R%)" to scale linearly with parent
61 // args:
62 //   pb: pixel width of parent (when biggest)
63 //   ps: pixel width of parent (when smallest)
64 //   cb: pixel width of child (when biggest)
65 //   cs: pixel width of child (when smallest)
66 linear_scale_calc(pb, ps, cb, cs)
67         // math explained: (figuring out F and R in calc(Fpx + R%))
68         // f + r * pb = cb   // known_1: formula(pb) -> cb
69         //     f = cb - r * pb    // solve_for_f: subtract (r * pb) from both sides
70         // f + r * ps = cs   // known_2: formula(pb) -> cb
71         //     cb - r * pb + r * ps = cs    // substitute solved_for_f into known_2
72         //     -r * pb + r * ps = cs - cb   // subtract cb from both sides
73         //     r * (-pb + ps) = cs - cb     // factor out r from left side
74         //     r = (cs - cb) / (-pb + ps)   // divide both sides by (-pb + ps)
75         //     r = (cb - cs) / (pb - ps)    // multiply left side by -1/-1
76         r = (cb - cs) / (pb - ps)
77         f = cb - r * pb
78         unquote("calc(" + unit(f, "px") + ' + ' + unit(100 * r, "%") + ")")
79
80 // Make children of this element inline-blocks that are spaced evenly accross.
81 //
82 // To create a minimum distance between: don't use word-spacing, it's broken in
83 // firefox. Instead, try padding on the children and negative margin on the
84 // parent.
85 space_evenly(line_height = 1.2)
86         line_height = unit(line_height, em)
87         text-align: justify
88         & > *
89                 display: inline-block
90                 relative: top line_height
91         &:before
92                 content: ''
93                 display: block
94                 width: 100%
95                 margin-bottom: -(line_height)
96         &:after
97                 content: ''
98                 display: inline-block
99                 width: 100%
100
101 // image layout: left column has normal, right column hover versions
102 //
103 // arguments: width/height are pixel dimensions of a single sprite
104 //
105 // markup: put classes n1, n2, etc on the items.
106 //
107 // Example
108 //
109 //     html:
110 //         <nav>
111 //             <ul>
112 //                 <li class="n0">home</li>
113 //                 <li class="n1">contact</li>
114 //             </ul>
115 //         </nav>
116 //     styl:
117 //         nav li
118 //             sprites_rollover "images/nav.png" 150 35 2
119 sprites_rollover(image, width, height, count, v_offset = 0, h_offset = 0)
120         width: unit(width, px)
121         height: unit(height, px)
122         if image[1]
123                 background: url(image[0])
124                 background: url(image[1]), linear-gradient(transparent, transparent);
125         else
126                 background-image: url(image)
127         background-position: top left
128         background-repeat: no-repeat;
129         for n in (0...count)
130                 &.n{n}
131                         y = - (@height * n) - unit(v_offset, px)
132                         background-position: (0 - unit(h_offset, px)) y
133                         &:hover
134                                 background-position: (0 - unit(h_offset, px) - @width) y
135 sprite_rollover(image, width, height, v_offset = 0, h_offset = 0)
136         sprites_rollover(image, width, height, 1, v_offset, h_offset)
137
138 // see sprites_rollover
139 sprites(image, height, count, v_offset = 0, h_offset = 0)
140         height: unit(height, px)
141         if image[1]
142                 background: url(image[0])
143                 background: url(image[1]), linear-gradient(transparent, transparent);
144         else
145                 background-image: url(image)
146         background-position: top left
147         background-repeat: no-repeat;
148         for n in (0...count)
149                 &.n{n}
150                         y = - (@height * n) - unit(v_offset, px)
151                         background-position: (0 - unit(h_offset, px)) y
152 sprite(image, height, v_offset = 0, h_offset = 0)
153         sprites(image, height, 1, v_offset, h_offset)
154
155 // Styling for a variable height element with an image background where the
156 // middle repeats vertically. You must split your image into three images, and
157 // name them with "-top", "-mid" and "-bot" suffixes (right before the file
158 // extension.) then pass a filepath as the first argument to this function
159 // which does not have a suffix.
160 //
161 // Example:
162 //
163 //     image files:
164 //         i/foo-top.png  (900x20)
165 //         i/foo-mid.png  (900xwhatever)
166 //         i/foo-bot.png  (900x30)
167 //
168 //     dom layout: <div id="expando"><div><div><div>...</div></div></div></div>
169 //
170 //     Stylus:
171 //
172 //         #expando
173 //            mangin: 10px auto;
174 //            width: 900px;
175 //            background_vertical_stretch("i/foo.png", 20, 30)
176 //
177 //     Notes:
178 //
179 //         You can (optionally) pass 1-4 additional arguments which
180 //         (effectively) add padding to the inner most div. The default is to
181 //         have the height and width of the inner div match exactly the outer
182 //         one. Parameters you specify are relative to that (not the section
183 //         filled with the "-mid" image).
184 background_vertical_stretch(image, top_height, bottom_height, top_pad = 0, right_pad = top_pad, bottom_pad = top_pad, left_pad = right_pad)
185         top_height = _px(top_height)
186         bottom_height = _px(bottom_height)
187         top_pad = _px(top_pad)
188         right_pad = _px(right_pad)
189         bottom_pad = _px(bottom_pad)
190         left_pad = _px(left_pad)
191         ext = extname(image)
192         path = pathjoin(dirname(image), basename(image, ext))
193
194         // bottom image (outermost block)
195         background: transparent url(path + '-bot' + ext)
196         padding-bottom: bottom_height + 1 // +1 to prevent margin collapsing
197         > *
198                 background: transparent url(path + '-top' + ext) 0 0 no-repeat
199                 padding-top: top_height + 1 // +1 to prevent margin collapsing
200         > * > *
201                 background: transparent url(path + '-mid' + ext) 0 0 repeat-y;
202                 margin-top: -1px; // correct for above +1 from top
203                 margin-bottom: -1px; // correct for above +1 from bottom
204                 padding: 1px; // to prevent margin collapsing
205         > * > * > *
206                 // all "2px" below are to correct for 1px above padding and below
207                 padding: 1px;
208                 margin-top: 2px - top_height + top_pad
209                 margin-left: left_pad - 2px
210                 margin-right: right_pad - 2px
211                 margin-bottom: 2px - bottom_height + bottom_pad
212                 background: transparent;
213
214 li_reset()
215         margin: 0
216         padding: 0
217         list-style: none
218
219 // Example:
220 //     input
221 //         +placeholder()
222 //             color: red
223 placeholder()
224         &::-webkit-input-placeholder
225                 {block}
226         &:-moz-placeholder // firefox 4-18
227                 {block}
228         &::-moz-placeholder // firefox 19+
229                 {block}
230         &:-ms-input-placeholder // ie
231                 {block}
232
233 // Example:
234 //     div.button
235 //         noselect()
236 noselect()
237         -webkit-touch-callout: none
238         -webkit-user-select: none
239         -khtml-user-select: none
240         -moz-user-select: none
241         -ms-user-select: none
242         user-select: none
243
244 // Do all the crazy math to get columns to fit properly
245 // and (optionally) scale for responsive designs.
246 //
247 // example:
248 //
249 //      columns = wfpl_columns({
250 //              type: 'node',
251 //              name: 'centerer',
252 //              margin: 15px,
253 //              width: 940px,
254 //              child: {
255 //                      type: 'node',
256 //                      name: 'middler',
257 //                      border-width: 1px,
258 //                      border-style: solid,
259 //                      border-color: black,
260 //                      padding-left: 30px,
261 //                      padding-right: 18px,
262 //                      child: {
263 //                              type: 'alternatives',
264 //                              full: {
265 //                                      type: 'series',
266 //                                      nav: {
267 //                                              type: 'node'
268 //                                              width: 219px
269 //                                              float: left
270 //                                      },
271 //                                      main: {
272 //                                              type: 'node'
273 //                                              margin-left: 48px,
274 //                                              width: 623px
275 //                                              float: left
276 //                                      }
277 //                              },
278 //                              with_sidebar: {
279 //                                      type: 'series',
280 //                                      nav: {
281 //                                              type: 'node',
282 //                                              width: 219px
283 //                                              float: left
284 //                                      },
285 //                                      main: {
286 //                                              type: 'node',
287 //                                              margin-left: 48px,
288 //                                              width: 343px
289 //                                              float: left
290 //                                      },
291 //                                      sidebar: {
292 //                                              type: 'alternatives',
293 //                                              plain: {
294 //                                                      type: 'node'
295 //                                                      margin-left: 30px,
296 //                                                      width: 250px
297 //                                                      float: left
298 //                                              },
299 //                                              bordered: {
300 //                                                      type: 'node'
301 //                                                      margin-left: 30px,
302 //                                                      border-width: 1px,
303 //                                                      border-style: solid
304 //                                                      border-color: red
305 //                                                      padding: 15px,
306 //                                                      width: 218px
307 //                                                      float: left
308 //                                              }
309 //                                      }
310 //                              }
311 //                      }
312 //              }
313 //      })
314 // output css from column calculations
315 //      for selector, css in columns.css
316 //              body > {selector}
317 //                      {css}
318 //      @media screen and (max-width: (columns.width))
319 //              // output responsive css from column calculations
320 //              for selector, css in columns.responsive_css
321 //                      body > {selector}
322 //                              {css}
323 //
324 //              // as big as it can be
325 //              body > .centerer
326 //                      width: auto
327 //
328 //              // make sure that borders (which won't scale) and rounding errors don't
329 //              // break the layout
330 //              body > .centerer.full > .main,
331 //              body > .centerer.with_sidebar > .sidebar.plain,
332 //              body > .centerer.with_sidebar > .sidebar.bordered
333 //                      margin-right: -10px
334 //
335 //      // switch to 1-column layout
336 //      @media screen and (max-width: (single_column_max))
337 //              for selector, css in columns.css
338 //                      body > {selector}
339 //                              if selector == '.centerer'
340 //                                      margin-top: 0
341 //                              else if selector in hide_in_one_column_mode
342 //                                      display: none
343 //                              else
344 //                                      border: none
345 //                                      float: none
346 //                                      width: auto
347 //                                      margin: 0
348 //                                      padding: 0
349 //                                      margin-top: columns['responsive_css']['.centerer']['margin']
350 wfpl_columns_helper(top, node, selector, parent_width, expected_width)
351         css_rules = {}
352         responsive_css_rules = {}
353         if node.type == 'node'
354                 left_width = 0px
355                 right_width = 0px
356                 width = null // inner
357                 outer_width = null
358                 if node.name
359                         if selector == ''
360                                 selector = '.' + node.name
361                         else
362                                 selector += ' > .' + node.name
363                 // callculate width and outer_width
364                 for k, v in node
365                         if match('^(margin|padding|border-width)$', k)
366                                 left_width += v
367                                 right_width += v
368                         if match('^((margin|padding)-left)|(border-left-width)$', k)
369                                 left_width += v
370                         if match('^((margin|padding)-right)|(border-right-width)$', k)
371                                 right_width += v
372                         if k != 'type' && k != 'child' && k != 'name' && k != 'outer-width' && k != 'outer_width'
373                                 css_rules[k] = v
374                 for k, v in node
375                         if k == 'width'
376                                 width = v
377                                 outer_width = left_width + v + right_width
378                         if k == 'outer-width' && k == 'outer_width'
379                                 outer_width = k
380                                 width = v - left_width - right_width
381                 if (!width) && (!outer_width)
382                         if parent_width
383                                 outer_width = parent_width
384                         else
385                                 error("Couldn't figure out width of " + selector);
386                 if outer_width && !width
387                         width = outer_width - left_width - right_width
388                 if width && !outer_width
389                         outer_width = width + left_width + right_width
390                 unless parent_width // should only happen at top level
391                         parent_width = outer_width
392                 top['css'][selector] = css_rules
393                 for k, v in node
394                         if k != 'outer-width' && k != 'outer_width' && k != 'border-width' && k != 'border-left-width' && k != 'border-right-width'
395                                 if typeof(v) == 'unit' && unit(v) == 'px'
396                                         responsive_css_rules[k] = floor(unit((v / parent_width) * 100, '%'), 4)
397                 top['responsive_css'][selector] = responsive_css_rules
398                 if 'child' in node
399                         child_width = wfpl_columns_helper(top, node.child, selector, width, width)
400                         _columns_recurser(rargs)
401                         if child_width != width
402                                 error(selector + " is the is wrong width. Expected: " + width + ", got: " + rargs.ret.w)
403                 top['widths'][selector] = width
404                 return outer_width
405         if node.type == 'alternatives'
406                 for name, child in node
407                         if name != 'type'
408                                 child_selector = selector + '.' + name
409                                 child_width = wfpl_columns_helper(top, child, child_selector, parent_width, expected_width)
410                                 if expected_width
411                                         if child_width != expected_width
412                                                 error(child_selector + " is wrong width. Expected: " + expected_width + ", got: " + child_width)
413                                 else
414                                         expected_width = child_width
415                                 unless parent_width
416                                         parent_width = child_width
417                 return expected_width
418         if node.type == 'series'
419                 series_width = 0px
420                 for name, child in node
421                         if name != 'type'
422                                 if selector == ''
423                                         child_selector = '.' + name
424                                 else
425                                         child_selector = selector + ' > .' + name
426                                 child_width = wfpl_columns_helper(top, child, child_selector, parent_width, null)
427                                 series_width += child_width
428                 if expected_width
429                         if series_width != expected_width
430                                 error(selector + " series total wrong. expected: " + expected_width + ", got: " + series_width);
431                 return series_width
432         return // never reached with valid data
433
434 wfpl_columns(hash)
435         top = { // contains settings, and is returned from porcelain
436                 responsive: responsive
437                 width: null
438                 css: {}
439                 responsive_css: {}
440                 widths: {}
441         }
442         top.width = wfpl_columns_helper(top, hash, '', null, null)
443         return top