4
4
[ ![ Build Status] ( https://travis-ci.org/jxnblk/cxs.svg?branch=master )] ( https://travis-ci.org/jxnblk/cxs )
5
5
[ ![ js-standard-style] ( https://img.shields.io/badge/code%20style-standard-brightgreen.svg )] ( http://standardjs.com/ )
6
6
7
- Functional CSS for functional UI components
7
+ High performance, lightweight CSS-in-JS
8
8
9
9
``` js
10
- const className = cxs ({ color: ' tomato' } )
10
+ const className = cxs (` color: tomato` )
11
11
```
12
12
13
- CXS is a functional CSS-in-JS solution that uses atomic styles
14
- to maximize deduplication and help with dead code elimination.
13
+ CXS is a minimal CSS-in-JS solution that uses
14
+ an API that closely follows the native CSSStyleSheet API
15
+ to maximize performance and reduce bloat.
15
16
16
17
## Features
17
18
18
- - Three different [ modes] ( #modes ) of operation: Atomic, Lite, & Monolithic
19
- - < 5KB
20
- - Avoids collisions with atomic rulesets
19
+ - < 1.5KB
21
20
- Deduplicates repeated styles
22
21
- Dead-code elimination
23
22
- Framework independent
@@ -26,8 +25,7 @@ to maximize deduplication and help with dead code elimination.
26
25
- Pseudoclasses
27
26
- Nested selectors
28
27
- Avoid maintaining separate stylesheets
29
- - Use plain JS objects and types
30
- - No tagged template literals
28
+ - Use plain CSS strings
31
29
32
30
## Install
33
31
@@ -49,50 +47,38 @@ const Box = (props) => {
49
47
)
50
48
}
51
49
52
- const className = cxs ({
53
- padding: 32 ,
54
- backgroundColor: ' tomato'
55
- } )
50
+ const className = cxs (`
51
+ padding: 32px;
52
+ backgroundColor: tomato;
53
+ ` )
56
54
57
55
export default Box
58
56
```
59
57
60
58
### Pseudoclasses
61
59
62
60
``` js
63
- cxs ({
64
- color: ' tomato' ,
65
- ' :hover' : {
66
- color: ' red'
67
- }
68
- })
61
+ cxs (' color: tomato' )
62
+ .hover (' color: red' )
69
63
```
70
64
71
65
### Media Queries
72
66
73
67
``` js
74
- cxs ({
75
- color: ' tomato' ,
76
- ' @media (min-width: 40em)' : {
77
- color: ' red'
78
- }
68
+ cxs (' color: tomato' )
69
+ .media (' @media (min-width: 40em)' , ' color: red' )
79
70
})
80
71
```
81
72
82
73
### Nested Selectors
83
74
84
75
``` js
85
- cxs ({
86
- color: ' tomato' ,
87
- h1: {
88
- color: ' red'
89
- }
90
- })
76
+ cxs (' color: tomato' , ' > h1' )
91
77
```
92
78
93
79
### Server-Side Rendering
94
80
95
- To use CXS in server environments, use the ` getCss() ` function to get the static CSS string * after* rendering a view.
81
+ To use CXS in server environments, use the ` css ` getter to get the static CSS string * after* rendering a view.
96
82
97
83
``` js
98
84
import React from ' react'
@@ -101,170 +87,116 @@ import cxs from 'cxs'
101
87
import App from ' ./App'
102
88
103
89
const html = ReactDOMServer .renderToString (< App / > )
104
- const css = cxs .getCss ()
90
+ const css = cxs .css
105
91
106
92
const doc = ` <!DOCTYPE html>
107
93
<style>${ css} </style>
108
94
${ html}
109
95
`
110
96
111
- // Optionally reset the cache for the next render
97
+ // Reset the cache for the next render
112
98
cxs .reset ()
113
99
114
100
```
115
101
116
- ## Modes
117
-
118
- CXS offers three different modes of operation, which produce different rules and class names.
102
+ ## API
119
103
120
104
``` js
121
- import cxsAtomic from ' cxs'
122
- import cxsMonolithic from ' cxs/monolithic'
123
- import cxsLite from ' cxs/lite'
124
-
125
- const styles = {
126
- margin: 0 ,
127
- marginBottom: 32 ,
128
- padding: 16 ,
129
- color: ' tomato' ,
130
- ' :hover' : {
131
- color: ' orangered'
132
- },
133
- ' @media (min-width: 40em)' : {
134
- padding: 32
135
- }
136
- }
105
+ const rule = cxs (cssDeclarations, descendantSelector, mediaQuery, className)
106
+ // cxsRuleObject
137
107
138
- // Each mode returns a different set of class names
108
+ // className
109
+ rule .toString () // '_0'
139
110
140
- cxsAtomic (styles)
141
- // m-0 mb-32 p-16 c-tomato -hover-c-orangered _zsp35u
111
+ // The following methods all return a rule object,
112
+ // which allows them to be chained together
142
113
143
- cxsMonolithic (styles)
144
- // _q5nmba
114
+ // Adds a pseudoclass rule with the same className
115
+ rule .hover ()
116
+ rule .focus ()
117
+ rule .active ()
118
+ rule .disabled ()
145
119
146
- cxsLite (styles)
147
- // a b c d e f
148
- ```
120
+ // Adds a media query rule with the same className
121
+ rule .media (mediaQuery, cssDeclarations)
149
122
150
- ### Atomic Mode (Default)
151
-
152
- ``` js
153
- import cxs from ' cxs'
123
+ // Adds another rule with the same className
124
+ rule .push ()
154
125
```
155
126
156
- The default mode is the atomic mode.
157
- This creates atomic rulesets and attempts to create human readable classnames.
158
- If a classname is longer than 24 characters, a hashed classname will be used instead.
159
-
160
- ### Lite Mode
161
-
162
127
``` js
163
- import cxs from ' cxs/lite'
164
- ```
128
+ import cxs from ' cxs'
165
129
166
- For super fast performance, use the ` cxs/lite ` module.
167
- Lite mode creates alphabetic class names in a sequential order and does not support nested selectors.
130
+ // Creates styles and returns micro classnames
131
+ cxs ( ' color: tomato ' )
168
132
169
- Since the class names in cxs/lite are * not * created in a functional manner,
170
- when using cxs/lite on both the server and client, the styles will need to be rehydrated.
133
+ // Gets a CSS string of attached rules. Useful for server-side rendering
134
+ cxs . css
171
135
172
- ``` js
173
- // Server
174
- const css = cxs .getCss ()
136
+ // Clear the cache and flush the stylesheet.
137
+ // This is useful for cleaning up in server-side contexts.
175
138
cxs .reset ()
176
-
177
- const html = ` <!DOCTYPE html>
178
- <style id='cxs-style'>${ css} </style>
179
- ${ body}
180
- `
181
139
```
182
140
183
- ``` js
184
- // Client
185
- import cxs from ' cxs/lite'
186
-
187
- const styleTag = document .getElementById (' cxs-style' )
188
- const serverCss = styleTag .innerHTML
141
+ Additional exports
189
142
190
- cxs .rehydrate (serverCss)
191
143
```
192
-
193
- ### Monolithic Mode
194
-
195
- ``` js
196
- import cxs from ' cxs/monolithic'
144
+ import {
145
+ Sheet, // create stylesheet function
146
+ sheet, // cxs stylesheet instance
147
+ css, // string of rendered CSS - same as cxs.css
148
+ reset // same as cxs.reset
149
+ } from 'cxs'
197
150
```
198
151
199
- To create encapsulated monolithic styles with CXS and use single hashed class names, import the monolithic module.
152
+ ## React Components
200
153
201
- The monolithic module also accepts custom selectors for styling things like the body element .
154
+ CXS also has a higher order component for creating styled React components, similar to [ styled-components ] [ 0 ] API .
202
155
203
156
``` js
204
- cxs (' body' , {
205
- fontFamily: ' -apple-system, sans-serif' ,
206
- margin: 0 ,
207
- lineHeight: 1.5
208
- })
209
- ```
157
+ import cxs from ' cxs/component'
210
158
211
- ## API
212
-
213
- ``` js
214
- import cxs from ' cxs'
159
+ const Heading = cxs (`
160
+ margin: 0;
161
+ font-size: 32px;
162
+ line-height: 1.25;
163
+ ` ).component (' h1' )
164
+ ```
215
165
216
- // Creates styles and returns micro classnames
217
- cxs ({ color: ' tomato' })
166
+ Unlike styled-components, CXS components can be composed with other higher order components and works seamlessly with libraries like [ Recompose] [ 1 ]
218
167
219
- // Returns a CSS string of attached rules. Useful for server-side rendering
220
- cxs .getCss ()
168
+ ## JS Objects
221
169
222
- // Clear the cache and flush the glamor stylesheet.
223
- // This is useful for cleaning up in server-side contexts.
224
- cxs .reset ()
225
- ```
170
+ T/K
226
171
227
- Additional exports
172
+ ``` js
173
+ import cxs from ' cxs/object'
228
174
229
- ```
230
- import {
231
- // The threepointone/glamor StyleSheet instance
232
- // See https://github.com/threepointone/glamor
233
- sheet,
234
- // Same as cxs.getCss
235
- getCss,
236
- // Same as cxs.reset
237
- reset
238
- } from 'cxs'
175
+ const style = cxs ({
176
+ color: ' tomato'
177
+ })
239
178
```
240
179
241
180
### Vendor prefixes
242
181
243
182
CXS ** does not** handle vendor prefixing to keep the module size at a minimum.
244
183
To add vendor prefixes, use a prefixing module like [ ` inline-style-prefixer ` ] ( https://github.com/rofrischmann/inline-style-prefixer )
245
184
246
- ``` js
247
- import cxs from ' cxs'
248
- import prefixer from ' inline-style-prefixer/static'
249
-
250
- const prefixed = prefixer ({
251
- display: ' flex'
252
- })
253
- const cx = cxs (prefixed)
254
- ```
255
-
256
- ### Browser support
185
+ <!--
186
+ ```js
187
+ import cxs from 'cxs'
188
+ import prefixer from 'inline-style-prefixer/static'
257
189
258
- IE9+, due to the following:
259
- - ` Array.filter `
260
- - ` Array.map `
261
- - ` Array.forEach `
190
+ const prefixed = prefixer({
191
+ display: 'flex'
192
+ })
193
+ const cx = cxs(prefixed)
194
+ ```
195
+ -->
262
196
263
197
## Related
264
198
265
- - [ cxs-components] ( https://github.com/jxnblk/cxs/tree/master/packages/cxs-components )
266
- - [ react-cxs] ( https://github.com/jxnblk/cxs/tree/master/packages/react-cxs )
267
- - [ react-cxs-hoc] ( https://github.com/jxnblk/cxs/tree/master/packages/react-cxs-hoc )
199
+ [ 0 ] : https://www.styled-components.com ( styled-components )
200
+ [ 1 ] : https://github.com/acdlite/recompose ( Recompose )
268
201
269
202
[ MIT License] ( LICENSE.md )
270
-
0 commit comments