@ui-sdk/react 0.0.0-stage → 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +10 -2
  3. package/dist/accordion.d.ts +331 -0
  4. package/dist/accordion.js +25 -0
  5. package/dist/agent.d.ts +2 -0
  6. package/dist/agent.js +8 -0
  7. package/dist/alert-dialog.d.ts +56 -0
  8. package/dist/alert-dialog.js +45 -0
  9. package/dist/async-options.d.ts +22 -0
  10. package/dist/async-options.js +56 -0
  11. package/dist/autocomplete.d.ts +102 -0
  12. package/dist/autocomplete.js +195 -0
  13. package/dist/avatar.d.ts +301 -0
  14. package/dist/avatar.js +17 -0
  15. package/dist/button.d.ts +21 -0
  16. package/dist/button.js +45 -0
  17. package/dist/calendar-context.d.ts +10 -0
  18. package/dist/calendar-context.js +9 -0
  19. package/dist/calendar.d.ts +80 -0
  20. package/dist/calendar.js +257 -0
  21. package/dist/carousel.d.ts +52 -0
  22. package/dist/carousel.js +151 -0
  23. package/dist/checkbox.d.ts +24 -0
  24. package/dist/checkbox.js +98 -0
  25. package/dist/children.d.ts +38 -0
  26. package/dist/children.js +63 -0
  27. package/dist/collapsible.d.ts +302 -0
  28. package/dist/collapsible.js +19 -0
  29. package/dist/color-picker.d.ts +72 -0
  30. package/dist/color-picker.js +133 -0
  31. package/dist/color.d.ts +2 -0
  32. package/dist/color.js +22 -0
  33. package/dist/combobox.d.ts +152 -0
  34. package/dist/combobox.js +338 -0
  35. package/dist/command.d.ts +88 -0
  36. package/dist/command.js +233 -0
  37. package/dist/controller-target.d.ts +3 -0
  38. package/dist/controller-target.js +59 -0
  39. package/dist/date-input.d.ts +19 -0
  40. package/dist/date-input.js +58 -0
  41. package/dist/date-picker.d.ts +47 -0
  42. package/dist/date-picker.js +310 -0
  43. package/dist/date.d.ts +2 -0
  44. package/dist/date.js +60 -0
  45. package/dist/dialog-parts.d.ts +23 -0
  46. package/dist/dialog-parts.js +142 -0
  47. package/dist/dialog.d.ts +57 -0
  48. package/dist/dialog.js +32 -0
  49. package/dist/drawer.d.ts +70 -0
  50. package/dist/drawer.js +158 -0
  51. package/dist/editor-context.d.ts +4 -0
  52. package/dist/editor-context.js +9 -0
  53. package/dist/field-context.d.ts +47 -0
  54. package/dist/field-context.js +87 -0
  55. package/dist/field.d.ts +49 -0
  56. package/dist/field.js +169 -0
  57. package/dist/file-input.d.ts +46 -0
  58. package/dist/file-input.js +170 -0
  59. package/dist/form-inputs.d.ts +5 -0
  60. package/dist/form-inputs.js +22 -0
  61. package/dist/gesture.d.ts +1 -0
  62. package/dist/gesture.js +16 -0
  63. package/dist/history.d.ts +2 -0
  64. package/dist/history.js +12 -0
  65. package/dist/hover-card.d.ts +33 -0
  66. package/dist/hover-card.js +84 -0
  67. package/dist/image-cropper.d.ts +333 -0
  68. package/dist/image-cropper.js +37 -0
  69. package/dist/image.d.ts +2 -0
  70. package/dist/image.js +6 -0
  71. package/dist/input.d.ts +22 -0
  72. package/dist/input.js +51 -0
  73. package/dist/label.d.ts +294 -0
  74. package/dist/label.js +9 -0
  75. package/dist/list-items.d.ts +15 -0
  76. package/dist/list-items.js +39 -0
  77. package/dist/list-text.d.ts +4 -0
  78. package/dist/list-text.js +15 -0
  79. package/dist/listbox.d.ts +56 -0
  80. package/dist/listbox.js +122 -0
  81. package/dist/menu.d.ts +116 -0
  82. package/dist/menu.js +426 -0
  83. package/dist/menubar-context.d.ts +4 -0
  84. package/dist/menubar-context.js +7 -0
  85. package/dist/menubar.d.ts +11 -0
  86. package/dist/menubar.js +32 -0
  87. package/dist/meter.d.ts +312 -0
  88. package/dist/meter.js +30 -0
  89. package/dist/navigation-menu.d.ts +61 -0
  90. package/dist/navigation-menu.js +169 -0
  91. package/dist/number-input.d.ts +44 -0
  92. package/dist/number-input.js +185 -0
  93. package/dist/number.d.ts +2 -0
  94. package/dist/number.js +40 -0
  95. package/dist/observe.d.ts +2 -0
  96. package/dist/observe.js +8 -0
  97. package/dist/otp-input.d.ts +29 -0
  98. package/dist/otp-input.js +94 -0
  99. package/dist/overlays.d.ts +26 -0
  100. package/dist/overlays.js +82 -0
  101. package/dist/pagination.d.ts +336 -0
  102. package/dist/pagination.js +71 -0
  103. package/dist/password-input.d.ts +23 -0
  104. package/dist/password-input.js +65 -0
  105. package/dist/popover.d.ts +54 -0
  106. package/dist/popover.js +151 -0
  107. package/dist/popup.d.ts +41 -0
  108. package/dist/popup.js +180 -0
  109. package/dist/preferences.d.ts +35 -0
  110. package/dist/preferences.js +93 -0
  111. package/dist/primitive.d.ts +2 -0
  112. package/dist/primitive.js +14 -0
  113. package/dist/progress.d.ts +312 -0
  114. package/dist/progress.js +30 -0
  115. package/dist/radio.d.ts +22 -0
  116. package/dist/radio.js +86 -0
  117. package/dist/rating.d.ts +20 -0
  118. package/dist/rating.js +94 -0
  119. package/dist/render-element.d.ts +35 -0
  120. package/dist/render-element.js +103 -0
  121. package/dist/resizable.d.ts +302 -0
  122. package/dist/resizable.js +23 -0
  123. package/dist/scroll-area.d.ts +324 -0
  124. package/dist/scroll-area.js +49 -0
  125. package/dist/segmented-control.d.ts +24 -0
  126. package/dist/segmented-control.js +78 -0
  127. package/dist/segmented-input.d.ts +15 -0
  128. package/dist/segmented-input.js +48 -0
  129. package/dist/select.d.ts +125 -0
  130. package/dist/select.js +330 -0
  131. package/dist/separator.d.ts +290 -0
  132. package/dist/separator.js +9 -0
  133. package/dist/shortcuts.d.ts +32 -0
  134. package/dist/shortcuts.js +61 -0
  135. package/dist/slider.d.ts +38 -0
  136. package/dist/slider.js +163 -0
  137. package/dist/switch.d.ts +15 -0
  138. package/dist/switch.js +39 -0
  139. package/dist/tabs.d.ts +331 -0
  140. package/dist/tabs.js +28 -0
  141. package/dist/text-control.d.ts +22 -0
  142. package/dist/text-control.js +24 -0
  143. package/dist/text-match.d.ts +2 -0
  144. package/dist/text-match.js +8 -0
  145. package/dist/textarea.d.ts +10 -0
  146. package/dist/textarea.js +48 -0
  147. package/dist/time-input.d.ts +19 -0
  148. package/dist/time-input.js +63 -0
  149. package/dist/timeline.d.ts +327 -0
  150. package/dist/timeline.js +31 -0
  151. package/dist/toast-parts.d.ts +63 -0
  152. package/dist/toast-parts.js +144 -0
  153. package/dist/toast.d.ts +2 -0
  154. package/dist/toast.js +22 -0
  155. package/dist/toggle.d.ts +23 -0
  156. package/dist/toggle.js +76 -0
  157. package/dist/token-input.d.ts +35 -0
  158. package/dist/token-input.js +100 -0
  159. package/dist/toolbar-context.d.ts +6 -0
  160. package/dist/toolbar-context.js +16 -0
  161. package/dist/toolbar.d.ts +38 -0
  162. package/dist/toolbar.js +63 -0
  163. package/dist/tooltip.d.ts +39 -0
  164. package/dist/tooltip.js +108 -0
  165. package/dist/tree.d.ts +51 -0
  166. package/dist/tree.js +150 -0
  167. package/dist/trigger-menu.d.ts +83 -0
  168. package/dist/trigger-menu.js +167 -0
  169. package/dist/use-primitive.d.ts +178 -0
  170. package/dist/use-primitive.js +674 -0
  171. package/dist/waterfall.d.ts +80 -0
  172. package/dist/waterfall.js +108 -0
  173. package/docs/README.md +89 -0
  174. package/docs/agents.md +1523 -0
  175. package/docs/components/accordion.md +1391 -0
  176. package/docs/components/alert-dialog.md +850 -0
  177. package/docs/components/autocomplete.md +1963 -0
  178. package/docs/components/avatar.md +252 -0
  179. package/docs/components/button.md +444 -0
  180. package/docs/components/calendar.md +1687 -0
  181. package/docs/components/carousel.md +1244 -0
  182. package/docs/components/checkbox.md +1069 -0
  183. package/docs/components/collapsible.md +567 -0
  184. package/docs/components/color-picker.md +736 -0
  185. package/docs/components/combobox.md +3647 -0
  186. package/docs/components/command.md +1573 -0
  187. package/docs/components/date-input.md +608 -0
  188. package/docs/components/date-picker.md +1266 -0
  189. package/docs/components/dialog.md +1469 -0
  190. package/docs/components/drawer.md +1197 -0
  191. package/docs/components/field.md +549 -0
  192. package/docs/components/file-input.md +609 -0
  193. package/docs/components/hover-card.md +514 -0
  194. package/docs/components/image-cropper.md +579 -0
  195. package/docs/components/input.md +301 -0
  196. package/docs/components/label.md +217 -0
  197. package/docs/components/listbox.md +1130 -0
  198. package/docs/components/menu.md +1286 -0
  199. package/docs/components/menubar.md +455 -0
  200. package/docs/components/meter.md +304 -0
  201. package/docs/components/navigation-menu.md +1581 -0
  202. package/docs/components/number-input.md +1086 -0
  203. package/docs/components/otp-input.md +565 -0
  204. package/docs/components/pagination.md +648 -0
  205. package/docs/components/password-input.md +737 -0
  206. package/docs/components/popover.md +1552 -0
  207. package/docs/components/preferences.md +220 -0
  208. package/docs/components/progress.md +443 -0
  209. package/docs/components/radio.md +846 -0
  210. package/docs/components/rating.md +458 -0
  211. package/docs/components/resizable.md +982 -0
  212. package/docs/components/scroll-area.md +980 -0
  213. package/docs/components/segmented-control.md +465 -0
  214. package/docs/components/select.md +2133 -0
  215. package/docs/components/separator.md +243 -0
  216. package/docs/components/slider.md +1169 -0
  217. package/docs/components/switch.md +613 -0
  218. package/docs/components/tabs.md +1014 -0
  219. package/docs/components/textarea.md +318 -0
  220. package/docs/components/time-input.md +231 -0
  221. package/docs/components/timeline.md +677 -0
  222. package/docs/components/toast.md +1395 -0
  223. package/docs/components/toggle.md +541 -0
  224. package/docs/components/token-input.md +1007 -0
  225. package/docs/components/toolbar.md +787 -0
  226. package/docs/components/tooltip.md +884 -0
  227. package/docs/components/tree.md +1346 -0
  228. package/docs/components/trigger-menu.md +1257 -0
  229. package/docs/components/waterfall.md +754 -0
  230. package/docs/controllers.md +89 -0
  231. package/docs/core.md +8027 -0
  232. package/docs/custom-parts.md +120 -0
  233. package/docs/first-component.md +83 -0
  234. package/docs/forms.md +148 -0
  235. package/docs/hooks/async-options.md +366 -0
  236. package/docs/hooks/shortcuts.md +221 -0
  237. package/docs/installation.md +45 -0
  238. package/docs/introduction.md +32 -0
  239. package/docs/lists-and-menus.md +170 -0
  240. package/docs/llms.txt +1844 -0
  241. package/docs/parts-and-composition.md +93 -0
  242. package/docs/popups.md +117 -0
  243. package/docs/rendering.md +121 -0
  244. package/docs/server-rendering.md +253 -0
  245. package/docs/state-and-events.md +105 -0
  246. package/docs/styling-and-animation.md +176 -0
  247. package/package.json +333 -3
@@ -0,0 +1,1346 @@
1
+ # Tree
2
+
3
+ Browse a hierarchy of files, pages, or choices with expandable branches.
4
+
5
+ Use a Tree when items have children, like files in folders or pages in a docs sidebar. Start with nested parts, then add selection, checkboxes, or children loaded from a server as you need them.
6
+
7
+ ```tsx title="demo.tsx"
8
+ "use client"
9
+
10
+ import {
11
+ Tree,
12
+ TreeGroup,
13
+ TreeItem,
14
+ TreeItemIndicator,
15
+ TreeItemLabel,
16
+ } from "@ui-sdk/react/tree"
17
+ import {
18
+ ChevronRightIcon,
19
+ FileIcon,
20
+ FolderIcon,
21
+ FolderOpenIcon,
22
+ } from "lucide-react"
23
+
24
+ type Node = { value: string; label: string; children?: Node[] }
25
+
26
+ const files: Node[] = [
27
+ {
28
+ value: "app",
29
+ label: "app",
30
+ children: [
31
+ { value: "app/layout.tsx", label: "layout.tsx" },
32
+ { value: "app/page.tsx", label: "page.tsx" },
33
+ {
34
+ value: "app/settings",
35
+ label: "settings",
36
+ children: [{ value: "app/settings/page.tsx", label: "page.tsx" }],
37
+ },
38
+ ],
39
+ },
40
+ {
41
+ value: "components",
42
+ label: "components",
43
+ children: [
44
+ { value: "components/button.tsx", label: "button.tsx" },
45
+ { value: "components/card.tsx", label: "card.tsx" },
46
+ ],
47
+ },
48
+ { value: "components.json", label: "components.json" },
49
+ { value: "package.json", label: "package.json" },
50
+ { value: "README.md", label: "README.md" },
51
+ ]
52
+
53
+ function Items({ items }: { items: Node[] }) {
54
+ return items.map((item) => (
55
+ <TreeItem key={item.value} value={item.value}>
56
+ <TreeItemLabel>
57
+ {item.children ? (
58
+ <>
59
+ <TreeItemIndicator>
60
+ <ChevronRightIcon aria-hidden="true" />
61
+ </TreeItemIndicator>
62
+ <FolderIcon aria-hidden="true" />
63
+ <FolderOpenIcon aria-hidden="true" />
64
+ </>
65
+ ) : (
66
+ <FileIcon aria-hidden="true" />
67
+ )}
68
+ <span>{item.label}</span>
69
+ </TreeItemLabel>
70
+ {item.children && (
71
+ <TreeGroup>
72
+ <Items items={item.children} />
73
+ </TreeGroup>
74
+ )}
75
+ </TreeItem>
76
+ ))
77
+ }
78
+
79
+ function Example() {
80
+ return (
81
+ <Tree
82
+ aria-label="Files"
83
+ defaultExpanded={["app"]}
84
+ defaultValue="app/page.tsx"
85
+ >
86
+ <Items items={files} />
87
+ </Tree>
88
+ )
89
+ }
90
+
91
+ export { Example }
92
+ ```
93
+
94
+ Expand components to see its files. Use the arrow keys to move between rows and open or close branches.
95
+
96
+ ## Installation
97
+
98
+ ```bash
99
+ npm install @ui-sdk/react @ui-sdk/core
100
+ ```
101
+
102
+ ## Composition
103
+
104
+ Use the following composition to build a `Tree`:
105
+
106
+ ```text
107
+ Tree
108
+ ├── TreeItem
109
+ │ ├── TreeItemLabel
110
+ │ │ └── TreeItemIndicator
111
+ │ └── TreeGroup
112
+ │ └── TreeItem
113
+ │ └── TreeItemLabel
114
+ └── TreeItem
115
+ └── TreeItemLabel
116
+ ```
117
+
118
+ Give the tree an accessible name and every item a unique `value`. An item with a `TreeGroup` inside it is a branch. The group holds its children; the label names the item. The indicator and checkbox are optional, and they belong to the item around them, so put them inside its label to make them part of the row.
119
+
120
+ ## Usage
121
+
122
+ Import the parts and put them together:
123
+
124
+ ```tsx
125
+ import {
126
+ Tree,
127
+ TreeGroup,
128
+ TreeItem,
129
+ TreeItemIndicator,
130
+ TreeItemLabel,
131
+ } from "@ui-sdk/react/tree"
132
+
133
+ function Example() {
134
+ return (
135
+ <Tree aria-label="Project files">
136
+ <TreeItem value="src">
137
+ <TreeItemLabel>
138
+ <TreeItemIndicator>›</TreeItemIndicator>
139
+ src
140
+ </TreeItemLabel>
141
+ <TreeGroup>
142
+ <TreeItem value="src/index.ts">
143
+ <TreeItemLabel>index.ts</TreeItemLabel>
144
+ </TreeItem>
145
+ </TreeGroup>
146
+ </TreeItem>
147
+ <TreeItem value="package.json">
148
+ <TreeItemLabel>package.json</TreeItemLabel>
149
+ </TreeItem>
150
+ </Tree>
151
+ )
152
+ }
153
+
154
+ export { Example }
155
+ ```
156
+
157
+ ## Items as Data
158
+
159
+ Pass `items` to give the tree its full hierarchy, including collapsed branches. Each `TreeItemData` has a `value`, a `label`, and optional `children`. With no children of its own, the tree renders bare parts from the data:
160
+
161
+ ```tsx
162
+ const items: TreeItemData[] = [
163
+ {
164
+ value: "fruit",
165
+ label: "Fruit",
166
+ children: [
167
+ { value: "apple", label: "Apple" },
168
+ { value: "banana", label: "Banana" },
169
+ ],
170
+ },
171
+ { value: "bread", label: "Bread" },
172
+ ]
173
+
174
+ function Example() {
175
+ return <Tree aria-label="Groceries" items={items} />
176
+ }
177
+ ```
178
+
179
+ To render the rows yourself, keep `items` on the tree and map them with a recursive component, so keyboard navigation, item counts, and controllers still know the whole hierarchy:
180
+
181
+ ```tsx
182
+ function Items({ items }: { items: TreeItemData[] }) {
183
+ return items.map((item) => (
184
+ <TreeItem key={item.value} value={item.value}>
185
+ <TreeItemLabel>{item.label}</TreeItemLabel>
186
+ {item.children && (
187
+ <TreeGroup>
188
+ <Items items={item.children} />
189
+ </TreeGroup>
190
+ )}
191
+ </TreeItem>
192
+ ))
193
+ }
194
+
195
+ function Example() {
196
+ return (
197
+ <Tree aria-label="Groceries" items={items}>
198
+ <Items items={items} />
199
+ </Tree>
200
+ )
201
+ }
202
+ ```
203
+
204
+ ## File Paths
205
+
206
+ Pass `paths` for a file explorer. Folders become branches and sort before files; a path ending in `/` represents an empty folder. `treeFromPaths()` returns the same data when you want to render the rows yourself.
207
+
208
+ Selection and activation are separate. This example uses `activation="automatic"` so arrow keys select a file, then `onAction` opens it. Return a promise from the action callback to show a pending state while it opens.
209
+
210
+ ```tsx title="paths.tsx"
211
+ "use client"
212
+
213
+ import {
214
+ Tree,
215
+ TreeGroup,
216
+ TreeItem,
217
+ TreeItemIndicator,
218
+ TreeItemLabel,
219
+ treeFromPaths,
220
+ type TreeItemData,
221
+ } from "@ui-sdk/react/tree"
222
+ import { ChevronRightIcon, LoaderCircleIcon } from "lucide-react"
223
+ import { useLog } from "./log"
224
+
225
+ const paths = [
226
+ "src/index.ts",
227
+ "src/lib/format.ts",
228
+ "src/lib/parse.ts",
229
+ "src/routes/home.tsx",
230
+ "src/routes/about.tsx",
231
+ "tests/",
232
+ "package.json",
233
+ "tsconfig.json",
234
+ ]
235
+
236
+ const items = treeFromPaths(paths)
237
+ const files = new Set(paths.filter((path) => !path.endsWith("/")))
238
+
239
+ function Items({ items }: { items: TreeItemData[] }) {
240
+ return items.map((item) => (
241
+ <TreeItem key={item.value} value={item.value}>
242
+ <TreeItemLabel>
243
+ {(state) => (
244
+ <>
245
+ {item.children && (
246
+ <TreeItemIndicator>
247
+ <ChevronRightIcon aria-hidden="true" />
248
+ </TreeItemIndicator>
249
+ )}
250
+ <span>{item.label}</span>
251
+ {state.pending && <LoaderCircleIcon aria-hidden="true" />}
252
+ </>
253
+ )}
254
+ </TreeItemLabel>
255
+ {item.children && (
256
+ <TreeGroup>
257
+ <Items items={item.children} />
258
+ </TreeGroup>
259
+ )}
260
+ </TreeItem>
261
+ ))
262
+ }
263
+
264
+ function Example() {
265
+ const log = useLog()
266
+ return (
267
+ <Tree
268
+ aria-label="Project"
269
+ paths={paths}
270
+ defaultExpanded={["src"]}
271
+ defaultValue="src/index.ts"
272
+ activation="automatic"
273
+ onAction={async (value) => {
274
+ if (!files.has(value)) {
275
+ return
276
+ }
277
+ await new Promise((resolve) => setTimeout(resolve, 600))
278
+ log(`Opened ${value}`)
279
+ }}
280
+ >
281
+ <Items items={items} />
282
+ </Tree>
283
+ )
284
+ }
285
+
286
+ export { Example }
287
+ ```
288
+
289
+ Arrow down to package.json, then press Enter. It shows a spinner while it opens, then the console logs Opened package.json.
290
+
291
+ ## Selection
292
+
293
+ The default `mode="single"` uses one string value or `null`. Use `"multiple"` for an array, or `"none"` when items should only navigate or perform actions.
294
+
295
+ `activation="automatic"` selects while arrow keys move in single selection mode. Pressing a branch also expands or collapses it; set `expandOnPress` to `false` to leave expansion to the indicator and arrow keys.
296
+
297
+ ## Multiple Selection
298
+
299
+ `mode="multiple"` makes the value an array. Cmd-click or Ctrl-click toggles one item, Shift extends a range, and Cmd+A or Ctrl+A selects the visible items. This example sets `expandOnPress` to `false`, so clicking Inbox or Projects selects the folder without closing it.
300
+
301
+ ```tsx title="multiple.tsx"
302
+ "use client"
303
+
304
+ import {
305
+ Tree,
306
+ TreeGroup,
307
+ TreeItem,
308
+ TreeItemIndicator,
309
+ TreeItemLabel,
310
+ type TreeItemData,
311
+ } from "@ui-sdk/react/tree"
312
+ import { ChevronRightIcon } from "lucide-react"
313
+ import { useLog } from "./log"
314
+
315
+ const items: TreeItemData[] = [
316
+ {
317
+ value: "inbox",
318
+ label: "Inbox",
319
+ children: [
320
+ { value: "updates", label: "Updates" },
321
+ { value: "forums", label: "Forums" },
322
+ { value: "promotions", label: "Promotions" },
323
+ ],
324
+ },
325
+ {
326
+ value: "projects",
327
+ label: "Projects",
328
+ children: [
329
+ { value: "website", label: "Website" },
330
+ { value: "mobile", label: "Mobile app" },
331
+ ],
332
+ },
333
+ { value: "archive", label: "Archive" },
334
+ { value: "trash", label: "Trash" },
335
+ ]
336
+
337
+ function Items({ items }: { items: TreeItemData[] }) {
338
+ return items.map((item) => (
339
+ <TreeItem key={item.value} value={item.value}>
340
+ <TreeItemLabel>
341
+ {item.children && (
342
+ <TreeItemIndicator>
343
+ <ChevronRightIcon aria-hidden="true" />
344
+ </TreeItemIndicator>
345
+ )}
346
+ {item.label}
347
+ </TreeItemLabel>
348
+ {item.children && (
349
+ <TreeGroup>
350
+ <Items items={item.children} />
351
+ </TreeGroup>
352
+ )}
353
+ </TreeItem>
354
+ ))
355
+ }
356
+
357
+ function Example() {
358
+ const log = useLog()
359
+ return (
360
+ <Tree
361
+ aria-label="Folders"
362
+ items={items}
363
+ mode="multiple"
364
+ defaultValue={["updates", "website"]}
365
+ onValueChange={(value) =>
366
+ log(`${value.length} selected: ${value.join(", ")}`)
367
+ }
368
+ expandOnPress={false}
369
+ defaultExpanded={["inbox", "projects"]}
370
+ >
371
+ <Items items={items} />
372
+ </Tree>
373
+ )
374
+ }
375
+
376
+ export { Example }
377
+ ```
378
+
379
+ Cmd-click or Ctrl-click Archive to add it to the selection. Shift-click or Shift+Arrow extends a range, and the console logs what's selected.
380
+
381
+ ## Checkboxes
382
+
383
+ Add `checkable` and `TreeItemCheckbox` for a checked state separate from selection. Space checks the focused item. Checking a branch checks its descendants, and a partly checked branch reports a mixed state.
384
+
385
+ `checkStrategy` chooses what the `checked` array reports:
386
+
387
+ | Strategy | Reported Values |
388
+ | --------------- | ---------------------------------------- |
389
+ | `"leaf"` | Checked leaves. This is the default. |
390
+ | `"trunk"` | The outermost fully checked items. |
391
+ | `"branch"` | Every fully checked item. |
392
+ | `"independent"` | Each item on its own, without cascading. |
393
+
394
+ ```tsx title="checkboxes.tsx"
395
+ "use client"
396
+
397
+ import {
398
+ Tree,
399
+ TreeGroup,
400
+ TreeItem,
401
+ TreeItemCheckbox,
402
+ TreeItemIndicator,
403
+ TreeItemLabel,
404
+ type TreeItemData,
405
+ } from "@ui-sdk/react/tree"
406
+ import { ChevronRightIcon } from "lucide-react"
407
+ import { useLog } from "./log"
408
+
409
+ const items: TreeItemData[] = [
410
+ {
411
+ value: "frontend",
412
+ label: "Frontend",
413
+ children: [
414
+ { value: "react", label: "React" },
415
+ { value: "vue", label: "Vue" },
416
+ { value: "svelte", label: "Svelte" },
417
+ ],
418
+ },
419
+ {
420
+ value: "backend",
421
+ label: "Backend",
422
+ children: [
423
+ { value: "node", label: "Node" },
424
+ {
425
+ value: "databases",
426
+ label: "Databases",
427
+ children: [
428
+ { value: "postgres", label: "Postgres" },
429
+ { value: "sqlite", label: "SQLite" },
430
+ ],
431
+ },
432
+ ],
433
+ },
434
+ ]
435
+
436
+ function Items({ items }: { items: TreeItemData[] }) {
437
+ return items.map((item) => (
438
+ <TreeItem key={item.value} value={item.value}>
439
+ <TreeItemLabel>
440
+ {item.children && (
441
+ <TreeItemIndicator>
442
+ <ChevronRightIcon aria-hidden="true" />
443
+ </TreeItemIndicator>
444
+ )}
445
+ <TreeItemCheckbox />
446
+ {item.label}
447
+ </TreeItemLabel>
448
+ {item.children && (
449
+ <TreeGroup>
450
+ <Items items={item.children} />
451
+ </TreeGroup>
452
+ )}
453
+ </TreeItem>
454
+ ))
455
+ }
456
+
457
+ function Example() {
458
+ const log = useLog()
459
+ return (
460
+ <Tree
461
+ aria-label="Stack"
462
+ items={items}
463
+ checkable
464
+ mode="none"
465
+ checkStrategy="trunk"
466
+ defaultChecked={["react", "postgres"]}
467
+ onCheckedChange={(checked) =>
468
+ log(`Checked ${checked.join(", ") || "nothing"}`)
469
+ }
470
+ defaultExpanded={["frontend", "backend", "databases"]}
471
+ >
472
+ <Items items={items} />
473
+ </Tree>
474
+ )
475
+ }
476
+
477
+ export { Example }
478
+ ```
479
+
480
+ Press Space on Vue, then on Frontend. Frontend is mixed while only some of its items are checked, and the console logs the trunk strategy's values.
481
+
482
+ ## Links
483
+
484
+ Set `mode="none"` for a navigation tree, then render labels as links. `current` identifies the current page with `aria-current="page"`, expands its ancestors, and gives it the initial Tab stop. Update it when your route changes.
485
+
486
+ ```tsx title="links.tsx"
487
+ "use client"
488
+
489
+ import {
490
+ Tree,
491
+ TreeGroup,
492
+ TreeItem,
493
+ TreeItemIndicator,
494
+ TreeItemLabel,
495
+ type TreeItemData,
496
+ } from "@ui-sdk/react/tree"
497
+ import { ChevronRightIcon } from "lucide-react"
498
+ import * as React from "react"
499
+
500
+ const pages: TreeItemData[] = [
501
+ {
502
+ value: "getting-started",
503
+ label: "Getting started",
504
+ children: [
505
+ { value: "introduction", label: "Introduction", href: "#introduction" },
506
+ { value: "installation", label: "Installation", href: "#installation" },
507
+ ],
508
+ },
509
+ {
510
+ value: "components",
511
+ label: "Components",
512
+ children: [
513
+ { value: "accordion", label: "Accordion", href: "#accordion" },
514
+ { value: "dialog", label: "Dialog", href: "#dialog" },
515
+ { value: "tree", label: "Tree", href: "#tree" },
516
+ ],
517
+ },
518
+ { value: "changelog", label: "Changelog", href: "#changelog" },
519
+ ]
520
+
521
+ const links = new Set(
522
+ pages
523
+ .flatMap((page) => [page, ...(page.children ?? [])])
524
+ .filter((page) => page.href)
525
+ .map((page) => page.value)
526
+ )
527
+
528
+ function Items({ items }: { items: TreeItemData[] }) {
529
+ return items.map((item) => (
530
+ <TreeItem key={item.value} value={item.value}>
531
+ <TreeItemLabel render={item.href ? <a href={item.href} /> : undefined}>
532
+ {item.children && (
533
+ <TreeItemIndicator>
534
+ <ChevronRightIcon aria-hidden="true" />
535
+ </TreeItemIndicator>
536
+ )}
537
+ {item.label}
538
+ </TreeItemLabel>
539
+ {item.children && (
540
+ <TreeGroup>
541
+ <Items items={item.children} />
542
+ </TreeGroup>
543
+ )}
544
+ </TreeItem>
545
+ ))
546
+ }
547
+
548
+ function Example() {
549
+ const [current, setCurrent] = React.useState("tree")
550
+ return (
551
+ <Tree
552
+ aria-label="Docs"
553
+ items={pages}
554
+ mode="none"
555
+ current={current}
556
+ onAction={(value, { event }) => {
557
+ if (!links.has(value)) {
558
+ return
559
+ }
560
+ event?.preventDefault()
561
+ setCurrent(value)
562
+ }}
563
+ >
564
+ <Items items={pages} />
565
+ </Tree>
566
+ )
567
+ }
568
+
569
+ export { Example }
570
+ ```
571
+
572
+ Choose Dialog. The current-page marker moves to it, and you stay on this docs page.
573
+
574
+ ## Disabled
575
+
576
+ Disabled items can't be selected, checked, expanded, or run. They stay focusable so screen readers can find them. Set `focusableWhenDisabled` to `false` to skip them, or `disabled` on the root to disable the whole tree. A disabled `<fieldset>` around the tree disables it too.
577
+
578
+ ```tsx title="disabled.tsx"
579
+ "use client"
580
+
581
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
582
+ import {
583
+ Tree,
584
+ TreeGroup,
585
+ TreeItem,
586
+ TreeItemIndicator,
587
+ TreeItemLabel,
588
+ type TreeItemData,
589
+ } from "@ui-sdk/react/tree"
590
+ import { ChevronRightIcon } from "lucide-react"
591
+ import * as React from "react"
592
+
593
+ const items: TreeItemData[] = [
594
+ {
595
+ value: "plans",
596
+ label: "Plans",
597
+ children: [
598
+ { value: "free", label: "Free" },
599
+ { value: "pro", label: "Pro" },
600
+ { value: "enterprise", label: "Enterprise", disabled: true },
601
+ ],
602
+ },
603
+ {
604
+ value: "legacy",
605
+ label: "Legacy plans",
606
+ disabled: true,
607
+ children: [{ value: "starter", label: "Starter" }],
608
+ },
609
+ { value: "custom", label: "Custom" },
610
+ ]
611
+
612
+ function Items({ items }: { items: TreeItemData[] }) {
613
+ return items.map((item) => (
614
+ <TreeItem key={item.value} value={item.value} disabled={item.disabled}>
615
+ <TreeItemLabel>
616
+ {item.children && (
617
+ <TreeItemIndicator>
618
+ <ChevronRightIcon aria-hidden="true" />
619
+ </TreeItemIndicator>
620
+ )}
621
+ {item.label}
622
+ </TreeItemLabel>
623
+ {item.children && (
624
+ <TreeGroup>
625
+ <Items items={item.children} />
626
+ </TreeGroup>
627
+ )}
628
+ </TreeItem>
629
+ ))
630
+ }
631
+
632
+ function Example() {
633
+ const [focusable, setFocusable] = React.useState(true)
634
+ return (
635
+ <div data-slot="stack">
636
+ <div data-slot="controls">
637
+ <label data-slot="label">
638
+ <Switch checked={focusable} onCheckedChange={setFocusable}>
639
+ <SwitchThumb />
640
+ </Switch>
641
+ Focusable when disabled
642
+ </label>
643
+ </div>
644
+ <Tree aria-label="Plans" items={items} defaultExpanded={["plans"]} focusableWhenDisabled={focusable}>
645
+ <Items items={items} />
646
+ </Tree>
647
+ </div>
648
+ )
649
+ }
650
+
651
+ export { Example }
652
+ ```
653
+
654
+ Arrow through Enterprise while Focusable when disabled is on. Turn it off and the same arrow keys skip disabled items.
655
+
656
+ ## Loading Children
657
+
658
+ Mark an item as a `branch` before its children exist. `loadChildren` runs the first time that branch expands while empty. Load and render its children, then let the promise resolve; the tree expands the branch once they are ready.
659
+
660
+ Use the supplied `signal` to cancel a request. Trying to expand the branch again while it loads aborts it. A rejection sets `data-failed`, and the next attempt retries. `announcement` lets you customize loading and failure messages.
661
+
662
+ ```tsx title="lazy.tsx"
663
+ "use client"
664
+
665
+ import {
666
+ Tree,
667
+ TreeGroup,
668
+ TreeItem,
669
+ TreeItemIndicator,
670
+ TreeItemLabel,
671
+ } from "@ui-sdk/react/tree"
672
+ import { ChevronRightIcon, LoaderCircleIcon, RotateCwIcon } from "lucide-react"
673
+ import * as React from "react"
674
+
675
+ type Node = { value: string; label: string; branch?: boolean }
676
+
677
+ const top: Node[] = [
678
+ { value: "users", label: "users", branch: true },
679
+ { value: "orders", label: "orders", branch: true },
680
+ { value: "archive", label: "archive", branch: true },
681
+ ]
682
+
683
+ let archiveAsked = false
684
+ async function fetchChildren(value: string, signal: AbortSignal) {
685
+ await new Promise((resolve, reject) => {
686
+ const timer = setTimeout(resolve, 800)
687
+ signal.addEventListener(
688
+ "abort",
689
+ () => (clearTimeout(timer), reject(signal.reason))
690
+ )
691
+ })
692
+ if (value === "archive" && !archiveAsked) {
693
+ archiveAsked = true
694
+ throw new Error("Network error")
695
+ }
696
+ return Array.from({ length: 3 }, (_, index) => ({
697
+ value: `${value}/${index + 1}`,
698
+ label: `${value.slice(0, -1)}_${index + 1}.json`,
699
+ }))
700
+ }
701
+
702
+ function Items({
703
+ items,
704
+ loaded,
705
+ }: {
706
+ items: Node[]
707
+ loaded: Record<string, Node[]>
708
+ }) {
709
+ return items.map((item) => (
710
+ <TreeItem key={item.value} value={item.value} branch={item.branch}>
711
+ <TreeItemLabel>
712
+ {item.branch && (
713
+ <TreeItemIndicator>
714
+ {(state) =>
715
+ state.loading ? (
716
+ <LoaderCircleIcon aria-hidden="true" />
717
+ ) : state.failed ? (
718
+ <RotateCwIcon aria-hidden="true" />
719
+ ) : (
720
+ <ChevronRightIcon aria-hidden="true" />
721
+ )
722
+ }
723
+ </TreeItemIndicator>
724
+ )}
725
+ {item.label}
726
+ </TreeItemLabel>
727
+ {loaded[item.value] && (
728
+ <TreeGroup>
729
+ <Items items={loaded[item.value]} loaded={loaded} />
730
+ </TreeGroup>
731
+ )}
732
+ </TreeItem>
733
+ ))
734
+ }
735
+
736
+ function Example() {
737
+ const [loaded, setLoaded] = React.useState<Record<string, Node[]>>({})
738
+ return (
739
+ <Tree
740
+ aria-label="Database"
741
+ loadChildren={async (value, { signal }) => {
742
+ const children = await fetchChildren(value, signal)
743
+ setLoaded((loaded) => ({ ...loaded, [value]: children }))
744
+ }}
745
+ announcement={({ type, label }) =>
746
+ ({
747
+ expanded: "Expanded",
748
+ collapsed: "Collapsed",
749
+ loading: `Loading ${label}`,
750
+ "load-failed": `${label} didn't load. Try again.`,
751
+ })[type]
752
+ }
753
+ >
754
+ <Items items={top} loaded={loaded} />
755
+ </Tree>
756
+ )
757
+ }
758
+
759
+ export { Example }
760
+ ```
761
+
762
+ Expand users to load its files. Archive fails on its first attempt and shows a retry icon; expand it again to load it.
763
+
764
+ ## Virtualized
765
+
766
+ Pass the full `items` hierarchy and use `visibleTreeRows(items, expanded)` to flatten the open branches. Render the nearby rows with their position; the tree supplies the correct ARIA hierarchy even though the DOM is flat.
767
+
768
+ `onHighlight` reports the focused item's index among visible rows. Use it to scroll before mounting a row that is outside the rendered range. Every item sets `--tree-level`, so the stylesheet indents the flat rows by their level.
769
+
770
+ ```tsx title="virtualized.tsx"
771
+ "use client"
772
+
773
+ import {
774
+ Tree,
775
+ TreeItem,
776
+ TreeItemIndicator,
777
+ TreeItemLabel,
778
+ visibleTreeRows,
779
+ type TreeItemData,
780
+ } from "@ui-sdk/react/tree"
781
+ import { ChevronRightIcon } from "lucide-react"
782
+ import * as React from "react"
783
+
784
+ const items: TreeItemData[] = Array.from({ length: 100 }, (_, folder) => ({
785
+ value: `folder-${folder + 1}`,
786
+ label: `Folder ${folder + 1}`,
787
+ children: Array.from({ length: 100 }, (_, file) => ({
788
+ value: `folder-${folder + 1}/file-${file + 1}`,
789
+ label: `File ${file + 1}.txt`,
790
+ })),
791
+ }))
792
+
793
+ const row = 32
794
+ const height = 288
795
+
796
+ function Example() {
797
+ const scroller = React.useRef<HTMLDivElement>(null)
798
+ const [scrollTop, setScrollTop] = React.useState(0)
799
+ const [expanded, setExpanded] = React.useState(["folder-1"])
800
+ const rows = React.useMemo(() => visibleTreeRows(items, expanded), [expanded])
801
+ const start = Math.max(0, Math.floor(scrollTop / row) - 4)
802
+ const end = Math.min(rows.length, Math.ceil((scrollTop + height) / row) + 4)
803
+ return (
804
+ <div
805
+ data-slot="scroll"
806
+ ref={scroller}
807
+ onScroll={(event) => setScrollTop(event.currentTarget.scrollTop)}
808
+ >
809
+ <Tree
810
+ aria-label="Archive"
811
+ items={items}
812
+ expanded={expanded}
813
+ onExpandedChange={setExpanded}
814
+ onHighlight={(_value, { index }) => {
815
+ const el = scroller.current
816
+ if (!el) {
817
+ return
818
+ }
819
+ const top = index * row
820
+ if (top < el.scrollTop) {
821
+ el.scrollTop = top
822
+ } else if (top + row > el.scrollTop + el.clientHeight - 10) {
823
+ el.scrollTop = top + row - el.clientHeight + 10
824
+ }
825
+ setScrollTop(el.scrollTop)
826
+ }}
827
+ style={{ height: rows.length * row }}
828
+ >
829
+ {rows.slice(start, end).map(({ item, index }) => (
830
+ <TreeItem key={item.value} value={item.value} style={{ top: index * row }}>
831
+ <TreeItemLabel>
832
+ {item.children && (
833
+ <TreeItemIndicator>
834
+ <ChevronRightIcon aria-hidden="true" />
835
+ </TreeItemIndicator>
836
+ )}
837
+ {item.label}
838
+ </TreeItemLabel>
839
+ </TreeItem>
840
+ ))}
841
+ </Tree>
842
+ </div>
843
+ )
844
+ }
845
+
846
+ export { Example }
847
+ ```
848
+
849
+ Expand folders or press End to focus Folder 100. The tree has 10,100 items, but only nearby rows render.
850
+
851
+ ## Find in Page
852
+
853
+ `hiddenUntilFound` keeps collapsed groups in the DOM with `hidden="until-found"`. In browsers that support it, Find in Page can match a hidden item and open its ancestors. Use `keepMounted` when you only need collapsed content to stay mounted, without browser search.
854
+
855
+ ```tsx title="find-in-page.tsx"
856
+ "use client"
857
+
858
+ import {
859
+ Tree,
860
+ TreeGroup,
861
+ TreeItem,
862
+ TreeItemIndicator,
863
+ TreeItemLabel,
864
+ type TreeItemData,
865
+ } from "@ui-sdk/react/tree"
866
+ import { ChevronRightIcon } from "lucide-react"
867
+
868
+ const items: TreeItemData[] = [
869
+ {
870
+ value: "planets",
871
+ label: "Planets",
872
+ children: [
873
+ { value: "mercury", label: "Mercury" },
874
+ { value: "venus", label: "Venus" },
875
+ {
876
+ value: "earth",
877
+ label: "Earth",
878
+ children: [{ value: "moon", label: "The Moon" }],
879
+ },
880
+ {
881
+ value: "mars",
882
+ label: "Mars",
883
+ children: [
884
+ { value: "phobos", label: "Phobos" },
885
+ { value: "deimos", label: "Deimos" },
886
+ ],
887
+ },
888
+ ],
889
+ },
890
+ {
891
+ value: "dwarf-planets",
892
+ label: "Dwarf planets",
893
+ children: [
894
+ { value: "ceres", label: "Ceres" },
895
+ {
896
+ value: "pluto",
897
+ label: "Pluto",
898
+ children: [{ value: "charon", label: "Charon" }],
899
+ },
900
+ ],
901
+ },
902
+ ]
903
+
904
+ function Items({ items }: { items: TreeItemData[] }) {
905
+ return items.map((item) => (
906
+ <TreeItem key={item.value} value={item.value}>
907
+ <TreeItemLabel>
908
+ {item.children && (
909
+ <TreeItemIndicator>
910
+ <ChevronRightIcon aria-hidden="true" />
911
+ </TreeItemIndicator>
912
+ )}
913
+ {item.label}
914
+ </TreeItemLabel>
915
+ {item.children && (
916
+ <TreeGroup>
917
+ <Items items={item.children} />
918
+ </TreeGroup>
919
+ )}
920
+ </TreeItem>
921
+ ))
922
+ }
923
+
924
+ function Example() {
925
+ return (
926
+ <Tree aria-label="Solar system" items={items} hiddenUntilFound>
927
+ <Items items={items} />
928
+ </Tree>
929
+ )
930
+ }
931
+
932
+ export { Example }
933
+ ```
934
+
935
+ Use your browser's Find command to look for Phobos, a moon of Mars. The matching branches open to reveal it.
936
+
937
+ ## Animation
938
+
939
+ A group exposes its measured height as `--tree-group-height`. Transition between that height and zero from `data-starting-style` and `data-ending-style`:
940
+
941
+ ```css
942
+ [data-slot="tree-group"] {
943
+ height: var(--tree-group-height);
944
+ overflow: hidden;
945
+ transition: height 150ms ease-out;
946
+
947
+ &:is([data-starting-style], [data-ending-style]) {
948
+ height: 0;
949
+ }
950
+ }
951
+ ```
952
+
953
+ The group stays mounted until its closing animation ends. Groups open on the first render, or revealed by Find in Page, don't animate.
954
+
955
+ ## Controlled
956
+
957
+ Selection, expanded branches, and checkboxes are independent:
958
+
959
+ | Value | Shape | Change Notification |
960
+ | ---------- | ---------------------------------------------- | ------------------- |
961
+ | `value` | A string or `null`; an array in multiple mode. | `onValueChange` |
962
+ | `expanded` | An array of branch values. | `onExpandedChange` |
963
+ | `checked` | An array shaped by the check strategy. | `onCheckedChange` |
964
+
965
+ Supply a value and its change callback to control it, or use `defaultValue`, `defaultExpanded`, and `defaultChecked` for starting state. Call `details.cancel()` in a change callback to reject a change. [State and Events](../state-and-events.md) covers the shared binding pattern.
966
+
967
+ ## Controller
968
+
969
+ A `createTreeController()` instance can expand or collapse branches, change selection and checkboxes, or focus an item. `reveal(value)` opens an item's ancestors, scrolls it into view, and focuses it. Supply the hierarchy through `items`, as this example does, or keep collapsed groups mounted so their descendants are known. See [Controllers](../controllers.md) for setup and lifecycle.
970
+
971
+ ```tsx title="controller.tsx"
972
+ "use client"
973
+
974
+ import {
975
+ createTreeController,
976
+ Tree,
977
+ TreeGroup,
978
+ TreeItem,
979
+ TreeItemIndicator,
980
+ TreeItemLabel,
981
+ type TreeItemData,
982
+ } from "@ui-sdk/react/tree"
983
+ import { ChevronRightIcon } from "lucide-react"
984
+ import * as React from "react"
985
+
986
+ const sections: TreeItemData[] = [
987
+ {
988
+ value: "account",
989
+ label: "Account",
990
+ children: [
991
+ { value: "profile", label: "Profile" },
992
+ {
993
+ value: "security",
994
+ label: "Security",
995
+ children: [
996
+ { value: "password", label: "Password" },
997
+ { value: "two-factor", label: "Two-factor authentication" },
998
+ ],
999
+ },
1000
+ ],
1001
+ },
1002
+ {
1003
+ value: "workspace",
1004
+ label: "Workspace",
1005
+ children: [
1006
+ { value: "members", label: "Members" },
1007
+ { value: "billing", label: "Billing" },
1008
+ ],
1009
+ },
1010
+ ]
1011
+
1012
+ function Items({ items }: { items: TreeItemData[] }) {
1013
+ return items.map((item) => (
1014
+ <TreeItem key={item.value} value={item.value}>
1015
+ <TreeItemLabel>
1016
+ {item.children && (
1017
+ <TreeItemIndicator>
1018
+ <ChevronRightIcon aria-hidden="true" />
1019
+ </TreeItemIndicator>
1020
+ )}
1021
+ {item.label}
1022
+ </TreeItemLabel>
1023
+ {item.children && (
1024
+ <TreeGroup>
1025
+ <Items items={item.children} />
1026
+ </TreeGroup>
1027
+ )}
1028
+ </TreeItem>
1029
+ ))
1030
+ }
1031
+
1032
+ function Example() {
1033
+ const [settings] = React.useState(() => createTreeController())
1034
+ return (
1035
+ <div data-slot="stack">
1036
+ <div data-slot="controls">
1037
+ <button type="button" onClick={() => settings.expandAll()}>
1038
+ Expand All
1039
+ </button>
1040
+ <button type="button" onClick={() => settings.collapseAll()}>
1041
+ Collapse All
1042
+ </button>
1043
+ <button
1044
+ type="button"
1045
+ onClick={() => {
1046
+ settings.setValue("two-factor")
1047
+ settings.reveal("two-factor")
1048
+ }}
1049
+ >
1050
+ Find Two-Factor
1051
+ </button>
1052
+ </div>
1053
+ <Tree aria-label="Settings" items={sections} controller={settings}>
1054
+ <Items items={sections} />
1055
+ </Tree>
1056
+ </div>
1057
+ )
1058
+ }
1059
+
1060
+ export { Example }
1061
+ ```
1062
+
1063
+ Press Find Two-Factor to open Account and Security, then select and focus Two-factor authentication. Collapse All closes every branch.
1064
+
1065
+ ## Custom Rendering
1066
+
1067
+ Every part takes `render`. The Links example renders `TreeItemLabel` as an `a`; a link in a label leaves the tab order to its item. `TreeItemLabel` and `TreeItemIndicator` also take a function of the item's state, which the File Paths and Loading Children examples use for their spinners. See [Parts and Composition](../parts-and-composition.md) for prop merging and state-based rendering.
1068
+
1069
+ ## Styling
1070
+
1071
+ Style each row through `TreeItemLabel`, which carries its item's `data-selected`, `data-expanded`, `data-branch`, `data-current`, `data-disabled`, `data-loading`, `data-failed` and `data-pending`. An item's own background would also cover its children. Focus stays on the item, so style a focused row as a label inside an item with `:focus-visible`. Turn the indicator with its `data-expanded`, and indent rows with `--tree-level` or padding on each group.
1072
+
1073
+ Each part has a `data-slot`, such as `tree-label`, and its state attributes are listed under [Data Attributes](#data-attributes).
1074
+
1075
+ ### TreeItem
1076
+ - `--tree-level`: The item's depth, 1 for a top-level item. Indent a flat tree with it.
1077
+
1078
+ ### TreeGroup
1079
+ - `--tree-group-height`: The group's content height in pixels while it animates, auto once expanded and 0px while collapsed.
1080
+
1081
+ ## Data Attributes
1082
+
1083
+ ### TreeItem
1084
+ `[data-slot="tree-item"]`
1085
+
1086
+ - `data-level` (`1`, `2`): The item's depth, from 1 at the top level. Also in the `--tree-level` variable.
1087
+ - `data-branch`: Present on a branch: an item with children, or with `branch` set.
1088
+ - `data-expanded`: Present while a branch is expanded.
1089
+ - `data-indeterminate`: Present while only some of a branch is checked, with `checkable`.
1090
+ - `data-selected`: Present while the item is selected.
1091
+ - `data-current`: Present on the item for the current page, from `current`.
1092
+ - `data-checked`: Present while the item's checkbox is checked, with `checkable`.
1093
+ - `data-disabled`: Present when the item, or the whole tree, is disabled.
1094
+ - `data-unchecked`: Present while the item's checkbox is unchecked, with `checkable`.
1095
+ - `data-loading`: Present while `loadChildren` loads the branch's children.
1096
+ - `data-failed`: Present after the branch's children failed to load, until it loads again.
1097
+ - `data-pending`: Present while the item's `onAction` Promise hasn't settled.
1098
+
1099
+ ### TreeItemLabel
1100
+ `[data-slot="tree-label"]`
1101
+
1102
+ - `data-level` (`1`, `2`): The item's depth, from 1 at the top level. Also in the `--tree-level` variable.
1103
+ - `data-branch`: Present on a branch: an item with children, or with `branch` set.
1104
+ - `data-expanded`: Present while a branch is expanded.
1105
+ - `data-selected`: Present while the item is selected.
1106
+ - `data-current`: Present on the item for the current page, from `current`.
1107
+ - `data-disabled`: Present when the item, or the whole tree, is disabled.
1108
+ - `data-loading`: Present while `loadChildren` loads the branch's children.
1109
+ - `data-failed`: Present after the branch's children failed to load, until it loads again.
1110
+ - `data-pending`: Present while the item's `onAction` Promise hasn't settled.
1111
+
1112
+ ### TreeItemIndicator
1113
+ `[data-slot="tree-indicator"]`
1114
+
1115
+ - `data-level` (`1`): The item's depth, from 1 at the top level. Also in the `--tree-level` variable.
1116
+ - `data-branch`: Present on a branch: an item with children, or with `branch` set.
1117
+ - `data-expanded`: Present while a branch is expanded.
1118
+ - `data-loading`: Present while `loadChildren` loads the branch's children.
1119
+ - `data-failed`: Present after the branch's children failed to load, until it loads again.
1120
+ - `data-pending`: Present while the item's `onAction` Promise hasn't settled.
1121
+
1122
+ ### TreeItemCheckbox
1123
+ `[data-slot="tree-checkbox"]`
1124
+
1125
+ - `data-indeterminate`: Present while only some of a branch is checked, with `checkable`.
1126
+ - `data-checked`: Present while the item's checkbox is checked, with `checkable`.
1127
+ - `data-unchecked`: Present while the item's checkbox is unchecked, with `checkable`.
1128
+
1129
+ ### TreeGroup
1130
+ `[data-slot="tree-group"]`
1131
+
1132
+ - `data-open`: Present while the group's branch is expanded.
1133
+ - `data-closed`: Present while the group's branch is collapsed, including while it animates out.
1134
+
1135
+ ## API Reference
1136
+
1137
+ ### Tree
1138
+
1139
+ - `items?` (`TreeItemData[]`): The items as data: each a `value`, a `label` and, for a branch, `children`. Renders them for you, and gives the tree its whole shape even where rows aren't rendered.
1140
+ - `paths?` (`readonly string[]`): Items from file paths, like `["src/index.ts", "README.md"]`: each folder becomes a branch, folders first. A path ending in `/` is an empty folder.
1141
+ - `mode?` (`Mode`): How many items can be selected.
1142
+ - `value?` (`TreeValue<Mode>`): The selected value. Pass it to control the selection. `null` selects nothing, and with `mode: "multiple"` it's a list.
1143
+ - `defaultValue?` (`TreeValue<Mode>`): The value selected at first, when uncontrolled.
1144
+ - `onValueChange?` (`((value: TreeValue<Mode>, details: ChangeDetails<TreeValueChangeReason>) => void)`): Runs when the selection wants to change. `details.reason` says why, and `details.cancel()` stops it.
1145
+ - `activation?` (`"automatic" | "manual"`): Whether moving focus with the keyboard selects, with one item selectable: `"automatic"` selects the focused item, `"manual"` waits for Enter, Space or a press.
1146
+ - `expanded?` (`string[]`): The expanded branches' values. Pass it to control which branches are open.
1147
+ - `defaultExpanded?` (`string[]`): The branches expanded at first, when uncontrolled. The ancestors of `current` are added.
1148
+ - `onExpandedChange?` (`((expanded: string[], details: ChangeDetails<TreeExpandedChangeReason>) => void)`): Runs when branches want to expand or collapse. `details.reason` says why, and `details.cancel()` stops it.
1149
+ - `expandOnPress?` (`boolean`): Whether pressing a branch's row expands or collapses it. Its indicator always does.
1150
+ - `checkable?` (`boolean`): Gives every item a tri-state checkbox, kept apart from the selection. Checking a branch checks everything in it, and a branch shows mixed while only some of it is checked. Space checks the focused item.
1151
+ - `checked?` (`string[]`): The checked items' values, as `checkStrategy` reports them. Pass it to control the checkboxes.
1152
+ - `defaultChecked?` (`string[]`): The items checked at first, when uncontrolled. A branch stands for everything in it.
1153
+ - `onCheckedChange?` (`((checked: string[], details: ChangeDetails<TreeCheckedChangeReason>) => void)`): Runs when checkboxes want to change. `details.reason` says why, and `details.cancel()` stops it.
1154
+ - `checkStrategy?` (`TreeCheckStrategy`): Which items the checked value lists: `"leaf"` the checked leaves, `"trunk"` the outermost fully checked items, `"branch"` every fully checked item, and `"independent"` each item on its own, with no cascading.
1155
+ - `onAction?` (`((value: string, details: TreeActionDetails) => unknown)`): Runs when an item is pressed or Enter is pressed on it, apart from selecting it, like opening a file. Return a Promise to mark the item pending until it settles.
1156
+ - `pendingAnnouncement?` (`string`): What screen readers hear once an item's action has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
1157
+ - `loadChildren?` (`((value: string, details: { signal: AbortSignal; }) => void | Promise<unknown>)`): Loads a branch's children the first time it expands while it has none. The branch shows `data-loading` and expands once the Promise resolves; a rejection marks it `data-failed` and the next expand tries again. Expanding it again while loading cancels through `signal`.
1158
+ - `current?` (`string | null`): The value of the item for the current page. It gets `aria-current="page"`, takes the tab stop first, and its ancestors expand, at first and whenever it changes.
1159
+ - `onHighlight?` (`((value: string, details: ListHighlightDetails) => void)`): Runs when the keyboard moves focus to an item, with its row among the items that show, from 0. A virtualized list scrolls that row into view here, so an item it hasn't rendered can take focus once it does.
1160
+ - `disabled?` (`boolean`): Disables every item. Items stay focusable unless `focusableWhenDisabled` is false.
1161
+ - `focusableWhenDisabled?` (`boolean`): Whether disabled items take focus, so a screen reader still finds them.
1162
+ - `keepMounted?` (`boolean`): Whether collapsed branches keep their groups in the page, hidden.
1163
+ - `hiddenUntilFound?` (`boolean`): Keeps collapsed groups in the page as `hidden="until-found"`, so the browser's find in page can match their text and expand them.
1164
+ - `announcement?` (`((change: TreeAnnouncement) => string | null | undefined)`): What screen readers hear when a branch expands, collapses, starts loading or fails to load. Return null for nothing. Expanding and collapsing are announced on Apple platforms only, where VoiceOver doesn't read the change.
1165
+ - `controller?` (`object`): Drives the tree from anywhere. Create one with `createTreeController()`.
1166
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
1167
+
1168
+ ### TreeItem
1169
+
1170
+ - `value` (`string`): Identifies the item, unique in the tree. Passed to `onAction`, and listed by the value, `expanded` and `checked`.
1171
+ - `branch?` (`boolean`): Whether it's a branch. Adapters set it when it has a group; set it yourself for a branch whose children load later.
1172
+ - `disabled?` (`boolean`): Stops it from being selected, checked, expanded or run. It stays focusable unless `focusableWhenDisabled` is false.
1173
+ - `label?` (`string`): The text typeahead matches, when its label isn't plain text.
1174
+
1175
+ ### TreeGroup
1176
+
1177
+ - `keepMounted?` (`boolean`): Whether the group stays in the page, hidden, while collapsed. Overrides the tree's.
1178
+ - `hiddenUntilFound?` (`boolean`): Whether the group stays findable with `hidden="until-found"` while collapsed. Overrides the tree's.
1179
+
1180
+ ### Items as Data
1181
+
1182
+ Each item in `items`:
1183
+
1184
+ - `value` (`string`): Identifies the item. Unique in the tree.
1185
+ - `label` (`string`): What it shows, and what typeahead matches.
1186
+ - `children?` (`TreeItemData[]`): Its children. Leave it out for a leaf. An empty array is a branch with no children, or one whose children load when it opens.
1187
+ - `disabled?` (`boolean`): Whether it can't be selected, checked or activated. It can still take focus.
1188
+ - `href?` (`string`): Makes the item a link to this address.
1189
+
1190
+ Each row `visibleTreeRows(items, expanded)` returns, for a flat list:
1191
+
1192
+ - `item` (`TreeItemData`): The object from your data this row shows, the same one you passed in, children included.
1193
+ - `parent` (`string | null`): The value of the item it's nested in, or `null` at the top level.
1194
+ - `level` (`number`): Its depth, 1 at the top level.
1195
+ - `index` (`number`): Its place among the rows that show, from 0, for a virtualized list.
1196
+
1197
+ ### State
1198
+
1199
+ What render functions, state-based class names and slots receive, per part.
1200
+
1201
+ `Tree`:
1202
+
1203
+ - `mode` (`TreeMode`): How many items can be selected.
1204
+ - `checkable` (`boolean`): Whether items have checkboxes.
1205
+ - `disabled` (`boolean`): Whether the whole tree is disabled, by its `disabled` option or a disabled fieldset around it.
1206
+
1207
+ `TreeItem`, and its `TreeItemLabel`, `TreeItemIndicator` and `TreeItemCheckbox`:
1208
+
1209
+ - `expanded` (`boolean`): Whether it's a branch that's expanded.
1210
+ - `selected` (`boolean`): Whether its value is in the tree's value. Separate from `checked`, which its checkbox sets.
1211
+ - `checked` (`TreeCheckedState`): Its checkbox: checked, unchecked or `"indeterminate"`. False without `checkable`.
1212
+ - `disabled` (`boolean`): Whether it can't be selected, checked, expanded or run.
1213
+ - `branch` (`boolean`): Whether it has children, or can load them.
1214
+ - `level` (`number`): Its depth, 1 at the top level.
1215
+ - `current` (`boolean`): Whether it's the current page's item.
1216
+ - `loading` (`boolean`): Whether its children are loading.
1217
+ - `failed` (`boolean`): Whether loading its children failed.
1218
+ - `pending` (`boolean`): Whether its `onAction` is still running.
1219
+
1220
+ `TreeGroup`:
1221
+
1222
+ - `expanded` (`boolean`): Whether its branch is expanded.
1223
+ - `level` (`number`): The depth of the items in it.
1224
+
1225
+ ### Reasons
1226
+
1227
+ Why branches expanded or collapsed:
1228
+
1229
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
1230
+ - `trigger-press` (`MouseEvent | KeyboardEvent | TouchEvent`): Click, tap, Enter or Space on the part that opens, closes or toggles. Arrow keys that open a menu or submenu count too. A tap on a combobox's input that opens a sheet reports its `touchend`.
1231
+ - `list-navigation` (`KeyboardEvent`): Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or ⌘ with A selected every item of a listbox.
1232
+ - `find-in-page` (`Event`): The browser's find in page matched hidden content.
1233
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
1234
+
1235
+ Why the selection changed:
1236
+
1237
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
1238
+ - `list-navigation` (`KeyboardEvent`): Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or ⌘ with A selected every item of a listbox.
1239
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
1240
+
1241
+ And why the checkboxes changed:
1242
+
1243
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
1244
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
1245
+
1246
+ ### Controller
1247
+
1248
+ Create one with `createTreeController()` and pass it as `controller`.
1249
+
1250
+ - `getExpanded()` (`() => string[]`): The values of the expanded branches.
1251
+ - `setExpanded(expanded, options?)` (`(expanded: string[], options?: TreeChangeOptions<TreeExpandedChangeReason> | undefined) => boolean`): Expands exactly these branches. Returns false when canceled.
1252
+ - `isExpanded(value)` (`(value: string) => boolean`): Whether a branch is expanded.
1253
+ - `expand(value, options?)` (`(value: string, options?: TreeChangeOptions<TreeExpandedChangeReason> | undefined) => boolean`): Expands a branch, loading its children first with `loadChildren` when it has none.
1254
+ - `collapse(value, options?)` (`(value: string, options?: TreeChangeOptions<TreeExpandedChangeReason> | undefined) => boolean`): Collapses a branch. Focus inside it moves to the branch.
1255
+ - `toggle(value, options?)` (`(value: string, options?: TreeChangeOptions<TreeExpandedChangeReason> | undefined) => boolean`): Expands a collapsed branch, or collapses an expanded one.
1256
+ - `expandAll(options?)` (`(options?: TreeChangeOptions<TreeExpandedChangeReason> | undefined) => boolean`): Expands every branch, and every branch that renders inside one later.
1257
+ - `collapseAll(options?)` (`(options?: TreeChangeOptions<TreeExpandedChangeReason> | undefined) => boolean`): Collapses every branch.
1258
+ - `getValue()` (`() => string | string[] | null`): The selected value: a value or `null`, or a list with `mode: "multiple"`.
1259
+ - `setValue(value, options?)` (`(value: string | string[] | null, options?: TreeChangeOptions<TreeValueChangeReason> | undefined) => boolean`): Selects these items.
1260
+ - `getChecked()` (`() => string[]`): The checked value, as `checkStrategy` reports it.
1261
+ - `setChecked(checked, options?)` (`(checked: string[], options?: TreeChangeOptions<TreeCheckedChangeReason> | undefined) => boolean`): Sets the checked value.
1262
+ - `checkedState(value)` (`(value: string) => TreeCheckedState`): Whether an item's checkbox is checked, unchecked or `"indeterminate"`.
1263
+ - `reveal(value)` (`(value: string) => void`): Expands an item's ancestors, scrolls it into view and focuses it.
1264
+ - `focus(value?)` (`(value?: string | undefined) => void`): Focuses an item, or the item with the tab stop.
1265
+
1266
+ ## Accessibility
1267
+
1268
+ The tree follows the [WAI-ARIA tree view pattern](https://www.w3.org/WAI/ARIA/apg/patterns/treeview/). Items have the `treeitem` role with `aria-level`, `aria-expanded` on branches, and `aria-checked` when the tree is checkable.
1269
+
1270
+ ### Keyboard
1271
+
1272
+ The tree has one tab stop. Tab enters at the current page, the selected item, or the first item, then returns to the item focused last.
1273
+
1274
+ | Key | Action |
1275
+ | --------------------------- | ------------------------------------------------------ |
1276
+ | Down / Up Arrow | Focus the next or previous visible item. |
1277
+ | Right Arrow | Expand a branch, or enter its first child. |
1278
+ | Left Arrow | Collapse a branch, or focus its parent. |
1279
+ | Home / End | Focus the first or last visible item. |
1280
+ | Alt+Right / Alt+Left Arrow | Expand or collapse a branch and its descendants. |
1281
+ | `*` | Expand sibling branches. |
1282
+ | Enter | Select and run the item's action, or follow its link. |
1283
+ | Space | Check the item when checkable, otherwise select it. |
1284
+ | Shift+Down / Shift+Up Arrow | Extend a multiple selection. |
1285
+ | Cmd+A / Ctrl+A | Select visible items in multiple selection mode. |
1286
+ | Typing | Focus the next item whose label starts with that text. |
1287
+
1288
+ Option is Alt on a Mac. In right-to-left text, Left and Right swap.
1289
+
1290
+ ### Tested Behavior
1291
+
1292
+ Every line below is an automated test.
1293
+
1294
+ **Expanding and collapsing**
1295
+
1296
+ - Branches start collapsed, and only top-level items show.
1297
+ - Clicking a branch expands it and shows its children.
1298
+ - Clicking the indicator expands without selecting.
1299
+ - A change can be canceled.
1300
+ - A default can expand nested branches.
1301
+ - A controller expands and collapses everything.
1302
+ - Reveal expands an item's ancestors and focuses it.
1303
+
1304
+ **Keyboard**
1305
+
1306
+ - Tab reaches the tree once, on the first item.
1307
+ - Down and Up move between the items that show.
1308
+ - Right expands a branch, then moves to its first child.
1309
+ - Right on a leaf does nothing.
1310
+ - Left collapses a branch, then moves to its parent.
1311
+ - Home and End move to the first and last items that show.
1312
+ - * expands every branch beside the focused item.
1313
+ - Alt+Right expands a branch and everything in it.
1314
+ - Typing moves to the next item that starts with the text.
1315
+ - Enter selects the item and runs its action.
1316
+ - A key on a nested item acts on that item only.
1317
+
1318
+ **Selection**
1319
+
1320
+ - Clicking an item selects it alone.
1321
+ - The selected item takes the tab stop.
1322
+ - Disabled items take focus but can't be selected.
1323
+ - With multiple selection, Shift+Down extends and Ctrl+A selects everything.
1324
+ - With automatic activation, moving focus selects.
1325
+ - Without selection, items have no aria-selected.
1326
+
1327
+ **Checkboxes**
1328
+
1329
+ - Checking a branch checks everything in it.
1330
+ - Space checks the focused item, and a disabled item keeps its check.
1331
+ - Checking a leaf leaves the selection and the branch alone.
1332
+
1333
+ **Positions**
1334
+
1335
+ - Every item says its level and place among its siblings.
1336
+ - The current page's item is marked.
1337
+
1338
+ Runs in React, Vue and web components.
1339
+
1340
+ Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:
1341
+
1342
+ - Announcements while using the tree with a keyboard.
1343
+ - Expanding the focused item is announced on Apple platforms.
1344
+ - Nothing is announced elsewhere, where screen readers read the change.
1345
+
1346
+ Runs in React, Vue and web components.