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