Skip to content

Commit f77d1b1

Browse files
committed
docs: explicitly document the part 'root' and fix some mistakes in docs
1 parent 5a4067f commit f77d1b1

8 files changed

Lines changed: 61 additions & 25 deletions

File tree

docs/api/jsx-props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Expose an element for external styling via `::part()`.
8787
```jsx
8888
function Button({ children }) {
8989
return (
90-
<button part="root" styleName="button">
90+
<button part="root" styleName="root">
9191
<span part="icon"></span>
9292
<span part="text">{children}</span>
9393
</button>

docs/api/pug.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ input.input(value=inputValue onChange=handleChange)
8282
div.wrapper(...props)
8383

8484
// Part attribute
85-
div.card(part="root")
85+
div.root(part="root")
8686
```
8787

8888
## Content
@@ -155,7 +155,7 @@ import { pug, styl } from 'cssxjs'
155155

156156
function UserProfile({ user, isOnline, onLogout }) {
157157
return pug`
158-
div.profile(part="root")
158+
div.root(part="root")
159159
div.header(part="header")
160160
img.avatar(src=user.avatar alt=user.name)
161161
div.info
@@ -173,7 +173,7 @@ function UserProfile({ user, isOnline, onLogout }) {
173173
`
174174

175175
styl`
176-
.profile
176+
.root
177177
background white
178178
border-radius 12px
179179
box-shadow 0 2px 8px rgba(0,0,0,0.1)

docs/examples/card.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { styl } from 'cssxjs'
77

88
function Card({ title, subtitle, children, actions }) {
99
return (
10-
<div part="root" styleName="card">
10+
<div part="root" styleName="root">
1111
<div part="header" styleName="header">
1212
<h3 part="title" styleName="title">{title}</h3>
1313
{subtitle && (
@@ -28,7 +28,7 @@ function Card({ title, subtitle, children, actions }) {
2828
)
2929

3030
styl`
31-
.card
31+
.root
3232
background white
3333
border-radius 12px
3434
box-shadow 0 2px 8px rgba(0,0,0,0.1)

docs/examples/form.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Input({
1616
const hasError = !!error
1717

1818
return (
19-
<div part="root" styleName="field">
19+
<div part="root" styleName="root">
2020
{label && (
2121
<label part="label" styleName="label">
2222
{label}
@@ -40,7 +40,7 @@ function Input({
4040
)
4141

4242
styl`
43-
.field
43+
.root
4444
display flex
4545
flex-direction column
4646
gap 6px

docs/examples/modal.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Modal({ isOpen, onClose, title, children, actions }) {
2424
<div styleName="overlay" onClick={onClose}>
2525
<div
2626
part="root"
27-
styleName="modal"
27+
styleName="root"
2828
onClick={e => e.stopPropagation()}
2929
role="dialog"
3030
aria-modal="true"
@@ -61,7 +61,7 @@ function Modal({ isOpen, onClose, title, children, actions }) {
6161
padding 20px
6262
z-index 1000
6363
64-
.modal
64+
.root
6565
background white
6666
border-radius 12px
6767
max-width 480px

docs/guide/component-parts.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ Use the `part` attribute to mark styleable elements:
3333
```jsx
3434
function Button({ icon, children }) {
3535
return (
36-
<div part="root" styleName="button">
36+
<div part="root" styleName="root">
3737
<span part="icon" styleName="icon">{icon}</span>
3838
<span part="text" styleName="text">{children}</span>
3939
</div>
4040
)
4141

4242
styl`
43-
.button
43+
.root
4444
display flex
4545
align-items center
4646
gap 8px
@@ -70,6 +70,8 @@ function App() {
7070
)
7171

7272
styl`
73+
.primary-button
74+
background #28a745
7375
.primary-button::part(icon)
7476
color gold
7577
.primary-button::part(text)
@@ -101,6 +103,24 @@ styl`
101103
<Button iconStyle={{ color: 'red' }} />
102104
```
103105

106+
### The `root` Part
107+
108+
The `part="root"` is special — it maps to the standard `style` prop instead of `rootStyle`. This means styles applied to a component's class name automatically reach the root element:
109+
110+
```jsx
111+
// This:
112+
<Button styleName="my-button" />
113+
styl`
114+
.my-button
115+
background green
116+
`
117+
118+
// Effectively becomes:
119+
<Button style={{ background: 'green' }} />
120+
```
121+
122+
You don't need to write `::part(root)` explicitly — just style the class directly. This makes `part="root"` work seamlessly with any component that accepts a `style` prop, including third-party components and React Native built-ins.
123+
104124
## Complete Example
105125

106126
Here's a full example showing a customizable Card component:
@@ -111,7 +131,7 @@ import { styl } from 'cssxjs'
111131

112132
export function Card({ title, children }) {
113133
return (
114-
<div part="root" styleName="card">
134+
<div part="root" styleName="root">
115135
<div part="header" styleName="header">
116136
<h3 part="title" styleName="title">{title}</h3>
117137
</div>
@@ -122,7 +142,7 @@ export function Card({ title, children }) {
122142
)
123143

124144
styl`
125-
.card
145+
.root
126146
background white
127147
border-radius 12px
128148
box-shadow 0 2px 8px rgba(0,0,0,0.1)

docs/guide/index.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,37 @@ With CSSX, you write real CSS:
3434
import { styl } from 'cssxjs'
3535

3636
function Button({ children }) {
37-
return <div styleName="button">{children}</div>
37+
return (
38+
<div part="root" styleName="root">
39+
{children}
40+
</div>
41+
)
3842

3943
styl`
40-
.button
44+
.root
4145
background-color #007bff
4246
padding 16px
4347
border-radius 8px
4448
`
4549
}
4650
```
4751

52+
The `part="root"` attribute allows parent components to style this element using `::part(root)` — a powerful composition pattern.
53+
4854
You can also use plain CSS if you prefer:
4955

5056
```jsx
5157
import { css } from 'cssxjs'
5258

5359
function Button({ children }) {
54-
return <div styleName="button">{children}</div>
60+
return (
61+
<div part="root" styleName="root">
62+
{children}
63+
</div>
64+
)
5565

5666
css`
57-
.button {
67+
.root {
5868
background-color: #007bff;
5969
padding: 16px;
6070
border-radius: 8px;
@@ -133,25 +143,31 @@ import { styl } from 'cssxjs'
133143

134144
function Card({ children, variant }) {
135145
return (
136-
<div styleName={`card ${variant}`}>
146+
<div part="root" styleName={['root', variant]}>
137147
<div part="header" styleName="header">
138-
<span part="title">Title</span>
148+
<span part="title" styleName="title">Title</span>
139149
</div>
140-
<div part="content">{children}</div>
150+
<div part="content" styleName="content">{children}</div>
141151
</div>
142152
)
143153

144154
styl`
145-
.card
155+
.root
146156
background white
147157
border-radius 8px
158+
148159
&.primary
149160
border 2px solid var(--primary-color)
161+
150162
.header
151163
padding 2u
164+
152165
.title
153166
font-size 18px
154167
font-weight bold
168+
169+
.content
170+
padding 2u
155171
`
156172
}
157173
```

docs/guide/pug.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ input.input(value=inputValue onChange=handleChange)
108108
div.wrapper(...props)
109109

110110
// Part attribute for CSSX
111-
div.card(part="root")
111+
div.root(part="root")
112112
```
113113

114114
## Content and Interpolation
@@ -183,7 +183,7 @@ import { pug, styl } from 'cssxjs'
183183

184184
function UserProfile({ user, isOnline, onLogout }) {
185185
return pug`
186-
div.profile(part="root")
186+
div.root(part="root")
187187
div.header(part="header")
188188
img.avatar(src=user.avatar alt=user.name)
189189
div.info
@@ -201,7 +201,7 @@ function UserProfile({ user, isOnline, onLogout }) {
201201
`
202202

203203
styl`
204-
.profile
204+
.root
205205
background white
206206
border-radius 12px
207207
overflow hidden

0 commit comments

Comments
 (0)