@mochi-css/vanilla 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,360 @@
1
- <p align="center">
2
- <img width="256" height="256" src="../../assets/mochi-vanilla256.png" alt="mochi-css logo">
3
- </p>
1
+ # Mochi-CSS/vanilla
4
2
 
5
- # 🧁 Mochi-CSS/vanilla
3
+ This package is part of the [Mochi-CSS project](https://github.com/Niikelion/mochi-css). It provides type-safe CSS-in-JS styling functions with static extraction support, allowing you to write styles in TypeScript that get extracted to plain CSS at build time.
6
4
 
7
- 🚧 Under construction 🚧
5
+ ## Functions
6
+
7
+ ### `css(...styles)`
8
+
9
+ `css` is the fundamental styling function of Mochi-CSS.
10
+ It takes style definitions as parameters, marks them for static extraction and returns class names to apply on elements.
11
+
12
+ ```tsx
13
+ import {css} from "@mochi-css/vanilla"
14
+
15
+ const buttonStyles = css({
16
+ borderRadius: 10,
17
+ border: "2px solid red"
18
+ })
19
+
20
+ // Use in JSX
21
+ <button className={buttonStyles}>Click me</button>
22
+ ```
23
+
24
+ Output of the `css` function is also a valid style definition, so you can split your styles:
25
+
26
+ ```ts
27
+ const textStyles = css({
28
+ color: "green"
29
+ })
30
+
31
+ const buttonStyles = css(textStyles, {
32
+ borderRadius: 10,
33
+ border: "2px solid red"
34
+ })
35
+ ```
36
+
37
+ ### `styled(component, ...styles)`
38
+
39
+ `styled` creates a styled component by combining a base element or component with style definitions. It automatically applies the generated class names and forwards variant props.
40
+
41
+ ```tsx
42
+ import {styled} from "@mochi-css/vanilla"
43
+
44
+ const Button = styled("button", {
45
+ borderRadius: 10,
46
+ border: "2px solid red"
47
+ })
48
+ ```
49
+
50
+ ## Style definitions
51
+
52
+ A style definition is either a bundle of styles returned by `css`, or an object containing:
53
+
54
+ * any number of valid CSS properties converted to camelCase, like in React's style property
55
+ * any number of CSS variable assignments (see [Tokens](#tokens))
56
+ * optional variants definition
57
+ * optional default variants definition
58
+
59
+ ## Nested Selectors
60
+
61
+ Mochi-CSS supports nested selectors, allowing you to define styles for child elements, pseudo-classes, and pseudo-elements directly within your style definitions.
62
+
63
+ The `&` character represents the parent selector and must be included in every nested selector:
64
+
65
+ ```ts
66
+ import { css } from "@mochi-css/vanilla"
67
+
68
+ const buttonStyle = css({
69
+ backgroundColor: "blue",
70
+ color: "white",
71
+
72
+ // Pseudo-classes
73
+ "&:hover": {
74
+ backgroundColor: "darkblue"
75
+ },
76
+ "&:active": {
77
+ backgroundColor: "navy"
78
+ },
79
+ "&:disabled": {
80
+ backgroundColor: "gray",
81
+ cursor: "not-allowed"
82
+ },
83
+
84
+ // Pseudo-elements
85
+ "&::before": {
86
+ content: '""',
87
+ display: "block"
88
+ },
89
+
90
+ // Child selectors
91
+ "& > span": {
92
+ fontWeight: "bold"
93
+ },
94
+
95
+ // Descendant selectors
96
+ "& p": {
97
+ margin: 0
98
+ },
99
+
100
+ // Compound selectors (when element also has another class)
101
+ "&.active": {
102
+ borderColor: "green"
103
+ },
104
+
105
+ // Adjacent sibling
106
+ "& + &": {
107
+ marginTop: 8
108
+ }
109
+ })
110
+ ```
111
+
112
+ ### Selector Position
113
+
114
+ The `&` can appear anywhere in the selector, allowing for flexible parent-child relationships:
115
+
116
+ ```ts
117
+ const linkStyle = css({
118
+ color: "blue",
119
+
120
+ // Parent context: style this element when inside .dark-mode
121
+ ".dark-mode &": {
122
+ color: "lightblue"
123
+ },
124
+
125
+ // Multiple parent contexts
126
+ "nav &, footer &": {
127
+ textDecoration: "underline"
128
+ }
129
+ })
130
+ ```
131
+
132
+ ## Media Selectors
133
+
134
+ Media selectors allow you to apply styles conditionally based on viewport size, color scheme preferences, and other media features. Media selectors start with the `@` symbol and are compiled into CSS media queries:
135
+
136
+ ```ts
137
+ import { css } from "@mochi-css/vanilla"
138
+
139
+ const responsiveContainer = css({
140
+ display: "flex",
141
+ flexDirection: "row",
142
+ padding: 32,
143
+
144
+ // Responsive breakpoints
145
+ "@max-width: 768px": {
146
+ flexDirection: "column",
147
+ padding: 16
148
+ },
149
+
150
+ "@max-width: 480px": {
151
+ padding: 8
152
+ },
153
+
154
+ // Modern range syntax
155
+ "@width <= 1024px": {
156
+ gap: 16
157
+ },
158
+
159
+ // Color scheme preferences
160
+ "@prefers-color-scheme: dark": {
161
+ backgroundColor: "#1a1a1a",
162
+ color: "white"
163
+ },
164
+
165
+ // Reduced motion
166
+ "@prefers-reduced-motion: reduce": {
167
+ transition: "none"
168
+ }
169
+ })
170
+ ```
171
+
172
+ ### Combined Media Selectors
173
+
174
+ You can combine multiple media conditions using `and` and `not` operators:
175
+
176
+ ```ts
177
+ const responsiveLayout = css({
178
+ display: "grid",
179
+ gridTemplateColumns: "1fr",
180
+
181
+ // Screen media type with width condition
182
+ "@screen and (width > 1000px)": {
183
+ gridTemplateColumns: "1fr 1fr"
184
+ },
185
+
186
+ // Multiple conditions with 'and'
187
+ "@screen and (min-width: 768px) and (max-width: 1024px)": {
188
+ gridTemplateColumns: "1fr 1fr",
189
+ gap: 16
190
+ },
191
+
192
+ // Print styles
193
+ "@print": {
194
+ display: "block"
195
+ },
196
+
197
+ // Combining media type with preference
198
+ "@screen and (not (prefers-color-scheme: dark))": {
199
+ backgroundColor: "#121212"
200
+ }
201
+ })
202
+ ```
203
+
204
+ ### Combining Nested Selectors with Media Selectors
205
+
206
+ Nested selectors and media selectors can be combined for fine-grained control:
207
+
208
+ ```ts
209
+ const buttonStyle = css({
210
+ backgroundColor: "blue",
211
+
212
+ "&:hover": {
213
+ backgroundColor: "darkblue",
214
+
215
+ // Media selector inside nested selector
216
+ "@width <= 480px": {
217
+ // Disable hover effects on mobile (touch devices)
218
+ backgroundColor: "blue"
219
+ }
220
+ },
221
+
222
+ // Media selector with nested selectors inside
223
+ "@max-width: 768px": {
224
+ padding: 8,
225
+
226
+ "& > span": {
227
+ display: "none"
228
+ }
229
+ }
230
+ })
231
+ ```
232
+
233
+ ### Using with Variants
234
+
235
+ Nested selectors and media selectors work seamlessly inside variant definitions:
236
+
237
+ ```ts
238
+ const cardStyle = css({
239
+ padding: 16,
240
+ borderRadius: 8,
241
+
242
+ variants: {
243
+ size: {
244
+ small: {
245
+ padding: 8,
246
+ "@max-width: 480px": {
247
+ padding: 4
248
+ }
249
+ },
250
+ large: {
251
+ padding: 32,
252
+ "@max-width: 480px": {
253
+ padding: 16
254
+ }
255
+ }
256
+ },
257
+ interactive: {
258
+ true: {
259
+ cursor: "pointer",
260
+ "&:hover": {
261
+ transform: "translateY(-2px)"
262
+ }
263
+ },
264
+ false: {}
265
+ }
266
+ },
267
+ defaultVariants: {
268
+ size: "small",
269
+ interactive: false
270
+ }
271
+ })
272
+ ```
273
+
274
+ ## Variants
275
+
276
+ You may want to create multiple variants of a button that share a common base style:
277
+
278
+ ```ts
279
+ import {css} from "@mochi-css/vanilla"
280
+
281
+ const baseButtonStyle = css({
282
+ border: "2px solid black",
283
+ color: "black",
284
+ backgroundColor: "white"
285
+ })
286
+
287
+ const redButtonStyle = css(baseButtonStyle, {
288
+ backgroundColor: "red"
289
+ })
290
+ ```
291
+
292
+ This works, but requires you to either manually select which style to apply in your component logic, or create separate components for each variant.
293
+ Mochi-CSS allows you to define variants directly in your style definition and automatically generates the corresponding props for your component.
294
+
295
+ ```tsx
296
+ import {styled, css} from "@mochi-css/vanilla"
297
+
298
+ const buttonStyle = css({
299
+ border: "2px solid black",
300
+ color: "black",
301
+ backgroundColor: "white",
302
+ variants: {
303
+ color: {
304
+ white: {
305
+ backgroundColor: "white"
306
+ },
307
+ red: {
308
+ backgroundColor: "red"
309
+ }
310
+ }
311
+ },
312
+ defaultVariants: {
313
+ color: "white"
314
+ }
315
+ })
316
+
317
+ const Button = styled("button", buttonStyle)
318
+
319
+ const SomeComponent = () => <div>
320
+ <Button>White button</Button>
321
+ <Button color="red">Red button</Button>
322
+ </div>
323
+ ```
324
+
325
+ `defaultVariants` is optional, but specifying defaults for all variants ensures predictable styling when variant props are omitted.
326
+
327
+ ## Tokens
328
+
329
+ Mochi-CSS provides typed wrappers around CSS variables to help with type safety. Tokens ensure that only valid values are assigned to your CSS variables.
330
+
331
+ Create tokens using the `createToken<T>(name)` function, where `T` is a CSS value type like `CssColorLike`, `CssLengthLike`, or `string`:
332
+
333
+ ```ts
334
+ import {createToken, css, CssColorLike} from "@mochi-css/vanilla"
335
+
336
+ const primaryColor = createToken<CssColorLike>("primaryColor")
337
+ const secondaryColor = createToken<CssColorLike>("secondaryColor")
338
+ const buttonColor = createToken<CssColorLike>("buttonColor")
339
+
340
+ const buttonStyle = css({
341
+ backgroundColor: buttonColor,
342
+ variants: {
343
+ variant: {
344
+ primary: {
345
+ [buttonColor.variable]: primaryColor
346
+ },
347
+ secondary: {
348
+ [buttonColor.variable]: secondaryColor
349
+ }
350
+ }
351
+ },
352
+ defaultVariants: {
353
+ variant: "primary"
354
+ }
355
+ })
356
+ ```
357
+
358
+ Tokens can be used in two ways:
359
+ - **As values**: Use the token directly (e.g., `backgroundColor: buttonColor`) to reference the CSS variable
360
+ - **As keys**: Use `token.variable` (e.g., `[buttonColor.variable]: primaryColor`) to assign a value to the CSS variable