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
Copy file name to clipboardExpand all lines: packages/dev/s2-docs/pages/s2/styling.mdx
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,9 @@ React Spectrum includes a build-time `style` macro that generates atomic CSS and
14
14
15
15
## Style macro
16
16
17
-
The `style` macro runs at build time and returns a class name for applying Spectrum 2 design tokens (colors, spacing, sizing, typography, etc.).
17
+
The `style` macro runs at build time and returns a class name for applying Spectrum 2 design tokens (colors, spacing, sizing, typography, etc.). As can been seen below,
18
+
the keys of the object passed to the `style` macro correspond to a CSS property, each paired with the property's desired value. See [here](./reference.html) for a full list
@@ -39,7 +41,7 @@ Colocating styles with your component code means:
39
41
<InlineAlertvariant="informative">
40
42
<Heading>Important Note</Heading>
41
43
<Content>
42
-
Due to the atomic nature of the generated CSS rules, it is strongly recommended that you follow the best practices listed [below](#css-optimization).
44
+
Due to the atomic nature of the generated CSS rules, it is strongly recommended that you follow the CSS optimization guide listed [below](#css-optimization).
43
45
Failure to do so can result in large number of duplicate rules and obtuse styling bugs.
44
46
</Content>
45
47
</InlineAlert>
@@ -96,23 +98,28 @@ import {Button} from '@react-spectrum/s2';
96
98
## Conditional styles
97
99
98
100
Define conditional values as objects to handle media queries, UI states (hover/press), and variants. This keeps all values for a property together.
99
-
Note how the example below uses a predefined [breakpoint](./reference.html#conditions) alongside a custom media query.
100
101
101
102
```tsx
102
103
<div
103
104
className={style({
104
105
padding: {
105
106
default: 8,
106
107
lg: 32,
107
-
'@media (min-width: 2560px)': 64,
108
+
'@media (min-width: 2560px)': 64
108
109
}
109
110
})}
110
111
/>
111
112
```
112
113
114
+
In the example above, the keys of the nested object now map out the "conditions" that govern the padding of the `div`. This translates to the following:
115
+
116
+
- If the viewport is larger than `2560px`, as specified by a user defined [media query](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries), the padding of the `div` is set to `64px`.
117
+
- If the viewport matches the `style` macro's predefined `lg`[breakpoint](./reference.html#conditions) (i.e. the viewport is larger than `1024px`), but does not exceed previous condition, the padding of the `div` is set to `32px`
118
+
- Otherwise, default to a padding of `8px`.
119
+
113
120
Conditions are mutually exclusive and ordered. The macro uses CSS cascade layers so the last matching condition wins without specificity issues.
114
121
115
-
## Runtime conditions
122
+
###Runtime conditions
116
123
117
124
When runtime conditions are detected (e.g., variants, UI states), the macro returns a function to resolve styles at runtime.
118
125
@@ -310,6 +317,8 @@ The `style` macro relies on CSS bundling and minification for optimal output. Fo
310
317
- Use a CSS minifier like `lightningcss` to deduplicate common rules (consider in dev for easier debugging).
311
318
- Bundle all CSS for S2 components and style macros into a single CSS bundle rather than code splitting to avoid duplicate rules across chunks.
312
319
320
+
**Failure to perform this optimization will result in a suboptimal developer experience and obtuse styling bugs.**
321
+
313
322
### Parcel
314
323
315
324
Parcel supports macros out of the box and optimizes CSS with [Lightning CSS](https://lightningcss.dev). You can bundle all S2 and macro CSS into a single file using [manual shared bundles](https://parceljs.org/features/code-splitting/#manual-shared-bundles).
These no longer exist. Please style <code><div></code>, <code><span></code>, and other standard HTML elements with the <code>style</code> macro instead.
33
33
</DisclosurePanel>
34
34
</Disclosure>
35
+
<DisclosureisQuiet>
36
+
<DisclosureTitle>Where can I find a list of what values are supported by the style macro?</DisclosureTitle>
37
+
<DisclosurePanel>
38
+
See the <Linkhref="./reference.html">following page</Link> for a full list of what values are supported by the <code>style</code> macro.
0 commit comments