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/s2/styling.mdx
+31-11Lines changed: 31 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -335,9 +335,36 @@ const childStyle = style({
335
335
});
336
336
```
337
337
338
-
## Creating custom components with style macros
338
+
## Creating custom components
339
339
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"
function MyComponent({isQuiet, styles}: {isQuiet?:boolean, styles?:StyleString}) {
352
+
let result =mergeStyles(
353
+
baselineStyles(null, styles),
354
+
isQuiet?quietStyles:null
355
+
);
356
+
357
+
return <divclassName={result}>My component</div>
358
+
}
359
+
360
+
// Displays quiet styles
361
+
<MyComponentisQuietstyles={userStyles} />
362
+
363
+
// Displays user overrides
364
+
<MyComponentstyles={userStyles} />
365
+
```
366
+
367
+
The `iconStyle` macro should be used when styling Icons, see the [docs]((icons.html#iconstyle)) for more information.
341
368
342
369
## CSS optimization
343
370
@@ -395,7 +422,7 @@ CSS resets are strongly discouraged. Global CSS selectors can unintentionally af
395
422
@import"reset.css" layer(reset);
396
423
```
397
424
398
-
###Developing with style macros
425
+
## Developing with style macros
399
426
400
427
Since `style` macros are quite different from using `className`/`style` directly, many may find it initially challenging to debug and develop against.
401
428
Below are some useful tools that may benefit your developer experience:
@@ -409,18 +436,13 @@ the `style` macros for quick prototyping.
409
436
- 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,
410
437
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.
411
438
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
-
416
439
## FAQ
417
440
418
441
> I'm getting a "Could not statically evaluate macro argument" error.
419
442
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
421
444
as if you've referenced non-constant variables within your `style` macro or if you've called non-macro functions within your `style` macro.
422
445
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"?
424
446
425
447
> I'm seeing a ton of duplicate rules being generated and/or my dev tools are very slow.
426
448
@@ -430,8 +452,6 @@ Please make sure you've followed the [best practices listed above](#css-optimiza
430
452
431
453
The `style` macro is not meant to be used with `UNSAFE_className`. Overrides to the Spectrum styles is highly discouraged,
432
454
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
435
455
436
456
> I'm coming from S1, but where are Flex/Grid/etc?
0 commit comments