@djangocfg/ui-tools 2.1.314 → 2.1.316

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 (56) hide show
  1. package/dist/TreeRoot-A25RIGYE.cjs +19 -0
  2. package/dist/TreeRoot-A25RIGYE.cjs.map +1 -0
  3. package/dist/TreeRoot-HBRJEHBH.mjs +4 -0
  4. package/dist/TreeRoot-HBRJEHBH.mjs.map +1 -0
  5. package/dist/chunk-4CEOJDMB.cjs +1300 -0
  6. package/dist/chunk-4CEOJDMB.cjs.map +1 -0
  7. package/dist/chunk-KR6B3LVY.mjs +59 -0
  8. package/dist/chunk-KR6B3LVY.mjs.map +1 -0
  9. package/dist/chunk-NFIMVYJU.mjs +1249 -0
  10. package/dist/chunk-NFIMVYJU.mjs.map +1 -0
  11. package/dist/chunk-YXBOAGIM.cjs +63 -0
  12. package/dist/chunk-YXBOAGIM.cjs.map +1 -0
  13. package/dist/index.cjs +151 -5
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +5 -1
  16. package/dist/index.d.ts +5 -1
  17. package/dist/index.mjs +11 -2
  18. package/dist/index.mjs.map +1 -1
  19. package/dist/tree/index.cjs +152 -0
  20. package/dist/tree/index.cjs.map +1 -0
  21. package/dist/tree/index.d.cts +442 -0
  22. package/dist/tree/index.d.ts +442 -0
  23. package/dist/tree/index.mjs +5 -0
  24. package/dist/tree/index.mjs.map +1 -0
  25. package/package.json +11 -6
  26. package/src/index.ts +4 -0
  27. package/src/tools/Tree/README.md +220 -0
  28. package/src/tools/Tree/Tree.story.tsx +536 -0
  29. package/src/tools/Tree/TreeRoot.tsx +164 -0
  30. package/src/tools/Tree/components/TreeChevron.tsx +39 -0
  31. package/src/tools/Tree/components/TreeContent.tsx +48 -0
  32. package/src/tools/Tree/components/TreeEmpty.tsx +21 -0
  33. package/src/tools/Tree/components/TreeError.tsx +24 -0
  34. package/src/tools/Tree/components/TreeIcon.tsx +29 -0
  35. package/src/tools/Tree/components/TreeIndentGuides.tsx +33 -0
  36. package/src/tools/Tree/components/TreeLabel.tsx +24 -0
  37. package/src/tools/Tree/components/TreeRow.tsx +163 -0
  38. package/src/tools/Tree/components/TreeSearchInput.tsx +50 -0
  39. package/src/tools/Tree/components/TreeSkeleton.tsx +22 -0
  40. package/src/tools/Tree/components/index.ts +22 -0
  41. package/src/tools/Tree/context/TreeContext.tsx +538 -0
  42. package/src/tools/Tree/context/hooks.ts +110 -0
  43. package/src/tools/Tree/context/index.ts +13 -0
  44. package/src/tools/Tree/data/appearance.ts +175 -0
  45. package/src/tools/Tree/data/childCache.ts +43 -0
  46. package/src/tools/Tree/data/createDemoTree.ts +42 -0
  47. package/src/tools/Tree/data/flatten.ts +51 -0
  48. package/src/tools/Tree/data/index.ts +24 -0
  49. package/src/tools/Tree/data/persist.ts +62 -0
  50. package/src/tools/Tree/hooks/index.ts +6 -0
  51. package/src/tools/Tree/hooks/useTreeKeyboard.ts +171 -0
  52. package/src/tools/Tree/hooks/useTreeTypeAhead.ts +100 -0
  53. package/src/tools/Tree/index.tsx +99 -0
  54. package/src/tools/Tree/lazy.tsx +14 -0
  55. package/src/tools/Tree/types.ts +136 -0
  56. package/src/tools/index.ts +75 -0
@@ -0,0 +1,1249 @@
1
+ import { __name } from './chunk-CGILA3WO.mjs';
2
+ import * as React from 'react';
3
+ import { createContext, useMemo, useReducer, useRef, useCallback, useEffect, Fragment as Fragment$1 } from 'react';
4
+ import { cn } from '@djangocfg/ui-core/lib';
5
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
6
+ import { ChevronDown, ChevronRight, FolderOpen, Folder, File, Loader2, Search, X } from 'lucide-react';
7
+ import { useHotkey } from '@djangocfg/ui-core/hooks';
8
+
9
+ // src/tools/Tree/types.ts
10
+ var DEFAULT_TREE_LABELS = {
11
+ loading: "Loading\u2026",
12
+ empty: "Nothing to show",
13
+ error: "Failed to load",
14
+ searchPlaceholder: "Search\u2026",
15
+ searchMatches: /* @__PURE__ */ __name((n) => `${n} match${n === 1 ? "" : "es"}`, "searchMatches"),
16
+ ariaLabel: "Tree"
17
+ };
18
+
19
+ // src/tools/Tree/data/childCache.ts
20
+ var createChildCache = /* @__PURE__ */ __name(() => /* @__PURE__ */ new Map(), "createChildCache");
21
+ var resolveChildren = /* @__PURE__ */ __name((cache, node) => {
22
+ if (Array.isArray(node.children)) {
23
+ return { children: node.children, status: "loaded" };
24
+ }
25
+ const entry = cache.get(node.id);
26
+ if (!entry) return { children: null, status: "idle" };
27
+ if (entry.status === "loaded") {
28
+ return { children: entry.children, status: "loaded" };
29
+ }
30
+ return { children: null, status: entry.status, error: entry.error };
31
+ }, "resolveChildren");
32
+
33
+ // src/tools/Tree/data/flatten.ts
34
+ var isNodeFolder = /* @__PURE__ */ __name((node) => {
35
+ if (typeof node.isFolder === "boolean") return node.isFolder;
36
+ return Array.isArray(node.children);
37
+ }, "isNodeFolder");
38
+ function flattenTree({ roots, expandedIds, cache }) {
39
+ const out = [];
40
+ const walk = /* @__PURE__ */ __name((nodes, level, parentId) => {
41
+ for (const node of nodes) {
42
+ const isFolder = isNodeFolder(node);
43
+ const isExpanded = expandedIds.has(node.id);
44
+ const resolved = isFolder ? resolveChildren(cache, node) : { children: [], status: "loaded" };
45
+ out.push({
46
+ node,
47
+ level,
48
+ parentId,
49
+ isFolder,
50
+ isExpanded,
51
+ isLoading: resolved.status === "loading",
52
+ hasError: resolved.status === "error"
53
+ });
54
+ if (isFolder && isExpanded && resolved.children) {
55
+ walk(resolved.children, level + 1, node.id);
56
+ }
57
+ }
58
+ }, "walk");
59
+ walk(roots, 0, null);
60
+ return out;
61
+ }
62
+ __name(flattenTree, "flattenTree");
63
+
64
+ // src/tools/Tree/data/persist.ts
65
+ var KEY_PREFIX = "@djangocfg/tree:";
66
+ var VERSION = 1;
67
+ function safeStorage() {
68
+ if (typeof window === "undefined") return null;
69
+ try {
70
+ return window.localStorage;
71
+ } catch {
72
+ return null;
73
+ }
74
+ }
75
+ __name(safeStorage, "safeStorage");
76
+ function loadTreeState(key) {
77
+ const storage = safeStorage();
78
+ if (!storage) return null;
79
+ try {
80
+ const raw = storage.getItem(KEY_PREFIX + key);
81
+ if (!raw) return null;
82
+ const parsed = JSON.parse(raw);
83
+ if (parsed.version !== VERSION) return null;
84
+ return {
85
+ expandedItems: Array.isArray(parsed.expandedItems) ? parsed.expandedItems : [],
86
+ selectedItems: Array.isArray(parsed.selectedItems) ? parsed.selectedItems : []
87
+ };
88
+ } catch {
89
+ return null;
90
+ }
91
+ }
92
+ __name(loadTreeState, "loadTreeState");
93
+ function saveTreeState(key, state) {
94
+ const storage = safeStorage();
95
+ if (!storage) return;
96
+ try {
97
+ const payload = { ...state, version: VERSION };
98
+ storage.setItem(KEY_PREFIX + key, JSON.stringify(payload));
99
+ } catch {
100
+ }
101
+ }
102
+ __name(saveTreeState, "saveTreeState");
103
+ function clearTreeState(key) {
104
+ const storage = safeStorage();
105
+ if (!storage) return;
106
+ try {
107
+ storage.removeItem(KEY_PREFIX + key);
108
+ } catch {
109
+ }
110
+ }
111
+ __name(clearTreeState, "clearTreeState");
112
+
113
+ // src/tools/Tree/data/appearance.ts
114
+ var DENSITY_PRESETS = {
115
+ compact: { rowHeight: 24, iconSize: 14, fontSize: 13, gap: 6 },
116
+ cozy: { rowHeight: 28, iconSize: 16, fontSize: 13, gap: 8 },
117
+ comfortable: { rowHeight: 32, iconSize: 16, fontSize: 14, gap: 8 }
118
+ };
119
+ var DEFAULT_TREE_APPEARANCE = {
120
+ density: "cozy",
121
+ ...DENSITY_PRESETS.cozy,
122
+ iconStrokeWidth: 1.5,
123
+ indent: 16,
124
+ accent: "default",
125
+ radius: "sm",
126
+ indentGuideOpacity: 0.4,
127
+ showActiveIndicator: true
128
+ };
129
+ function resolveAppearance(input, outerIndent) {
130
+ if (!input && outerIndent === void 0) return DEFAULT_TREE_APPEARANCE;
131
+ const density = input?.density ?? "cozy";
132
+ const preset = DENSITY_PRESETS[density];
133
+ return {
134
+ density,
135
+ rowHeight: input?.rowHeight ?? preset.rowHeight,
136
+ iconSize: input?.iconSize ?? preset.iconSize,
137
+ iconStrokeWidth: input?.iconStrokeWidth ?? DEFAULT_TREE_APPEARANCE.iconStrokeWidth,
138
+ fontSize: input?.fontSize ?? preset.fontSize,
139
+ gap: input?.gap ?? preset.gap,
140
+ indent: input?.indent ?? outerIndent ?? DEFAULT_TREE_APPEARANCE.indent,
141
+ accent: input?.accent ?? DEFAULT_TREE_APPEARANCE.accent,
142
+ radius: input?.radius ?? DEFAULT_TREE_APPEARANCE.radius,
143
+ indentGuideOpacity: input?.indentGuideOpacity ?? DEFAULT_TREE_APPEARANCE.indentGuideOpacity,
144
+ showActiveIndicator: input?.showActiveIndicator ?? DEFAULT_TREE_APPEARANCE.showActiveIndicator
145
+ };
146
+ }
147
+ __name(resolveAppearance, "resolveAppearance");
148
+ function appearanceToStyle(a) {
149
+ return {
150
+ ["--tree-row-height"]: `${a.rowHeight}px`,
151
+ ["--tree-icon-size"]: `${a.iconSize}px`,
152
+ ["--tree-icon-stroke"]: a.iconStrokeWidth,
153
+ ["--tree-font-size"]: `${a.fontSize}px`,
154
+ ["--tree-gap"]: `${a.gap}px`,
155
+ ["--tree-indent"]: `${a.indent}px`,
156
+ ["--tree-guide-opacity"]: a.indentGuideOpacity
157
+ };
158
+ }
159
+ __name(appearanceToStyle, "appearanceToStyle");
160
+ var RADIUS_CLASS = {
161
+ none: "rounded-none",
162
+ sm: "rounded-sm",
163
+ md: "rounded-md"
164
+ };
165
+ function radiusClass(a) {
166
+ return RADIUS_CLASS[a.radius];
167
+ }
168
+ __name(radiusClass, "radiusClass");
169
+ var HOVER = {
170
+ subtle: "hover:bg-foreground/[.03]",
171
+ default: "hover:bg-foreground/[.06]",
172
+ strong: "hover:bg-foreground/[.09]"
173
+ };
174
+ var FOCUSED_NOT_SELECTED = {
175
+ subtle: "data-[focused=true]:bg-foreground/[.05]",
176
+ default: "data-[focused=true]:bg-foreground/[.08]",
177
+ strong: "data-[focused=true]:bg-foreground/[.12]"
178
+ };
179
+ var SELECTED_INACTIVE = {
180
+ subtle: "data-[selected=true]:bg-foreground/[.06]",
181
+ default: "data-[selected=true]:bg-foreground/[.10]",
182
+ strong: "data-[selected=true]:bg-foreground/[.14]"
183
+ };
184
+ var SELECTED_ACTIVE = {
185
+ subtle: "data-[selected=true]:group-focus-within/tree:bg-primary/10 data-[selected=true]:group-focus-within/tree:text-primary",
186
+ default: "data-[selected=true]:group-focus-within/tree:bg-primary/15 data-[selected=true]:group-focus-within/tree:text-primary",
187
+ strong: "data-[selected=true]:group-focus-within/tree:bg-primary/25 data-[selected=true]:group-focus-within/tree:text-primary"
188
+ };
189
+ function rowStateClasses(a) {
190
+ return [
191
+ HOVER[a.accent],
192
+ FOCUSED_NOT_SELECTED[a.accent],
193
+ SELECTED_INACTIVE[a.accent],
194
+ SELECTED_ACTIVE[a.accent]
195
+ ].join(" ");
196
+ }
197
+ __name(rowStateClasses, "rowStateClasses");
198
+ var reducer = /* @__PURE__ */ __name((state, action) => {
199
+ switch (action.type) {
200
+ case "expand": {
201
+ if (state.expanded.has(action.id)) return state;
202
+ const next = new Set(state.expanded);
203
+ next.add(action.id);
204
+ return { ...state, expanded: next };
205
+ }
206
+ case "collapse": {
207
+ if (!state.expanded.has(action.id)) return state;
208
+ const next = new Set(state.expanded);
209
+ next.delete(action.id);
210
+ return { ...state, expanded: next };
211
+ }
212
+ case "toggle": {
213
+ const next = new Set(state.expanded);
214
+ if (next.has(action.id)) next.delete(action.id);
215
+ else next.add(action.id);
216
+ return { ...state, expanded: next };
217
+ }
218
+ case "set-expanded":
219
+ return { ...state, expanded: new Set(action.ids) };
220
+ case "select": {
221
+ if (action.mode === "none") return state;
222
+ if (action.mode === "single") {
223
+ return { ...state, selected: /* @__PURE__ */ new Set([action.id]), focused: action.id };
224
+ }
225
+ const next = new Set(state.selected);
226
+ if (next.has(action.id)) next.delete(action.id);
227
+ else next.add(action.id);
228
+ return { ...state, selected: next, focused: action.id };
229
+ }
230
+ case "select-many":
231
+ return { ...state, selected: new Set(action.ids) };
232
+ case "clear-selection":
233
+ return { ...state, selected: /* @__PURE__ */ new Set() };
234
+ case "focus":
235
+ return { ...state, focused: action.id };
236
+ case "set-query":
237
+ return { ...state, query: action.q };
238
+ case "cache-tick":
239
+ return { ...state, cacheTick: state.cacheTick + 1 };
240
+ default:
241
+ return state;
242
+ }
243
+ }, "reducer");
244
+ var TreeContext = createContext(null);
245
+ function useTreeContext() {
246
+ const ctx = React.useContext(TreeContext);
247
+ if (!ctx) {
248
+ throw new Error("useTreeContext must be used inside <TreeProvider>");
249
+ }
250
+ return ctx;
251
+ }
252
+ __name(useTreeContext, "useTreeContext");
253
+ var setEqualsArr = /* @__PURE__ */ __name((set, arr) => {
254
+ if (set.size !== arr.length) return false;
255
+ for (const id of arr) if (!set.has(id)) return false;
256
+ return true;
257
+ }, "setEqualsArr");
258
+ var collectAllIds = /* @__PURE__ */ __name((roots, cache, out) => {
259
+ for (const node of roots) {
260
+ if (Array.isArray(node.children)) {
261
+ out.push(node.id);
262
+ collectAllIds(node.children, cache, out);
263
+ } else if (node.isFolder) {
264
+ out.push(node.id);
265
+ const entry = cache.get(node.id);
266
+ if (entry?.children) collectAllIds(entry.children, cache, out);
267
+ }
268
+ }
269
+ }, "collectAllIds");
270
+ function TreeProvider(props) {
271
+ const {
272
+ data,
273
+ getItemName,
274
+ loadChildren,
275
+ selectionMode = "single",
276
+ initialExpandedIds,
277
+ initialSelectedIds,
278
+ indent,
279
+ appearance,
280
+ onSelectionChange,
281
+ onExpansionChange,
282
+ onActivate,
283
+ enableSearch = false,
284
+ showIndentGuides = false,
285
+ renderIcon,
286
+ renderLabel,
287
+ renderActions,
288
+ renderContextMenu,
289
+ labels: labelsOverride,
290
+ persistKey,
291
+ persistSelection = false,
292
+ children
293
+ } = props;
294
+ const labels = useMemo(
295
+ () => ({ ...DEFAULT_TREE_LABELS, ...labelsOverride }),
296
+ [labelsOverride]
297
+ );
298
+ const resolvedAppearance = useMemo(
299
+ () => resolveAppearance(appearance, indent),
300
+ [appearance, indent]
301
+ );
302
+ const persisted = useMemo(
303
+ () => persistKey ? loadTreeState(persistKey) : null,
304
+ [persistKey]
305
+ );
306
+ const [state, dispatch] = useReducer(reducer, void 0, () => ({
307
+ expanded: new Set(persisted?.expandedItems ?? initialExpandedIds ?? []),
308
+ selected: new Set(
309
+ (persistSelection ? persisted?.selectedItems : void 0) ?? initialSelectedIds ?? []
310
+ ),
311
+ focused: null,
312
+ query: "",
313
+ cacheTick: 0
314
+ }));
315
+ const cacheRef = useRef(createChildCache());
316
+ const inflightRef = useRef(/* @__PURE__ */ new Map());
317
+ const fetchChildren = useCallback(
318
+ async (node) => {
319
+ if (!loadChildren) return;
320
+ if (Array.isArray(node.children)) return;
321
+ const existing = cacheRef.current.get(node.id);
322
+ if (existing?.status === "loaded" || existing?.status === "loading") return;
323
+ const inflight = inflightRef.current.get(node.id);
324
+ if (inflight) return inflight;
325
+ cacheRef.current.set(node.id, { status: "loading", children: [] });
326
+ dispatch({ type: "cache-tick" });
327
+ const promise = (async () => {
328
+ try {
329
+ const children2 = await loadChildren(node);
330
+ cacheRef.current.set(node.id, { status: "loaded", children: children2 });
331
+ } catch (err) {
332
+ cacheRef.current.set(node.id, {
333
+ status: "error",
334
+ children: [],
335
+ error: err instanceof Error ? err.message : String(err)
336
+ });
337
+ } finally {
338
+ inflightRef.current.delete(node.id);
339
+ dispatch({ type: "cache-tick" });
340
+ }
341
+ })();
342
+ inflightRef.current.set(node.id, promise);
343
+ return promise;
344
+ },
345
+ [loadChildren]
346
+ );
347
+ const nodeById = useMemo(() => {
348
+ const map = /* @__PURE__ */ new Map();
349
+ const walk = /* @__PURE__ */ __name((nodes) => {
350
+ for (const n of nodes) {
351
+ map.set(n.id, n);
352
+ if (Array.isArray(n.children)) walk(n.children);
353
+ else {
354
+ const entry = cacheRef.current.get(n.id);
355
+ if (entry?.children) walk(entry.children);
356
+ }
357
+ }
358
+ }, "walk");
359
+ walk(data);
360
+ return map;
361
+ }, [data, state.cacheTick]);
362
+ useEffect(() => {
363
+ if (!loadChildren) return;
364
+ for (const id of state.expanded) {
365
+ const node = nodeById.get(id);
366
+ if (!node) continue;
367
+ void fetchChildren(node);
368
+ }
369
+ }, [loadChildren, state.expanded, state.cacheTick, nodeById, fetchChildren]);
370
+ const flatRows = useMemo(
371
+ () => flattenTree({
372
+ roots: data,
373
+ expandedIds: state.expanded,
374
+ cache: cacheRef.current
375
+ }),
376
+ [data, state.expanded, state.cacheTick]
377
+ );
378
+ const matchingIds = useMemo(() => {
379
+ const set = /* @__PURE__ */ new Set();
380
+ if (!enableSearch || state.query.trim() === "") return set;
381
+ const q = state.query.trim().toLowerCase();
382
+ for (const row of flatRows) {
383
+ if (getItemName(row.node).toLowerCase().includes(q)) {
384
+ set.add(row.node.id);
385
+ }
386
+ }
387
+ return set;
388
+ }, [enableSearch, state.query, flatRows, getItemName]);
389
+ const onSelectionChangeRef = useRef(onSelectionChange);
390
+ const onExpansionChangeRef = useRef(onExpansionChange);
391
+ const onActivateRef = useRef(onActivate);
392
+ onSelectionChangeRef.current = onSelectionChange;
393
+ onExpansionChangeRef.current = onExpansionChange;
394
+ onActivateRef.current = onActivate;
395
+ const lastSelectedArrRef = useRef([...state.selected]);
396
+ const lastExpandedArrRef = useRef([...state.expanded]);
397
+ useEffect(() => {
398
+ const arr = [...state.expanded];
399
+ if (!setEqualsArr(state.expanded, lastExpandedArrRef.current)) {
400
+ lastExpandedArrRef.current = arr;
401
+ onExpansionChangeRef.current?.(arr);
402
+ if (persistKey) {
403
+ saveTreeState(persistKey, {
404
+ expandedItems: arr,
405
+ selectedItems: persistSelection ? [...state.selected] : []
406
+ });
407
+ }
408
+ }
409
+ }, [state.expanded, persistKey, persistSelection, state.selected]);
410
+ useEffect(() => {
411
+ const arr = [...state.selected];
412
+ if (!setEqualsArr(state.selected, lastSelectedArrRef.current)) {
413
+ lastSelectedArrRef.current = arr;
414
+ onSelectionChangeRef.current?.(arr);
415
+ if (persistKey && persistSelection) {
416
+ saveTreeState(persistKey, {
417
+ expandedItems: [...state.expanded],
418
+ selectedItems: arr
419
+ });
420
+ }
421
+ }
422
+ }, [state.selected, persistKey, persistSelection, state.expanded]);
423
+ const expand = useCallback((id) => dispatch({ type: "expand", id }), []);
424
+ const collapse = useCallback((id) => dispatch({ type: "collapse", id }), []);
425
+ const toggle = useCallback((id) => dispatch({ type: "toggle", id }), []);
426
+ const expandAll = useCallback(() => {
427
+ const ids = [];
428
+ collectAllIds(data, cacheRef.current, ids);
429
+ dispatch({ type: "set-expanded", ids });
430
+ }, [data]);
431
+ const collapseAll = useCallback(
432
+ () => dispatch({ type: "set-expanded", ids: [] }),
433
+ []
434
+ );
435
+ const select = useCallback(
436
+ (id) => dispatch({ type: "select", id, mode: selectionMode }),
437
+ [selectionMode]
438
+ );
439
+ const setSelectedIds = useCallback(
440
+ (ids) => dispatch({ type: "select-many", ids }),
441
+ []
442
+ );
443
+ const clearSelection = useCallback(() => dispatch({ type: "clear-selection" }), []);
444
+ const setFocus = useCallback(
445
+ (id) => dispatch({ type: "focus", id }),
446
+ []
447
+ );
448
+ const setQuery = useCallback((q) => dispatch({ type: "set-query", q }), []);
449
+ const refresh = useCallback(
450
+ async (id) => {
451
+ const node = nodeById.get(id);
452
+ if (!node || !loadChildren) return;
453
+ cacheRef.current.delete(id);
454
+ dispatch({ type: "cache-tick" });
455
+ await fetchChildren(node);
456
+ },
457
+ [nodeById, loadChildren, fetchChildren]
458
+ );
459
+ const refreshAll = useCallback(async () => {
460
+ cacheRef.current.clear();
461
+ dispatch({ type: "cache-tick" });
462
+ if (!loadChildren) return;
463
+ await Promise.all(
464
+ [...state.expanded].map((id) => {
465
+ const node = nodeById.get(id);
466
+ return node ? fetchChildren(node) : void 0;
467
+ })
468
+ );
469
+ }, [loadChildren, state.expanded, nodeById, fetchChildren]);
470
+ const activate = useCallback(
471
+ (node) => onActivateRef.current?.(node),
472
+ []
473
+ );
474
+ const value = useMemo(
475
+ () => ({
476
+ expanded: state.expanded,
477
+ selected: state.selected,
478
+ focused: state.focused,
479
+ query: state.query,
480
+ flatRows,
481
+ matchingIds,
482
+ expand,
483
+ collapse,
484
+ toggle,
485
+ expandAll,
486
+ collapseAll,
487
+ select,
488
+ setSelectedIds,
489
+ clearSelection,
490
+ setFocus,
491
+ setQuery,
492
+ refresh,
493
+ refreshAll,
494
+ activate,
495
+ labels,
496
+ appearance: resolvedAppearance,
497
+ indent: resolvedAppearance.indent,
498
+ selectionMode,
499
+ enableSearch,
500
+ showIndentGuides,
501
+ getItemName,
502
+ renderIcon,
503
+ renderLabel,
504
+ renderActions,
505
+ renderContextMenu
506
+ }),
507
+ [
508
+ state.expanded,
509
+ state.selected,
510
+ state.focused,
511
+ state.query,
512
+ flatRows,
513
+ matchingIds,
514
+ expand,
515
+ collapse,
516
+ toggle,
517
+ expandAll,
518
+ collapseAll,
519
+ select,
520
+ setSelectedIds,
521
+ clearSelection,
522
+ setFocus,
523
+ setQuery,
524
+ refresh,
525
+ refreshAll,
526
+ activate,
527
+ labels,
528
+ resolvedAppearance,
529
+ selectionMode,
530
+ enableSearch,
531
+ showIndentGuides,
532
+ getItemName,
533
+ renderIcon,
534
+ renderLabel,
535
+ renderActions,
536
+ renderContextMenu
537
+ ]
538
+ );
539
+ return /* @__PURE__ */ jsx(TreeContext.Provider, { value, children });
540
+ }
541
+ __name(TreeProvider, "TreeProvider");
542
+ function TreeChevron({ isExpanded, isFolder, className }) {
543
+ const { appearance } = useTreeContext();
544
+ const size = { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" };
545
+ if (!isFolder) {
546
+ return /* @__PURE__ */ jsx(
547
+ "span",
548
+ {
549
+ "aria-hidden": true,
550
+ style: size,
551
+ className: cn("inline-block shrink-0", className)
552
+ }
553
+ );
554
+ }
555
+ const Icon = isExpanded ? ChevronDown : ChevronRight;
556
+ return /* @__PURE__ */ jsx(
557
+ Icon,
558
+ {
559
+ "aria-hidden": true,
560
+ strokeWidth: appearance.iconStrokeWidth,
561
+ style: size,
562
+ className: cn(
563
+ "shrink-0 text-muted-foreground/70 transition-transform",
564
+ className
565
+ )
566
+ }
567
+ );
568
+ }
569
+ __name(TreeChevron, "TreeChevron");
570
+ function TreeIcon({ isFolder, isExpanded, className }) {
571
+ const { appearance } = useTreeContext();
572
+ const Icon = isFolder ? isExpanded ? FolderOpen : Folder : File;
573
+ return /* @__PURE__ */ jsx(
574
+ Icon,
575
+ {
576
+ "aria-hidden": true,
577
+ strokeWidth: appearance.iconStrokeWidth,
578
+ style: { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" },
579
+ className: cn(
580
+ "shrink-0",
581
+ isFolder ? "text-foreground/70" : "text-muted-foreground/80",
582
+ className
583
+ )
584
+ }
585
+ );
586
+ }
587
+ __name(TreeIcon, "TreeIcon");
588
+ function TreeIndentGuides({ level, indent }) {
589
+ const { appearance } = useTreeContext();
590
+ if (level <= 0) return null;
591
+ return /* @__PURE__ */ jsx(
592
+ "span",
593
+ {
594
+ "aria-hidden": true,
595
+ className: "pointer-events-none absolute inset-y-0 left-0",
596
+ style: { width: 8 + level * indent, opacity: appearance.indentGuideOpacity },
597
+ children: Array.from({ length: level }).map((_, i) => /* @__PURE__ */ jsx(
598
+ "span",
599
+ {
600
+ className: "absolute inset-y-0 w-px bg-border",
601
+ style: { left: 8 + i * indent + indent / 2 - 0.5 }
602
+ },
603
+ i
604
+ ))
605
+ }
606
+ );
607
+ }
608
+ __name(TreeIndentGuides, "TreeIndentGuides");
609
+ function TreeLabel({ children, isMatchingSearch, className }) {
610
+ return /* @__PURE__ */ jsx(
611
+ "span",
612
+ {
613
+ style: { fontSize: "var(--tree-font-size)" },
614
+ className: cn(
615
+ "truncate leading-tight tracking-[-0.005em]",
616
+ isMatchingSearch && "font-medium text-foreground",
617
+ className
618
+ ),
619
+ children
620
+ }
621
+ );
622
+ }
623
+ __name(TreeLabel, "TreeLabel");
624
+ function TreeRow({ row, className }) {
625
+ const ctx = useTreeContext();
626
+ const {
627
+ appearance,
628
+ showIndentGuides,
629
+ selected,
630
+ focused,
631
+ matchingIds,
632
+ select,
633
+ toggle,
634
+ setFocus,
635
+ activate,
636
+ getItemName,
637
+ renderIcon,
638
+ renderLabel,
639
+ renderActions,
640
+ renderContextMenu
641
+ } = ctx;
642
+ const { node, level, isFolder, isExpanded, isLoading } = row;
643
+ const isSelected = selected.has(node.id);
644
+ const isFocused = focused === node.id;
645
+ const isMatchingSearch = matchingIds.has(node.id);
646
+ const slot = {
647
+ node,
648
+ level,
649
+ isSelected,
650
+ isExpanded,
651
+ isFocused,
652
+ isFolder,
653
+ isLoading,
654
+ isMatchingSearch
655
+ };
656
+ const handleActivate = /* @__PURE__ */ __name(() => {
657
+ if (node.disabled) return;
658
+ setFocus(node.id);
659
+ select(node.id);
660
+ if (isFolder) toggle(node.id);
661
+ else activate(node);
662
+ }, "handleActivate");
663
+ const handleClick = /* @__PURE__ */ __name((e) => {
664
+ handleActivate();
665
+ e.currentTarget.scrollIntoView?.({ block: "nearest" });
666
+ }, "handleClick");
667
+ const handleDoubleClick = /* @__PURE__ */ __name(() => {
668
+ if (node.disabled) return;
669
+ if (!isFolder) activate(node);
670
+ }, "handleDoubleClick");
671
+ const trigger = /* @__PURE__ */ jsxs(
672
+ "div",
673
+ {
674
+ role: "treeitem",
675
+ "aria-level": level + 1,
676
+ "aria-expanded": isFolder ? isExpanded : void 0,
677
+ "aria-selected": isSelected || void 0,
678
+ "aria-current": isSelected ? "true" : void 0,
679
+ "aria-disabled": node.disabled || void 0,
680
+ "data-tree-row": "",
681
+ "data-id": node.id,
682
+ "data-selected": isSelected ? "true" : void 0,
683
+ "data-focused": isFocused && !isSelected ? "true" : void 0,
684
+ "data-folder": isFolder || void 0,
685
+ "data-expanded": isExpanded || void 0,
686
+ tabIndex: isFocused ? 0 : -1,
687
+ style: {
688
+ paddingLeft: 6 + level * appearance.indent,
689
+ height: "var(--tree-row-height)",
690
+ gap: "var(--tree-gap)"
691
+ },
692
+ onClick: handleClick,
693
+ onDoubleClick: handleDoubleClick,
694
+ onFocus: () => setFocus(node.id),
695
+ className: cn(
696
+ "group/row relative flex w-full select-none items-center pr-2 text-left",
697
+ "transition-colors outline-none",
698
+ node.disabled ? "cursor-not-allowed" : "cursor-pointer",
699
+ radiusClass(appearance),
700
+ rowStateClasses(appearance),
701
+ "focus-visible:ring-1 focus-visible:ring-ring/50",
702
+ isMatchingSearch && "ring-1 ring-primary/30",
703
+ node.disabled && "opacity-50",
704
+ className
705
+ ),
706
+ children: [
707
+ appearance.showActiveIndicator && isSelected ? /* @__PURE__ */ jsx(
708
+ "span",
709
+ {
710
+ "aria-hidden": true,
711
+ className: cn(
712
+ "absolute left-0 top-1 bottom-1 w-0.5 rounded-r-full",
713
+ "bg-foreground/30 group-focus-within/tree:bg-primary"
714
+ )
715
+ }
716
+ ) : null,
717
+ showIndentGuides && level > 0 ? /* @__PURE__ */ jsx(TreeIndentGuides, { level, indent: appearance.indent }) : null,
718
+ /* @__PURE__ */ jsx(TreeChevron, { isExpanded, isFolder }),
719
+ isLoading ? /* @__PURE__ */ jsx(
720
+ Loader2,
721
+ {
722
+ "aria-hidden": true,
723
+ strokeWidth: appearance.iconStrokeWidth,
724
+ style: { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" },
725
+ className: "shrink-0 animate-spin text-muted-foreground/70"
726
+ }
727
+ ) : renderIcon ? renderIcon(slot) : /* @__PURE__ */ jsx(TreeIcon, { isFolder, isExpanded }),
728
+ /* @__PURE__ */ jsx(
729
+ "span",
730
+ {
731
+ className: "flex min-w-0 flex-1 items-center",
732
+ style: { gap: "var(--tree-gap)" },
733
+ children: renderLabel ? renderLabel(slot) : /* @__PURE__ */ jsx(TreeLabel, { isMatchingSearch, children: getItemName(node) })
734
+ }
735
+ ),
736
+ renderActions ? /* @__PURE__ */ jsx(
737
+ "span",
738
+ {
739
+ className: "ml-auto flex shrink-0 items-center gap-1 opacity-0 transition-opacity group-hover/row:opacity-100 group-focus-within/row:opacity-100",
740
+ onClick: (e) => e.stopPropagation(),
741
+ children: renderActions(slot)
742
+ }
743
+ ) : null
744
+ ]
745
+ }
746
+ );
747
+ if (renderContextMenu) {
748
+ return /* @__PURE__ */ jsx(Fragment, { children: renderContextMenu(slot, trigger) });
749
+ }
750
+ return trigger;
751
+ }
752
+ __name(TreeRow, "TreeRow");
753
+ function TreeEmpty({ children, className }) {
754
+ return /* @__PURE__ */ jsx(
755
+ "div",
756
+ {
757
+ className: cn(
758
+ "flex h-full min-h-32 items-center justify-center px-4 py-6 text-sm text-muted-foreground",
759
+ className
760
+ ),
761
+ children
762
+ }
763
+ );
764
+ }
765
+ __name(TreeEmpty, "TreeEmpty");
766
+ function TreeContent({ children, className, ariaLabel }) {
767
+ const { flatRows, labels, selected, focused, matchingIds } = useTreeContext();
768
+ if (flatRows.length === 0) {
769
+ return /* @__PURE__ */ jsx(TreeEmpty, { children: labels.empty });
770
+ }
771
+ return /* @__PURE__ */ jsx(
772
+ "div",
773
+ {
774
+ role: "tree",
775
+ "aria-label": ariaLabel ?? labels.ariaLabel,
776
+ className: cn("relative flex flex-col py-1", className),
777
+ children: flatRows.map((row) => {
778
+ const slot = {
779
+ node: row.node,
780
+ level: row.level,
781
+ isSelected: selected.has(row.node.id),
782
+ isExpanded: row.isExpanded,
783
+ isFocused: focused === row.node.id,
784
+ isFolder: row.isFolder,
785
+ isLoading: row.isLoading,
786
+ isMatchingSearch: matchingIds.has(row.node.id)
787
+ };
788
+ const node = children ? children(slot) : /* @__PURE__ */ jsx(TreeRow, { row });
789
+ return /* @__PURE__ */ jsx(Fragment$1, { children: node }, row.node.id);
790
+ })
791
+ }
792
+ );
793
+ }
794
+ __name(TreeContent, "TreeContent");
795
+ function useTreeLabels() {
796
+ return useTreeContext().labels;
797
+ }
798
+ __name(useTreeLabels, "useTreeLabels");
799
+ function useTreeRows() {
800
+ return useTreeContext().flatRows;
801
+ }
802
+ __name(useTreeRows, "useTreeRows");
803
+ function useTreeSelection() {
804
+ const ctx = useTreeContext();
805
+ const selectedIds = useMemo(() => [...ctx.selected], [ctx.selected]);
806
+ const isSelected = useCallback(
807
+ (id) => ctx.selected.has(id),
808
+ [ctx.selected]
809
+ );
810
+ return useMemo(
811
+ () => ({
812
+ selectedIds,
813
+ select: ctx.select,
814
+ setSelectedIds: ctx.setSelectedIds,
815
+ clear: ctx.clearSelection,
816
+ isSelected
817
+ }),
818
+ [selectedIds, ctx.select, ctx.setSelectedIds, ctx.clearSelection, isSelected]
819
+ );
820
+ }
821
+ __name(useTreeSelection, "useTreeSelection");
822
+ function useTreeExpansion() {
823
+ const ctx = useTreeContext();
824
+ const expandedIds = useMemo(() => [...ctx.expanded], [ctx.expanded]);
825
+ const isExpanded = useCallback(
826
+ (id) => ctx.expanded.has(id),
827
+ [ctx.expanded]
828
+ );
829
+ return useMemo(
830
+ () => ({
831
+ expandedIds,
832
+ expand: ctx.expand,
833
+ collapse: ctx.collapse,
834
+ toggle: ctx.toggle,
835
+ expandAll: ctx.expandAll,
836
+ collapseAll: ctx.collapseAll,
837
+ isExpanded
838
+ }),
839
+ [
840
+ expandedIds,
841
+ ctx.expand,
842
+ ctx.collapse,
843
+ ctx.toggle,
844
+ ctx.expandAll,
845
+ ctx.collapseAll,
846
+ isExpanded
847
+ ]
848
+ );
849
+ }
850
+ __name(useTreeExpansion, "useTreeExpansion");
851
+ function useTreeFocus() {
852
+ const ctx = useTreeContext();
853
+ return useMemo(
854
+ () => ({ focusedId: ctx.focused, setFocus: ctx.setFocus }),
855
+ [ctx.focused, ctx.setFocus]
856
+ );
857
+ }
858
+ __name(useTreeFocus, "useTreeFocus");
859
+ function useTreeSearch() {
860
+ const ctx = useTreeContext();
861
+ return useMemo(
862
+ () => ({
863
+ isOpen: ctx.enableSearch,
864
+ query: ctx.query,
865
+ setQuery: ctx.setQuery,
866
+ matchingIds: ctx.matchingIds,
867
+ matchCount: ctx.matchingIds.size
868
+ }),
869
+ [ctx.enableSearch, ctx.query, ctx.setQuery, ctx.matchingIds]
870
+ );
871
+ }
872
+ __name(useTreeSearch, "useTreeSearch");
873
+ function useTreeActions() {
874
+ const ctx = useTreeContext();
875
+ return useMemo(
876
+ () => ({
877
+ expand: ctx.expand,
878
+ collapse: ctx.collapse,
879
+ toggle: ctx.toggle,
880
+ expandAll: ctx.expandAll,
881
+ collapseAll: ctx.collapseAll,
882
+ refresh: ctx.refresh,
883
+ refreshAll: ctx.refreshAll,
884
+ activate: ctx.activate
885
+ }),
886
+ [
887
+ ctx.expand,
888
+ ctx.collapse,
889
+ ctx.toggle,
890
+ ctx.expandAll,
891
+ ctx.collapseAll,
892
+ ctx.refresh,
893
+ ctx.refreshAll,
894
+ ctx.activate
895
+ ]
896
+ );
897
+ }
898
+ __name(useTreeActions, "useTreeActions");
899
+ function TreeSearchInput({ className, showMatches = true }) {
900
+ const { labels } = useTreeContext();
901
+ const { query, setQuery, matchCount } = useTreeSearch();
902
+ return /* @__PURE__ */ jsxs(
903
+ "div",
904
+ {
905
+ className: cn(
906
+ "flex items-center gap-2 rounded-md border border-border bg-background px-2",
907
+ className
908
+ ),
909
+ children: [
910
+ /* @__PURE__ */ jsx(Search, { "aria-hidden": true, className: "size-3.5 shrink-0 text-muted-foreground" }),
911
+ /* @__PURE__ */ jsx(
912
+ "input",
913
+ {
914
+ type: "search",
915
+ value: query,
916
+ onChange: (e) => setQuery(e.target.value),
917
+ placeholder: labels.searchPlaceholder,
918
+ className: "h-7 w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground"
919
+ }
920
+ ),
921
+ showMatches && query.trim().length > 0 ? /* @__PURE__ */ jsx("span", { className: "shrink-0 text-xs text-muted-foreground", children: labels.searchMatches(matchCount) }) : null,
922
+ query.length > 0 ? /* @__PURE__ */ jsx(
923
+ "button",
924
+ {
925
+ type: "button",
926
+ onClick: () => setQuery(""),
927
+ "aria-label": "Clear search",
928
+ className: "shrink-0 rounded p-1 text-muted-foreground hover:bg-accent hover:text-foreground",
929
+ children: /* @__PURE__ */ jsx(X, { "aria-hidden": true, className: "size-3.5" })
930
+ }
931
+ ) : null
932
+ ]
933
+ }
934
+ );
935
+ }
936
+ __name(TreeSearchInput, "TreeSearchInput");
937
+ function useTreeKeyboard({
938
+ rows,
939
+ focusedId,
940
+ enabled = true,
941
+ onFocus,
942
+ onSelect,
943
+ onActivate,
944
+ onExpand,
945
+ onCollapse,
946
+ onClearSelection
947
+ }) {
948
+ const rowsRef = useRef(rows);
949
+ const focusedIdRef = useRef(focusedId);
950
+ rowsRef.current = rows;
951
+ focusedIdRef.current = focusedId;
952
+ const getCurrent = /* @__PURE__ */ __name(() => {
953
+ const r = rowsRef.current;
954
+ const id = focusedIdRef.current;
955
+ const idx = id ? r.findIndex((x) => x.node.id === id) : -1;
956
+ return { rows: r, idx, current: idx >= 0 ? r[idx] : null };
957
+ }, "getCurrent");
958
+ const refDown = useHotkey(
959
+ "down",
960
+ () => {
961
+ const { rows: r, idx } = getCurrent();
962
+ if (r.length === 0) return;
963
+ const next = r[Math.min(idx + 1, r.length - 1)] ?? r[0];
964
+ onFocus(next.node.id);
965
+ },
966
+ { enabled, preventDefault: true, description: "Next row" }
967
+ );
968
+ const refUp = useHotkey(
969
+ "up",
970
+ () => {
971
+ const { rows: r, idx } = getCurrent();
972
+ if (r.length === 0) return;
973
+ const prev = r[Math.max(idx - 1, 0)] ?? r[0];
974
+ onFocus(prev.node.id);
975
+ },
976
+ { enabled, preventDefault: true, description: "Previous row" }
977
+ );
978
+ const refHome = useHotkey(
979
+ "home",
980
+ () => {
981
+ const { rows: r } = getCurrent();
982
+ if (r.length === 0) return;
983
+ onFocus(r[0].node.id);
984
+ },
985
+ { enabled, preventDefault: true, description: "First row" }
986
+ );
987
+ const refEnd = useHotkey(
988
+ "end",
989
+ () => {
990
+ const { rows: r } = getCurrent();
991
+ if (r.length === 0) return;
992
+ onFocus(r[r.length - 1].node.id);
993
+ },
994
+ { enabled, preventDefault: true, description: "Last row" }
995
+ );
996
+ const refRight = useHotkey(
997
+ "right",
998
+ () => {
999
+ const { rows: r, idx, current } = getCurrent();
1000
+ if (!current) return;
1001
+ if (current.isFolder && !current.isExpanded) {
1002
+ onExpand(current.node.id);
1003
+ } else if (current.isFolder && current.isExpanded) {
1004
+ const next = r[idx + 1];
1005
+ if (next) onFocus(next.node.id);
1006
+ }
1007
+ },
1008
+ { enabled, preventDefault: true, description: "Expand / first child" }
1009
+ );
1010
+ const refLeft = useHotkey(
1011
+ "left",
1012
+ () => {
1013
+ const { current } = getCurrent();
1014
+ if (!current) return;
1015
+ if (current.isFolder && current.isExpanded) {
1016
+ onCollapse(current.node.id);
1017
+ } else if (current.parentId) {
1018
+ onFocus(current.parentId);
1019
+ }
1020
+ },
1021
+ { enabled, preventDefault: true, description: "Collapse / parent" }
1022
+ );
1023
+ const refActivate = useHotkey(
1024
+ ["enter", "space"],
1025
+ () => {
1026
+ const { current } = getCurrent();
1027
+ if (!current) return;
1028
+ onSelect(current.node.id);
1029
+ if (current.isFolder) {
1030
+ if (current.isExpanded) onCollapse(current.node.id);
1031
+ else onExpand(current.node.id);
1032
+ } else {
1033
+ onActivate(current.node.id);
1034
+ }
1035
+ },
1036
+ { enabled, preventDefault: true, description: "Activate / toggle" }
1037
+ );
1038
+ const refEscape = useHotkey(
1039
+ "escape",
1040
+ () => onClearSelection(),
1041
+ { enabled, preventDefault: true, description: "Clear selection" }
1042
+ );
1043
+ const ref = useCallback(
1044
+ (instance) => {
1045
+ refDown(instance);
1046
+ refUp(instance);
1047
+ refHome(instance);
1048
+ refEnd(instance);
1049
+ refRight(instance);
1050
+ refLeft(instance);
1051
+ refActivate(instance);
1052
+ refEscape(instance);
1053
+ },
1054
+ [refDown, refUp, refHome, refEnd, refRight, refLeft, refActivate, refEscape]
1055
+ );
1056
+ return { ref };
1057
+ }
1058
+ __name(useTreeKeyboard, "useTreeKeyboard");
1059
+ var FLUSH_MS = 600;
1060
+ function useTreeTypeAhead({
1061
+ rows,
1062
+ getItemName,
1063
+ containerRef,
1064
+ onMatch,
1065
+ enabled = true
1066
+ }) {
1067
+ const bufferRef = useRef("");
1068
+ const timerRef = useRef(null);
1069
+ const rowsRef = useRef(rows);
1070
+ const getNameRef = useRef(getItemName);
1071
+ const onMatchRef = useRef(onMatch);
1072
+ rowsRef.current = rows;
1073
+ getNameRef.current = getItemName;
1074
+ onMatchRef.current = onMatch;
1075
+ useEffect(() => {
1076
+ if (!enabled) return;
1077
+ const target = containerRef.current;
1078
+ if (!target) return;
1079
+ const reset = /* @__PURE__ */ __name(() => {
1080
+ bufferRef.current = "";
1081
+ if (timerRef.current) {
1082
+ clearTimeout(timerRef.current);
1083
+ timerRef.current = null;
1084
+ }
1085
+ }, "reset");
1086
+ const handler = /* @__PURE__ */ __name((e) => {
1087
+ const tag = e.target?.tagName;
1088
+ if (tag === "INPUT" || tag === "TEXTAREA") return;
1089
+ if (e.target?.isContentEditable) return;
1090
+ if (e.metaKey || e.ctrlKey || e.altKey) return;
1091
+ if (e.key === "Escape" || e.key === "Enter" || e.key === "Tab" || e.key.startsWith("Arrow") || e.key === "Home" || e.key === "End" || e.key === "PageUp" || e.key === "PageDown") {
1092
+ reset();
1093
+ return;
1094
+ }
1095
+ if (e.key.length !== 1) return;
1096
+ bufferRef.current += e.key.toLowerCase();
1097
+ if (timerRef.current) clearTimeout(timerRef.current);
1098
+ timerRef.current = setTimeout(reset, FLUSH_MS);
1099
+ const prefix = bufferRef.current;
1100
+ const hit = rowsRef.current.find(
1101
+ (row) => getNameRef.current(row.node).toLowerCase().startsWith(prefix)
1102
+ );
1103
+ if (hit) {
1104
+ e.preventDefault();
1105
+ onMatchRef.current(hit.node.id);
1106
+ }
1107
+ }, "handler");
1108
+ target.addEventListener("keydown", handler);
1109
+ return () => {
1110
+ target.removeEventListener("keydown", handler);
1111
+ reset();
1112
+ };
1113
+ }, [containerRef, enabled]);
1114
+ }
1115
+ __name(useTreeTypeAhead, "useTreeTypeAhead");
1116
+ function TreeRoot(props) {
1117
+ const {
1118
+ data,
1119
+ getItemName,
1120
+ loadChildren,
1121
+ selectionMode,
1122
+ initialExpandedIds,
1123
+ initialSelectedIds,
1124
+ indent,
1125
+ appearance,
1126
+ onSelectionChange,
1127
+ onExpansionChange,
1128
+ onActivate,
1129
+ enableSearch = false,
1130
+ enableTypeAhead = true,
1131
+ showIndentGuides = false,
1132
+ renderRow,
1133
+ renderIcon,
1134
+ renderLabel,
1135
+ renderActions,
1136
+ renderContextMenu,
1137
+ labels,
1138
+ persistKey,
1139
+ persistSelection = false,
1140
+ className,
1141
+ style
1142
+ } = props;
1143
+ return /* @__PURE__ */ jsx(
1144
+ TreeProvider,
1145
+ {
1146
+ data,
1147
+ getItemName,
1148
+ loadChildren,
1149
+ selectionMode,
1150
+ initialExpandedIds,
1151
+ initialSelectedIds,
1152
+ indent,
1153
+ appearance,
1154
+ onSelectionChange,
1155
+ onExpansionChange,
1156
+ onActivate,
1157
+ enableSearch,
1158
+ showIndentGuides,
1159
+ renderIcon,
1160
+ renderLabel,
1161
+ renderActions,
1162
+ renderContextMenu,
1163
+ labels,
1164
+ persistKey,
1165
+ persistSelection,
1166
+ children: /* @__PURE__ */ jsx(
1167
+ TreeRootShell,
1168
+ {
1169
+ className,
1170
+ style,
1171
+ enableSearch,
1172
+ enableTypeAhead,
1173
+ renderRow
1174
+ }
1175
+ )
1176
+ }
1177
+ );
1178
+ }
1179
+ __name(TreeRoot, "TreeRoot");
1180
+ function TreeRootShell({
1181
+ className,
1182
+ style,
1183
+ enableSearch,
1184
+ enableTypeAhead,
1185
+ renderRow
1186
+ }) {
1187
+ const containerRef = useRef(null);
1188
+ const ctx = useTreeContext();
1189
+ const { ref: keyboardRef } = useTreeKeyboard({
1190
+ rows: ctx.flatRows,
1191
+ focusedId: ctx.focused,
1192
+ onFocus: ctx.setFocus,
1193
+ onSelect: ctx.select,
1194
+ onActivate: /* @__PURE__ */ __name((id) => {
1195
+ const row = ctx.flatRows.find((r) => r.node.id === id);
1196
+ if (row) ctx.activate(row.node);
1197
+ }, "onActivate"),
1198
+ onExpand: ctx.expand,
1199
+ onCollapse: ctx.collapse,
1200
+ onClearSelection: ctx.clearSelection
1201
+ });
1202
+ const setContainerRef = useCallback(
1203
+ (instance) => {
1204
+ containerRef.current = instance;
1205
+ keyboardRef(instance);
1206
+ },
1207
+ [keyboardRef]
1208
+ );
1209
+ const onTypeAheadMatch = useCallback(
1210
+ (id) => {
1211
+ ctx.setFocus(id);
1212
+ const el = containerRef.current?.querySelector(
1213
+ `[data-tree-row][data-id="${CSS.escape(id)}"]`
1214
+ );
1215
+ el?.scrollIntoView({ block: "nearest" });
1216
+ },
1217
+ [ctx]
1218
+ );
1219
+ useTreeTypeAhead({
1220
+ rows: ctx.flatRows,
1221
+ getItemName: ctx.getItemName,
1222
+ containerRef,
1223
+ onMatch: onTypeAheadMatch,
1224
+ enabled: enableTypeAhead
1225
+ });
1226
+ return /* @__PURE__ */ jsxs(
1227
+ "div",
1228
+ {
1229
+ ref: setContainerRef,
1230
+ tabIndex: 0,
1231
+ className: cn(
1232
+ "group/tree flex h-full w-full flex-col gap-2 outline-none",
1233
+ className
1234
+ ),
1235
+ style: { ...appearanceToStyle(ctx.appearance), ...style },
1236
+ "data-tree-root": "",
1237
+ children: [
1238
+ enableSearch ? /* @__PURE__ */ jsx(TreeSearchInput, { className: "mx-2 mt-2" }) : null,
1239
+ /* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1 overflow-auto px-1", children: /* @__PURE__ */ jsx(TreeContent, { children: renderRow }) })
1240
+ ]
1241
+ }
1242
+ );
1243
+ }
1244
+ __name(TreeRootShell, "TreeRootShell");
1245
+ var TreeRoot_default = TreeRoot;
1246
+
1247
+ export { DEFAULT_TREE_APPEARANCE, DEFAULT_TREE_LABELS, TreeChevron, TreeContent, TreeEmpty, TreeIcon, TreeIndentGuides, TreeLabel, TreeProvider, TreeRoot, TreeRoot_default, TreeRow, TreeSearchInput, appearanceToStyle, clearTreeState, createChildCache, flattenTree, loadTreeState, resolveAppearance, resolveChildren, saveTreeState, useTreeActions, useTreeContext, useTreeExpansion, useTreeFocus, useTreeKeyboard, useTreeLabels, useTreeRows, useTreeSearch, useTreeSelection, useTreeTypeAhead };
1248
+ //# sourceMappingURL=chunk-NFIMVYJU.mjs.map
1249
+ //# sourceMappingURL=chunk-NFIMVYJU.mjs.map