@exegia/corpora-ui 0.19.0 → 0.21.0

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 (162) hide show
  1. package/dist-lib/components/blocks/auth/__tests__/auth-state-atom.test.d.ts +1 -0
  2. package/dist-lib/components/blocks/auth/auth-flow-atom.d.ts +77 -0
  3. package/dist-lib/components/blocks/auth/auth-session-atom.d.ts +28 -0
  4. package/dist-lib/components/blocks/auth/auth-state-type.d.ts +71 -0
  5. package/dist-lib/components/blocks/auth/auth-state.d.ts +9 -0
  6. package/dist-lib/components/blocks/auth/use-auth-state.d.ts +52 -0
  7. package/dist-lib/components/blocks/nav/sidebar/__tests__/ai-sidebar-atom.test.d.ts +1 -0
  8. package/dist-lib/components/blocks/nav/sidebar/__tests__/use-ai-sidebar.test.d.ts +1 -0
  9. package/dist-lib/components/blocks/nav/sidebar/ai-sidebar-atom.d.ts +217 -0
  10. package/dist-lib/components/blocks/nav/sidebar/ai-sidebar.d.ts +11 -2
  11. package/dist-lib/components/blocks/nav/sidebar/index.d.ts +3 -1
  12. package/dist-lib/components/blocks/nav/sidebar/sidebar-context.d.ts +3 -0
  13. package/dist-lib/components/blocks/nav/sidebar/sidebar-row.d.ts +3 -1
  14. package/dist-lib/components/blocks/nav/sidebar/type.d.ts +191 -29
  15. package/dist-lib/components/blocks/nav/sidebar/use-ai-sidebar-state.d.ts +29 -0
  16. package/dist-lib/components/blocks/nav/sidebar/use-ai-sidebar.d.ts +27 -31
  17. package/dist-lib/components/blocks/nav/sidebar/utils.d.ts +6 -1
  18. package/dist-lib/components/blocks/profile/index.d.ts +7 -0
  19. package/dist-lib/components/blocks/profile/profile-card-atom.d.ts +68 -0
  20. package/dist-lib/components/blocks/profile/profile-card-block.d.ts +28 -39
  21. package/dist-lib/components/blocks/profile/type.d.ts +80 -0
  22. package/dist-lib/components/blocks/profile/use-profile-card-state.d.ts +24 -0
  23. package/dist-lib/components/blocks/profile/use-profile-card.d.ts +34 -0
  24. package/dist-lib/components/blocks/scaffold/constants.d.ts +5 -0
  25. package/dist-lib/components/blocks/scaffold/scaffold-canvas.d.ts +2 -0
  26. package/dist-lib/components/blocks/scaffold/scaffold-panel.d.ts +1 -1
  27. package/dist-lib/components/blocks/scaffold/scaffold-tab.d.ts +6 -1
  28. package/dist-lib/components/blocks/scaffold/type.d.ts +28 -0
  29. package/dist-lib/components/blocks/scaffold/use-panel-visibility.d.ts +19 -0
  30. package/dist-lib/components/blocks/scaffold/utils.d.ts +6 -0
  31. package/dist-lib/components/blocks/shell/__tests__/shell-fit-atom.test.d.ts +1 -0
  32. package/dist-lib/components/blocks/shell/__tests__/shell-metrics.test.d.ts +1 -0
  33. package/dist-lib/components/blocks/shell/animated-panel-provider.d.ts +1 -1
  34. package/dist-lib/components/blocks/shell/animated-panel.d.ts +1 -1
  35. package/dist-lib/components/blocks/shell/index.d.ts +4 -0
  36. package/dist-lib/components/blocks/shell/shell-fit-atom.d.ts +68 -0
  37. package/dist-lib/components/blocks/shell/shell-metrics.d.ts +54 -0
  38. package/dist-lib/components/blocks/shell/type.d.ts +115 -3
  39. package/dist-lib/components/blocks/shell/use-shell-fit-state.d.ts +25 -0
  40. package/dist-lib/components/blocks/shell/use-shell-fit.d.ts +14 -0
  41. package/dist-lib/components/blocks/shell/use-shell-panels.d.ts +12 -1
  42. package/dist-lib/components/blocks/shell/utils.d.ts +32 -0
  43. package/dist-lib/components/composed/tree/__tests__/tree-atom.test.d.ts +1 -0
  44. package/dist-lib/components/composed/tree/__tests__/tree.test.d.ts +1 -0
  45. package/dist-lib/components/composed/tree/__tests__/use-tree.test.d.ts +1 -0
  46. package/dist-lib/components/composed/tree/constants.d.ts +35 -0
  47. package/dist-lib/components/composed/tree/index.d.ts +6 -0
  48. package/dist-lib/components/composed/tree/tree-atom.d.ts +160 -0
  49. package/dist-lib/components/composed/tree/tree-context.d.ts +4 -0
  50. package/dist-lib/components/composed/tree/tree-node.d.ts +12 -0
  51. package/dist-lib/components/composed/tree/tree.d.ts +19 -0
  52. package/dist-lib/components/composed/tree/type.d.ts +290 -0
  53. package/dist-lib/components/composed/tree/use-tree-dnd.d.ts +18 -0
  54. package/dist-lib/components/composed/tree/use-tree-state.d.ts +25 -0
  55. package/dist-lib/components/composed/tree/use-tree.d.ts +24 -0
  56. package/dist-lib/components/composed/tree/utils.d.ts +28 -0
  57. package/dist-lib/components/composed/user-avatar.d.ts +5 -28
  58. package/dist-lib/components/user-avatar/__tests__/user-avatar.test.d.ts +1 -0
  59. package/dist-lib/components/user-avatar/audio-wave.d.ts +2 -0
  60. package/dist-lib/components/user-avatar/component.d.ts +3 -0
  61. package/dist-lib/components/user-avatar/fallback.d.ts +5 -0
  62. package/dist-lib/components/user-avatar/index.d.ts +18 -0
  63. package/dist-lib/components/user-avatar/presence-badge.d.ts +16 -0
  64. package/dist-lib/components/user-avatar/type.d.ts +71 -0
  65. package/dist-lib/components/user-avatar/use-user-avatar-state.d.ts +26 -0
  66. package/dist-lib/components/user-avatar/use-user-avatar.d.ts +32 -0
  67. package/dist-lib/components/user-avatar/user-avatar-atom.d.ts +55 -0
  68. package/dist-lib/components/user-avatar/utils.d.ts +2 -0
  69. package/dist-lib/index.d.ts +6 -2
  70. package/dist-lib/index.js +3576 -1366
  71. package/dist-lib/index.js.map +1 -1
  72. package/dist-lib/state/exegia-provider.d.ts +51 -0
  73. package/dist-lib/state/index.d.ts +4 -0
  74. package/dist-lib/state/store.d.ts +17 -0
  75. package/package.json +16 -12
  76. package/src/components/beste/piece/browser-frame.tsx +9 -6
  77. package/src/components/blocks/auth/__tests__/auth-state-atom.test.tsx +247 -0
  78. package/src/components/blocks/auth/auth-flow-atom.ts +238 -0
  79. package/src/components/blocks/auth/auth-session-atom.ts +82 -0
  80. package/src/components/blocks/auth/auth-state-type.ts +97 -0
  81. package/src/components/blocks/auth/auth-state.ts +52 -0
  82. package/src/components/blocks/auth/use-auth-state.ts +127 -0
  83. package/src/components/blocks/nav/sidebar/__tests__/ai-sidebar-atom.test.tsx +325 -0
  84. package/src/components/blocks/nav/sidebar/__tests__/use-ai-sidebar.test.tsx +310 -0
  85. package/src/components/blocks/nav/sidebar/ai-sidebar-atom.ts +698 -0
  86. package/src/components/blocks/nav/sidebar/ai-sidebar.tsx +112 -76
  87. package/src/components/blocks/nav/sidebar/index.ts +53 -1
  88. package/src/components/blocks/nav/sidebar/sidebar-context.ts +15 -0
  89. package/src/components/blocks/nav/sidebar/sidebar-row.tsx +119 -50
  90. package/src/components/blocks/nav/sidebar/type.ts +228 -29
  91. package/src/components/blocks/nav/sidebar/use-ai-sidebar-state.ts +123 -0
  92. package/src/components/blocks/nav/sidebar/use-ai-sidebar.ts +455 -225
  93. package/src/components/blocks/nav/sidebar/utils.ts +29 -1
  94. package/src/components/blocks/profile/__tests__/profile-card-block.test.tsx +194 -0
  95. package/src/components/blocks/profile/index.ts +28 -0
  96. package/src/components/blocks/profile/profile-card-atom.ts +247 -0
  97. package/src/components/blocks/profile/profile-card-block.tsx +153 -74
  98. package/src/components/blocks/profile/type.ts +95 -0
  99. package/src/components/blocks/profile/use-profile-card-state.ts +67 -0
  100. package/src/components/blocks/profile/use-profile-card.ts +126 -0
  101. package/src/components/blocks/scaffold/__tests__/scaffold.test.tsx +160 -1
  102. package/src/components/blocks/scaffold/constants.ts +7 -0
  103. package/src/components/blocks/scaffold/panel-menu-button.tsx +3 -0
  104. package/src/components/blocks/scaffold/scaffold-canvas.tsx +48 -5
  105. package/src/components/blocks/scaffold/scaffold-inspector.tsx +3 -2
  106. package/src/components/blocks/scaffold/scaffold-main.tsx +1 -1
  107. package/src/components/blocks/scaffold/scaffold-panel.tsx +41 -5
  108. package/src/components/blocks/scaffold/scaffold-root.tsx +18 -3
  109. package/src/components/blocks/scaffold/scaffold-sidebar.tsx +1 -1
  110. package/src/components/blocks/scaffold/scaffold-tab.tsx +45 -7
  111. package/src/components/blocks/scaffold/type.ts +28 -0
  112. package/src/components/blocks/scaffold/use-panel-visibility.ts +180 -0
  113. package/src/components/blocks/scaffold/utils.ts +19 -0
  114. package/src/components/blocks/shell/__tests__/shell-fit-atom.test.tsx +360 -0
  115. package/src/components/blocks/shell/__tests__/shell-layout.test.tsx +192 -3
  116. package/src/components/blocks/shell/__tests__/shell-metrics.test.ts +108 -0
  117. package/src/components/blocks/shell/animated-panel-inset.tsx +5 -1
  118. package/src/components/blocks/shell/animated-panel-provider.tsx +77 -11
  119. package/src/components/blocks/shell/animated-panel-trigger.tsx +4 -0
  120. package/src/components/blocks/shell/animated-panel.tsx +126 -29
  121. package/src/components/blocks/shell/index.ts +18 -0
  122. package/src/components/blocks/shell/shell-fit-atom.ts +243 -0
  123. package/src/components/blocks/shell/shell-layout.tsx +55 -53
  124. package/src/components/blocks/shell/shell-metrics.ts +79 -0
  125. package/src/components/blocks/shell/type.ts +130 -3
  126. package/src/components/blocks/shell/use-shell-fit-state.ts +49 -0
  127. package/src/components/blocks/shell/use-shell-fit.ts +135 -0
  128. package/src/components/blocks/shell/use-shell-panels.ts +57 -4
  129. package/src/components/blocks/shell/utils.ts +44 -4
  130. package/src/components/composed/tree/CLAUDE.md +132 -0
  131. package/src/components/composed/tree/__tests__/tree-atom.test.tsx +217 -0
  132. package/src/components/composed/tree/__tests__/tree.test.tsx +525 -0
  133. package/src/components/composed/tree/__tests__/use-tree.test.tsx +333 -0
  134. package/src/components/composed/tree/constants.ts +60 -0
  135. package/src/components/composed/tree/index.ts +51 -0
  136. package/src/components/composed/tree/tree-atom.ts +590 -0
  137. package/src/components/composed/tree/tree-context.ts +13 -0
  138. package/src/components/composed/tree/tree-node.tsx +490 -0
  139. package/src/components/composed/tree/tree.tsx +286 -0
  140. package/src/components/composed/tree/type.ts +322 -0
  141. package/src/components/composed/tree/use-tree-dnd.ts +179 -0
  142. package/src/components/composed/tree/use-tree-state.ts +105 -0
  143. package/src/components/composed/tree/use-tree.ts +307 -0
  144. package/src/components/composed/tree/utils.ts +172 -0
  145. package/src/components/composed/user-avatar.tsx +11 -96
  146. package/src/components/docs/component-preview.tsx +11 -11
  147. package/src/components/user-avatar/__tests__/user-avatar.test.tsx +245 -0
  148. package/src/components/user-avatar/audio-wave.tsx +22 -0
  149. package/src/components/user-avatar/component.tsx +152 -0
  150. package/src/components/user-avatar/fallback.tsx +26 -0
  151. package/src/components/user-avatar/index.ts +34 -0
  152. package/src/components/user-avatar/presence-badge.tsx +56 -0
  153. package/src/components/user-avatar/type.ts +85 -0
  154. package/src/components/user-avatar/use-user-avatar-state.ts +60 -0
  155. package/src/components/user-avatar/use-user-avatar.ts +144 -0
  156. package/src/components/user-avatar/user-avatar-atom.ts +198 -0
  157. package/src/components/user-avatar/utils.ts +8 -0
  158. package/src/index.css +211 -153
  159. package/src/index.ts +35 -2
  160. package/src/state/exegia-provider.tsx +79 -0
  161. package/src/state/index.ts +4 -0
  162. package/src/state/store.ts +19 -0
@@ -0,0 +1,286 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+
5
+ import { cn } from "@/lib/utils"
6
+ import { TooltipProvider } from "@/components/ui/tooltip"
7
+ import { TreeContext } from "./tree-context"
8
+ import { TreeRow } from "./tree-node"
9
+ import type {
10
+ TreeContextValue,
11
+ TreeController,
12
+ TreeDataProps,
13
+ TreeNode,
14
+ TreeProps,
15
+ } from "./type"
16
+ import {
17
+ RAIL_COLLAPSED_WIDTH,
18
+ TREE_COLLAPSE_DURATION,
19
+ TREE_EASE,
20
+ } from "./constants"
21
+ import { useTree } from "./use-tree"
22
+ import { useTreeDndHandlers } from "./use-tree-dnd"
23
+ import { motion, useReducedMotion } from "motion/react"
24
+
25
+ const DEFAULT_LABELS: Record<TreeController["variant"], string> = {
26
+ navigation: "Main",
27
+ toc: "On this page",
28
+ sidebar: "Main",
29
+ files: "Files",
30
+ }
31
+
32
+ /**
33
+ * A nested item tree with four shapes: `navigation` (app nav — 3-level
34
+ * data promotes the top level to collapsible section names), `toc`
35
+ * (routes on top, in-page `#` anchors at the leaves), `sidebar` (a
36
+ * single-level rail that collapses to icons) and `files` (a compact file
37
+ * explorer with rename, drag-and-drop and trailing row actions).
38
+ *
39
+ * Two ways to drive it. Data-driven like the sidebar block: pass `items`,
40
+ * wire `onNavigate` into your router. Or hold a `useTree` controller and
41
+ * pass it as `tree` — same rendering, but expand, collapse, select, rename
42
+ * and reorder are then callable from anywhere in your app.
43
+ *
44
+ * Branches expand with a soft height morph and row stagger; the rail's
45
+ * labels fold away when `collapsed` flips. Arrow keys walk visible rows,
46
+ * expand and collapse; F2 renames in `files`.
47
+ */
48
+ export function Tree(props: TreeProps): React.ReactElement {
49
+ // Two components rather than one: hooks may not be called conditionally,
50
+ // and the controller form has no props to build a fallback controller
51
+ // from. Nobody switches a tree between the two forms at runtime.
52
+ return props.tree ? (
53
+ <TreeView
54
+ ariaLabel={props.ariaLabel}
55
+ className={props.className}
56
+ renderTrailing={props.renderTrailing}
57
+ tree={props.tree}
58
+ />
59
+ ) : (
60
+ <UncontrolledTree {...props} />
61
+ )
62
+ }
63
+
64
+ /** The props form: builds its own controller and renders through it. */
65
+ function UncontrolledTree(props: TreeDataProps): React.ReactElement {
66
+ const {
67
+ variant,
68
+ items,
69
+ activeId,
70
+ onNavigate,
71
+ sound = true,
72
+ ariaLabel,
73
+ className,
74
+ } = props
75
+ const tree = useTree({
76
+ treeId: props.treeId,
77
+ variant,
78
+ items,
79
+ activeId,
80
+ onNavigate,
81
+ sound,
82
+ collapsed: variant === "sidebar" ? (props.collapsed ?? false) : undefined,
83
+ onMove: variant === "files" ? props.onMove : undefined,
84
+ onRename: variant === "files" ? props.onRename : undefined,
85
+ })
86
+ return (
87
+ <TreeView
88
+ ariaLabel={ariaLabel}
89
+ className={className}
90
+ renderTrailing={variant === "files" ? props.renderTrailing : undefined}
91
+ tree={tree}
92
+ />
93
+ )
94
+ }
95
+
96
+ interface TreeViewProps {
97
+ tree: TreeController
98
+ ariaLabel?: string
99
+ className?: string
100
+ renderTrailing?: (node: TreeNode) => React.ReactNode
101
+ }
102
+
103
+ /** Rendering only — every piece of state and behaviour lives on `tree`. */
104
+ function TreeView({
105
+ tree,
106
+ ariaLabel,
107
+ className,
108
+ renderTrailing,
109
+ }: TreeViewProps): React.ReactElement {
110
+ const { variant, items, sectioned, collapsed } = tree
111
+ const rootRef = React.useRef<HTMLUListElement>(null)
112
+ const reduce = useReducedMotion()
113
+
114
+ // The rail animates between two px widths, so the expanded one has to be
115
+ // measured off the container. Watched rather than read once: the rail is
116
+ // as wide as whatever holds it, which can change on resize.
117
+ //
118
+ // A container with no width of its own sizes to the list — so it shrinks
119
+ // around the 40px rail once collapsed and follows the list mid-tween. Both
120
+ // would feed the list's own width back in as its target and pin it. So:
121
+ // no observing while collapsed (the last expanded width stands), and each
122
+ // sample is taken with the list's inline width cleared, so the classes'
123
+ // resting width — not the tween — is what the container is holding.
124
+ const railRef = React.useRef<HTMLElement>(null)
125
+ const [railWidth, setRailWidth] = React.useState<number | null>(null)
126
+ React.useLayoutEffect(() => {
127
+ const container = railRef.current
128
+ const list = rootRef.current
129
+ if (variant !== "sidebar" || collapsed || !container || !list) return
130
+ const measure = () => {
131
+ const inline = list.style.width
132
+ list.style.width = ""
133
+ const width = container.clientWidth
134
+ list.style.width = inline
135
+ setRailWidth(width)
136
+ }
137
+ measure()
138
+ const observer = new ResizeObserver(measure)
139
+ observer.observe(container)
140
+ return () => observer.disconnect()
141
+ }, [variant, collapsed])
142
+
143
+ // Until the measurement lands the className carries the width, so the
144
+ // rail is never wrong — just not animated on its very first frame.
145
+ const railTarget =
146
+ variant === "sidebar" && railWidth !== null
147
+ ? { width: collapsed ? RAIL_COLLAPSED_WIDTH : railWidth }
148
+ : undefined
149
+
150
+ // Roving over the DOM rather than a parallel model: collapsed branches
151
+ // unmount, so a row query is exactly the visible set in order.
152
+ function handleKeyDown(event: React.KeyboardEvent) {
153
+ const row = (event.target as HTMLElement).closest<HTMLElement>(
154
+ '[data-slot="tree-row"]'
155
+ )
156
+ const root = rootRef.current
157
+ if (!row || !root) return
158
+ const rows = Array.from(
159
+ root.querySelectorAll<HTMLElement>('[data-slot="tree-row"]')
160
+ )
161
+ const index = rows.indexOf(row)
162
+ if (index < 0) return
163
+ const id = row.getAttribute("data-id")
164
+ const branch = row.hasAttribute("data-branch")
165
+ const open = row.hasAttribute("data-expanded")
166
+
167
+ const focusRow = (target: HTMLElement | undefined) => {
168
+ if (!target) return
169
+ event.preventDefault()
170
+ target.focus()
171
+ }
172
+
173
+ switch (event.key) {
174
+ case "ArrowDown":
175
+ focusRow(rows[index + 1])
176
+ break
177
+ case "ArrowUp":
178
+ focusRow(rows[index - 1])
179
+ break
180
+ case "Home":
181
+ focusRow(rows[0])
182
+ break
183
+ case "End":
184
+ focusRow(rows[rows.length - 1])
185
+ break
186
+ case "ArrowRight":
187
+ if (branch && !open && id) {
188
+ event.preventDefault()
189
+ tree.expand(id)
190
+ } else if (open) {
191
+ focusRow(rows[index + 1])
192
+ }
193
+ break
194
+ case "ArrowLeft":
195
+ if (open && id) {
196
+ event.preventDefault()
197
+ tree.collapse(id)
198
+ } else {
199
+ focusRow(
200
+ row
201
+ .closest("li")
202
+ ?.parentElement?.closest("li")
203
+ ?.querySelector<HTMLElement>('[data-slot="tree-row"]') ??
204
+ undefined
205
+ )
206
+ }
207
+ break
208
+ case "F2":
209
+ if (id && tree.canRename) {
210
+ event.preventDefault()
211
+ tree.startRename(id)
212
+ }
213
+ break
214
+ }
215
+ }
216
+
217
+ // Rows take the tree's id and the stable drag handlers, never the
218
+ // controller — so this value keeps its identity and a toggle re-renders only
219
+ // the rows whose own atoms changed.
220
+ const dnd = useTreeDndHandlers(tree.treeId)
221
+ const context = React.useMemo<TreeContextValue>(
222
+ () => ({ treeId: tree.treeId, renderTrailing, dnd }),
223
+ [tree.treeId, renderTrailing, dnd]
224
+ )
225
+
226
+ const label = ariaLabel ?? DEFAULT_LABELS[variant]
227
+ let rendered = (
228
+ <motion.ul
229
+ aria-label={label}
230
+ className={cn(
231
+ "flex h-full min-w-0 flex-col",
232
+ // Resting widths, and the fallback until the rail is measured. The
233
+ // motion target below overrides these with an inline width.
234
+ collapsed ? "w-10 gap-y-2!" : "w-full",
235
+ variant === "files" ? "gap-px" : sectioned ? "gap-3" : "gap-0.5"
236
+ )}
237
+ // Both endpoints must be px: motion cannot interpolate a number
238
+ // against "100%", and animating 40 -> "100%" pinned the inline width
239
+ // at 40px, so the rail collapsed once and never reopened. The
240
+ // expanded target is the rail's own measured width.
241
+ initial={false}
242
+ animate={railTarget}
243
+ transition={
244
+ reduce
245
+ ? { duration: 0 }
246
+ : { duration: TREE_COLLAPSE_DURATION, ease: TREE_EASE }
247
+ }
248
+ data-collapsed={collapsed ? "" : undefined}
249
+ data-slot="tree"
250
+ data-variant={variant}
251
+ onKeyDown={handleKeyDown}
252
+ ref={rootRef}
253
+ role="tree"
254
+ >
255
+ {items.map((node) => (
256
+ <TreeRow depth={0} key={node.id} node={node} />
257
+ ))}
258
+ </motion.ul>
259
+ )
260
+
261
+ // The collapsed rail's rows carry tooltips — one provider groups their
262
+ // open state and drops the hover delay across the rail. Mounted for the
263
+ // expanded rail too: toggling the wrapper with `collapsed` would remount
264
+ // every row and skip the label fold animation.
265
+ if (variant === "sidebar")
266
+ rendered = <TooltipProvider>{rendered}</TooltipProvider>
267
+
268
+ return (
269
+ <TreeContext.Provider value={context}>
270
+ {variant === "files" ? (
271
+ <div className={cn("min-w-0", className)} data-slot="tree-root">
272
+ {rendered}
273
+ </div>
274
+ ) : (
275
+ <nav
276
+ aria-label={label}
277
+ className={cn("flex h-full min-w-0 justify-center", className)}
278
+ data-slot="tree-root"
279
+ ref={railRef}
280
+ >
281
+ {rendered}
282
+ </nav>
283
+ )}
284
+ </TreeContext.Provider>
285
+ )
286
+ }
@@ -0,0 +1,322 @@
1
+ import type * as React from "react"
2
+
3
+ /** One node in the tree. Nesting depth carries meaning per variant — see
4
+ * `TreeProps["variant"]`. */
5
+ export interface TreeNode {
6
+ id: string
7
+ label: string
8
+ /** Leading icon. Required in practice for `sidebar` — it is all that
9
+ * remains of a row while the rail is collapsed. */
10
+ icon?: React.ReactNode
11
+ /** Route metadata for the consumer's `onNavigate` — every row renders as
12
+ * a button, so this is never turned into an anchor element. Read it (with
13
+ * `target`) off the node your handler receives. One native behaviour is
14
+ * kept: `toc` rows below the route level jump to `#{id}` after select
15
+ * (a `#hash` href overrides the id; a non-hash href disables the jump). */
16
+ href?: string
17
+ target?: "_blank" | "_self" | "_parent" | "_top"
18
+ disabled?: boolean
19
+ /** Start expanded. Ancestors of `activeId` expand regardless. */
20
+ defaultOpen?: boolean
21
+ /** Trailing hint — a count, a "New" pill. */
22
+ badge?: React.ReactNode
23
+ /** Fires on selection, before the tree-level `onNavigate`. */
24
+ onSelect?: () => void
25
+ children?: TreeNode[]
26
+ }
27
+
28
+ export type TreeVariant = "navigation" | "toc" | "sidebar" | "files"
29
+
30
+ /** Where a dragged row lands relative to its drop target. */
31
+ export type TreeDropPosition = "before" | "after" | "inside"
32
+
33
+ /** Key for one tree’s state in the store. Any stable string; `useTree`
34
+ * generates one when you don't pass it. */
35
+ export type TreeInstanceId = string
36
+
37
+ /** Row a drag hovers, and where the drop would land. */
38
+ export interface TreeDropTarget {
39
+ id: string
40
+ position: TreeDropPosition
41
+ }
42
+
43
+ interface TreeBaseProps {
44
+ items: TreeNode[]
45
+ /** Key this tree's state under a name your app can address —
46
+ * `useTreeState("app-nav")` / `useTreeActions("app-nav")` reach it from
47
+ * anywhere under `ExegiaProvider`. Without one the tree generates a key
48
+ * and its state is dropped when it unmounts. */
49
+ treeId?: TreeInstanceId
50
+ /** Never set on the data form — `tree` selects the controller form. */
51
+ tree?: never
52
+ /** `id` of the current entry — matches any depth. Its ancestors expand. */
53
+ activeId?: string
54
+ /** Fires for every selection (rows and leaves alike), after the node's
55
+ * own `onSelect`. Rows are buttons, so this is the routing path — wire
56
+ * your router's navigate here. `toc` rows below the route level still jump
57
+ * to `#{id}` natively, after this fires. */
58
+ onNavigate?: (node: TreeNode) => void
59
+ /** Expand/collapse cues. Silent until `bindSounds()`. */
60
+ sound?: boolean
61
+ ariaLabel?: string
62
+ className?: string
63
+ }
64
+
65
+ interface TreeReadonlyProps {
66
+ collapsed?: never
67
+ onMove?: never
68
+ onRename?: never
69
+ renderTrailing?: never
70
+ }
71
+
72
+ /** Drive the tree from a `useTree` controller instead of raw props. The
73
+ * controller carries the variant, the data and every handler; only the
74
+ * presentational props stay here. */
75
+ export interface TreeControllerProps {
76
+ tree: TreeController
77
+ items?: never
78
+ ariaLabel?: string
79
+ className?: string
80
+ /** Row actions revealed on hover/focus (`files` only — ignored by the
81
+ * other variants, which the controller form cannot type-gate). */
82
+ renderTrailing?: (node: TreeNode) => React.ReactNode
83
+ }
84
+
85
+ /** Either form: raw props, or a `useTree` controller via `tree`. */
86
+ export type TreeProps = TreeDataProps | TreeControllerProps
87
+
88
+ /** The four shapes a Tree takes. Editing props only exist on `files`;
89
+ * `collapsed` only on `sidebar` — the union is the contract. */
90
+ export type TreeDataProps = TreeBaseProps &
91
+ (
92
+ | ({
93
+ /** Nested app navigation. With 3 levels of nodes the top level
94
+ * becomes collapsible section names (styled as headings, not
95
+ * rows); with 2 it renders plain selectable rows. */
96
+ variant: "navigation"
97
+ } & TreeReadonlyProps)
98
+ | ({
99
+ /** Table of contents: top-level nodes are routes (handled in
100
+ * `onNavigate`); rows below jump to `#{id}` after select, like an
101
+ * anchor would. Parents select on the row and expand from a
102
+ * separate overlay chevron. */
103
+ variant: "toc"
104
+ } & TreeReadonlyProps)
105
+ | ({
106
+ /** Single-level icon rail. `collapsed` shrinks rows to their
107
+ * leading icon; nested children are ignored. */
108
+ variant: "sidebar"
109
+ collapsed?: boolean
110
+ } & Omit<TreeReadonlyProps, "collapsed">)
111
+ | {
112
+ /** File explorer: only leaves navigate, folders toggle. Compact
113
+ * spacing; rename, drag-and-drop and trailing actions enabled. */
114
+ variant: "files"
115
+ /** Enables drag-and-drop. Reorder `items` yourself: `parentId`
116
+ * `null` means the root list, `index` is the slot among the new
117
+ * siblings (computed on the list without the dragged node). */
118
+ onMove?: (id: string, parentId: string | null, index: number) => void
119
+ /** Enables inline rename (double-click or F2). */
120
+ onRename?: (id: string, label: string) => void
121
+ /** Row actions revealed on hover/focus — a menu, a delete icon. */
122
+ renderTrailing?: (node: TreeNode) => React.ReactNode
123
+ collapsed?: never
124
+ }
125
+ )
126
+
127
+ /** Options for `useTree`. Each of `items`, `activeId` and `collapsed` is
128
+ * controlled when passed and hook-owned when its `default*` twin is used
129
+ * instead. */
130
+ export interface UseTreeOptions {
131
+ /** Key this tree's state under a name your app can address —
132
+ * `useTreeState("app-nav")` / `useTreeActions("app-nav")` reach it from
133
+ * anywhere under `ExegiaProvider`. Without one the hook generates a key
134
+ * and the state is dropped when the component unmounts. */
135
+ treeId?: TreeInstanceId
136
+ /** Which shape the tree takes — gates rename/reorder (`files`) and the
137
+ * collapsible rail (`sidebar`). */
138
+ variant: TreeVariant
139
+ /** Controlled data. With it, `rename`/`move` only report the edit unless
140
+ * you also pass `onItemsChange`. */
141
+ items?: TreeNode[]
142
+ /** Hook-owned data — `rename` and `move` apply the edit themselves. */
143
+ defaultItems?: TreeNode[]
144
+ /** Next tree after a `rename`/`move`. Also enables self-applying edits
145
+ * on top of controlled `items`. */
146
+ onItemsChange?: (items: TreeNode[]) => void
147
+ /** Controlled selection. Ancestors of the active node expand. */
148
+ activeId?: string
149
+ defaultActiveId?: string
150
+ /** Fires for every selection (rows and leaves alike), after the node's
151
+ * own `onSelect` — wire your router's navigate here. */
152
+ onNavigate?: (node: TreeNode) => void
153
+ /** Start with these ids expanded instead of the `defaultOpen` set. */
154
+ defaultExpandedIds?: Iterable<string>
155
+ onExpandedChange?: (ids: string[]) => void
156
+ /** Controlled rail fold (`sidebar` only). */
157
+ collapsed?: boolean
158
+ defaultCollapsed?: boolean
159
+ onCollapsedChange?: (collapsed: boolean) => void
160
+ /** Enables rename (`files`). Not needed when the hook owns the data. */
161
+ onRename?: (id: string, label: string) => void
162
+ /** Enables drag-and-drop (`files`). `parentId` `null` means the root
163
+ * list, `index` is the slot among the new siblings (computed on the list
164
+ * without the moved node). Not needed when the hook owns the data. */
165
+ onMove?: (id: string, parentId: string | null, index: number) => void
166
+ /** Expand/collapse cues. Silent until `bindSounds()`. */
167
+ sound?: boolean
168
+ }
169
+
170
+ /** Everything the tree can do, callable from outside the component.
171
+ * Returned by `useTree` and accepted by `<Tree tree={…} />`. */
172
+ export interface TreeController {
173
+ /** The key this tree's state is stored under. */
174
+ treeId: TreeInstanceId
175
+ variant: TreeVariant
176
+ items: TreeNode[]
177
+ /** `navigation` with 3 levels of nodes — depth 0 renders as sections. */
178
+ sectioned: boolean
179
+ /** Ids of the section headings, empty unless `sectioned`. Pass one to
180
+ * `expand`/`collapse`/`toggleExpanded` to work a whole section. */
181
+ sectionIds: string[]
182
+ sound: boolean
183
+ getNode: (id: string) => TreeNode | null
184
+
185
+ expandedIds: ReadonlySet<string>
186
+ isExpanded: (id: string) => boolean
187
+ expand: (id: string) => void
188
+ collapse: (id: string) => void
189
+ toggleExpanded: (id: string) => void
190
+ expandAll: () => void
191
+ collapseAll: () => void
192
+ /** Open every ancestor of `id`, leaving the rest of the tree alone. */
193
+ reveal: (id: string) => void
194
+
195
+ /** Rail folded to icons — always `false` outside `sidebar`. */
196
+ collapsed: boolean
197
+ setCollapsed: (collapsed: boolean) => void
198
+ toggleCollapsed: () => void
199
+
200
+ activeId?: string
201
+ /** Select a node: runs its `onSelect`, then `onNavigate`. Inert on
202
+ * disabled and unknown nodes. */
203
+ select: (id: string) => void
204
+
205
+ /** Whether rename is wired up at all (`files` + a handler or owned data). */
206
+ canRename: boolean
207
+ /** Rename target id, `null` while idle. */
208
+ renamingId: string | null
209
+ startRename: (id: string) => void
210
+ cancelRename: () => void
211
+ /** Commit a rename and leave rename mode. Blank or unchanged labels are
212
+ * dropped. */
213
+ rename: (id: string, label: string) => void
214
+
215
+ /** Whether reorder is wired up at all. */
216
+ canMove: boolean
217
+ move: (id: string, parentId: string | null, index: number) => void
218
+
219
+ /** Back to the values the tree mounted with. */
220
+ reset: () => void
221
+
222
+ dnd: TreeDndContextValue
223
+ }
224
+
225
+ /** @internal What a row needs from the root, threaded through context so
226
+ * the recursive rows stay prop-light.
227
+ *
228
+ * Deliberately free of tree state. Rows read state from per-node atoms, so
229
+ * this value keeps its identity for the life of the tree and expanding one
230
+ * branch no longer re-renders every row through context. */
231
+ export interface TreeContextValue {
232
+ treeId: TreeInstanceId
233
+ renderTrailing?: (node: TreeNode) => React.ReactNode
234
+ dnd: TreeDndHandlers
235
+ }
236
+
237
+ /** @internal The drag event handlers, stable for the life of the tree —
238
+ * they read drag state out of the store instead of closing over it, so
239
+ * handing them to every row costs no re-renders. */
240
+ export interface TreeDndHandlers {
241
+ enabled: boolean
242
+ onRowDragStart: (event: React.DragEvent, id: string) => void
243
+ onRowDragOver: (event: React.DragEvent, node: TreeNode) => void
244
+ onRowDragLeave: (event: React.DragEvent) => void
245
+ onRowDrop: (event: React.DragEvent, node: TreeNode) => void
246
+ onRowDragEnd: () => void
247
+ }
248
+
249
+ /** @internal Handlers plus live drag state — what `TreeController.dnd`
250
+ * exposes (all `null`/no-op outside `files`). */
251
+ export interface TreeDndContextValue extends TreeDndHandlers {
252
+ draggedId: string | null
253
+ /** Row the pointer is over and where the drop would land. */
254
+ dropTarget: TreeDropTarget | null
255
+ }
256
+
257
+ /** Everything observable about one tree, for consumers reading it by id. */
258
+ export interface TreeState {
259
+ variant: TreeVariant
260
+ items: TreeNode[]
261
+ activeId?: string
262
+ expandedIds: ReadonlySet<string>
263
+ collapsed: boolean
264
+ renamingId: string | null
265
+ sectioned: boolean
266
+ sectionIds: string[]
267
+ canRename: boolean
268
+ canMove: boolean
269
+ draggedId: string | null
270
+ dropTarget: TreeDropTarget | null
271
+ }
272
+
273
+ /** Everything doable to one tree from outside its component. */
274
+ export interface TreeActions {
275
+ expand: (id: string) => void
276
+ collapse: (id: string) => void
277
+ toggleExpanded: (id: string) => void
278
+ expandAll: () => void
279
+ collapseAll: () => void
280
+ reveal: (id: string) => void
281
+ setCollapsed: (collapsed: boolean) => void
282
+ toggleCollapsed: () => void
283
+ select: (id: string) => void
284
+ startRename: (id: string) => void
285
+ cancelRename: () => void
286
+ rename: (id: string, label: string) => void
287
+ move: (id: string, parentId: string | null, index: number) => void
288
+ setItems: (items: TreeNode[]) => void
289
+ reset: () => void
290
+ }
291
+
292
+ /** @internal Projection of `useTree`'s options — primitives only, so the
293
+ * store write runs once per real change instead of once per render. */
294
+ export interface TreeConfig {
295
+ variant: TreeVariant
296
+ sound: boolean
297
+ controlsItems: boolean
298
+ controlsActiveId: boolean
299
+ controlsCollapsed: boolean
300
+ managesItems: boolean
301
+ hasRenameHandler: boolean
302
+ hasMoveHandler: boolean
303
+ }
304
+
305
+ /** @internal Latest option callbacks. Only write atoms read this, so it
306
+ * can be refreshed every commit without re-rendering anything. */
307
+ export interface TreeHandlers {
308
+ onNavigate?: (node: TreeNode) => void
309
+ onItemsChange?: (items: TreeNode[]) => void
310
+ onExpandedChange?: (ids: string[]) => void
311
+ onCollapsedChange?: (collapsed: boolean) => void
312
+ onRename?: (id: string, label: string) => void
313
+ onMove?: (id: string, parentId: string | null, index: number) => void
314
+ }
315
+
316
+ /** @internal What an instance starts from, replayed by `resetTreeAtom`. */
317
+ export interface TreeSeed {
318
+ items: TreeNode[]
319
+ activeId?: string
320
+ collapsed: boolean
321
+ expandedIds?: string[]
322
+ }