From edeaa2f1bb65e2a11ab7b1ae207ca1061b31bdde Mon Sep 17 00:00:00 2001 From: Daniel Lu Date: Thu, 23 Oct 2025 11:48:10 -0700 Subject: [PATCH 01/27] add FAQ and other sections --- packages/dev/s2-docs/pages/s2/styling.mdx | 89 +++++++++++++++++++++-- packages/dev/s2-docs/src/Layout.tsx | 23 +++++- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/packages/dev/s2-docs/pages/s2/styling.mdx b/packages/dev/s2-docs/pages/s2/styling.mdx index 3735835c868..4f2d70bf6f3 100644 --- a/packages/dev/s2-docs/pages/s2/styling.mdx +++ b/packages/dev/s2-docs/pages/s2/styling.mdx @@ -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 @@ -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. + + Important Note + + 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. + + + ## 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. @@ -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. @@ -153,7 +161,7 @@ Type scales include: UI, Body, Heading, Title, Detail, and Code. Each scale has Important Note - Only use `` and `` inside Spectrum components with predefined styles (e.g., ``, ``). They are unstyled by default and should not be used standalone. Use HTML elements with the style macro instead. + Only use `` and `` inside Spectrum components with predefined styles (e.g., ``, ``). They are unstyled by default and should not be used standalone. Use HTML elements with the `style` macro instead. @@ -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' } }); @@ -222,7 +231,7 @@ import {style} from '@react-spectrum/s2/style' with {type: 'macro'}; isSelected: 'gray-900' } })} -/> +/> ``` ### Nesting conditions @@ -308,9 +317,31 @@ const buttonStyle = style({ ``` +## 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 with style macros + +TODO: I want to add the usuage of mergeStyles here from that one thread. Maybe an example with RAC + style macros as well + ## CSS optimization -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 `