JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
15bebc44c959a551f266237cefa34b95fa357e6e
[wfpl.git] / stylus-helpers.styl
1 // Copyright 2014 Jason Woofenden -- Public Domain (CC0)
2
3 // This file contains helpers for using stylus in your project.
4 //
5 // Put something like this in your styl.styl:
6 //     @require 'code/wfpl/stylus-helpers.styl'
7
8
9 // set units to px if it doesn't have a unit already
10 _px(i)
11         unit(i) == '' ? unit(i, px) : i
12
13 // transparently support vender-specific tags
14 border-radius()
15         -webkit-border-radius: arguments
16         border-radius: arguments
17 box-shadow()
18         -webkit-box-shadow: arguments
19         box-shadow: arguments
20 box-sizing()
21         -webkit-box-sizing: arguments
22         -moz-box-sizing: arguments
23         box-sizing: arguments
24
25 // map "whitespace:" to "white-space:"
26 whitespace()
27         white-space: arguments
28
29 // helper function
30 _pos(type, args)
31         position: unquote(type)
32         for i in (0...length(args))
33                 if not args[i] is a 'unit'
34                         {args[i]}: args[i + 1] is a 'unit' ? args[i + 1] : 0
35
36 // you can pass directions and units, or just directions. Examples:
37 //     absolute: top 20px left 0
38 //     fixed: top right
39 //     relative: top -4px
40 absolute()
41         _pos('absolute', arguments)
42 fixed()
43         _pos('fixed', arguments)
44 relative()
45         _pos('relative', arguments)
46
47 // Make children of this element inline-blocks that are spaced evenly accross.
48 //
49 // To create a minimum distance between: don't use word-spacing, it's broken in
50 // firefox. Instead, try padding on the children and negative margin on the
51 // parent.
52 space_evenly(line_height = 1.2em)
53         text-align justify
54         & > *
55                 display inline-block
56                 relative top line_height
57         &:before
58                 content ''
59                 display block
60                 width 100%
61                 margin-bottom -(line_height)
62         &:after
63                 content ''
64                 display inline-block
65                 width 100%
66
67 // image layout: left column has normal, right column hover versions
68 //
69 // arguments: width/height are pixel dimensions of a single sprite
70 //
71 // markup: put classes n1, n2, etc on the items.
72 //
73 // Example
74 //
75 //     html:
76 //         <nav>
77 //             <ul>
78 //                 <li class="n1">home</li>
79 //                 <li class="n2">contact</li>
80 //             </ul>
81 //         </nav>
82 //     styl:
83 //         nav li
84 //             sprite-rollover "images/nav.png" 150 35 2
85 sprite-rollover(image, width, height, count)
86         width = unit(width, px)
87         height = unit(width, px)
88         width width
89         height height
90         background: transparent url(image) top left no-repeat;
91         for n in (1..count)
92                 &.n{n}
93                         y = (2px - n * 35px)
94                         background-position 0 y
95                         &:hover
96                                 background-position -(width) y
97
98 // Styling for a variable height element with an image background where the
99 // middle repeats vertically. You must split your image into three images, and
100 // name them with "-top", "-mid" and "-bot" suffixes (right before the file
101 // extension.) then pass a filepath as the first argument to this function
102 // which does not have a suffix.
103 //
104 // Example:
105 //
106 //     image files:
107 //         i/foo-top.png  (900x20)
108 //         i/foo-mid.png  (900xwhatever)
109 //         i/foo-bot.png  (900x30)
110 //
111 //     dom layout: <div id="expando"><div><div><div>...</div></div></div></div>
112 //
113 //     Stylus:
114 //
115 //         #expando
116 //            mangin: 10px auto;
117 //            width: 900px;
118 //            background-vertical-stretch("i/foo.png", 20, 30)
119 //
120 //     Notes:
121 //
122 //         You can (optionally) pass 1-4 additional arguments which
123 //         (effectively) add padding to the inner most div. The default is to
124 //         have the height and width of the inner div match exactly the outer
125 //         one. Parameters you specify are relative to that (not the section
126 //         filled with the "-mid" image).
127 background-vertical-stretch(image, top-height, bottom-height, top-pad = 0, right-pad = top-pad, bottom-pad = top-pad, left-pad = right-pad)
128         top-height = _px(top-height)
129         bottom-height = _px(bottom-height)
130         top-pad = _px(top-pad)
131         right-pad = _px(right-pad)
132         bottom-pad = _px(bottom-pad)
133         left-pad = _px(left-pad)
134         ext = extname(image)
135         path = pathjoin(dirname(image), basename(image, ext))
136
137         // bottom image (outermost block)
138         background transparent url(path + '-bot' + ext)
139         padding-bottom bottom-height + 1 // +1 to prevent margin collapsing
140         > *
141                 background transparent url(path + '-top' + ext) 0 0 no-repeat
142                 padding-top top-height + 1 // +1 to prevent margin collapsing
143         > * > *
144                 background transparent url(path + '-mid' + ext) 0 0 repeat-y;
145                 margin-top -1px; // correct for above +1 from top
146                 margin-bottom -1px; // correct for above +1 from bottom
147                 padding 1px; // to prevent margin collapsing
148         > * > * > *
149                 // all "2px" below are to correct for 1px above padding and below
150                 padding 1px;
151                 margin-top 2px - top-height + top-pad
152                 margin-left: left-pad - 2px
153                 margin-right: right-pad - 2px
154                 margin-bottom: 2px - bottom-height + bottom-pad
155                 background transparent;
156
157 li-reset()
158         margin 0
159         padding 0
160         list-style none