Skip to content

Commit 8a1f97e

Browse files
committed
add section for creating custom components
1 parent edeaa2f commit 8a1f97e

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,36 @@ const childStyle = style({
335335
});
336336
```
337337

338-
## Creating custom components with style macros
338+
## Creating custom components
339339

340-
TODO: I want to add the usuage of mergeStyles here from that one thread. Maybe an example with RAC + style macros as well
340+
In-depth examples are coming soon!
341+
342+
`mergeStyles` can be used to merge the style strings from multiple macros together, with the latter styles taking precedence similar to object spreading.
343+
This behavior can be leveraged to create a component API that allows an end user to only override specific styles conditionally.
344+
345+
```tsx
346+
// User can override the component's background color ONLY if it isn't "quiet"
347+
const baselineStyles = style({backgroundColor: 'gray-100'}, ['backgroundColor']);
348+
const quietStyles = style({backgroundColor: 'transparent'});
349+
const userStyles = style({backgroundColor: 'celery-600'});
350+
351+
function MyComponent({isQuiet, styles}: {isQuiet?: boolean, styles?: StyleString}) {
352+
let result = mergeStyles(
353+
baselineStyles(null, styles),
354+
isQuiet ? quietStyles : null
355+
);
356+
357+
return <div className={result}>My component</div>
358+
}
359+
360+
// Displays quiet styles
361+
<MyComponent isQuiet styles={userStyles} />
362+
363+
// Displays user overrides
364+
<MyComponent styles={userStyles} />
365+
```
366+
367+
The `iconStyle` macro should be used when styling Icons, see the [docs]((icons.html#iconstyle)) for more information.
341368

342369
## CSS optimization
343370

@@ -395,7 +422,7 @@ CSS resets are strongly discouraged. Global CSS selectors can unintentionally af
395422
@import "reset.css" layer(reset);
396423
```
397424

398-
### Developing with style macros
425+
## Developing with style macros
399426

400427
Since `style` macros are quite different from using `className`/`style` directly, many may find it initially challenging to debug and develop against.
401428
Below are some useful tools that may benefit your developer experience:
@@ -409,18 +436,13 @@ the `style` macros for quick prototyping.
409436
- 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,
410437
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.
411438

412-
413-
- TODO: link to the dev tool Rob is making, perhaps replacing the atomic css dev tool. Any others we wanna call out
414-
415-
416439
## FAQ
417440

418441
> I'm getting a "Could not statically evaluate macro argument" error.
419442
420-
This happens if your `style` macro has a condition that isn't evaluable at build time. This can happen for a variety of reasons such
443+
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
421444
as if you've referenced non-constant variables within your `style` macro or if you've called non-macro functions within your `style` macro.
422445
If you are using Typescript, it can be as simple as forgetting to add `as const` to your own defined reusable macro.
423-
TODO: create an example here, should I link to the sections above like "reusing styles"?
424446

425447
> I'm seeing a ton of duplicate rules being generated and/or my dev tools are very slow.
426448
@@ -430,8 +452,6 @@ Please make sure you've followed the [best practices listed above](#css-optimiza
430452
431453
The `style` macro is not meant to be used with `UNSAFE_className`. Overrides to the Spectrum styles is highly discouraged,
432454
consider styling an equivalent React Aria Component instead.
433-
TODO: we already touch on this via the UNSAFE Style Overrides section
434-
TODO: example of RAC + style macros? maybe later
435455

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

0 commit comments

Comments
 (0)