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/react-aria/styling.mdx
-19Lines changed: 0 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -242,25 +242,6 @@ With this configured, all states for React Aria Components can be accessed with
242
242
</ListBoxItem>
243
243
```
244
244
245
-
## Style macro
246
-
247
-
If you want to build custom components that follow Spectrum design tokens and styling, you can use the [style macro](../s2/styling.html) from React Spectrum. The `style` macro is a build-time CSS generator that provides type safe access to Spectrum 2 design tokens including colors, spacing, sizing, and typography.
React Aria Components supports both [CSS transitions](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_transitions/Using_CSS_transitions) and [keyframe animations](https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes), and works with JavaScript animation libraries like [Motion](https://motion.dev/).
Copy file name to clipboardExpand all lines: packages/dev/s2-docs/pages/s2/styling.mdx
+31-20Lines changed: 31 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,7 @@ 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.). 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
19
-
of supported values.
17
+
The `style` macro runs at build time and returns a class name that applies Spectrum 2 design tokens (colors, spacing, sizing, typography, etc.). See the [reference](style-macro.html) for a full list of supported values.
@@ -42,7 +40,7 @@ Colocating styles with your component code means:
42
40
<Heading>Important Note</Heading>
43
41
<Content>
44
42
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).
45
-
Failure to do so can result in large number of duplicate rules and obtuse styling bugs.
43
+
Without these optimizations, the generated CSS may contain duplicate rules that affect bundle size and debugging.
46
44
</Content>
47
45
</InlineAlert>
48
46
@@ -97,7 +95,7 @@ import {Button} from '@react-spectrum/s2';
97
95
98
96
## Conditional styles
99
97
100
-
Define conditional values as objects to handle media queries, UI states (hover/press), and variants. This keeps all values for a property together.
98
+
Define conditional values such as media queries, UI states (e.g. hover, press), and style variants as objects. Conditional values are mutually exclusive: the last matching condition always wins.
101
99
102
100
```tsx
103
101
<div
@@ -113,9 +111,9 @@ Define conditional values as objects to handle media queries, UI states (hover/p
113
111
114
112
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
113
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`.
114
+
- If the viewport is larger than `2560px`, the padding is`64px`.
115
+
-Else if the viewport matches the `lg`[breakpoint](style-macro.html#conditions) (`1024px`), the padding is `32px`.
116
+
- Otherwise, the padding is`8px`.
119
117
120
118
Conditions are mutually exclusive and ordered. The macro uses CSS cascade layers so the last matching condition wins without specificity issues.
121
119
@@ -196,14 +194,10 @@ const styles = style({
196
194
197
195
## Reusing styles
198
196
199
-
Extract common styles into constants and spread them into `style` calls. These must be in the same file or imported from another file as a macro.
197
+
Extract common styles into constants and spread them into `style` calls within the same file.
Create custom utilities by defining your own macros.
213
+
Create custom utilities by defining your own macros as functions in a separate file.
221
214
222
215
```ts
223
216
// style-utils.ts
@@ -280,7 +273,7 @@ const childStyle = style({
280
273
281
274
## CSS optimization
282
275
283
-
The `style` macro relies on CSS bundling and minification for optimal output. Failure to perform this optimization will result in a suboptimal developer experience and obtuse styling bugs.
276
+
The `style` macro relies on CSS bundling and minification for optimal output. Without these optimizations, the generated CSS may contain duplicate rules that affect bundle size and debugging.
284
277
Follow these best practices:
285
278
286
279
- Ensure styles are extracted into a CSS bundle; do not inject at runtime with `<style>` tags.
@@ -335,10 +328,28 @@ CSS resets are strongly discouraged. Global CSS selectors can unintentionally af
335
328
@import"reset.css" layer(reset);
336
329
```
337
330
338
-
## Developing with style macros
331
+
## Custom components
332
+
333
+
If you want to build custom components that follow Spectrum styling, you can use the `style` macro with [React Aria Components](../react-aria/index.html).
Since `style` macros are quite different from using `className`/`style` directly, many may find it initially challenging to debug and develop against.
341
-
Below are some useful tools that may benefit your developer experience:
352
+
These tools improve the developer experience when using style macros:
342
353
343
354
- The [atomic-css-devtools](https://github.com/astahmer/atomic-css-devtools) extension presents an inspected element's atomic CSS rules
344
355
in a non-atomic format, making it easier to scan.
@@ -347,7 +358,7 @@ in a non-atomic format, making it easier to scan.
347
358
the `style` macros for quick prototyping.
348
359
349
360
- If you are using Cursor, we offer a set of [Cursor rules](https://github.com/adobe/react-spectrum/blob/main/rules/style-macro.mdc) to use when developing with style macros. Additionally,
350
-
we have MCP servers for [React Aria](../react-aria/mcp.html) and [React Spectrum](./mcp.html) respectively that interface with the docs.
361
+
we have MCP servers for [React Aria](../react-aria/mcp.html) and [React Spectrum](mcp.html) respectively that interface with the docs.
0 commit comments