Skip to content

Commit 91022f0

Browse files
authored
docs: copy updates for style macro docs (#9170)
1 parent 2485ffa commit 91022f0

File tree

4 files changed

+32
-40
lines changed

4 files changed

+32
-40
lines changed

packages/dev/s2-docs/pages/react-aria/styling.mdx

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,25 +242,6 @@ With this configured, all states for React Aria Components can be accessed with
242242
</ListBoxItem>
243243
```
244244

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.
248-
249-
```tsx
250-
import {Checkbox} from 'react-aria-components';
251-
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
252-
253-
<Checkbox
254-
className={style({
255-
backgroundColor: {
256-
default: 'gray-100',
257-
isHovered: 'gray-200',
258-
isSelected: 'gray-900'
259-
}
260-
})}
261-
/>
262-
```
263-
264245
## Animation
265246

266247
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/).
File renamed without changes.

packages/dev/s2-docs/pages/s2/styling.mdx

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ React Spectrum includes a build-time `style` macro that generates atomic CSS and
1414

1515
## Style macro
1616

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.
2018

2119
```tsx
2220
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
@@ -42,7 +40,7 @@ Colocating styles with your component code means:
4240
<Heading>Important Note</Heading>
4341
<Content>
4442
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.
4644
</Content>
4745
</InlineAlert>
4846

@@ -97,7 +95,7 @@ import {Button} from '@react-spectrum/s2';
9795

9896
## Conditional styles
9997

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.
10199

102100
```tsx
103101
<div
@@ -113,9 +111,9 @@ Define conditional values as objects to handle media queries, UI states (hover/p
113111

114112
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:
115113

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`.
119117

120118
Conditions are mutually exclusive and ordered. The macro uses CSS cascade layers so the last matching condition wins without specificity issues.
121119

@@ -196,14 +194,10 @@ const styles = style({
196194

197195
## Reusing styles
198196

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.
200198

201199
```tsx
202-
// style-utils.ts
203-
export const bannerBackground = () => 'blue-1000' as const;
204-
205200
// component.tsx
206-
import {bannerBackground} from './style-utils' with {type: 'macro'};
207201
const horizontalStack = {
208202
display: 'flex',
209203
alignItems: 'center',
@@ -212,12 +206,11 @@ const horizontalStack = {
212206

213207
const styles = style({
214208
...horizontalStack,
215-
backgroundColor: bannerBackground(),
216209
columnGap: 4
217210
});
218211
```
219212

220-
Create custom utilities by defining your own macros.
213+
Create custom utilities by defining your own macros as functions in a separate file.
221214

222215
```ts
223216
// style-utils.ts
@@ -280,7 +273,7 @@ const childStyle = style({
280273

281274
## CSS optimization
282275

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.
284277
Follow these best practices:
285278

286279
- 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
335328
@import "reset.css" layer(reset);
336329
```
337330

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).
334+
335+
```tsx
336+
import {Checkbox} from 'react-aria-components';
337+
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
338+
339+
<Checkbox
340+
className={style({
341+
backgroundColor: {
342+
default: 'gray-100',
343+
isHovered: 'gray-200',
344+
isSelected: 'gray-900'
345+
}
346+
})}
347+
/>
348+
```
349+
350+
## Developer tools
339351

340-
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:
342353

343354
- The [atomic-css-devtools](https://github.com/astahmer/atomic-css-devtools) extension presents an inspected element's atomic CSS rules
344355
in a non-atomic format, making it easier to scan.
@@ -347,7 +358,7 @@ in a non-atomic format, making it easier to scan.
347358
the `style` macros for quick prototyping.
348359

349360
- 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.
351362

352363
## FAQ
353364

packages/dev/s2-docs/src/S2FAQ.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function S2FAQ() {
3535
<Disclosure isQuiet>
3636
<DisclosureTitle>Where can I find a list of what values are supported by the style macro?</DisclosureTitle>
3737
<DisclosurePanel>
38-
See the <Link href="./reference.html">following page</Link> for a full list of what values are supported by the <code>style</code> macro.
38+
See the <Link href="style-macro.html">following page</Link> for a full list of what values are supported by the <code>style</code> macro.
3939
</DisclosurePanel>
4040
</Disclosure>
4141
</div>

0 commit comments

Comments
 (0)