torch-glare 2.1.1 → 2.1.3

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 (73) hide show
  1. package/apps/lib/components/Avatar.tsx +1 -1
  2. package/apps/lib/components/BadgeField.tsx +2 -2
  3. package/apps/lib/components/Card.tsx +68 -54
  4. package/apps/lib/components/DataViews/ARCHITECTURE.md +439 -0
  5. package/apps/lib/components/DataViews/DataViewRadio.tsx +47 -0
  6. package/apps/lib/components/DataViews/DataViewsConfigPanel.tsx +427 -0
  7. package/apps/lib/components/DataViews/DataViewsHeader.tsx +228 -0
  8. package/apps/lib/components/DataViews/DataViewsLayout.tsx +330 -0
  9. package/apps/lib/components/DataViews/FilterPanel.tsx +469 -0
  10. package/apps/lib/components/DataViews/HeaderSearch.tsx +97 -0
  11. package/apps/lib/components/DataViews/InboxView.tsx +495 -0
  12. package/apps/lib/components/DataViews/InboxViewCard.tsx +136 -0
  13. package/apps/lib/components/DataViews/KanbanView.tsx +353 -0
  14. package/apps/lib/components/DataViews/PanelControls.tsx +49 -0
  15. package/apps/lib/components/DataViews/SettingsPanel.tsx +285 -0
  16. package/apps/lib/components/DataViews/TableView.tsx +232 -0
  17. package/apps/lib/components/DataViews/TreeView.tsx +392 -0
  18. package/apps/lib/components/DataViews/badgeAdapter.ts +45 -0
  19. package/apps/lib/components/DataViews/fieldRenderers.tsx +334 -0
  20. package/apps/lib/components/DataViews/filters/DateRangePopover.tsx +113 -0
  21. package/apps/lib/components/DataViews/filters/PresetChips.tsx +45 -0
  22. package/apps/lib/components/DataViews/filters/RangeSliderWithInputs.tsx +154 -0
  23. package/apps/lib/components/DataViews/index.ts +36 -0
  24. package/apps/lib/components/DataViews/tree/TreeDrawer.tsx +54 -0
  25. package/apps/lib/components/DataViews/tree/TreeSidebar.tsx +77 -0
  26. package/apps/lib/components/DataViews/types.ts +206 -0
  27. package/apps/lib/components/Radio.tsx +18 -21
  28. package/apps/lib/components/Switch.tsx +3 -1
  29. package/apps/lib/components/Table.tsx +1 -1
  30. package/apps/lib/components/TreeFolder/TreeFolder.tsx +410 -0
  31. package/apps/lib/components/TreeFolder/TreeFolderBreadcrumb.tsx +80 -0
  32. package/apps/lib/components/TreeFolder/TreeFolderRow.tsx +363 -0
  33. package/apps/lib/components/TreeFolder/TreeFolderStyles.tsx +60 -0
  34. package/apps/lib/components/TreeFolder/icons.tsx +63 -0
  35. package/apps/lib/components/TreeFolder/index.ts +17 -0
  36. package/apps/lib/components/TreeFolder/treeFolderUtils.ts +114 -0
  37. package/apps/lib/components/TreeFolder/types.ts +77 -0
  38. package/apps/lib/components/TreeFolder/useTreeFolderDnD.ts +261 -0
  39. package/apps/lib/hooks/useDataViewsState.ts +169 -0
  40. package/apps/lib/hooks/useIsMobile.ts +21 -0
  41. package/apps/lib/layouts/DataViewCard.tsx +76 -0
  42. package/apps/lib/utils/dataViews/columnUtils.ts +130 -0
  43. package/apps/lib/utils/dataViews/fieldUtils.ts +198 -0
  44. package/apps/lib/utils/dataViews/nestedDataUtils.tsx +364 -0
  45. package/apps/lib/utils/dataViews/pathUtils.ts +132 -0
  46. package/apps/lib/utils/dataViews/rangeUtils.ts +225 -0
  47. package/apps/lib/utils/dataViews/treeUtils.ts +403 -0
  48. package/dist/bin/index.js +3 -3
  49. package/dist/bin/index.js.map +1 -1
  50. package/dist/src/commands/add.d.ts.map +1 -1
  51. package/dist/src/commands/add.js +29 -6
  52. package/dist/src/commands/add.js.map +1 -1
  53. package/dist/src/commands/utils.d.ts.map +1 -1
  54. package/dist/src/commands/utils.js +22 -2
  55. package/dist/src/commands/utils.js.map +1 -1
  56. package/dist/src/shared/copyComponentsRecursively.d.ts.map +1 -1
  57. package/dist/src/shared/copyComponentsRecursively.js +17 -2
  58. package/dist/src/shared/copyComponentsRecursively.js.map +1 -1
  59. package/dist/src/shared/getDependenciesAndInstallNestedComponents.d.ts +18 -4
  60. package/dist/src/shared/getDependenciesAndInstallNestedComponents.d.ts.map +1 -1
  61. package/dist/src/shared/getDependenciesAndInstallNestedComponents.js +110 -40
  62. package/dist/src/shared/getDependenciesAndInstallNestedComponents.js.map +1 -1
  63. package/docs/components/data-views-config-panel.md +204 -0
  64. package/docs/components/data-views-layout.md +270 -0
  65. package/docs/components/form-stepper.md +244 -0
  66. package/docs/components/stepper.md +215 -0
  67. package/docs/components/timeline.md +248 -0
  68. package/package.json +6 -6
  69. package/apps/lib/components/Charts-dev.tsx +0 -365
  70. package/apps/lib/components/Command-dev.tsx +0 -151
  71. package/apps/lib/components/IosDatePicker-dev.tsx +0 -341
  72. /package/docs/components/{labeled-checkbox.md → labeled-check-box.md} +0 -0
  73. /package/docs/components/{tree-dropdown.md → tree-drop-down.md} +0 -0
@@ -0,0 +1,363 @@
1
+ "use client";
2
+
3
+ import { ChevronRight, ChevronDown, GripVertical } from "lucide-react";
4
+ import type { DragEvent } from "react";
5
+ import { cn } from "../../utils/cn";
6
+ import { resolveIcon } from "./icons";
7
+ import type { TreeFolderIconResolver, TreeFolderVisibleRow } from "./types";
8
+
9
+ export type TreeFolderRowDragHandlers = {
10
+ draggable: boolean;
11
+ onDragStart: (e: DragEvent<HTMLElement>) => void;
12
+ onDragEnd: (e: DragEvent<HTMLElement>) => void;
13
+ onDragOver: (e: DragEvent<HTMLElement>) => void;
14
+ onDragLeave: (e: DragEvent<HTMLElement>) => void;
15
+ onDrop: (e: DragEvent<HTMLElement>) => void;
16
+ };
17
+
18
+ export type TreeFolderRowProps = {
19
+ row: TreeFolderVisibleRow;
20
+ rowHeight: number;
21
+ indent: number;
22
+ iconFor?: TreeFolderIconResolver;
23
+
24
+ isSelected: boolean;
25
+ isAncestor: boolean;
26
+ isDescendantOfSelected: boolean;
27
+ /** True when the previous visible row is part of the same selected-subtree band. */
28
+ isPrevInBand: boolean;
29
+ /** True when the next visible row is part of the same selected-subtree band. */
30
+ isNextInBand: boolean;
31
+
32
+ isDragging: boolean;
33
+ isDropTargetInside: boolean;
34
+ isDropBefore: boolean;
35
+ isDropAfter: boolean;
36
+
37
+ dndEnabled: boolean;
38
+ onSelect: (id: string | null) => void;
39
+ onToggle: (id: string) => void;
40
+ dragHandlers: TreeFolderRowDragHandlers;
41
+ };
42
+
43
+ export function TreeFolderRow({
44
+ row,
45
+ rowHeight,
46
+ indent,
47
+ iconFor,
48
+ isSelected,
49
+ isAncestor,
50
+ isDescendantOfSelected,
51
+ isPrevInBand,
52
+ isNextInBand,
53
+ isDragging,
54
+ isDropTargetInside,
55
+ isDropBefore,
56
+ isDropAfter,
57
+ dndEnabled,
58
+ onSelect,
59
+ onToggle,
60
+ dragHandlers,
61
+ }: TreeFolderRowProps) {
62
+ const { node, level, isOpen, isInternal, ancestorHasMoreSiblings } = row;
63
+ const data = node;
64
+ const hasChildren = isInternal;
65
+
66
+ const willReceiveDrop = isDropTargetInside && dndEnabled;
67
+ const inSubtreeOfSelected = isDescendantOfSelected;
68
+ const inAncestorChain = isAncestor;
69
+ // Selected row gets the strong fill; direct children get a softer overlay so
70
+ // they read as "members of the selected group" without competing with the
71
+ // selection itself. Rows stand alone — no neighbor-aware joining anymore.
72
+ const showSelected = isSelected && !willReceiveDrop;
73
+ const showChildOfSelected =
74
+ inSubtreeOfSelected && !isSelected && !willReceiveDrop;
75
+ // A row is "in the selection group" if it's the selected node itself or a
76
+ // descendant tinted by it. Neighbor-aware rounding then merges adjacent rows
77
+ // into one continuous pill instead of stacked individual chips.
78
+ const inGroup = showSelected || showChildOfSelected;
79
+ const isGroupStart = inGroup && !isPrevInBand;
80
+ const isGroupEnd = inGroup && !isNextInBand;
81
+
82
+ const icon = resolveIcon(iconFor, data, {
83
+ isOpen,
84
+ isInternal,
85
+ isSelected,
86
+ });
87
+
88
+ const outerClassName = cn(
89
+ "relative w-full min-w-max",
90
+ isDragging && "opacity-40",
91
+ data.disabled && "opacity-50 pointer-events-none",
92
+ );
93
+
94
+ const bandClassName = cn(
95
+ "pointer-events-none absolute inset-y-0 inset-x-[2px] z-0 rounded-md transition-colors duration-100",
96
+ inGroup && !isGroupStart && "rounded-t-none",
97
+ inGroup && !isGroupEnd && "rounded-b-none",
98
+ !willReceiveDrop &&
99
+ !inGroup &&
100
+ "group-hover/row:bg-background-presentation-form-field-hover group-active/row:bg-background-presentation-action-hover/20",
101
+ showSelected && "bg-background-presentation-state-information-primary",
102
+ // Token isn't exposed as channels, so Tailwind's /alpha modifier doesn't
103
+ // work on it — paint the descendant tint with the same hex at 30% alpha.
104
+ showChildOfSelected && "bg-[#005ECC]/30",
105
+ inAncestorChain &&
106
+ !inGroup &&
107
+ "bg-background-presentation-state-information-secondary",
108
+ willReceiveDrop && "bg-background-presentation-state-information-primary",
109
+ );
110
+
111
+ const rowClassName = cn(
112
+ "relative z-10 flex items-center gap-1 py-1 pr-2 cursor-pointer text-sm min-w-max",
113
+ showSelected && "text-white",
114
+ willReceiveDrop && "text-white",
115
+ );
116
+
117
+ // Grip occupies a fixed slot at the outer-left edge. `contentStart` is where
118
+ // the indent-padding origin (depth 0) begins, leaving breathing room between
119
+ // the grip column and the first connector / chevron.
120
+ const gripSlotWidth = 16;
121
+ const gripGutterPad = 6;
122
+ const contentStart = gripSlotWidth + gripGutterPad; // 22px
123
+
124
+ const rowStyle = {
125
+ paddingInlineStart: contentStart + level * indent,
126
+ height: rowHeight,
127
+ };
128
+
129
+ const handleRowClick = () => {
130
+ if (data.disabled) return;
131
+ onSelect(node.id);
132
+ };
133
+
134
+ // Insert lines for "between" drops (above/below sibling). Inset to the row's
135
+ // indent so they line up with the new sibling's level.
136
+ const insertLineInset = contentStart + level * indent;
137
+
138
+ return (
139
+ <div
140
+ data-row-id={node.id}
141
+ className={cn("select-none group/row", outerClassName)}
142
+ style={{ height: rowHeight }}
143
+ {...dragHandlers}
144
+ >
145
+ <span aria-hidden className={bandClassName} />
146
+
147
+ {/* Drag handle sits in a fixed slot at the outer-left of the row so it
148
+ never collides with the connector verticals — those start to the
149
+ right of this 16px reserved column. Hidden until row hover. */}
150
+ {dndEnabled && (
151
+ <span
152
+ className="absolute start-1.5 top-0 z-[6] flex h-full w-4 items-center justify-center opacity-0 group-hover/row:opacity-100 transition-opacity duration-150 cursor-grab active:cursor-grabbing"
153
+ aria-hidden
154
+ >
155
+ <GripVertical
156
+ className={cn(
157
+ "w-3.5 h-3.5",
158
+ showSelected
159
+ ? "text-white/80"
160
+ : "text-content-presentation-global-tertiary",
161
+ )}
162
+ />
163
+ </span>
164
+ )}
165
+
166
+ {level > 0 && (
167
+ <div className="pointer-events-none absolute inset-0 z-[5]">
168
+ <TreeConnectors
169
+ level={level}
170
+ indent={indent}
171
+ rowHeight={rowHeight}
172
+ contentStart={contentStart}
173
+ ancestorHasMoreSiblings={ancestorHasMoreSiblings}
174
+ />
175
+ </div>
176
+ )}
177
+
178
+ {isDropBefore && (
179
+ <span
180
+ aria-hidden
181
+ className="pointer-events-none absolute -top-px left-0 right-0 z-20 h-0.5 bg-background-presentation-state-information-primary"
182
+ style={{ marginInlineStart: insertLineInset }}
183
+ />
184
+ )}
185
+ {isDropAfter && (
186
+ <span
187
+ aria-hidden
188
+ className="pointer-events-none absolute -bottom-px left-0 right-0 z-20 h-0.5 bg-background-presentation-state-information-primary"
189
+ style={{ marginInlineStart: insertLineInset }}
190
+ />
191
+ )}
192
+
193
+ <div
194
+ role="treeitem"
195
+ aria-expanded={isInternal ? isOpen : undefined}
196
+ aria-selected={isSelected}
197
+ aria-disabled={data.disabled || undefined}
198
+ aria-level={level + 1}
199
+ onClick={handleRowClick}
200
+ className={rowClassName}
201
+ style={rowStyle}
202
+ >
203
+ {hasChildren ? (
204
+ <button
205
+ type="button"
206
+ onClick={(e) => {
207
+ e.stopPropagation();
208
+ onToggle(node.id);
209
+ }}
210
+ className={cn(
211
+ "shrink-0 w-4 h-4 flex items-center justify-center rounded",
212
+ showSelected
213
+ ? "text-white/80 hover:text-white"
214
+ : "text-content-presentation-global-tertiary hover:text-content-presentation-global-primary",
215
+ )}
216
+ aria-label={isOpen ? "Collapse" : "Expand"}
217
+ >
218
+ {isOpen ? (
219
+ <ChevronDown className="w-3.5 h-3.5" />
220
+ ) : (
221
+ <ChevronRight className="w-3.5 h-3.5" />
222
+ )}
223
+ </button>
224
+ ) : (
225
+ <span className="shrink-0 w-4 h-4" />
226
+ )}
227
+
228
+ <span
229
+ className={cn(
230
+ "shrink-0 w-4 h-4 flex items-center justify-center",
231
+ showSelected
232
+ ? "text-white/90"
233
+ : "text-content-presentation-global-tertiary",
234
+ )}
235
+ aria-hidden
236
+ >
237
+ {icon}
238
+ </span>
239
+
240
+ <span className="whitespace-nowrap pr-2" title={data.name}>
241
+ {data.name}
242
+ </span>
243
+
244
+ {hasChildren && !isOpen && (
245
+ <span
246
+ className={cn(
247
+ "shrink-0 text-xs tabular-nums",
248
+ showSelected
249
+ ? "text-white/80"
250
+ : "text-content-presentation-global-tertiary",
251
+ )}
252
+ >
253
+ ({countDescendants(node)})
254
+ </span>
255
+ )}
256
+ </div>
257
+ </div>
258
+ );
259
+ }
260
+
261
+ /**
262
+ * Connector lines (T/L bends) aligned to where each ancestor's chevron sits in
263
+ * the row layout, so verticals drop directly from the parent's twisty rather
264
+ * than floating in the indent column. Geometry:
265
+ *
266
+ * row paddingLeft = 4 + level * indent
267
+ * row content order = [grip?(16)] [chevron(16)] [icon(16)] [text]
268
+ *
269
+ * So the chevron of a row at depth D is centered at:
270
+ * chevronX(D) = 4 + D * indent + (dndEnabled ? 16 : 0) + 8
271
+ *
272
+ * A child at depth D+1 hangs its vertical at chevronX(D) and runs a horizontal
273
+ * stub from there out to (paddingLeft - 2) — landing just before the child's
274
+ * own grip/chevron. Last child renders an "L" (vertical stops at midline);
275
+ * other siblings render a "T" (vertical runs full height). Ancestor gutters
276
+ * keep their verticals only when that ancestor still has siblings below.
277
+ */
278
+ function TreeConnectors({
279
+ level,
280
+ indent,
281
+ rowHeight,
282
+ contentStart,
283
+ ancestorHasMoreSiblings,
284
+ }: {
285
+ level: number;
286
+ indent: number;
287
+ rowHeight: number;
288
+ contentStart: number;
289
+ ancestorHasMoreSiblings: boolean[];
290
+ }) {
291
+ // Grip handle sits in a fixed outer-left slot, so the chevron column for a
292
+ // row at depth D is: contentStart + D*indent + chevron-half(8).
293
+ const chevronX = (depth: number) => contentStart + depth * indent + 8;
294
+ // Connector color is theme-aware: black/20 on default+light, white/20 on dark.
295
+ // The dark swap relies on a `data-theme="dark"` ancestor (set by TreeFolder).
296
+ const lineClass = "bg-black-alpha-20 [[data-theme=dark]_&]:bg-white-alpha-20";
297
+ const segments: React.ReactNode[] = [];
298
+
299
+ // Ancestor verticals: full-height line at each ancestor's chevron column,
300
+ // skipped when the ancestor at that depth has no more siblings below.
301
+ // ancestorHasMoreSiblings[d] === "ancestor at depth d still has siblings
302
+ // below this row," so it controls whether to draw the vertical in that
303
+ // ancestor's chevron gutter (chevronX(d)).
304
+ for (let d = 0; d < level - 1; d++) {
305
+ if (!ancestorHasMoreSiblings[d]) continue;
306
+ segments.push(
307
+ <span
308
+ key={`v-${d}`}
309
+ aria-hidden
310
+ className={cn("pointer-events-none absolute top-0 bottom-0 w-px", lineClass)}
311
+ style={{ insetInlineStart: chevronX(d) }}
312
+ />,
313
+ );
314
+ }
315
+
316
+ // Parent's gutter (depth = level - 1). T or L vertical, plus horizontal stub
317
+ // landing just before this row's own grip/chevron starts.
318
+ const parentDepth = level - 1;
319
+ const parentX = chevronX(parentDepth);
320
+ const midY = rowHeight / 2;
321
+ const ownContentX = contentStart + level * indent + 2; // extend 4px further toward the row content
322
+
323
+ segments.push(
324
+ <span
325
+ key="v-own"
326
+ aria-hidden
327
+ className={cn("pointer-events-none absolute w-px", lineClass)}
328
+ style={{
329
+ insetInlineStart: parentX,
330
+ top: 0,
331
+ height: "100%",
332
+ }}
333
+ />,
334
+ );
335
+ segments.push(
336
+ <span
337
+ key="h-own"
338
+ aria-hidden
339
+ className={cn("pointer-events-none absolute h-px", lineClass)}
340
+ style={{
341
+ insetInlineStart: parentX,
342
+ top: midY,
343
+ width: Math.max(4, ownContentX - parentX),
344
+ }}
345
+ />,
346
+ );
347
+
348
+ return <>{segments}</>;
349
+ }
350
+
351
+ function countDescendants(node: {
352
+ children?: { children?: any[] }[] | null;
353
+ }): number {
354
+ if (!node.children) return 0;
355
+ let n = 0;
356
+ const stack: any[] = [...node.children];
357
+ while (stack.length) {
358
+ n++;
359
+ const top = stack.pop();
360
+ if (top.children) for (const c of top.children) stack.push(c);
361
+ }
362
+ return n;
363
+ }
@@ -0,0 +1,60 @@
1
+ "use client"
2
+
3
+ /**
4
+ * Component-scoped CSS for TreeFolder. Rendered once by TreeFolder itself so
5
+ * the styles ship with the component (no globals.css required by consumers).
6
+ *
7
+ * Why a <style> tag instead of a CSS import:
8
+ * - Some consumers may not run a CSS pipeline that picks up imports from
9
+ * node_modules.
10
+ * - The styles target pseudo-elements (::-webkit-scrollbar-*) that Tailwind
11
+ * can't express without a plugin, and CSS-in-JS libraries would add a dep.
12
+ * - Inlining keeps the public package zero-config.
13
+ *
14
+ * The rules are scoped to `.tf-scroll`, which only TreeFolder applies — no
15
+ * risk of leaking to other elements on the page.
16
+ */
17
+ const CSS = `
18
+ .tf-scroll {
19
+ scrollbar-width: thin;
20
+ scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
21
+ }
22
+ .tf-scroll::-webkit-scrollbar {
23
+ width: 10px;
24
+ height: 10px;
25
+ }
26
+ .tf-scroll::-webkit-scrollbar-track {
27
+ background: transparent;
28
+ }
29
+ .tf-scroll::-webkit-scrollbar-thumb {
30
+ background: rgba(255, 255, 255, 0.18);
31
+ border: 2px solid transparent;
32
+ background-clip: content-box;
33
+ border-radius: 999px;
34
+ }
35
+ .tf-scroll::-webkit-scrollbar-thumb:hover {
36
+ background: rgba(255, 255, 255, 0.3);
37
+ background-clip: content-box;
38
+ }
39
+ .tf-scroll::-webkit-scrollbar-corner {
40
+ background: transparent;
41
+ }
42
+ [data-theme="light"] .tf-scroll::-webkit-scrollbar-thumb {
43
+ background: rgba(0, 0, 0, 0.22);
44
+ background-clip: content-box;
45
+ }
46
+ [data-theme="light"] .tf-scroll::-webkit-scrollbar-thumb:hover {
47
+ background: rgba(0, 0, 0, 0.35);
48
+ background-clip: content-box;
49
+ }
50
+ `
51
+
52
+ /**
53
+ * Renders a single <style> tag containing TreeFolder's scoped CSS.
54
+ * Mounting multiple TreeFolder instances is fine — browsers deduplicate
55
+ * identical inline stylesheets; we additionally key on a known id so React
56
+ * keeps just one DOM node when it can.
57
+ */
58
+ export function TreeFolderStyles() {
59
+ return <style id="torch-treefolder-styles" dangerouslySetInnerHTML={{ __html: CSS }} />
60
+ }
@@ -0,0 +1,63 @@
1
+ "use client"
2
+
3
+ import {
4
+ Folder,
5
+ FolderOpen,
6
+ File,
7
+ Frame,
8
+ Group,
9
+ Component,
10
+ Box,
11
+ Type,
12
+ Image as ImageIcon,
13
+ Spline,
14
+ Link as LinkIcon,
15
+ Square,
16
+ LayoutGrid,
17
+ } from "lucide-react"
18
+ import type { ReactNode } from "react"
19
+ import type { TreeFolderIconResolver, TreeFolderNode } from "./types"
20
+
21
+ export const defaultIconRegistry: Record<
22
+ string,
23
+ (state: { isOpen: boolean }) => ReactNode
24
+ > = {
25
+ folder: ({ isOpen }) =>
26
+ isOpen ? <FolderOpen className="w-3.5 h-3.5" /> : <Folder className="w-3.5 h-3.5" />,
27
+ file: () => <File className="w-3.5 h-3.5" />,
28
+ frame: () => <Frame className="w-3.5 h-3.5" />,
29
+ group: () => <Group className="w-3.5 h-3.5" />,
30
+ component: () => <Component className="w-3.5 h-3.5" />,
31
+ instance: () => <Box className="w-3.5 h-3.5" />,
32
+ text: () => <Type className="w-3.5 h-3.5" />,
33
+ image: () => <ImageIcon className="w-3.5 h-3.5" />,
34
+ vector: () => <Spline className="w-3.5 h-3.5" />,
35
+ link: () => <LinkIcon className="w-3.5 h-3.5" />,
36
+ section: () => <LayoutGrid className="w-3.5 h-3.5" />,
37
+ container: () => <Square className="w-3.5 h-3.5" />,
38
+ }
39
+
40
+ export function defaultIconFor(
41
+ node: TreeFolderNode,
42
+ state: { isOpen: boolean; isInternal: boolean; isSelected: boolean },
43
+ ): ReactNode {
44
+ if (node.icon !== undefined) return node.icon
45
+ const type = node.type ?? (state.isInternal ? "folder" : "file")
46
+ const resolver = defaultIconRegistry[type]
47
+ if (resolver) return resolver({ isOpen: state.isOpen })
48
+ return state.isInternal
49
+ ? defaultIconRegistry.folder({ isOpen: state.isOpen })
50
+ : defaultIconRegistry.file({ isOpen: state.isOpen })
51
+ }
52
+
53
+ export function resolveIcon(
54
+ iconFor: TreeFolderIconResolver | undefined,
55
+ node: TreeFolderNode,
56
+ state: { isOpen: boolean; isInternal: boolean; isSelected: boolean },
57
+ ): ReactNode {
58
+ if (iconFor) {
59
+ const custom = iconFor(node, state)
60
+ if (custom !== undefined && custom !== null) return custom
61
+ }
62
+ return defaultIconFor(node, state)
63
+ }
@@ -0,0 +1,17 @@
1
+ export { TreeFolder } from "./TreeFolder"
2
+ export type { TreeFolderHandle, TreeFolderProps } from "./TreeFolder"
3
+ export { TreeFolderRow } from "./TreeFolderRow"
4
+ export type { TreeFolderRowProps, TreeFolderRowDragHandlers } from "./TreeFolderRow"
5
+ export { TreeFolderBreadcrumb } from "./TreeFolderBreadcrumb"
6
+ export { defaultIconRegistry, defaultIconFor } from "./icons"
7
+ export { applyMove, findPath, findNode, isAncestor, descendantIds, toBreadcrumb } from "./treeFolderUtils"
8
+ export type {
9
+ TreeFolderNode,
10
+ TreeFolderNodeType,
11
+ TreeFolderMoveArgs,
12
+ TreeFolderIconResolver,
13
+ TreeFolderVisibleRow,
14
+ TreeFolderDropPosition,
15
+ TreeFolderDropTarget,
16
+ TreeFolderBreadcrumb as TreeFolderBreadcrumbItems,
17
+ } from "./types"
@@ -0,0 +1,114 @@
1
+ import type { TreeFolderBreadcrumb, TreeFolderMoveArgs, TreeFolderNode } from "./types"
2
+
3
+ export function findPath(roots: TreeFolderNode[], id: string): TreeFolderNode[] | null {
4
+ for (const root of roots) {
5
+ const trail = walkPath(root, id, [])
6
+ if (trail) return trail
7
+ }
8
+ return null
9
+ }
10
+
11
+ function walkPath(
12
+ node: TreeFolderNode,
13
+ id: string,
14
+ trail: TreeFolderNode[],
15
+ ): TreeFolderNode[] | null {
16
+ const next = [...trail, node]
17
+ if (node.id === id) return next
18
+ if (node.children) {
19
+ for (const c of node.children) {
20
+ const found = walkPath(c, id, next)
21
+ if (found) return found
22
+ }
23
+ }
24
+ return null
25
+ }
26
+
27
+ export function toBreadcrumb(path: TreeFolderNode[]): TreeFolderBreadcrumb {
28
+ return path.map((n) => ({ id: n.id, name: n.name }))
29
+ }
30
+
31
+ export function findNode(roots: TreeFolderNode[], id: string): TreeFolderNode | null {
32
+ for (const root of roots) {
33
+ if (root.id === id) return root
34
+ if (root.children) {
35
+ const inChild = findNode(root.children, id)
36
+ if (inChild) return inChild
37
+ }
38
+ }
39
+ return null
40
+ }
41
+
42
+ export function isAncestor(parent: TreeFolderNode, id: string): boolean {
43
+ if (parent.id === id) return true
44
+ if (!parent.children) return false
45
+ for (const c of parent.children) {
46
+ if (isAncestor(c, id)) return true
47
+ }
48
+ return false
49
+ }
50
+
51
+ export function descendantIds(node: TreeFolderNode, includeSelf = false): Set<string> {
52
+ const out = new Set<string>()
53
+ if (includeSelf) out.add(node.id)
54
+ const walk = (n: TreeFolderNode) => {
55
+ if (!n.children) return
56
+ for (const c of n.children) {
57
+ out.add(c.id)
58
+ walk(c)
59
+ }
60
+ }
61
+ walk(node)
62
+ return out
63
+ }
64
+
65
+ export function applyMove(
66
+ roots: TreeFolderNode[],
67
+ args: TreeFolderMoveArgs,
68
+ ): TreeFolderNode[] {
69
+ const dragSet = new Set(args.dragIds)
70
+
71
+ if (args.parentId) {
72
+ for (const id of args.dragIds) {
73
+ const node = findNode(roots, id)
74
+ if (node && isAncestor(node, args.parentId)) return roots
75
+ }
76
+ }
77
+
78
+ const extracted: TreeFolderNode[] = []
79
+
80
+ const extract = (records: TreeFolderNode[]): TreeFolderNode[] =>
81
+ records
82
+ .map((n) => {
83
+ const next: TreeFolderNode = n.children
84
+ ? { ...n, children: extract(n.children) }
85
+ : n
86
+ if (dragSet.has(n.id)) {
87
+ extracted.push(next)
88
+ return null
89
+ }
90
+ return next
91
+ })
92
+ .filter((n): n is TreeFolderNode => n !== null)
93
+
94
+ const pruned = extract(roots)
95
+
96
+ if (args.parentId == null) {
97
+ const at = Math.max(0, Math.min(args.index, pruned.length))
98
+ return [...pruned.slice(0, at), ...extracted, ...pruned.slice(at)]
99
+ }
100
+
101
+ const insert = (records: TreeFolderNode[]): TreeFolderNode[] =>
102
+ records.map((n) => {
103
+ if (n.id === args.parentId) {
104
+ const kids = n.children ? [...n.children] : []
105
+ const at = Math.max(0, Math.min(args.index, kids.length))
106
+ kids.splice(at, 0, ...extracted)
107
+ return { ...n, children: kids }
108
+ }
109
+ if (n.children) return { ...n, children: insert(n.children) }
110
+ return n
111
+ })
112
+
113
+ return insert(pruned)
114
+ }
@@ -0,0 +1,77 @@
1
+ import type { ReactNode } from "react"
2
+
3
+ export type TreeFolderNodeType =
4
+ | "folder"
5
+ | "file"
6
+ | "frame"
7
+ | "group"
8
+ | "component"
9
+ | "instance"
10
+ | "text"
11
+ | "image"
12
+ | "vector"
13
+ | "link"
14
+ | "section"
15
+ | "container"
16
+ | (string & {})
17
+
18
+ export type TreeFolderNode = {
19
+ id: string
20
+ name: string
21
+ type?: TreeFolderNodeType
22
+ icon?: ReactNode
23
+ meta?: ReactNode
24
+ disabled?: boolean
25
+ draggable?: boolean
26
+ droppable?: boolean
27
+ data?: unknown
28
+ children?: TreeFolderNode[]
29
+ }
30
+
31
+ export type TreeFolderMoveArgs = {
32
+ dragIds: string[]
33
+ parentId: string | null
34
+ index: number
35
+ }
36
+
37
+ export type TreeFolderIconResolver = (
38
+ node: TreeFolderNode,
39
+ state: { isOpen: boolean; isInternal: boolean; isSelected: boolean },
40
+ ) => ReactNode
41
+
42
+ export type TreeFolderBreadcrumb = Array<{ id: string; name: string }>
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // Visible-row + DnD primitives.
46
+ // ---------------------------------------------------------------------------
47
+
48
+ export type TreeFolderVisibleRow = {
49
+ node: TreeFolderNode
50
+ level: number
51
+ /** Resolved parent id (null for roots). */
52
+ parentId: string | null
53
+ /** Position of this node among its siblings in the source tree (0-based). */
54
+ childIndex: number
55
+ /** True when the node is internal AND open. */
56
+ isOpen: boolean
57
+ /** True for internal nodes (any node with non-empty children). */
58
+ isInternal: boolean
59
+ /**
60
+ * Connector-line geometry. `isLastChild` toggles the row's own bend between L
61
+ * (last) and T (non-last). `ancestorHasMoreSiblings[d]` is true when the
62
+ * ancestor at depth `d` still has visible siblings after this row — that
63
+ * controls whether the vertical guide renders in that ancestor's gutter.
64
+ * Length === `level`.
65
+ */
66
+ isLastChild: boolean
67
+ ancestorHasMoreSiblings: boolean[]
68
+ }
69
+
70
+ /** Where the pointer is dropping relative to the hovered row. */
71
+ export type TreeFolderDropPosition = "before" | "after" | "inside"
72
+
73
+ export type TreeFolderDropTarget = {
74
+ /** The id whose `parentId` the drop will resolve to. */
75
+ rowId: string
76
+ position: TreeFolderDropPosition
77
+ }