Skip to content

Commit 23ea462

Browse files
Add Sprinkles usage to docs site (#542)
Co-authored-by: Michael Taranto <[email protected]>
1 parent 5e7ec86 commit 23ea462

File tree

1 file changed

+224
-21
lines changed

1 file changed

+224
-21
lines changed

site/docs/sprinkles-api.md

Lines changed: 224 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,234 @@ Generate a static set of custom utility classes and compose them either statical
88

99
Basically, it’s like building your own zero-runtime, type-safe version of [Tailwind](https://tailwindcss.com), [Styled System](https://styled-system.com), etc.
1010

11+
**Compose sprinkles statically at build time.**
12+
13+
```ts
14+
// styles.css.ts
15+
16+
export const 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+
> 🖥 &nbsp; [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+
1158
```bash
1259
$ npm install @vanilla-extract/sprinkles
1360
```
1461

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)' }
86+
},
87+
defaultCondition: 'mobile',
88+
properties: {
89+
display: ['none', 'flex', 'block', 'inline'],
90+
flexDirection: ['row', 'column'],
91+
justifyContent: [
92+
'stretch',
93+
'flex-start',
94+
'center',
95+
'flex-end',
96+
'space-around',
97+
'space-between'
98+
],
99+
alignItems: [
100+
'stretch',
101+
'flex-start',
102+
'center',
103+
'flex-end'
104+
],
105+
paddingTop: space,
106+
paddingBottom: space,
107+
paddingLeft: space,
108+
paddingRight: space
109+
// etc.
110+
},
111+
shorthands: {
112+
padding: [
113+
'paddingTop',
114+
'paddingBottom',
115+
'paddingLeft',
116+
'paddingRight'
117+
],
118+
paddingX: ['paddingLeft', 'paddingRight'],
119+
paddingY: ['paddingTop', 'paddingBottom'],
120+
placeItems: ['justifyContent', 'alignItems']
121+
}
122+
});
123+
124+
const colors = {
125+
'blue-50': '#eff6ff',
126+
'blue-100': '#dbeafe',
127+
'blue-200': '#bfdbfe',
128+
'gray-700': '#374151',
129+
'gray-800': '#1f2937',
130+
'gray-900': '#111827'
131+
// etc.
132+
};
133+
134+
const colorProperties = defineProperties({
135+
conditions: {
136+
lightMode: {},
137+
darkMode: { '@media': '(prefers-color-scheme: dark)' }
138+
},
139+
defaultCondition: 'lightMode',
140+
properties: {
141+
color: colors,
142+
background: colors
143+
// etc.
144+
}
145+
});
146+
147+
export const sprinkles = createSprinkles(
148+
responsiveProperties,
149+
colorProperties
150+
);
151+
152+
// It's a good idea to export the Sprinkles type too
153+
export type Sprinkles = Parameters<typeof sprinkles>[0];
154+
```
155+
156+
**🎉 That's it — you’re ready to go!**
157+
158+
## Usage
159+
160+
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+
export const 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+
export const 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.
224+
225+
```ts
226+
// styles.css.ts
227+
import { globalStyle } from '@vanilla-extract/css';
228+
import { sprinkles } from './sprinkles.css.ts';
229+
230+
export const container = sprinkles({
231+
padding: 'small'
232+
});
233+
234+
globalStyle(`${container} *`, {
235+
boxSizing: 'border-box'
236+
});
237+
```
238+
15239
## defineProperties
16240

17241
Defines a collection of utility classes with [properties](#properties), [conditions](#conditions) and [shorthands.](#shorthands)
@@ -342,27 +566,6 @@ export const sprinkles = createSprinkles(
342566
);
343567
```
344568

345-
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-
export const 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-
366569
The sprinkles function also exposes a static `properties` key that lets you check whether a given property can be handled by the function.
367570

368571
```ts

0 commit comments

Comments
 (0)