You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -8,10 +8,234 @@ Generate a static set of custom utility classes and compose them either statical
8
8
9
9
Basically, it’s like building your own zero-runtime, type-safe version of [Tailwind](https://tailwindcss.com), [Styled System](https://styled-system.com), etc.
10
10
11
+
**Compose sprinkles statically at build time.**
12
+
13
+
```ts
14
+
// styles.css.ts
15
+
16
+
exportconst className =sprinkles({
17
+
display: 'flex',
18
+
paddingX: 'small',
19
+
flexDirection: {
20
+
mobile: 'column',
21
+
desktop: 'row'
22
+
},
23
+
background: {
24
+
lightMode: 'blue-50',
25
+
darkMode: 'gray-700'
26
+
}
27
+
});
28
+
```
29
+
30
+
**Or compose them dynamically at runtime! 🏃♂️**
31
+
32
+
```ts
33
+
// app.ts
34
+
35
+
import { sprinkles } from'./sprinkles.css.ts';
36
+
37
+
const flexDirection =
38
+
Math.random() >0.5?'column':'row';
39
+
40
+
document.write(`
41
+
<section class="${sprinkles({
42
+
display: 'flex',
43
+
flexDirection
44
+
})}">
45
+
...
46
+
</section>
47
+
`);
48
+
```
49
+
50
+
> 🖥 [Try it out for yourself in CodeSandbox.](https://codesandbox.io/s/github/seek-oss/vanilla-extract/tree/master/examples/webpack-react?file=/src/sprinkles.css.ts)
51
+
52
+
## Setup
53
+
54
+
> 💡 Before starting, ensure you've set up [vanilla-extract.](/documentation/setup)
55
+
56
+
Install Sprinkles.
57
+
11
58
```bash
12
59
$ npm install @vanilla-extract/sprinkles
13
60
```
14
61
62
+
Create a `sprinkles.css.ts` file, then configure and export your `sprinkles` function.
63
+
64
+
> 💡 This is just an example! Feel free to customise properties, values and conditions to match your requirements.
65
+
66
+
```ts
67
+
// sprinkles.css.ts
68
+
import {
69
+
defineProperties,
70
+
createSprinkles
71
+
} from'@vanilla-extract/sprinkles';
72
+
73
+
const space = {
74
+
none: 0,
75
+
small: '4px',
76
+
medium: '8px',
77
+
large: '16px'
78
+
// etc.
79
+
};
80
+
81
+
const responsiveProperties =defineProperties({
82
+
conditions: {
83
+
mobile: {},
84
+
tablet: { '@media': 'screen and (min-width: 768px)' },
85
+
desktop: { '@media': 'screen and (min-width: 1024px)' }
You can now use your `sprinkles` function in `.css.ts` files for zero-runtime usage.
161
+
162
+
```ts
163
+
// styles.css.ts
164
+
import { sprinkles } from'./sprinkles.css.ts';
165
+
166
+
exportconst container =sprinkles({
167
+
display: 'flex',
168
+
paddingX: 'small',
169
+
170
+
// Conditional sprinkles:
171
+
flexDirection: {
172
+
mobile: 'column',
173
+
desktop: 'row'
174
+
},
175
+
background: {
176
+
lightMode: 'blue-50',
177
+
darkMode: 'gray-700'
178
+
}
179
+
});
180
+
```
181
+
182
+
If you want, you can even use your `sprinkles` function at runtime! 🏃♂️
183
+
184
+
```tsx
185
+
// app.ts
186
+
import { sprinkles } from'./sprinkles.css.ts';
187
+
188
+
const flexDirection =
189
+
Math.random() >0.5?'column':'row';
190
+
191
+
document.write(`
192
+
<section class="${sprinkles({
193
+
display: 'flex',
194
+
flexDirection
195
+
})}">
196
+
...
197
+
</section>
198
+
`);
199
+
```
200
+
201
+
> 💡 Although you don’t need to use this library at runtime, it’s designed to be as small and performant as possible. The runtime is only used to look up pre-existing class names. All styles are still generated at build time!
202
+
203
+
Within `.css.ts` files, combine with any custom styles by providing an array to vanilla-extract’s [`style`](/documentation/styling-api/#style) function.
204
+
205
+
```ts
206
+
// styles.css.ts
207
+
import { style } from'@vanilla-extract/css';
208
+
import { sprinkles } from'./sprinkles.css.ts';
209
+
210
+
exportconst container =style([
211
+
sprinkles({
212
+
display: 'flex',
213
+
padding: 'small'
214
+
}),
215
+
{
216
+
':hover': {
217
+
outline: '2px solid currentColor'
218
+
}
219
+
}
220
+
]);
221
+
```
222
+
223
+
Sprinkles uses this internally, which means that a class list returned by `sprinkles` can be treated as if it were a single class within vanilla-extract selectors.
As the sprinkles function returns a standard class list, you can combine it with a custom style using [style composition](/documentation/styling-api/#style).
346
-
347
-
```ts
348
-
// button.css.ts
349
-
import { style } from'@vanilla-extract/css';
350
-
import { sprinkles } from'./sprinkles.css';
351
-
352
-
exportconst button =style([
353
-
sprinkles({
354
-
paddingX: 'medium',
355
-
paddingY: 'small'
356
-
}),
357
-
{
358
-
transition: 'transform 0.2s ease-in-out',
359
-
':hover': {
360
-
transform: 'scale(1.1)'
361
-
}
362
-
}
363
-
]);
364
-
```
365
-
366
569
The sprinkles function also exposes a static `properties` key that lets you check whether a given property can be handled by the function.
0 commit comments