Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 187 lines (137 sloc) 6.172 kb
292b400 @ndp more formatting README
authored
1 # Csster
f531da7 @ndp update readme with the intent
authored
2
bdbc77c @ndp add demos to the web
authored
3 Concisely generate CSS style rules within Javascript. Features:
4
27ae3c6 @ndp add demos to the web
authored
5 * standard "object literal"/JSON format with good editor support
bdbc77c @ndp add demos to the web
authored
6 * nesting to DRY up stylesheets
27ae3c6 @ndp add demos to the web
authored
7 * color functions like <code>darken</code> and <code>saturate</code>
8 * built-in macros for common CSS idioms like *clearfix*, *rounded corners*, *drop shadows*.
9 * and all the plain old Javascript behavior: functions, data structures, looping, Math operations, etc.
0bdd67f @ndp add demos to the web
authored
10
11
292b400 @ndp more formatting README
authored
12 ## Usage
06de1e3 @ndp rename README and try to format
authored
13
de891fa @ndp add demos to the web
authored
14 All code is packaged into a single Javascript file download, [csster.js](http://ndpsoftware.com/csster/csster.js). There are no external dependencies. The project itself is hosted on [GitHub](http://github.com/ndp/csster).
bdbc77c @ndp add demos to the web
authored
15
e1387e2 @ndp more formatting README
authored
16 <pre>
17 require('csster.js'); // however you manage dependencies
18
67b54b4 @ndp figure out how to insert styles into the document
authored
19 Csster.style({
e1387e2 @ndp more formatting README
authored
20 h1: {
21 fontSize: 18,
22 color: 'red'
23 }
24 });
25
26 ...
27 </pre>
28
b6d72d8 @ndp update readme
authored
29 Result, inserted in DOM automatically at the bottom of the &lt;head&gt; element:
30 <pre>
31 ...
e4cf475 @ndp update readme
authored
32 &lt;style type="text/stylesheet"&gt;
b6d72d8 @ndp update readme
authored
33 h1 {
34 font-size: 18px;
35 color: red;
36 }
e4cf475 @ndp update readme
authored
37 &lt;/style&gt;
38 &lt;/head&gt;
b6d72d8 @ndp update readme
authored
39 ...
40 </pre>
41
292b400 @ndp more formatting README
authored
42 ### Format of CSS Rules
5b6d112 @ndp more formatting README
authored
43
67b54b4 @ndp figure out how to insert styles into the document
authored
44 The *style* method accepts CSS rules passed either as arrays or hashes, arrays just being
de5d869 @ndp add some example documentation
authored
45 a way to order the hashes. For example:
46
47 <pre>
48 {
49 ul: {
cb8d55f @ndp update readme
authored
50 margin: 5,
51 padding: 0,
de5d869 @ndp add some example documentation
authored
52 }
5b6d112 @ndp more formatting README
authored
53 'ul li:first': {
cb8d55f @ndp update readme
authored
54 paddingLeft: 20px
de5d869 @ndp add some example documentation
authored
55 }
56 }
94279b6 @ndp add some example documentation
authored
57 </pre>
de5d869 @ndp add some example documentation
authored
58
59 Note that
588af51 @ndp more formatting README
authored
60
292b400 @ndp more formatting README
authored
61 * property names are automatically converted to the correct format from camelcase. Feel free to quote them as well.
f99a96e @ndp work on readme
authored
62 * all raw numbers are assumed to be "pixels" (or "px"), and rendered as such.
292b400 @ndp more formatting README
authored
63 * any sort of selectors are allowed... they are just passed through to the stylesheet.
64
65 #### Nesting
7726ab5 @ndp more formatting README
authored
66 Csster supports nesting of rules to keep things more concise:
de5d869 @ndp add some example documentation
authored
67 <pre>
68 {
69 ul: {
cb8d55f @ndp update readme
authored
70 margin: 5,
de5d869 @ndp add some example documentation
authored
71 li: {
80353b1 @ndp rename macro to has
authored
72 paddingLeft: 20,
73 '&:hover': {
74 color: 'red'
75 }
de5d869 @ndp add some example documentation
authored
76 }
77 }
78 }
94279b6 @ndp add some example documentation
authored
79 </pre>
de5d869 @ndp add some example documentation
authored
80
81 The "li" property in this case might be a selector or might be a property name. A list of valid
82 property names is used to identify properties right now, and otherwise it's considered a sub-selector.
83
80353b1 @ndp rename macro to has
authored
84 Csster supports SASS's "&" operator, to indicate that the selector should be combined with the parent selector.
85 Instead of the default "any descendent" space character being inserted, no space is inserted.
86
1703d0a @ndp update readme
authored
87 #### Functions
b50ec79 @ndp improve docs on functions
authored
88 Most manipulations will fall into Javascript's language support, as far as any math or looping. Use Javascript to write necessary functions.
80353b1 @ndp rename macro to has
authored
89
b50ec79 @ndp improve docs on functions
authored
90 function/color.js contains SASS-like color functions:
8f276b6 @ndp update readme
authored
91
b50ec79 @ndp improve docs on functions
authored
92 * <code>"#ab342c".darken(%)</code> -- make color darker by given percent
93 * <code>"#ab342c".lighten(%)</code> -- make color lighter by given percent
94 * <code>"#ab342c".saturate(%)</code> -- make color more saturated by given percent. To *desaturate*, use negative values for the percent. Note that <code>"#ab342c".saturate(-100)</code> renders in grayscale.
95
96 There are also color conversion routines if you want to build your own manipulation.
97
98 * <code>"#ab342c".toRGB()</code>
99 * <code>"#ab342c".toHSL()</code>
100 * <code>Csster.hslToHexColor(h,s,l)</code>
101
102 Opacity is currently not supported by the color model.
1703d0a @ndp update readme
authored
103
80353b1 @ndp rename macro to has
authored
104 #### Macros using "has" key
de5d869 @ndp add some example documentation
authored
105
717a1e3 @ndp work on readme
authored
106 There are a host of pre-made macros that may be useful:
107
108 * <code>roundedCorners(radius)</code> -- add rounded corners on all sides
109 * <code>roundedCorners(side, radius)</code> -- add rounded corners on specified side: <code>'top'</code>, <code>'left'</code>, <code>'bottom'</code> or <code>'right'</code>
110 * <code>roundedCorners(corner, radius)</code> -- add rounded corners to a specified corner: <code>'tl'</code>, <code>'tr'</code>, <code>'bl'</code> or <code>'br'</code>
111 * <code>phark(width, height, img, imgXPosition=0, imgYPosition=0)</code> -- standard phark image replacement with optional background image offset.
112 * <code>boxShadow([xoffset, yoffset], radius, color)</code>
113 * <code>verticalCentering(height)</code> and <code>horizontalCentering(width)</code> -- center using the top 50% / margin-top -width/2 technique. See http://stackoverflow.com/questions/148251/css-centering-tricks
f99a96e @ndp work on readme
authored
114 * <code>clearfix()</code> -- standard clearfix
de5d869 @ndp add some example documentation
authored
115
80353b1 @ndp rename macro to has
authored
116 To "mix these in", use the "has" key:
de5d869 @ndp add some example documentation
authored
117
118 <pre>
119 {
120 'div#featured_box': {
cb8d55f @ndp update readme
authored
121 backgroundColor: '#394c89',
122 has: roundedCorner(5)
de5d869 @ndp add some example documentation
authored
123 }
124 }
5b6d112 @ndp more formatting README
authored
125 </pre>
126
27ae3c6 @ndp add demos to the web
authored
127 Multiple macros can be included by making that a list, eg. <code>has: [roundedCorners(5), dropShadow()]</code>.
80353b1 @ndp rename macro to has
authored
128
717a1e3 @ndp work on readme
authored
129 It's all Javascript, so macros and more complex functions are easy to write. To mix in a set of values, create a function
130 that returns a hash of values, for example:
131
132 <pre>
133 function roundedCorners(radius) {
134 return {
135 '-webkit-border-radius': radius,
136 '-moz-border-radius': radius,
137 'border-radius': radius
138 }
139 }
140 </pre>
141
bdbc77c @ndp add demos to the web
authored
142 [Demo of the color functions and macros](http://ndpsoftware.com/csster/demo.html)
80353b1 @ndp rename macro to has
authored
143
27ae3c6 @ndp add demos to the web
authored
144 A macro's properties will be overwritten by properties within including selector (or later included macros), similar to how the cascade takes the last defined value.
145
146
632baa2 @ndp add demos to the web
authored
147
b5f9f8b @ndp add demos to the web
authored
148 ## TDD
632baa2 @ndp add demos to the web
authored
149
27ae3c6 @ndp add demos to the web
authored
150 The design was driven by [the specs](http://ndpsoftware.com/csster/spec_runner.html).
80353b1 @ndp rename macro to has
authored
151
de5d869 @ndp add some example documentation
authored
152
292b400 @ndp more formatting README
authored
153 ## Todo
de5d869 @ndp add some example documentation
authored
154
c56959d @ndp add centering and clearfix
authored
155 * decompile existing stylesheets
156 * support comma in rules
157 * different way to concatenate selectors
158 * jQuery rules
159 * naming stylesheets so they can be updated.
ee77c60 @ndp start to add color functions
authored
160 * Fix license
5b6d112 @ndp more formatting README
authored
161 * Better name
06de1e3 @ndp rename README and try to format
authored
162
163
ee77c60 @ndp start to add color functions
authored
164
292b400 @ndp more formatting README
authored
165 ## Motivation
f531da7 @ndp update readme with the intent
authored
166
167 This project comes from my frustration of trying to build standalone Javascript widgets. Web
168 projects always involve the combination of HTML DOM, CSS and Javascript. It's often simpler to
169 generate the necessary DOM within your Javascript, removing any coupling (and a simpler calling
170 convention) to a specific web page. But most widgets have certain style rules. To avoid
171 any coupling with the CSS at all, styles can be included inline, but these gets bulky
172 and hard to read. The "rule" nature of CSS is nice. So widgets then have a Javascript
173 and CSS component. Wouldn't it be nice, though, to remove that CSS component.
174
175 With the advent of SASS, the coupling is even more complicated, as now there's some other
176 tool completely unrelated to your component, written in some other language. Wouldn't
177 a unified approach be nice?
178
179
292b400 @ndp more formatting README
authored
180 ## Similar projects
de5d869 @ndp add some example documentation
authored
181
06de1e3 @ndp rename README and try to format
authored
182 http://revnode.com/oss/css/
183
292b400 @ndp more formatting README
authored
184 ## Legal
de5d869 @ndp add some example documentation
authored
185
06de1e3 @ndp rename README and try to format
authored
186 Copyright (c) 2010 Andrew J. Peterson
187 All Rights Reserved.
Something went wrong with that request. Please try again.