Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
edeaa2f
add FAQ and other sections
LFDanLu Oct 23, 2025
8a1f97e
add section for creating custom components
LFDanLu Oct 23, 2025
28eb788
initial stab at extracting types from style macro
LFDanLu Oct 24, 2025
457c59a
testing sizing properties
LFDanLu Oct 24, 2025
a15c24f
Merge branch 'main' of github.com:adobe/react-spectrum into reference…
LFDanLu Oct 24, 2025
819c076
revert export of theme object since it was breaking inference
LFDanLu Oct 24, 2025
3b991ea
adding more values
LFDanLu Oct 28, 2025
42b0406
reorg
LFDanLu Oct 28, 2025
36f1a6f
add the rest of the properties and double check them
LFDanLu Oct 28, 2025
e2b29c9
add more type links
LFDanLu Oct 28, 2025
4c94cd0
add mdn links for common types and specific css properties
LFDanLu Oct 29, 2025
83262ef
add relative links so basecolors and various dimension strings link b…
LFDanLu Oct 29, 2025
5d2b172
add descriptions to table and get rid of unessasary isRelative
LFDanLu Oct 30, 2025
980d3d6
add short hand and condition tables
LFDanLu Oct 30, 2025
59e9400
add subpage for reference and advanced style macro section
LFDanLu Oct 30, 2025
a414886
support subpage list under the ToC
LFDanLu Oct 31, 2025
0b2751d
forgot to move last bit of content
LFDanLu Oct 31, 2025
4ef4996
update FAQ, add mobile related pages, link from RAC styling
LFDanLu Oct 31, 2025
b04b7af
Merge branch 'main' of github.com:adobe/react-spectrum into style_mac…
LFDanLu Oct 31, 2025
807beb0
fix mobile styling
LFDanLu Oct 31, 2025
0c6ac3f
review comments
LFDanLu Nov 3, 2025
a3d57a9
fix build links?
LFDanLu Nov 3, 2025
30bab22
rename mdx file to fix all links
LFDanLu Nov 3, 2025
44b0d8d
Merge branch 'main' of github.com:adobe/react-spectrum into style_mac…
LFDanLu Nov 3, 2025
5084b6b
small fixes
LFDanLu Nov 3, 2025
34ad8cc
move reference out of subpage and merge styling page
LFDanLu Nov 3, 2025
6bf1724
make icons page lowercase and fix shorthands link
LFDanLu Nov 4, 2025
6e508f2
whoops forgot to make git track the capitalization rename
LFDanLu Nov 4, 2025
4b38f98
addressing feedback from quarry team
LFDanLu Nov 4, 2025
2e13868
Merge branch 'main' of github.com:adobe/react-spectrum into style_mac…
LFDanLu Nov 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@react-spectrum/s2/style/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const iconSizes = {

export function iconStyle(this: MacroContext | void, options: IconStyle): StyleString<Exclude<keyof IconStyle, 'color' | 'size'>> {
let {size = 'M', color, ...styles} = options;

if (color) {
styles['--iconPrimary'] = {
type: 'fill',
Expand Down
80 changes: 80 additions & 0 deletions packages/dev/s2-docs/pages/s2/Reference.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {Layout} from '../../src/Layout';
import {InlineAlert, Heading, Content, Link} from '@react-spectrum/s2';
import {S2Colors} from '../../src/S2Colors';
import {S2Typography} from '../../src/S2Typography';
import {StyleMacroProperties} from '../../src/types';
import {getPropertyDefinitions, getShorthandDefinitions} from '../../src/styleProperties';
export default Layout;

export const section = 'Guides';
export const tags = ['style', 'macro', 'spectrum', 'custom', 'values', 'reference'];
export const description = 'Reference table for the `style` macro';

# Style Macro Reference

Reference for the properties and values supported by React Spectrum `style` macro.

## Colors

All Spectrum 2 color tokens are available across color properties (e.g., `backgroundColor`, `color`, `borderColor`).

<S2Colors />

<StyleMacroProperties properties={getPropertyDefinitions('color')} />

## Dimensions

Spacing props like `margin` and `padding` accept values on a **4px grid**. These are specified in `px` and get converted to `rem`. In addition to numbers, these named options are available:

- `edge-to-text` – default spacing between the edge of a control and its text. Relative to control height.
- `pill` – default spacing between the edge of a pill-shaped control and its text. Relative to control height.
- `text-to-control` – default spacing between text and a control (e.g., label and input). Scales with font size.
- `text-to-visual` – default spacing between text and a visual element (e.g., icon). Scales with font size.

Size props like `width` and `height` accept arbitrary pixel values. Values are converted to `rem` and multiplied by 1.25x on touch devices to increase hit targets.

<StyleMacroProperties properties={getPropertyDefinitions('dimensions')} />

## Text

Spectrum 2 typography can be applied via the `font` [shorthand](#shorthand), which sets `fontFamily`, `fontSize`, `fontWeight`, `lineHeight`, and `color`. You can override any of these individually.

```tsx
<main>
<h1 className={style({font: 'heading-xl'})}>Heading</h1>
<p className={style({font: 'body'})}>Body</p>
<ul className={style({font: 'body-sm', fontWeight: 'bold'})}>
<li>List item</li>
</ul>
</main>
```

Type scales include: UI, Body, Heading, Title, Detail, and Code. Each scale has a default and additional t-shirt sizes (e.g., `ui-sm`, `heading-2xl`, `code-xl`).

<S2Typography />


<StyleMacroProperties properties={getPropertyDefinitions('text')} />


## Effects

<StyleMacroProperties properties={getPropertyDefinitions('effects')} />

## Layout

<StyleMacroProperties properties={getPropertyDefinitions('layout')} />

## Misc

<StyleMacroProperties properties={getPropertyDefinitions('misc')} />

## Shorthands

Shorthands apply their provided value to commonly grouped properties.

<StyleMacroProperties properties={getShorthandDefinitions('shorthand')} />

## Conditions

<StyleMacroProperties properties={getPropertyDefinitions('conditions')} />
109 changes: 101 additions & 8 deletions packages/dev/s2-docs/pages/s2/styling.mdx
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For those reviewing this page, compare the current experience with the old: https://reactspectrum.blob.core.windows.net/reactspectrum/5084b6bfa7066fbe90e520c9575422bf0df27dd0/s2-docs/s2/styling.html

In particular the organization/existence of subpage/contents, open to opinions as to which is preferred

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const description = 'Styling in React Spectrum';

# Styling

React Spectrum includes a build-time style macro that generates atomic CSS and lets you apply Spectrum tokens directly in your components with type-safe autocompletion.
React Spectrum includes a build-time `style` macro that generates atomic CSS and lets you apply Spectrum tokens directly in your components with type-safe autocompletion.

## Style macro

Expand All @@ -37,6 +37,14 @@ Colocating styles with your component code means:
- Develop more efficiently – no switching files or writing selectors.
- Refactor with confidence – changes are isolated; deleting a component removes its styles.

<InlineAlert variant="informative">
<Heading>Important Note</Heading>
<Content>
Due to the atomic nature of the generated CSS rules, it is strongly recommended that you follow the best practices listed [below](#css-optimization).
Failure to do so can result in large number of duplicate rules and obtuse styling bugs.
</Content>
</InlineAlert>
Comment on lines 41 to 47
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe overkill, but it felt like it was important to mention this up front so people realize that this is important


## Spectrum components

The `styles` prop accepts a limited set of CSS properties, including layout, spacing, sizing, and positioning. Other styles such as colors and internal padding cannot be customized within Spectrum components.
Expand Down Expand Up @@ -88,7 +96,7 @@ import {Button} from '@react-spectrum/s2';

### UNSAFE Style Overrides

We highly discourage overriding the styles of React Spectrum components because it may break at any time when we change our implementation, making it difficult for you to update in the future. Consider using [React Aria Components](https://react-spectrum.adobe.com/react-aria/) with our style macro to build a custom component with Spectrum styles instead.
We highly discourage overriding the styles of React Spectrum components because it may break at any time when we change our implementation, making it difficult for you to update in the future. Consider using [React Aria Components](https://react-spectrum.adobe.com/react-aria/) with our `style` macro to build a custom component with Spectrum styles instead.

With that being said, the `UNSAFE_className` and `UNSAFE_style` props are supported on Spectrum 2 components as last-resort escape hatches.

Expand Down Expand Up @@ -153,7 +161,7 @@ Type scales include: UI, Body, Heading, Title, Detail, and Code. Each scale has
<InlineAlert variant="notice">
<Heading>Important Note</Heading>
<Content>
Only use `<Heading>` and `<Text>` inside Spectrum components with predefined styles (e.g., `<Dialog>`, `<MenuItem>`). They are unstyled by default and should not be used standalone. Use HTML elements with the style macro instead.
Only use `<Heading>` and `<Text>` inside Spectrum components with predefined styles (e.g., `<Dialog>`, `<MenuItem>`). They are unstyled by default and should not be used standalone. Use HTML elements with the `style` macro instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typography section should also explain that you're expected to specify the font everywhere, we do not set it at a global level any longer
people have gotten confused on this point
I'll send a link in slack for one of the discussions

</Content>
</InlineAlert>

Expand Down Expand Up @@ -195,13 +203,14 @@ function MyComponent({variant}: {variant: 'primary' | 'secondary'}) {
}
```

Boolean conditions starting with `is` can be used directly without nesting:
Boolean conditions starting with `is` or `allows` can be used directly without nesting:

```tsx
const styles = style({
backgroundColor: {
default: 'gray-100',
isSelected: 'gray-900'
isSelected: 'gray-900',
allowsRemoving: 'gray-400'
}
});

Expand All @@ -222,7 +231,7 @@ import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
isSelected: 'gray-900'
}
})}
/>
/>
```

### Nesting conditions
Expand Down Expand Up @@ -308,9 +317,58 @@ const buttonStyle = style({
<Button styles={buttonStyle}>Press me</Button>
```

## Setting CSS variables

CSS variables can be directly defined in a `style` macro, allowing child elements to then access them in their own styles.
A `type` should be provided to specify the CSS property type the `value` represents.

```tsx
const parentStyle = style({
'--rowBackgroundColor': {
type: 'backgroundColor',
value: 'gray-400'
}
});

const childStyle = style({
backgroundColor: '--rowBackgroundColor'
});
```

## Creating custom components

In-depth examples are coming soon!

`mergeStyles` can be used to merge the style strings from multiple macros together, with the latter styles taking precedence similar to object spreading.
This behavior can be leveraged to create a component API that allows an end user to only override specific styles conditionally.
Comment on lines +281 to +286
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed previously that for this first pass that we want to stick with just modifying this page and create examples of style macros with RAC after the release of the docs. I've opted to keep this section and the once above it for now just to let people know about the existence of mergeStyles/iconStyle/and setting CSS variables with macros, but ideally these would move into an "Advanced" page or something similar to cut down on length here. Will comment on the file as to what I'm thinking the final division should look like

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we export mergeStyles yet? I don't see it in our exports?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not yet, but there was mention of doing so a while back in the beta slack channel. I've put it in the S2 1.0 canvas so we can track that


```tsx
// User can override the component's background color ONLY if it isn't "quiet"
const baselineStyles = style({backgroundColor: 'gray-100'}, ['backgroundColor']);
const quietStyles = style({backgroundColor: 'transparent'});
const userStyles = style({backgroundColor: 'celery-600'});

function MyComponent({isQuiet, styles}: {isQuiet?: boolean, styles?: StyleString}) {
let result = mergeStyles(
baselineStyles(null, styles),
isQuiet ? quietStyles : null
);

return <div className={result}>My component</div>
}

// Displays quiet styles
<MyComponent isQuiet styles={userStyles} />

// Displays user overrides
<MyComponent styles={userStyles} />
```

The `iconStyle` macro should be used when styling Icons, see the [docs]((icons.html#iconstyle)) for more information.

## CSS optimization
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimization sounds so optional

maybe it'd be better to structure all bundler setups into their own page which includes how the optimization setup works, but then it all sounds much more mandatory

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy for the team to weigh in here then, especially since it would fall within the changes made in #9126


The style macro relies on CSS bundling and minification for optimal output. Follow these best practices:
The `style` macro relies on CSS bundling and minification for optimal output. Follow these best practices:

- Ensure styles are extracted into a CSS bundle; do not inject at runtime with `<style>` tags.
- Use a CSS minifier like `lightningcss` to deduplicate common rules (consider in dev for easier debugging).
Expand Down Expand Up @@ -362,4 +420,39 @@ CSS resets are strongly discouraged. Global CSS selectors can unintentionally af
/* App.css */
@layer reset, _;
@import "reset.css" layer(reset);
```
```

## Developing with style macros

Since `style` macros are quite different from using `className`/`style` directly, many may find it initially challenging to debug and develop against.
Below are some useful tools that may benefit your developer experience:

- The [atomic-css-devtools](https://github.com/astahmer/atomic-css-devtools) extension presents an inspected element's atomic CSS rules
in a non-atomic format, making it easier to scan.
Comment on lines +375 to +376
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: replace with the new devtool extension @snowystinger is making


- This [sandbox](https://codesandbox.io/p/devbox/react-spectrum-s2-style-macro-template-h6fpsq) is preconfigured to support React Spectrum S2, React Aria Components, and
the `style` macros for quick prototyping.

- 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,
we have MCP servers for [React Aria](#TODO) and [React Spectrum](https://www.npmjs.com/package/@react-spectrum/mcp) respectively that interface with the docs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can link to the respective /mcp docs pages instead.


## FAQ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make use of disclosures here? (but maybe a follow-up if it takes too long to style and what not)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats a good suggestion, I was a bit torn on if the current style was better or now (inspired by our own FAQ in github) but I've definitely seen both styles on other sites like MUI and Untitled UI. I'll bring this up with the rest of the team and see what people like


> I'm getting a "Could not statically evaluate macro argument" error.

This indicates that your `style` macro has a condition that isn't evaluable at build time. This can happen for a variety of reasons such
as if you've referenced non-constant variables within your `style` macro or if you've called non-macro functions within your `style` macro.
If you are using Typescript, it can be as simple as forgetting to add `as const` to your own defined reusable macro.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an example of a macro that would cause this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like there's several ways you could encounter this error (based on vibes) so a single example wouldn't be enough and might cause confusion for some people if their code doesn't match the example.

maybe would be good to find the most common ways you can encounter this error and offer a couple of possible solutions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, its just been kinda hard finding which ways are the common ways people encounter this. I tried to allude to those fixes above but didn't want to fully document each since it might take too much room


> I'm seeing a ton of duplicate rules being generated and/or my dev tools are very slow.

Please make sure you've followed the [best practices listed above](#css-optimization).

> I tried to pass my `style` macro to `UNSAFE_className` but it doesn't work.

The `style` macro is not meant to be used with `UNSAFE_className`. Overrides to the Spectrum styles is highly discouraged,
consider styling an equivalent React Aria Component instead.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already mentioned in the UNSAFE style overrides section but felt it was worth adding here anyways


> I'm coming from S1, but where are Flex/Grid/etc?

These no longer exist. Please style `<div>`, `<span>`, and other standard HTML elements with the `style` macro instead.
23 changes: 20 additions & 3 deletions packages/dev/s2-docs/src/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ const components = {
p: ({children, ...props}) => <p {...props} className={style({font: {default: 'body', lg: 'body-lg'}, marginY: 24})}>{children}</p>,
ul: (props) => <ul {...props} />,
li: ({children, ...props}) => <li {...props} className={style({font: {default: 'body', lg: 'body-lg'}, marginY: 0})}>{children}</li>,
blockquote: ({children, ...props}) => (
<blockquote
{...props}
className={style({
borderStartWidth: 4,
borderEndWidth: 0,
borderTopWidth: 0,
borderBottomWidth: 0,
borderStyle: 'solid',
borderColor: 'gray-400',
paddingStart: 12,
font: {default: 'body', lg: 'body-lg'},
margin: 'unset'
})}>
{children}
</blockquote>
),
Figure: (props) => <figure {...props} className={style({display: 'flex', flexDirection: 'column', alignItems: 'center', marginY: 32, marginX: 0})} />,
Caption: (props) => <figcaption {...props} className={style({font: 'body-sm'})} />,
CodeBlock: CodeBlock,
Expand All @@ -56,14 +73,14 @@ const getTitle = (currentPage: Page): string => {
if (explicitTitle && explicitTitle !== currentPage.tableOfContents?.[0]?.title && explicitTitle !== currentPage.name) {
return explicitTitle as string;
}

let library = getLibraryLabel(getLibraryFromPage(currentPage));
const pageTitle = currentPage.tableOfContents?.[0]?.title ?? currentPage.name;

if (currentPage.name === 'index.html' || currentPage.name.endsWith('/index.html')) {
return library || 'React Spectrum';
}

return library ? `${pageTitle} | ${library}` : pageTitle;
};

Expand Down
Loading