|
| 1 | +# JSX Props |
| 2 | + |
| 3 | +CSSX extends JSX with custom props for styling. |
| 4 | + |
| 5 | +## styleName |
| 6 | + |
| 7 | +Apply class-based styles to an element. Works like `className` but with scoped, processed styles. |
| 8 | + |
| 9 | +```jsx |
| 10 | +<div styleName="card featured">Content</div> |
| 11 | +``` |
| 12 | + |
| 13 | +**Syntax Options:** |
| 14 | + |
| 15 | +```jsx |
| 16 | +// String |
| 17 | +<div styleName="card" /> |
| 18 | + |
| 19 | +// Object - keys with truthy values are included |
| 20 | +<div styleName={{ card: true, active, highlighted }} /> |
| 21 | + |
| 22 | +// Array - falsy values are filtered out |
| 23 | +<div styleName={['card', active && 'active']} /> |
| 24 | + |
| 25 | +// Mixed - combine arrays, strings, and objects (recommended) |
| 26 | +<div styleName={['button', variant, { active, disabled }]} /> |
| 27 | +``` |
| 28 | + |
| 29 | +### Modifier Classes with Object/Array Syntax |
| 30 | + |
| 31 | +The object and array syntax is cleaner than string interpolation for conditional classes. Name your variables to match the CSS class names for the cleanest syntax: |
| 32 | + |
| 33 | +```jsx |
| 34 | +function Card({ highlighted, compact }) { |
| 35 | + return ( |
| 36 | + // Object shorthand - cleanest when variable names match class names |
| 37 | + <div styleName={{ card: true, highlighted, compact }}> |
| 38 | + Content |
| 39 | + </div> |
| 40 | + ) |
| 41 | + |
| 42 | + styl` |
| 43 | + .card |
| 44 | + background white |
| 45 | + border 1px solid #ddd |
| 46 | + padding 16px |
| 47 | + .card.highlighted |
| 48 | + border-color gold |
| 49 | + box-shadow 0 0 10px gold |
| 50 | + .card.compact |
| 51 | + padding 8px |
| 52 | + ` |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Compare this to the less readable string interpolation: |
| 57 | + |
| 58 | +```jsx |
| 59 | +// Avoid - harder to read with multiple modifiers |
| 60 | +<div styleName={`card ${highlighted ? 'highlighted' : ''} ${compact ? 'compact' : ''}`}> |
| 61 | +``` |
| 62 | + |
| 63 | +### Dynamic Styles |
| 64 | + |
| 65 | +For truly dynamic values, combine `styleName` with the `style` prop: |
| 66 | + |
| 67 | +```jsx |
| 68 | +function ProgressBar({ progress }) { |
| 69 | + return ( |
| 70 | + <div styleName="bar" style={{ width: `${progress}%` }}> |
| 71 | + {progress}% |
| 72 | + </div> |
| 73 | + ) |
| 74 | + |
| 75 | + styl` |
| 76 | + .bar |
| 77 | + height 20px |
| 78 | + background-color #4caf50 |
| 79 | + transition width 0.3s |
| 80 | + ` |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## part |
| 87 | + |
| 88 | +Expose an element for external styling via `::part()`. |
| 89 | + |
| 90 | +```jsx |
| 91 | +function Button({ children }) { |
| 92 | + return ( |
| 93 | + <button part="root" styleName="button"> |
| 94 | + <span part="icon">★</span> |
| 95 | + <span part="text">{children}</span> |
| 96 | + </button> |
| 97 | + ) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +The Babel transform automatically injects the part style props — no manual prop spreading needed. |
| 102 | + |
| 103 | +Parent components can then style these parts: |
| 104 | + |
| 105 | +```jsx |
| 106 | +styl` |
| 107 | + .my-button::part(icon) |
| 108 | + color gold |
| 109 | + .my-button::part(text) |
| 110 | + font-weight bold |
| 111 | +` |
| 112 | +``` |
| 113 | + |
| 114 | +### How Parts Work |
| 115 | + |
| 116 | +1. Child component marks elements with `part="name"` |
| 117 | +2. Parent uses `::part(name)` selector to target those elements |
| 118 | +3. Babel transforms both sides to connect them automatically |
| 119 | + |
| 120 | +See the [Component Parts guide](/guide/component-parts) for detailed examples. |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## matcher |
| 125 | + |
| 126 | +The internal function that matches `styleName` values against compiled styles. Advanced use only. |
| 127 | + |
| 128 | +**Signature:** |
| 129 | +```ts |
| 130 | +function matcher( |
| 131 | + styleName: string, |
| 132 | + fileStyles: object, |
| 133 | + globalStyles: object, |
| 134 | + localStyles: object, |
| 135 | + inlineStyleProps: object |
| 136 | +): object |
| 137 | +``` |
| 138 | + |
| 139 | +**Parameters:** |
| 140 | +- `styleName` - Space-separated class names (supports classnames-like syntax) |
| 141 | +- `fileStyles` - Styles from the imported CSS file |
| 142 | +- `globalStyles` - Module-level `styl` styles |
| 143 | +- `localStyles` - Function-level `styl` styles |
| 144 | +- `inlineStyleProps` - Inline style overrides |
| 145 | + |
| 146 | +**Returns:** An object with style props, including `style` and any `{part}Style` props. |
0 commit comments