@djangocfg/ui-tools 2.1.390 → 2.1.394

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 (184) hide show
  1. package/README.md +7 -10
  2. package/dist/chunk-PAWJFY3S.mjs +6 -0
  3. package/dist/{chunk-N2XQF2OL.mjs.map → chunk-PAWJFY3S.mjs.map} +1 -1
  4. package/dist/chunk-PK6SKIKE.cjs +8 -0
  5. package/dist/{chunk-OLISEQHS.cjs.map → chunk-PK6SKIKE.cjs.map} +1 -1
  6. package/dist/file-icon/index.cjs +6 -6
  7. package/dist/file-icon/index.d.cts +1 -1
  8. package/dist/file-icon/index.d.ts +1 -1
  9. package/dist/file-icon/index.mjs +1 -1
  10. package/dist/tree/index.cjs +1372 -143
  11. package/dist/tree/index.cjs.map +1 -1
  12. package/dist/tree/index.d.cts +2 -2
  13. package/dist/tree/index.d.ts +2 -2
  14. package/dist/tree/index.mjs +1322 -3
  15. package/dist/tree/index.mjs.map +1 -1
  16. package/dist/{types-CevSbyfD.d.cts → types-B_zhyAqR.d.cts} +1 -1
  17. package/dist/{types-CevSbyfD.d.ts → types-B_zhyAqR.d.ts} +1 -1
  18. package/package.json +6 -14
  19. package/src/tools/AudioPlayer/README.md +4 -4
  20. package/src/tools/Chat/README.md +6 -6
  21. package/src/tools/CronScheduler/index.tsx +1 -1
  22. package/src/tools/CronScheduler/lazy.tsx +1 -1
  23. package/src/tools/ImageViewer/README.md +1 -1
  24. package/src/tools/JsonForm/README.md +2 -2
  25. package/src/tools/MarkdownEditor/README.md +3 -3
  26. package/src/tools/MarkdownMessage/README.md +2 -2
  27. package/src/tools/OpenapiViewer/components/DocsLayout/grouping.ts +5 -1
  28. package/src/tools/PrettyCode/lazy.tsx +6 -0
  29. package/src/tools/SpeechRecognition/README.md +1 -1
  30. package/dist/ChatRoot-EFNXQXXN.cjs +0 -15
  31. package/dist/ChatRoot-EFNXQXXN.cjs.map +0 -1
  32. package/dist/ChatRoot-FITF5RVP.mjs +0 -6
  33. package/dist/ChatRoot-FITF5RVP.mjs.map +0 -1
  34. package/dist/ChatRoot-PNNGQCYF.css +0 -7
  35. package/dist/ChatRoot-PNNGQCYF.css.map +0 -1
  36. package/dist/CronScheduler.client-DLMXCPAJ.mjs +0 -67
  37. package/dist/CronScheduler.client-DLMXCPAJ.mjs.map +0 -1
  38. package/dist/CronScheduler.client-WEJF4PWQ.cjs +0 -72
  39. package/dist/CronScheduler.client-WEJF4PWQ.cjs.map +0 -1
  40. package/dist/DictationField-AS2F33WI.cjs +0 -13
  41. package/dist/DictationField-AS2F33WI.cjs.map +0 -1
  42. package/dist/DictationField-WPONUCYE.mjs +0 -4
  43. package/dist/DictationField-WPONUCYE.mjs.map +0 -1
  44. package/dist/DocsLayout-EKASBSP7.mjs +0 -3448
  45. package/dist/DocsLayout-EKASBSP7.mjs.map +0 -1
  46. package/dist/DocsLayout-MBFIB4NO.css +0 -7
  47. package/dist/DocsLayout-MBFIB4NO.css.map +0 -1
  48. package/dist/DocsLayout-OURFYWQE.cjs +0 -3455
  49. package/dist/DocsLayout-OURFYWQE.cjs.map +0 -1
  50. package/dist/JsonSchemaForm-DD7CLRIG.cjs +0 -13
  51. package/dist/JsonSchemaForm-DD7CLRIG.cjs.map +0 -1
  52. package/dist/JsonSchemaForm-XKUIVELK.mjs +0 -4
  53. package/dist/JsonSchemaForm-XKUIVELK.mjs.map +0 -1
  54. package/dist/JsonTree-43PQAJKY.mjs +0 -5
  55. package/dist/JsonTree-43PQAJKY.mjs.map +0 -1
  56. package/dist/JsonTree-MLET23ZA.css +0 -7
  57. package/dist/JsonTree-MLET23ZA.css.map +0 -1
  58. package/dist/JsonTree-X6W5YEVY.cjs +0 -11
  59. package/dist/JsonTree-X6W5YEVY.cjs.map +0 -1
  60. package/dist/LottiePlayer.client-2S7ISJ2S.cjs +0 -168
  61. package/dist/LottiePlayer.client-2S7ISJ2S.cjs.map +0 -1
  62. package/dist/LottiePlayer.client-5LDSSJWS.mjs +0 -161
  63. package/dist/LottiePlayer.client-5LDSSJWS.mjs.map +0 -1
  64. package/dist/MapContainer-AKIPABJK.mjs +0 -4
  65. package/dist/MapContainer-AKIPABJK.mjs.map +0 -1
  66. package/dist/MapContainer-STVDMC36.cjs +0 -17
  67. package/dist/MapContainer-STVDMC36.cjs.map +0 -1
  68. package/dist/Mermaid.client-DDXWXZXY.css +0 -7
  69. package/dist/Mermaid.client-DDXWXZXY.css.map +0 -1
  70. package/dist/Mermaid.client-NL4SVR7F.mjs +0 -481
  71. package/dist/Mermaid.client-NL4SVR7F.mjs.map +0 -1
  72. package/dist/Mermaid.client-NNTI6DFX.cjs +0 -487
  73. package/dist/Mermaid.client-NNTI6DFX.cjs.map +0 -1
  74. package/dist/Player-BRV7XTWR.mjs +0 -4
  75. package/dist/Player-BRV7XTWR.mjs.map +0 -1
  76. package/dist/Player-PM7F7DD7.cjs +0 -13
  77. package/dist/Player-PM7F7DD7.cjs.map +0 -1
  78. package/dist/Player-ZGQKKOWI.css +0 -66
  79. package/dist/Player-ZGQKKOWI.css.map +0 -1
  80. package/dist/PrettyCode.client-GWFAIVFN.css +0 -7
  81. package/dist/PrettyCode.client-GWFAIVFN.css.map +0 -1
  82. package/dist/PrettyCode.client-KOHDVPPN.cjs +0 -285
  83. package/dist/PrettyCode.client-KOHDVPPN.cjs.map +0 -1
  84. package/dist/PrettyCode.client-ZGYGKE7G.mjs +0 -283
  85. package/dist/PrettyCode.client-ZGYGKE7G.mjs.map +0 -1
  86. package/dist/TreeRoot-5COOOSWG.mjs +0 -4
  87. package/dist/TreeRoot-5COOOSWG.mjs.map +0 -1
  88. package/dist/TreeRoot-AABP2J6Y.cjs +0 -19
  89. package/dist/TreeRoot-AABP2J6Y.cjs.map +0 -1
  90. package/dist/chunk-2NG4SXEP.mjs +0 -743
  91. package/dist/chunk-2NG4SXEP.mjs.map +0 -1
  92. package/dist/chunk-4LFB7I5K.cjs +0 -1387
  93. package/dist/chunk-4LFB7I5K.cjs.map +0 -1
  94. package/dist/chunk-5D2OCOPQ.cjs +0 -222
  95. package/dist/chunk-5D2OCOPQ.cjs.map +0 -1
  96. package/dist/chunk-5I5QNGUG.cjs +0 -611
  97. package/dist/chunk-5I5QNGUG.cjs.map +0 -1
  98. package/dist/chunk-6ZX2G25W.mjs +0 -1361
  99. package/dist/chunk-6ZX2G25W.mjs.map +0 -1
  100. package/dist/chunk-76NNDZH6.cjs +0 -1061
  101. package/dist/chunk-76NNDZH6.cjs.map +0 -1
  102. package/dist/chunk-7CWGZPO3.mjs +0 -214
  103. package/dist/chunk-7CWGZPO3.mjs.map +0 -1
  104. package/dist/chunk-7EYHNP3E.cjs +0 -965
  105. package/dist/chunk-7EYHNP3E.cjs.map +0 -1
  106. package/dist/chunk-7IYXZUJO.cjs +0 -769
  107. package/dist/chunk-7IYXZUJO.cjs.map +0 -1
  108. package/dist/chunk-ADEN3UA4.cjs +0 -892
  109. package/dist/chunk-ADEN3UA4.cjs.map +0 -1
  110. package/dist/chunk-B6IR5KSC.mjs +0 -59
  111. package/dist/chunk-B6IR5KSC.mjs.map +0 -1
  112. package/dist/chunk-C6GXVH5J.mjs +0 -338
  113. package/dist/chunk-C6GXVH5J.mjs.map +0 -1
  114. package/dist/chunk-DMX7W4XZ.mjs +0 -1113
  115. package/dist/chunk-DMX7W4XZ.mjs.map +0 -1
  116. package/dist/chunk-ECONRHIG.mjs +0 -212
  117. package/dist/chunk-ECONRHIG.mjs.map +0 -1
  118. package/dist/chunk-FEN5S772.cjs +0 -1227
  119. package/dist/chunk-FEN5S772.cjs.map +0 -1
  120. package/dist/chunk-FP2RLYQZ.cjs +0 -187
  121. package/dist/chunk-FP2RLYQZ.cjs.map +0 -1
  122. package/dist/chunk-FVVF7VCD.cjs +0 -1325
  123. package/dist/chunk-FVVF7VCD.cjs.map +0 -1
  124. package/dist/chunk-GYIO7W7M.mjs +0 -1197
  125. package/dist/chunk-GYIO7W7M.mjs.map +0 -1
  126. package/dist/chunk-KNDLV4PI.cjs +0 -1356
  127. package/dist/chunk-KNDLV4PI.cjs.map +0 -1
  128. package/dist/chunk-KNEQRUBA.mjs +0 -181
  129. package/dist/chunk-KNEQRUBA.mjs.map +0 -1
  130. package/dist/chunk-N2XQF2OL.mjs +0 -14
  131. package/dist/chunk-N4MZYNR4.mjs +0 -1342
  132. package/dist/chunk-N4MZYNR4.mjs.map +0 -1
  133. package/dist/chunk-NTVBIIUD.mjs +0 -1439
  134. package/dist/chunk-NTVBIIUD.mjs.map +0 -1
  135. package/dist/chunk-OBRSGM64.mjs +0 -607
  136. package/dist/chunk-OBRSGM64.mjs.map +0 -1
  137. package/dist/chunk-ODO4GMW7.mjs +0 -79
  138. package/dist/chunk-ODO4GMW7.mjs.map +0 -1
  139. package/dist/chunk-OLISEQHS.cjs +0 -18
  140. package/dist/chunk-PVAX67JG.mjs +0 -1041
  141. package/dist/chunk-PVAX67JG.mjs.map +0 -1
  142. package/dist/chunk-QJ6GTUCO.cjs +0 -81
  143. package/dist/chunk-QJ6GTUCO.cjs.map +0 -1
  144. package/dist/chunk-T3MWM23F.cjs +0 -214
  145. package/dist/chunk-T3MWM23F.cjs.map +0 -1
  146. package/dist/chunk-TBSHZO5R.cjs +0 -1134
  147. package/dist/chunk-TBSHZO5R.cjs.map +0 -1
  148. package/dist/chunk-UNCS5V5F.mjs +0 -887
  149. package/dist/chunk-UNCS5V5F.mjs.map +0 -1
  150. package/dist/chunk-VWQ5WOIL.mjs +0 -2059
  151. package/dist/chunk-VWQ5WOIL.mjs.map +0 -1
  152. package/dist/chunk-W75B7Y6C.cjs +0 -1478
  153. package/dist/chunk-W75B7Y6C.cjs.map +0 -1
  154. package/dist/chunk-Y6UTOBF6.mjs +0 -938
  155. package/dist/chunk-Y6UTOBF6.mjs.map +0 -1
  156. package/dist/chunk-YDPDTOSP.cjs +0 -2061
  157. package/dist/chunk-YDPDTOSP.cjs.map +0 -1
  158. package/dist/chunk-YW5IVWHQ.cjs +0 -346
  159. package/dist/chunk-YW5IVWHQ.cjs.map +0 -1
  160. package/dist/chunk-YXZ6GU7H.cjs +0 -63
  161. package/dist/chunk-YXZ6GU7H.cjs.map +0 -1
  162. package/dist/chunk-ZL7FH4NW.mjs +0 -1274
  163. package/dist/chunk-ZL7FH4NW.mjs.map +0 -1
  164. package/dist/components-EHOGXATG.cjs +0 -22
  165. package/dist/components-EHOGXATG.cjs.map +0 -1
  166. package/dist/components-MQ6DR7TX.cjs +0 -26
  167. package/dist/components-MQ6DR7TX.cjs.map +0 -1
  168. package/dist/components-XRX7QGLB.mjs +0 -5
  169. package/dist/components-XRX7QGLB.mjs.map +0 -1
  170. package/dist/components-YATKRWLH.mjs +0 -5
  171. package/dist/components-YATKRWLH.mjs.map +0 -1
  172. package/dist/index.cjs +0 -3674
  173. package/dist/index.cjs.map +0 -1
  174. package/dist/index.css +0 -234
  175. package/dist/index.css.map +0 -1
  176. package/dist/index.d.cts +0 -4929
  177. package/dist/index.d.ts +0 -4929
  178. package/dist/index.mjs +0 -2912
  179. package/dist/index.mjs.map +0 -1
  180. package/dist/launcher-5Y42OBSN.mjs +0 -6
  181. package/dist/launcher-5Y42OBSN.mjs.map +0 -1
  182. package/dist/launcher-PMW2YB24.cjs +0 -59
  183. package/dist/launcher-PMW2YB24.cjs.map +0 -1
  184. package/src/index.ts +0 -157
@@ -1,5 +1,1324 @@
1
- export { TreeError, TreeSkeleton, createDemoTree } from '../chunk-B6IR5KSC.mjs';
2
- export { DEFAULT_TREE_APPEARANCE, DEFAULT_TREE_LABELS, TreeRoot as Tree, TreeChevron, TreeContent, TreeEmpty, TreeIcon, TreeIndentGuides, TreeLabel, TreeProvider, TreeRoot, TreeRow, TreeSearchInput, appearanceToStyle, clearTreeState, createChildCache, TreeRoot_default as default, flattenTree, loadTreeState, resolveAppearance, resolveChildren, saveTreeState, useTreeActions, useTreeContext, useTreeExpansion, useTreeFocus, useTreeKeyboard, useTreeLabels, useTreeRows, useTreeSearch, useTreeSelection, useTreeTypeAhead } from '../chunk-ZL7FH4NW.mjs';
3
- import '../chunk-N2XQF2OL.mjs';
1
+ import { __name } from '../chunk-PAWJFY3S.mjs';
2
+ import * as React from 'react';
3
+ import { createContext, memo, 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, AlertCircle } 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({
39
+ roots,
40
+ expandedIds,
41
+ cache,
42
+ filterNode
43
+ }) {
44
+ const out = [];
45
+ const walk = /* @__PURE__ */ __name((nodes, level, parentId) => {
46
+ for (const node of nodes) {
47
+ if (filterNode && !filterNode(node)) continue;
48
+ const isFolder = isNodeFolder(node);
49
+ const isExpanded = expandedIds.has(node.id);
50
+ const resolved = isFolder ? resolveChildren(cache, node) : { children: [], status: "loaded" };
51
+ out.push({
52
+ node,
53
+ level,
54
+ parentId,
55
+ isFolder,
56
+ isExpanded,
57
+ isLoading: resolved.status === "loading",
58
+ hasError: resolved.status === "error"
59
+ });
60
+ if (isFolder && isExpanded && resolved.children) {
61
+ walk(resolved.children, level + 1, node.id);
62
+ }
63
+ }
64
+ }, "walk");
65
+ walk(roots, 0, null);
66
+ return out;
67
+ }
68
+ __name(flattenTree, "flattenTree");
69
+
70
+ // src/tools/Tree/data/persist.ts
71
+ var KEY_PREFIX = "@djangocfg/tree:";
72
+ var VERSION = 1;
73
+ function safeStorage() {
74
+ if (typeof window === "undefined") return null;
75
+ try {
76
+ return window.localStorage;
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
81
+ __name(safeStorage, "safeStorage");
82
+ function loadTreeState(key) {
83
+ const storage = safeStorage();
84
+ if (!storage) return null;
85
+ try {
86
+ const raw = storage.getItem(KEY_PREFIX + key);
87
+ if (!raw) return null;
88
+ const parsed = JSON.parse(raw);
89
+ if (parsed.version !== VERSION) return null;
90
+ return {
91
+ expandedItems: Array.isArray(parsed.expandedItems) ? parsed.expandedItems : [],
92
+ selectedItems: Array.isArray(parsed.selectedItems) ? parsed.selectedItems : []
93
+ };
94
+ } catch {
95
+ return null;
96
+ }
97
+ }
98
+ __name(loadTreeState, "loadTreeState");
99
+ function saveTreeState(key, state) {
100
+ const storage = safeStorage();
101
+ if (!storage) return;
102
+ try {
103
+ const payload = { ...state, version: VERSION };
104
+ storage.setItem(KEY_PREFIX + key, JSON.stringify(payload));
105
+ } catch {
106
+ }
107
+ }
108
+ __name(saveTreeState, "saveTreeState");
109
+ function clearTreeState(key) {
110
+ const storage = safeStorage();
111
+ if (!storage) return;
112
+ try {
113
+ storage.removeItem(KEY_PREFIX + key);
114
+ } catch {
115
+ }
116
+ }
117
+ __name(clearTreeState, "clearTreeState");
118
+
119
+ // src/tools/Tree/data/appearance.ts
120
+ var DENSITY_PRESETS = {
121
+ compact: { rowHeight: 24, iconSize: 14, fontSize: 13, gap: 6 },
122
+ cozy: { rowHeight: 28, iconSize: 16, fontSize: 13, gap: 8 },
123
+ comfortable: { rowHeight: 32, iconSize: 16, fontSize: 14, gap: 8 }
124
+ };
125
+ var DEFAULT_TREE_APPEARANCE = {
126
+ density: "cozy",
127
+ ...DENSITY_PRESETS.cozy,
128
+ iconStrokeWidth: 1.5,
129
+ indent: 16,
130
+ accent: "default",
131
+ radius: "sm",
132
+ indentGuideOpacity: 0.4,
133
+ showActiveIndicator: true
134
+ };
135
+ function resolveAppearance(input, outerIndent) {
136
+ if (!input && outerIndent === void 0) return DEFAULT_TREE_APPEARANCE;
137
+ const density = input?.density ?? "cozy";
138
+ const preset = DENSITY_PRESETS[density];
139
+ return {
140
+ density,
141
+ rowHeight: input?.rowHeight ?? preset.rowHeight,
142
+ iconSize: input?.iconSize ?? preset.iconSize,
143
+ iconStrokeWidth: input?.iconStrokeWidth ?? DEFAULT_TREE_APPEARANCE.iconStrokeWidth,
144
+ fontSize: input?.fontSize ?? preset.fontSize,
145
+ gap: input?.gap ?? preset.gap,
146
+ indent: input?.indent ?? outerIndent ?? DEFAULT_TREE_APPEARANCE.indent,
147
+ accent: input?.accent ?? DEFAULT_TREE_APPEARANCE.accent,
148
+ radius: input?.radius ?? DEFAULT_TREE_APPEARANCE.radius,
149
+ indentGuideOpacity: input?.indentGuideOpacity ?? DEFAULT_TREE_APPEARANCE.indentGuideOpacity,
150
+ showActiveIndicator: input?.showActiveIndicator ?? DEFAULT_TREE_APPEARANCE.showActiveIndicator
151
+ };
152
+ }
153
+ __name(resolveAppearance, "resolveAppearance");
154
+ function appearanceToStyle(a) {
155
+ return {
156
+ ["--tree-row-height"]: `${a.rowHeight}px`,
157
+ ["--tree-icon-size"]: `${a.iconSize}px`,
158
+ ["--tree-icon-stroke"]: a.iconStrokeWidth,
159
+ ["--tree-font-size"]: `${a.fontSize}px`,
160
+ ["--tree-gap"]: `${a.gap}px`,
161
+ ["--tree-indent"]: `${a.indent}px`,
162
+ ["--tree-guide-opacity"]: a.indentGuideOpacity
163
+ };
164
+ }
165
+ __name(appearanceToStyle, "appearanceToStyle");
166
+ var RADIUS_CLASS = {
167
+ none: "rounded-none",
168
+ sm: "rounded-sm",
169
+ md: "rounded-md"
170
+ };
171
+ function radiusClass(a) {
172
+ return RADIUS_CLASS[a.radius];
173
+ }
174
+ __name(radiusClass, "radiusClass");
175
+ var HOVER = {
176
+ subtle: "hover:bg-foreground/[.03]",
177
+ default: "hover:bg-foreground/[.06]",
178
+ strong: "hover:bg-foreground/[.09]"
179
+ };
180
+ var FOCUSED_NOT_SELECTED = {
181
+ subtle: "data-[focused=true]:bg-foreground/[.05]",
182
+ default: "data-[focused=true]:bg-foreground/[.08]",
183
+ strong: "data-[focused=true]:bg-foreground/[.12]"
184
+ };
185
+ var SELECTED_INACTIVE = {
186
+ subtle: "data-[selected=true]:bg-foreground/[.06]",
187
+ default: "data-[selected=true]:bg-foreground/[.10]",
188
+ strong: "data-[selected=true]:bg-foreground/[.14]"
189
+ };
190
+ var SELECTED_ACTIVE = {
191
+ subtle: "data-[selected=true]:group-focus-within/tree:bg-primary/10 data-[selected=true]:group-focus-within/tree:text-primary",
192
+ default: "data-[selected=true]:group-focus-within/tree:bg-primary/15 data-[selected=true]:group-focus-within/tree:text-primary",
193
+ strong: "data-[selected=true]:group-focus-within/tree:bg-primary/25 data-[selected=true]:group-focus-within/tree:text-primary"
194
+ };
195
+ function rowStateClasses(a) {
196
+ return [
197
+ HOVER[a.accent],
198
+ FOCUSED_NOT_SELECTED[a.accent],
199
+ SELECTED_INACTIVE[a.accent],
200
+ SELECTED_ACTIVE[a.accent]
201
+ ].join(" ");
202
+ }
203
+ __name(rowStateClasses, "rowStateClasses");
204
+ var reducer = /* @__PURE__ */ __name((state, action) => {
205
+ switch (action.type) {
206
+ case "expand": {
207
+ if (state.expanded.has(action.id)) return state;
208
+ const next = new Set(state.expanded);
209
+ next.add(action.id);
210
+ return { ...state, expanded: next };
211
+ }
212
+ case "collapse": {
213
+ if (!state.expanded.has(action.id)) return state;
214
+ const next = new Set(state.expanded);
215
+ next.delete(action.id);
216
+ return { ...state, expanded: next };
217
+ }
218
+ case "toggle": {
219
+ const next = new Set(state.expanded);
220
+ if (next.has(action.id)) next.delete(action.id);
221
+ else next.add(action.id);
222
+ return { ...state, expanded: next };
223
+ }
224
+ case "set-expanded":
225
+ return { ...state, expanded: new Set(action.ids) };
226
+ case "select": {
227
+ if (action.mode === "none") return state;
228
+ if (action.mode === "single") {
229
+ return { ...state, selected: /* @__PURE__ */ new Set([action.id]), focused: action.id };
230
+ }
231
+ const next = new Set(state.selected);
232
+ if (next.has(action.id)) next.delete(action.id);
233
+ else next.add(action.id);
234
+ return { ...state, selected: next, focused: action.id };
235
+ }
236
+ case "select-many":
237
+ return { ...state, selected: new Set(action.ids) };
238
+ case "clear-selection":
239
+ return { ...state, selected: /* @__PURE__ */ new Set() };
240
+ case "focus":
241
+ return { ...state, focused: action.id };
242
+ case "set-query":
243
+ return { ...state, query: action.q };
244
+ case "cache-tick":
245
+ return { ...state, cacheTick: state.cacheTick + 1 };
246
+ default:
247
+ return state;
248
+ }
249
+ }, "reducer");
250
+ var TreeContext = createContext(null);
251
+ function useTreeContext() {
252
+ const ctx = React.useContext(TreeContext);
253
+ if (!ctx) {
254
+ throw new Error("useTreeContext must be used inside <TreeProvider>");
255
+ }
256
+ return ctx;
257
+ }
258
+ __name(useTreeContext, "useTreeContext");
259
+ var setEqualsArr = /* @__PURE__ */ __name((set, arr) => {
260
+ if (set.size !== arr.length) return false;
261
+ for (const id of arr) if (!set.has(id)) return false;
262
+ return true;
263
+ }, "setEqualsArr");
264
+ var collectAllIds = /* @__PURE__ */ __name((roots, cache, out) => {
265
+ for (const node of roots) {
266
+ if (Array.isArray(node.children)) {
267
+ out.push(node.id);
268
+ collectAllIds(node.children, cache, out);
269
+ } else if (node.isFolder) {
270
+ out.push(node.id);
271
+ const entry = cache.get(node.id);
272
+ if (entry?.children) collectAllIds(entry.children, cache, out);
273
+ }
274
+ }
275
+ }, "collectAllIds");
276
+ function TreeProvider(props) {
277
+ const {
278
+ data,
279
+ getItemName,
280
+ loadChildren,
281
+ selectionMode = "single",
282
+ activationMode = "single-click",
283
+ initialExpandedIds,
284
+ initialSelectedIds,
285
+ indent,
286
+ appearance,
287
+ onSelectionChange,
288
+ onExpansionChange,
289
+ onActivate,
290
+ filterNode,
291
+ enableSearch = false,
292
+ showIndentGuides = false,
293
+ renderIcon,
294
+ renderLabel,
295
+ renderActions,
296
+ renderContextMenu,
297
+ labels: labelsOverride,
298
+ persistKey,
299
+ persistSelection = false,
300
+ children
301
+ } = props;
302
+ const labels = useMemo(
303
+ () => ({ ...DEFAULT_TREE_LABELS, ...labelsOverride }),
304
+ [labelsOverride]
305
+ );
306
+ const resolvedAppearance = useMemo(
307
+ () => resolveAppearance(appearance, indent),
308
+ [appearance, indent]
309
+ );
310
+ const persisted = useMemo(
311
+ () => persistKey ? loadTreeState(persistKey) : null,
312
+ [persistKey]
313
+ );
314
+ const [state, dispatch] = useReducer(reducer, void 0, () => ({
315
+ expanded: new Set(persisted?.expandedItems ?? initialExpandedIds ?? []),
316
+ selected: new Set(
317
+ (persistSelection ? persisted?.selectedItems : void 0) ?? initialSelectedIds ?? []
318
+ ),
319
+ focused: null,
320
+ query: "",
321
+ cacheTick: 0
322
+ }));
323
+ const cacheRef = useRef(createChildCache());
324
+ const inflightRef = useRef(/* @__PURE__ */ new Map());
325
+ const fetchChildren = useCallback(
326
+ async (node) => {
327
+ if (!loadChildren) return;
328
+ if (Array.isArray(node.children)) return;
329
+ const existing = cacheRef.current.get(node.id);
330
+ if (existing?.status === "loaded" || existing?.status === "loading") return;
331
+ const inflight = inflightRef.current.get(node.id);
332
+ if (inflight) return inflight;
333
+ cacheRef.current.set(node.id, { status: "loading", children: [] });
334
+ dispatch({ type: "cache-tick" });
335
+ const promise = (async () => {
336
+ try {
337
+ const children2 = await loadChildren(node);
338
+ cacheRef.current.set(node.id, { status: "loaded", children: children2 });
339
+ } catch (err) {
340
+ cacheRef.current.set(node.id, {
341
+ status: "error",
342
+ children: [],
343
+ error: err instanceof Error ? err.message : String(err)
344
+ });
345
+ } finally {
346
+ inflightRef.current.delete(node.id);
347
+ dispatch({ type: "cache-tick" });
348
+ }
349
+ })();
350
+ inflightRef.current.set(node.id, promise);
351
+ return promise;
352
+ },
353
+ [loadChildren]
354
+ );
355
+ const nodeById = useMemo(() => {
356
+ const map = /* @__PURE__ */ new Map();
357
+ const walk = /* @__PURE__ */ __name((nodes) => {
358
+ for (const n of nodes) {
359
+ map.set(n.id, n);
360
+ if (Array.isArray(n.children)) walk(n.children);
361
+ else {
362
+ const entry = cacheRef.current.get(n.id);
363
+ if (entry?.children) walk(entry.children);
364
+ }
365
+ }
366
+ }, "walk");
367
+ walk(data);
368
+ return map;
369
+ }, [data, state.cacheTick]);
370
+ useEffect(() => {
371
+ if (!loadChildren) return;
372
+ for (const id of state.expanded) {
373
+ const node = nodeById.get(id);
374
+ if (!node) continue;
375
+ void fetchChildren(node);
376
+ }
377
+ }, [loadChildren, state.expanded, state.cacheTick, nodeById, fetchChildren]);
378
+ const flatRows = useMemo(
379
+ () => flattenTree({
380
+ roots: data,
381
+ expandedIds: state.expanded,
382
+ cache: cacheRef.current,
383
+ filterNode
384
+ }),
385
+ [data, state.expanded, state.cacheTick, filterNode]
386
+ );
387
+ const matchingIds = useMemo(() => {
388
+ const set = /* @__PURE__ */ new Set();
389
+ if (!enableSearch || state.query.trim() === "") return set;
390
+ const q = state.query.trim().toLowerCase();
391
+ for (const row of flatRows) {
392
+ if (getItemName(row.node).toLowerCase().includes(q)) {
393
+ set.add(row.node.id);
394
+ }
395
+ }
396
+ return set;
397
+ }, [enableSearch, state.query, flatRows, getItemName]);
398
+ const onSelectionChangeRef = useRef(onSelectionChange);
399
+ const onExpansionChangeRef = useRef(onExpansionChange);
400
+ const onActivateRef = useRef(onActivate);
401
+ onSelectionChangeRef.current = onSelectionChange;
402
+ onExpansionChangeRef.current = onExpansionChange;
403
+ onActivateRef.current = onActivate;
404
+ const lastSelectedArrRef = useRef([...state.selected]);
405
+ const lastExpandedArrRef = useRef([...state.expanded]);
406
+ useEffect(() => {
407
+ const arr = [...state.expanded];
408
+ if (!setEqualsArr(state.expanded, lastExpandedArrRef.current)) {
409
+ lastExpandedArrRef.current = arr;
410
+ onExpansionChangeRef.current?.(arr);
411
+ if (persistKey) {
412
+ saveTreeState(persistKey, {
413
+ expandedItems: arr,
414
+ selectedItems: persistSelection ? [...state.selected] : []
415
+ });
416
+ }
417
+ }
418
+ }, [state.expanded, persistKey, persistSelection, state.selected]);
419
+ useEffect(() => {
420
+ const arr = [...state.selected];
421
+ if (!setEqualsArr(state.selected, lastSelectedArrRef.current)) {
422
+ lastSelectedArrRef.current = arr;
423
+ onSelectionChangeRef.current?.(arr);
424
+ if (persistKey && persistSelection) {
425
+ saveTreeState(persistKey, {
426
+ expandedItems: [...state.expanded],
427
+ selectedItems: arr
428
+ });
429
+ }
430
+ }
431
+ }, [state.selected, persistKey, persistSelection, state.expanded]);
432
+ const expand = useCallback((id) => dispatch({ type: "expand", id }), []);
433
+ const collapse = useCallback((id) => dispatch({ type: "collapse", id }), []);
434
+ const toggle = useCallback((id) => dispatch({ type: "toggle", id }), []);
435
+ const expandAll = useCallback(() => {
436
+ const ids = [];
437
+ collectAllIds(data, cacheRef.current, ids);
438
+ dispatch({ type: "set-expanded", ids });
439
+ }, [data]);
440
+ const collapseAll = useCallback(
441
+ () => dispatch({ type: "set-expanded", ids: [] }),
442
+ []
443
+ );
444
+ const select = useCallback(
445
+ (id) => dispatch({ type: "select", id, mode: selectionMode }),
446
+ [selectionMode]
447
+ );
448
+ const setSelectedIds = useCallback(
449
+ (ids) => dispatch({ type: "select-many", ids }),
450
+ []
451
+ );
452
+ const clearSelection = useCallback(() => dispatch({ type: "clear-selection" }), []);
453
+ const setFocus = useCallback(
454
+ (id) => dispatch({ type: "focus", id }),
455
+ []
456
+ );
457
+ const setQuery = useCallback((q) => dispatch({ type: "set-query", q }), []);
458
+ const refresh = useCallback(
459
+ async (id) => {
460
+ const node = nodeById.get(id);
461
+ if (!node || !loadChildren) return;
462
+ cacheRef.current.delete(id);
463
+ dispatch({ type: "cache-tick" });
464
+ await fetchChildren(node);
465
+ },
466
+ [nodeById, loadChildren, fetchChildren]
467
+ );
468
+ const refreshAll = useCallback(async () => {
469
+ cacheRef.current.clear();
470
+ dispatch({ type: "cache-tick" });
471
+ if (!loadChildren) return;
472
+ await Promise.all(
473
+ [...state.expanded].map((id) => {
474
+ const node = nodeById.get(id);
475
+ return node ? fetchChildren(node) : void 0;
476
+ })
477
+ );
478
+ }, [loadChildren, state.expanded, nodeById, fetchChildren]);
479
+ const activate = useCallback(
480
+ (node, opts = { preview: false }) => onActivateRef.current?.(node, opts),
481
+ []
482
+ );
483
+ const value = useMemo(
484
+ () => ({
485
+ expanded: state.expanded,
486
+ selected: state.selected,
487
+ focused: state.focused,
488
+ query: state.query,
489
+ flatRows,
490
+ matchingIds,
491
+ expand,
492
+ collapse,
493
+ toggle,
494
+ expandAll,
495
+ collapseAll,
496
+ select,
497
+ setSelectedIds,
498
+ clearSelection,
499
+ setFocus,
500
+ setQuery,
501
+ refresh,
502
+ refreshAll,
503
+ activate,
504
+ labels,
505
+ appearance: resolvedAppearance,
506
+ indent: resolvedAppearance.indent,
507
+ selectionMode,
508
+ activationMode,
509
+ enableSearch,
510
+ showIndentGuides,
511
+ getItemName,
512
+ renderIcon,
513
+ renderLabel,
514
+ renderActions,
515
+ renderContextMenu
516
+ }),
517
+ [
518
+ state.expanded,
519
+ state.selected,
520
+ state.focused,
521
+ state.query,
522
+ flatRows,
523
+ matchingIds,
524
+ expand,
525
+ collapse,
526
+ toggle,
527
+ expandAll,
528
+ collapseAll,
529
+ select,
530
+ setSelectedIds,
531
+ clearSelection,
532
+ setFocus,
533
+ setQuery,
534
+ refresh,
535
+ refreshAll,
536
+ activate,
537
+ labels,
538
+ resolvedAppearance,
539
+ selectionMode,
540
+ activationMode,
541
+ enableSearch,
542
+ showIndentGuides,
543
+ getItemName,
544
+ renderIcon,
545
+ renderLabel,
546
+ renderActions,
547
+ renderContextMenu
548
+ ]
549
+ );
550
+ return /* @__PURE__ */ jsx(TreeContext.Provider, { value, children });
551
+ }
552
+ __name(TreeProvider, "TreeProvider");
553
+ function TreeChevronRaw({ isExpanded, isFolder, className }) {
554
+ const { appearance } = useTreeContext();
555
+ const size = { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" };
556
+ if (!isFolder) {
557
+ return /* @__PURE__ */ jsx(
558
+ "span",
559
+ {
560
+ "aria-hidden": true,
561
+ style: size,
562
+ className: cn("inline-block shrink-0", className)
563
+ }
564
+ );
565
+ }
566
+ const Icon = isExpanded ? ChevronDown : ChevronRight;
567
+ return /* @__PURE__ */ jsx(
568
+ Icon,
569
+ {
570
+ "aria-hidden": true,
571
+ strokeWidth: appearance.iconStrokeWidth,
572
+ style: size,
573
+ className: cn(
574
+ "shrink-0 text-muted-foreground/70 transition-transform",
575
+ className
576
+ )
577
+ }
578
+ );
579
+ }
580
+ __name(TreeChevronRaw, "TreeChevronRaw");
581
+ var TreeChevron = memo(TreeChevronRaw);
582
+ function TreeIconRaw({ isFolder, isExpanded, className }) {
583
+ const { appearance } = useTreeContext();
584
+ const Icon = isFolder ? isExpanded ? FolderOpen : Folder : File;
585
+ return /* @__PURE__ */ jsx(
586
+ Icon,
587
+ {
588
+ "aria-hidden": true,
589
+ strokeWidth: appearance.iconStrokeWidth,
590
+ style: { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" },
591
+ className: cn(
592
+ "shrink-0",
593
+ isFolder ? "text-foreground/70" : "text-muted-foreground/80",
594
+ className
595
+ )
596
+ }
597
+ );
598
+ }
599
+ __name(TreeIconRaw, "TreeIconRaw");
600
+ var TreeIcon = memo(TreeIconRaw);
601
+ function TreeIndentGuides({ level, indent }) {
602
+ const { appearance } = useTreeContext();
603
+ if (level <= 0) return null;
604
+ return /* @__PURE__ */ jsx(
605
+ "span",
606
+ {
607
+ "aria-hidden": true,
608
+ className: "pointer-events-none absolute inset-y-0 left-0",
609
+ style: { width: 8 + level * indent, opacity: appearance.indentGuideOpacity },
610
+ children: Array.from({ length: level }).map((_, i) => /* @__PURE__ */ jsx(
611
+ "span",
612
+ {
613
+ className: "absolute inset-y-0 w-px bg-border",
614
+ style: { left: 8 + i * indent + indent / 2 - 0.5 }
615
+ },
616
+ i
617
+ ))
618
+ }
619
+ );
620
+ }
621
+ __name(TreeIndentGuides, "TreeIndentGuides");
622
+ function TreeLabelRaw({ children, isMatchingSearch, className }) {
623
+ return /* @__PURE__ */ jsx(
624
+ "span",
625
+ {
626
+ style: { fontSize: "var(--tree-font-size)" },
627
+ className: cn(
628
+ "truncate leading-tight tracking-[-0.005em]",
629
+ isMatchingSearch && "font-medium text-foreground",
630
+ className
631
+ ),
632
+ children
633
+ }
634
+ );
635
+ }
636
+ __name(TreeLabelRaw, "TreeLabelRaw");
637
+ var TreeLabel = memo(TreeLabelRaw);
638
+ function TreeRowRaw({ row, className }) {
639
+ const ctx = useTreeContext();
640
+ const {
641
+ appearance,
642
+ activationMode,
643
+ showIndentGuides,
644
+ selected,
645
+ focused,
646
+ matchingIds,
647
+ select,
648
+ toggle,
649
+ setFocus,
650
+ activate,
651
+ getItemName,
652
+ renderIcon,
653
+ renderLabel,
654
+ renderActions,
655
+ renderContextMenu
656
+ } = ctx;
657
+ const { node, level, isFolder, isExpanded, isLoading } = row;
658
+ const isSelected = selected.has(node.id);
659
+ const isFocused = focused === node.id;
660
+ const isMatchingSearch = matchingIds.has(node.id);
661
+ const slot = {
662
+ node,
663
+ level,
664
+ isSelected,
665
+ isExpanded,
666
+ isFocused,
667
+ isFolder,
668
+ isLoading,
669
+ isMatchingSearch
670
+ };
671
+ const handleClick = /* @__PURE__ */ __name((e) => {
672
+ if (node.disabled) return;
673
+ setFocus(node.id);
674
+ select(node.id);
675
+ if (isFolder) {
676
+ toggle(node.id);
677
+ } else if (activationMode === "single-click") {
678
+ activate(node, { preview: false });
679
+ } else if (activationMode === "single-click-preview") {
680
+ activate(node, { preview: true });
681
+ }
682
+ e.currentTarget.scrollIntoView?.({ block: "nearest" });
683
+ }, "handleClick");
684
+ const handleDoubleClick = /* @__PURE__ */ __name(() => {
685
+ if (node.disabled) return;
686
+ if (isFolder) return;
687
+ activate(node, { preview: false });
688
+ }, "handleDoubleClick");
689
+ const trigger = /* @__PURE__ */ jsxs(
690
+ "div",
691
+ {
692
+ role: "treeitem",
693
+ "aria-level": level + 1,
694
+ "aria-expanded": isFolder ? isExpanded : void 0,
695
+ "aria-selected": isSelected || void 0,
696
+ "aria-current": isSelected ? "true" : void 0,
697
+ "aria-disabled": node.disabled || void 0,
698
+ "data-tree-row": "",
699
+ "data-id": node.id,
700
+ "data-activation-mode": activationMode,
701
+ "data-selected": isSelected ? "true" : void 0,
702
+ "data-focused": isFocused && !isSelected ? "true" : void 0,
703
+ "data-folder": isFolder || void 0,
704
+ "data-expanded": isExpanded || void 0,
705
+ tabIndex: isFocused ? 0 : -1,
706
+ style: {
707
+ paddingLeft: 6 + level * appearance.indent,
708
+ height: "var(--tree-row-height)",
709
+ gap: "var(--tree-gap)"
710
+ },
711
+ onClick: handleClick,
712
+ onDoubleClick: handleDoubleClick,
713
+ onFocus: () => setFocus(node.id),
714
+ className: cn(
715
+ "group/row relative flex w-full select-none items-center pr-2 text-left",
716
+ "transition-colors outline-none",
717
+ node.disabled ? "cursor-not-allowed" : "cursor-pointer",
718
+ radiusClass(appearance),
719
+ rowStateClasses(appearance),
720
+ "focus-visible:ring-1 focus-visible:ring-ring/50",
721
+ isMatchingSearch && "ring-1 ring-primary/30",
722
+ node.disabled && "opacity-50",
723
+ className
724
+ ),
725
+ children: [
726
+ appearance.showActiveIndicator && isSelected ? /* @__PURE__ */ jsx(
727
+ "span",
728
+ {
729
+ "aria-hidden": true,
730
+ className: cn(
731
+ "absolute left-0 top-1 bottom-1 w-0.5 rounded-r-full",
732
+ "bg-foreground/30 group-focus-within/tree:bg-primary"
733
+ )
734
+ }
735
+ ) : null,
736
+ showIndentGuides && level > 0 ? /* @__PURE__ */ jsx(TreeIndentGuides, { level, indent: appearance.indent }) : null,
737
+ /* @__PURE__ */ jsx(TreeChevron, { isExpanded, isFolder }),
738
+ isLoading ? /* @__PURE__ */ jsx(
739
+ Loader2,
740
+ {
741
+ "aria-hidden": true,
742
+ strokeWidth: appearance.iconStrokeWidth,
743
+ style: { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" },
744
+ className: "shrink-0 animate-spin text-muted-foreground/70"
745
+ }
746
+ ) : renderIcon ? renderIcon(slot) : /* @__PURE__ */ jsx(TreeIcon, { isFolder, isExpanded }),
747
+ /* @__PURE__ */ jsx(
748
+ "span",
749
+ {
750
+ className: "flex min-w-0 flex-1 items-center",
751
+ style: { gap: "var(--tree-gap)" },
752
+ children: renderLabel ? renderLabel(slot) : /* @__PURE__ */ jsx(TreeLabel, { isMatchingSearch, children: getItemName(node) })
753
+ }
754
+ ),
755
+ renderActions ? /* @__PURE__ */ jsx(
756
+ "span",
757
+ {
758
+ 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",
759
+ onClick: (e) => e.stopPropagation(),
760
+ children: renderActions(slot)
761
+ }
762
+ ) : null
763
+ ]
764
+ }
765
+ );
766
+ if (renderContextMenu) {
767
+ return /* @__PURE__ */ jsx(Fragment, { children: renderContextMenu(slot, trigger) });
768
+ }
769
+ return trigger;
770
+ }
771
+ __name(TreeRowRaw, "TreeRowRaw");
772
+ var TreeRow = memo(TreeRowRaw);
773
+ function TreeEmpty({ children, className }) {
774
+ return /* @__PURE__ */ jsx(
775
+ "div",
776
+ {
777
+ className: cn(
778
+ "flex h-full min-h-32 items-center justify-center px-4 py-6 text-sm text-muted-foreground",
779
+ className
780
+ ),
781
+ children
782
+ }
783
+ );
784
+ }
785
+ __name(TreeEmpty, "TreeEmpty");
786
+ function TreeContent({ children, className, ariaLabel }) {
787
+ const { flatRows, labels, selected, focused, matchingIds, appearance } = useTreeContext();
788
+ if (flatRows.length === 0) {
789
+ return /* @__PURE__ */ jsx(TreeEmpty, { children: labels.empty });
790
+ }
791
+ return /* @__PURE__ */ jsx(
792
+ "div",
793
+ {
794
+ role: "tree",
795
+ "aria-label": ariaLabel ?? labels.ariaLabel,
796
+ className: cn("relative flex flex-col py-1", className),
797
+ style: appearanceToStyle(appearance),
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__ */ jsx(TreeRow, { row });
810
+ return /* @__PURE__ */ jsx(Fragment$1, { children: node }, row.node.id);
811
+ })
812
+ }
813
+ );
814
+ }
815
+ __name(TreeContent, "TreeContent");
816
+ function useTreeLabels() {
817
+ return useTreeContext().labels;
818
+ }
819
+ __name(useTreeLabels, "useTreeLabels");
820
+ function useTreeRows() {
821
+ return useTreeContext().flatRows;
822
+ }
823
+ __name(useTreeRows, "useTreeRows");
824
+ function useTreeSelection() {
825
+ const ctx = useTreeContext();
826
+ const selectedIds = useMemo(() => [...ctx.selected], [ctx.selected]);
827
+ const isSelected = useCallback(
828
+ (id) => ctx.selected.has(id),
829
+ [ctx.selected]
830
+ );
831
+ return 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
+ __name(useTreeSelection, "useTreeSelection");
843
+ function useTreeExpansion() {
844
+ const ctx = useTreeContext();
845
+ const expandedIds = useMemo(() => [...ctx.expanded], [ctx.expanded]);
846
+ const isExpanded = useCallback(
847
+ (id) => ctx.expanded.has(id),
848
+ [ctx.expanded]
849
+ );
850
+ return 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
+ __name(useTreeExpansion, "useTreeExpansion");
872
+ function useTreeFocus() {
873
+ const ctx = useTreeContext();
874
+ return useMemo(
875
+ () => ({ focusedId: ctx.focused, setFocus: ctx.setFocus }),
876
+ [ctx.focused, ctx.setFocus]
877
+ );
878
+ }
879
+ __name(useTreeFocus, "useTreeFocus");
880
+ function useTreeSearch() {
881
+ const ctx = useTreeContext();
882
+ return 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
+ __name(useTreeSearch, "useTreeSearch");
894
+ function useTreeActions() {
895
+ const ctx = useTreeContext();
896
+ return 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
+ __name(useTreeActions, "useTreeActions");
920
+ function TreeSearchInput({ className, showMatches = true }) {
921
+ const { labels } = useTreeContext();
922
+ const { query, setQuery, matchCount } = useTreeSearch();
923
+ return /* @__PURE__ */ jsxs(
924
+ "div",
925
+ {
926
+ className: cn(
927
+ "flex items-center gap-2 rounded-md border border-border bg-background px-2",
928
+ className
929
+ ),
930
+ children: [
931
+ /* @__PURE__ */ jsx(Search, { "aria-hidden": true, className: "size-3.5 shrink-0 text-muted-foreground" }),
932
+ /* @__PURE__ */ 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__ */ jsx("span", { className: "shrink-0 text-xs text-muted-foreground", children: labels.searchMatches(matchCount) }) : null,
943
+ query.length > 0 ? /* @__PURE__ */ 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__ */ jsx(X, { "aria-hidden": true, className: "size-3.5" })
951
+ }
952
+ ) : null
953
+ ]
954
+ }
955
+ );
956
+ }
957
+ __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 = useRef(rows);
970
+ const focusedIdRef = useRef(focusedId);
971
+ rowsRef.current = rows;
972
+ focusedIdRef.current = focusedId;
973
+ const getCurrent = /* @__PURE__ */ __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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = useHotkey(
1060
+ "escape",
1061
+ () => onClearSelection(),
1062
+ { enabled, preventDefault: true, description: "Clear selection" }
1063
+ );
1064
+ const ref = 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
+ __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 = useRef("");
1089
+ const timerRef = useRef(null);
1090
+ const rowsRef = useRef(rows);
1091
+ const getNameRef = useRef(getItemName);
1092
+ const onMatchRef = useRef(onMatch);
1093
+ rowsRef.current = rows;
1094
+ getNameRef.current = getItemName;
1095
+ onMatchRef.current = onMatch;
1096
+ useEffect(() => {
1097
+ if (!enabled) return;
1098
+ const target = containerRef.current;
1099
+ if (!target) return;
1100
+ const reset = /* @__PURE__ */ __name(() => {
1101
+ bufferRef.current = "";
1102
+ if (timerRef.current) {
1103
+ clearTimeout(timerRef.current);
1104
+ timerRef.current = null;
1105
+ }
1106
+ }, "reset");
1107
+ const handler = /* @__PURE__ */ __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
+ __name(useTreeTypeAhead, "useTreeTypeAhead");
1137
+ function TreeRoot(props) {
1138
+ const {
1139
+ data,
1140
+ getItemName,
1141
+ loadChildren,
1142
+ selectionMode,
1143
+ activationMode,
1144
+ initialExpandedIds,
1145
+ initialSelectedIds,
1146
+ indent,
1147
+ appearance,
1148
+ onSelectionChange,
1149
+ onExpansionChange,
1150
+ onActivate,
1151
+ filterNode,
1152
+ enableSearch = false,
1153
+ enableTypeAhead = true,
1154
+ showIndentGuides = false,
1155
+ renderRow,
1156
+ renderIcon,
1157
+ renderLabel,
1158
+ renderActions,
1159
+ renderContextMenu,
1160
+ labels,
1161
+ persistKey,
1162
+ persistSelection = false,
1163
+ className,
1164
+ style
1165
+ } = props;
1166
+ return /* @__PURE__ */ jsx(
1167
+ TreeProvider,
1168
+ {
1169
+ data,
1170
+ getItemName,
1171
+ loadChildren,
1172
+ selectionMode,
1173
+ activationMode,
1174
+ initialExpandedIds,
1175
+ initialSelectedIds,
1176
+ indent,
1177
+ appearance,
1178
+ onSelectionChange,
1179
+ onExpansionChange,
1180
+ onActivate,
1181
+ filterNode,
1182
+ enableSearch,
1183
+ showIndentGuides,
1184
+ renderIcon,
1185
+ renderLabel,
1186
+ renderActions,
1187
+ renderContextMenu,
1188
+ labels,
1189
+ persistKey,
1190
+ persistSelection,
1191
+ children: /* @__PURE__ */ jsx(
1192
+ TreeRootShell,
1193
+ {
1194
+ className,
1195
+ style,
1196
+ enableSearch,
1197
+ enableTypeAhead,
1198
+ renderRow
1199
+ }
1200
+ )
1201
+ }
1202
+ );
1203
+ }
1204
+ __name(TreeRoot, "TreeRoot");
1205
+ function TreeRootShell({
1206
+ className,
1207
+ style,
1208
+ enableSearch,
1209
+ enableTypeAhead,
1210
+ renderRow
1211
+ }) {
1212
+ const containerRef = useRef(null);
1213
+ const ctx = useTreeContext();
1214
+ const { ref: keyboardRef } = useTreeKeyboard({
1215
+ rows: ctx.flatRows,
1216
+ focusedId: ctx.focused,
1217
+ onFocus: ctx.setFocus,
1218
+ onSelect: ctx.select,
1219
+ onActivate: /* @__PURE__ */ __name((id) => {
1220
+ const row = ctx.flatRows.find((r) => r.node.id === id);
1221
+ if (row) ctx.activate(row.node, { preview: false });
1222
+ }, "onActivate"),
1223
+ onExpand: ctx.expand,
1224
+ onCollapse: ctx.collapse,
1225
+ onClearSelection: ctx.clearSelection
1226
+ });
1227
+ const setContainerRef = useCallback(
1228
+ (instance) => {
1229
+ containerRef.current = instance;
1230
+ keyboardRef(instance);
1231
+ },
1232
+ [keyboardRef]
1233
+ );
1234
+ const onTypeAheadMatch = useCallback(
1235
+ (id) => {
1236
+ ctx.setFocus(id);
1237
+ const el = containerRef.current?.querySelector(
1238
+ `[data-tree-row][data-id="${CSS.escape(id)}"]`
1239
+ );
1240
+ el?.scrollIntoView({ block: "nearest" });
1241
+ },
1242
+ [ctx]
1243
+ );
1244
+ useTreeTypeAhead({
1245
+ rows: ctx.flatRows,
1246
+ getItemName: ctx.getItemName,
1247
+ containerRef,
1248
+ onMatch: onTypeAheadMatch,
1249
+ enabled: enableTypeAhead
1250
+ });
1251
+ return /* @__PURE__ */ jsxs(
1252
+ "div",
1253
+ {
1254
+ ref: setContainerRef,
1255
+ tabIndex: 0,
1256
+ className: cn(
1257
+ "group/tree flex h-full w-full flex-col gap-2 outline-none",
1258
+ className
1259
+ ),
1260
+ style: { ...appearanceToStyle(ctx.appearance), ...style },
1261
+ "data-tree-root": "",
1262
+ children: [
1263
+ enableSearch ? /* @__PURE__ */ jsx(TreeSearchInput, { className: "mx-2 mt-2" }) : null,
1264
+ /* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1 overflow-auto px-1", children: /* @__PURE__ */ jsx(TreeContent, { children: renderRow }) })
1265
+ ]
1266
+ }
1267
+ );
1268
+ }
1269
+ __name(TreeRootShell, "TreeRootShell");
1270
+ var TreeRoot_default = TreeRoot;
1271
+ function TreeSkeleton({ rows = 6, className }) {
1272
+ return /* @__PURE__ */ jsx("div", { className: cn("flex flex-col gap-1 p-2", className), "aria-hidden": true, children: Array.from({ length: rows }).map((_, i) => /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", style: { paddingLeft: i % 3 * 16 }, children: [
1273
+ /* @__PURE__ */ jsx("span", { className: "size-4 shrink-0 animate-pulse rounded bg-muted" }),
1274
+ /* @__PURE__ */ jsx("span", { className: "size-4 shrink-0 animate-pulse rounded bg-muted" }),
1275
+ /* @__PURE__ */ jsx("span", { className: "h-3 flex-1 animate-pulse rounded bg-muted" })
1276
+ ] }, i)) });
1277
+ }
1278
+ __name(TreeSkeleton, "TreeSkeleton");
1279
+ function TreeError({ children, className }) {
1280
+ return /* @__PURE__ */ jsxs(
1281
+ "div",
1282
+ {
1283
+ className: cn(
1284
+ "flex items-start gap-2 rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-sm text-destructive",
1285
+ className
1286
+ ),
1287
+ role: "alert",
1288
+ children: [
1289
+ /* @__PURE__ */ jsx(AlertCircle, { "aria-hidden": true, className: "mt-0.5 size-4 shrink-0" }),
1290
+ /* @__PURE__ */ jsx("span", { children })
1291
+ ]
1292
+ }
1293
+ );
1294
+ }
1295
+ __name(TreeError, "TreeError");
1296
+
1297
+ // src/tools/Tree/data/createDemoTree.ts
1298
+ function createDemoTree({
1299
+ depth = 3,
1300
+ breadth = 4,
1301
+ rootPrefix = "node"
1302
+ } = {}) {
1303
+ const make = /* @__PURE__ */ __name((id, name, level) => {
1304
+ if (level >= depth) {
1305
+ return { id, data: { name } };
1306
+ }
1307
+ const children = [];
1308
+ for (let i = 0; i < breadth; i++) {
1309
+ const childId = `${id}/${i}`;
1310
+ children.push(make(childId, `${name}-${i}`, level + 1));
1311
+ }
1312
+ return { id, data: { name }, children };
1313
+ }, "make");
1314
+ const roots = [];
1315
+ for (let i = 0; i < breadth; i++) {
1316
+ roots.push(make(`${rootPrefix}-${i}`, `${rootPrefix} ${i}`, 1));
1317
+ }
1318
+ return roots;
1319
+ }
1320
+ __name(createDemoTree, "createDemoTree");
1321
+
1322
+ export { DEFAULT_TREE_APPEARANCE, DEFAULT_TREE_LABELS, TreeRoot as Tree, TreeChevron, TreeContent, TreeEmpty, TreeError, TreeIcon, TreeIndentGuides, TreeLabel, TreeProvider, TreeRoot, TreeRow, TreeSearchInput, TreeSkeleton, appearanceToStyle, clearTreeState, createChildCache, createDemoTree, TreeRoot_default as default, flattenTree, loadTreeState, resolveAppearance, resolveChildren, saveTreeState, useTreeActions, useTreeContext, useTreeExpansion, useTreeFocus, useTreeKeyboard, useTreeLabels, useTreeRows, useTreeSearch, useTreeSelection, useTreeTypeAhead };
4
1323
  //# sourceMappingURL=index.mjs.map
5
1324
  //# sourceMappingURL=index.mjs.map