@behavioral-agentic-design-system/core 0.28.0 → 0.30.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.
- package/package.json +2 -2
- package/scripts/a11y-lint.cjs +78 -9
- package/scripts/build-tokens.cjs +273 -15
- package/scripts/wcag-audit.cjs +67 -11
- package/src/api/layers/file.ts +1 -0
- package/src/app/font-ready.tsx +6 -5
- package/src/app/layout.tsx +54 -3
- package/src/app/preview/components/page.tsx +5679 -3073
- package/src/app/preview/health/page.tsx +201 -0
- package/src/app/preview/layout.tsx +4 -1
- package/src/app/preview/showcase-filter.tsx +66 -0
- package/src/app/preview/tab-replay.tsx +63 -0
- package/src/app/shell.tsx +74 -18
- package/src/components/customize/save-bar.tsx +19 -6
- package/src/components/docs-tab.tsx +36 -24
- package/src/components/layers-tab.tsx +37 -5
- package/src/components/ui/alert.tsx +12 -0
- package/src/components/ui/badge.tsx +19 -0
- package/src/components/ui/button.tsx +10 -0
- package/src/components/ui/chat-marker.tsx +120 -0
- package/src/components/ui/checkbox.tsx +9 -1
- package/src/components/ui/close-button.tsx +71 -0
- package/src/components/ui/combobox.tsx +62 -3
- package/src/components/ui/container.tsx +65 -0
- package/src/components/ui/date-field.tsx +368 -0
- package/src/components/ui/drawer.tsx +334 -0
- package/src/components/ui/file-button.tsx +145 -0
- package/src/components/ui/floating-indicator.tsx +217 -0
- package/src/components/ui/format-byte.tsx +113 -0
- package/src/components/ui/index.ts +21 -0
- package/src/components/ui/input-clear-button.tsx +52 -0
- package/src/components/ui/input.tsx +39 -20
- package/src/components/ui/keyboard-shortcut.tsx +146 -0
- package/src/components/ui/loading-dots.tsx +99 -0
- package/src/components/ui/month-picker.tsx +296 -0
- package/src/components/ui/multi-select.tsx +35 -19
- package/src/components/ui/nav-link.tsx +200 -0
- package/src/components/ui/navigation-rail.tsx +256 -0
- package/src/components/ui/notification.tsx +20 -0
- package/src/components/ui/password-input.tsx +34 -1
- package/src/components/ui/progress.tsx +15 -1
- package/src/components/ui/prose.tsx +62 -0
- package/src/components/ui/questionnaire.tsx +334 -0
- package/src/components/ui/result.tsx +151 -0
- package/src/components/ui/rolling-number.tsx +1 -0
- package/src/components/ui/sidebar.tsx +5 -0
- package/src/components/ui/simple-grid.tsx +110 -0
- package/src/components/ui/skeleton.tsx +52 -3
- package/src/components/ui/squircle.tsx +274 -0
- package/src/components/ui/stack.tsx +105 -0
- package/src/components/ui/status-badge.tsx +93 -0
- package/src/components/ui/tabs.tsx +34 -7
- package/src/components/ui/tag.tsx +46 -5
- package/src/components/ui/textarea.tsx +5 -0
- package/src/components/ui/theme-icon.tsx +138 -0
- package/src/components/ui/timeline.tsx +27 -10
- package/src/components/ui/tooltip.tsx +12 -1
- package/src/components/ui/tree-select.tsx +386 -0
- package/src/components/ui/year-picker.tsx +307 -0
- package/src/data/docs-index.ts +12 -0
- package/src/hooks/use-global-primary-color.ts +57 -6
- package/src/lib/get-icon-color.ts +66 -0
- package/src/stories/Alert.stories.tsx +31 -1
- package/src/stories/Badge.stories.tsx +63 -6
- package/src/stories/Button.stories.tsx +3 -0
- package/src/stories/ChatMarker.stories.tsx +59 -0
- package/src/stories/Checkbox.stories.tsx +5 -0
- package/src/stories/CloseButton.stories.tsx +65 -0
- package/src/stories/Combobox.stories.tsx +74 -3
- package/src/stories/Container.stories.tsx +134 -0
- package/src/stories/DateField.stories.tsx +114 -0
- package/src/stories/Drawer.stories.tsx +211 -0
- package/src/stories/FileButton.stories.tsx +117 -0
- package/src/stories/FloatingIndicator.stories.tsx +343 -0
- package/src/stories/FormatByte.stories.tsx +142 -0
- package/src/stories/Input.stories.tsx +94 -0
- package/src/stories/InputClearButton.stories.tsx +53 -0
- package/src/stories/KeyboardShortcut.stories.tsx +87 -0
- package/src/stories/LoadingDots.stories.tsx +73 -0
- package/src/stories/MonthPicker.stories.tsx +99 -0
- package/src/stories/MultiSelect.stories.tsx +7 -1
- package/src/stories/NavLink.stories.tsx +213 -0
- package/src/stories/NavigationRail.stories.tsx +413 -0
- package/src/stories/Notification.stories.tsx +53 -0
- package/src/stories/PasswordInput.stories.tsx +21 -0
- package/src/stories/Progress.stories.tsx +38 -0
- package/src/stories/Prose.stories.tsx +232 -0
- package/src/stories/Questionnaire.stories.tsx +287 -0
- package/src/stories/Result.stories.tsx +145 -0
- package/src/stories/RollingNumber.stories.tsx +1 -1
- package/src/stories/Sidebar.stories.tsx +2 -0
- package/src/stories/SimpleGrid.stories.tsx +130 -0
- package/src/stories/Skeleton.stories.tsx +62 -1
- package/src/stories/Squircle.stories.tsx +127 -0
- package/src/stories/Stack.stories.tsx +140 -0
- package/src/stories/StatusBadge.stories.tsx +125 -0
- package/src/stories/Tabs.stories.tsx +56 -1
- package/src/stories/Tag.stories.tsx +31 -0
- package/src/stories/Textarea.stories.tsx +2 -0
- package/src/stories/ThemeIcon.stories.tsx +97 -0
- package/src/stories/Timeline.stories.tsx +69 -0
- package/src/stories/Tooltip.stories.tsx +18 -0
- package/src/stories/TreeSelect.stories.tsx +222 -0
- package/src/stories/YearPicker.stories.tsx +100 -0
- package/src/styles.css +289 -15
- package/src/tokens.json +96 -1
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { CheckIcon, ChevronDownIcon, ChevronRightIcon } from "lucide-react"
|
|
5
|
+
import * as PopoverPrimitive from "@radix-ui/react-popover"
|
|
6
|
+
|
|
7
|
+
import { cn } from "../../lib/utils"
|
|
8
|
+
import { usePortalThemeClass } from "../../hooks/use-portal-theme-class"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* TreeSelect — a select-shaped trigger that opens a popover containing an
|
|
12
|
+
* expandable tree. The user picks one leaf (single mode: commit + close) or
|
|
13
|
+
* several leaves (multiple mode: toggle + stay open) from an arbitrary-depth
|
|
14
|
+
* hierarchy. Branch rows organize (click / Enter toggles expansion); leaf rows
|
|
15
|
+
* carry the selection — the same leaf-commit semantics as Cascader, applied to
|
|
16
|
+
* a tree instead of columns.
|
|
17
|
+
*
|
|
18
|
+
* Fills the gap between Cascader (known-depth ≤4 column browser), TreeView
|
|
19
|
+
* (persistent inline tree with no committed value), and Select / Combobox /
|
|
20
|
+
* MultiSelect (flat lists). Sources: Ant Design TreeSelect, Mantine Combobox
|
|
21
|
+
* TreeSelect example.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export interface TreeSelectNode {
|
|
25
|
+
value: string
|
|
26
|
+
label: string
|
|
27
|
+
disabled?: boolean
|
|
28
|
+
children?: TreeSelectNode[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface TreeSelectProps {
|
|
32
|
+
/** The tree of options. Nodes with `children` are branches (expand/collapse); nodes without are selectable leaves. */
|
|
33
|
+
data: TreeSelectNode[]
|
|
34
|
+
/** Controlled value — a leaf value in single mode, an array of leaf values in multiple mode. */
|
|
35
|
+
value?: string | string[]
|
|
36
|
+
/** Fires with the committed value: a string in single mode, a string[] in multiple mode. */
|
|
37
|
+
onChange?: (value: string | string[]) => void
|
|
38
|
+
placeholder?: string
|
|
39
|
+
disabled?: boolean
|
|
40
|
+
/** Multi-select mode: leaves toggle independently and the popover stays open between toggles. */
|
|
41
|
+
multiple?: boolean
|
|
42
|
+
/** Branch values expanded on first render. Ancestors of the current selection auto-expand on open. */
|
|
43
|
+
defaultExpanded?: string[]
|
|
44
|
+
className?: string
|
|
45
|
+
/** aria-label for the trigger when no visible label is associated. */
|
|
46
|
+
"aria-label"?: string
|
|
47
|
+
/** aria-labelledby pointing at an external label element. */
|
|
48
|
+
"aria-labelledby"?: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Map every node value to its label + ancestor branch values (for display + auto-expand). */
|
|
52
|
+
function indexTree(
|
|
53
|
+
nodes: TreeSelectNode[],
|
|
54
|
+
ancestors: string[] = [],
|
|
55
|
+
into: Map<string, { label: string; ancestors: string[] }> = new Map()
|
|
56
|
+
): Map<string, { label: string; ancestors: string[] }> {
|
|
57
|
+
for (const node of nodes) {
|
|
58
|
+
into.set(node.value, { label: node.label, ancestors })
|
|
59
|
+
if (node.children?.length) {
|
|
60
|
+
indexTree(node.children, [...ancestors, node.value], into)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return into
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getVisibleItems(contentEl: HTMLElement): HTMLButtonElement[] {
|
|
67
|
+
return Array.from(
|
|
68
|
+
contentEl.querySelectorAll<HTMLButtonElement>('[data-slot="tree-select-item"]')
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function TreeSelect({
|
|
73
|
+
data,
|
|
74
|
+
value,
|
|
75
|
+
onChange,
|
|
76
|
+
placeholder = "Select…",
|
|
77
|
+
disabled,
|
|
78
|
+
multiple = false,
|
|
79
|
+
defaultExpanded = [],
|
|
80
|
+
className,
|
|
81
|
+
"aria-label": ariaLabel,
|
|
82
|
+
"aria-labelledby": ariaLabelledBy,
|
|
83
|
+
}: TreeSelectProps) {
|
|
84
|
+
const [open, setOpen] = React.useState(false)
|
|
85
|
+
const [internalValue, setInternalValue] = React.useState<string | string[]>(
|
|
86
|
+
multiple ? [] : ""
|
|
87
|
+
)
|
|
88
|
+
const [expanded, setExpanded] = React.useState<Set<string>>(
|
|
89
|
+
() => new Set(defaultExpanded)
|
|
90
|
+
)
|
|
91
|
+
const contentRef = React.useRef<HTMLDivElement>(null)
|
|
92
|
+
const portalThemeClass = usePortalThemeClass()
|
|
93
|
+
|
|
94
|
+
const nodeIndex = React.useMemo(() => indexTree(data), [data])
|
|
95
|
+
|
|
96
|
+
const currentValue = value !== undefined ? value : internalValue
|
|
97
|
+
const selectedValues = React.useMemo<string[]>(() => {
|
|
98
|
+
if (Array.isArray(currentValue)) return currentValue
|
|
99
|
+
return currentValue ? [currentValue] : []
|
|
100
|
+
}, [currentValue])
|
|
101
|
+
|
|
102
|
+
const selectedLabels = selectedValues
|
|
103
|
+
.map((v) => nodeIndex.get(v)?.label)
|
|
104
|
+
.filter((l): l is string => !!l)
|
|
105
|
+
const displayValue = selectedLabels.length > 0 ? selectedLabels.join(", ") : null
|
|
106
|
+
|
|
107
|
+
function commit(next: string | string[]) {
|
|
108
|
+
if (value === undefined) setInternalValue(next)
|
|
109
|
+
onChange?.(next)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function handleOpenChange(nextOpen: boolean) {
|
|
113
|
+
setOpen(nextOpen)
|
|
114
|
+
if (nextOpen && selectedValues.length > 0) {
|
|
115
|
+
// Auto-expand every ancestor of the current selection so the picked
|
|
116
|
+
// leaf is visible when the popup opens.
|
|
117
|
+
setExpanded((prev) => {
|
|
118
|
+
const next = new Set(prev)
|
|
119
|
+
for (const v of selectedValues) {
|
|
120
|
+
for (const ancestor of nodeIndex.get(v)?.ancestors ?? []) {
|
|
121
|
+
next.add(ancestor)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return next
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function toggleExpanded(nodeValue: string) {
|
|
130
|
+
setExpanded((prev) => {
|
|
131
|
+
const next = new Set(prev)
|
|
132
|
+
if (next.has(nodeValue)) next.delete(nodeValue)
|
|
133
|
+
else next.add(nodeValue)
|
|
134
|
+
return next
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function handleActivate(node: TreeSelectNode) {
|
|
139
|
+
if (node.disabled) return
|
|
140
|
+
if (node.children?.length) {
|
|
141
|
+
toggleExpanded(node.value)
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
if (multiple) {
|
|
145
|
+
const next = selectedValues.includes(node.value)
|
|
146
|
+
? selectedValues.filter((v) => v !== node.value)
|
|
147
|
+
: [...selectedValues, node.value]
|
|
148
|
+
commit(next)
|
|
149
|
+
} else {
|
|
150
|
+
commit(node.value)
|
|
151
|
+
setOpen(false)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function handleItemKeyDown(
|
|
156
|
+
e: React.KeyboardEvent<HTMLButtonElement>,
|
|
157
|
+
node: TreeSelectNode,
|
|
158
|
+
level: number
|
|
159
|
+
) {
|
|
160
|
+
const content = contentRef.current
|
|
161
|
+
if (!content) return
|
|
162
|
+
const items = getVisibleItems(content)
|
|
163
|
+
const idx = items.indexOf(e.currentTarget)
|
|
164
|
+
const isBranch = !!node.children?.length
|
|
165
|
+
const isExpanded = expanded.has(node.value)
|
|
166
|
+
|
|
167
|
+
switch (e.key) {
|
|
168
|
+
case "ArrowDown":
|
|
169
|
+
e.preventDefault()
|
|
170
|
+
items[idx + 1]?.focus()
|
|
171
|
+
break
|
|
172
|
+
case "ArrowUp":
|
|
173
|
+
e.preventDefault()
|
|
174
|
+
items[idx - 1]?.focus()
|
|
175
|
+
break
|
|
176
|
+
case "ArrowRight":
|
|
177
|
+
e.preventDefault()
|
|
178
|
+
if (isBranch && !isExpanded) toggleExpanded(node.value)
|
|
179
|
+
else if (isBranch && isExpanded) items[idx + 1]?.focus()
|
|
180
|
+
break
|
|
181
|
+
case "ArrowLeft":
|
|
182
|
+
e.preventDefault()
|
|
183
|
+
if (isBranch && isExpanded) {
|
|
184
|
+
toggleExpanded(node.value)
|
|
185
|
+
} else {
|
|
186
|
+
// Focus the closest ancestor (a visible item with a lower aria-level)
|
|
187
|
+
for (let i = idx - 1; i >= 0; i--) {
|
|
188
|
+
const itemLevel = parseInt(items[i].getAttribute("data-level") ?? "1", 10)
|
|
189
|
+
if (itemLevel < level) {
|
|
190
|
+
items[i].focus()
|
|
191
|
+
break
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
break
|
|
196
|
+
case "Home":
|
|
197
|
+
e.preventDefault()
|
|
198
|
+
items[0]?.focus()
|
|
199
|
+
break
|
|
200
|
+
case "End":
|
|
201
|
+
e.preventDefault()
|
|
202
|
+
items[items.length - 1]?.focus()
|
|
203
|
+
break
|
|
204
|
+
case "Enter":
|
|
205
|
+
case " ":
|
|
206
|
+
e.preventDefault()
|
|
207
|
+
handleActivate(node)
|
|
208
|
+
break
|
|
209
|
+
default: {
|
|
210
|
+
// Typeahead: printable character jumps to the next visible node whose
|
|
211
|
+
// label starts with it (WAI-ARIA tree pattern), wrapping around.
|
|
212
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && e.key !== " ") {
|
|
213
|
+
const char = e.key.toLowerCase()
|
|
214
|
+
for (let step = 1; step <= items.length; step++) {
|
|
215
|
+
const candidate = items[(idx + step) % items.length]
|
|
216
|
+
const label = candidate.getAttribute("data-label") ?? ""
|
|
217
|
+
if (label.toLowerCase().startsWith(char)) {
|
|
218
|
+
e.preventDefault()
|
|
219
|
+
candidate.focus()
|
|
220
|
+
break
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function focusInitialItem() {
|
|
229
|
+
const content = contentRef.current
|
|
230
|
+
if (!content) return
|
|
231
|
+
const items = getVisibleItems(content)
|
|
232
|
+
const selected = items.find((el) => el.hasAttribute("data-selected"))
|
|
233
|
+
;(selected ?? items[0])?.focus()
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function renderNodes(nodes: TreeSelectNode[], level: number): React.ReactNode {
|
|
237
|
+
return (
|
|
238
|
+
<ul
|
|
239
|
+
role={level === 1 ? "tree" : "group"}
|
|
240
|
+
aria-label={level === 1 ? (ariaLabel ?? "Options") : undefined}
|
|
241
|
+
aria-multiselectable={level === 1 && multiple ? true : undefined}
|
|
242
|
+
className="space-y-0.5"
|
|
243
|
+
>
|
|
244
|
+
{nodes.map((node, nodeIdx) => {
|
|
245
|
+
const isBranch = !!node.children?.length
|
|
246
|
+
const isExpanded = expanded.has(node.value)
|
|
247
|
+
const isSelected = !isBranch && selectedValues.includes(node.value)
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
<li
|
|
251
|
+
key={node.value}
|
|
252
|
+
role="treeitem"
|
|
253
|
+
aria-expanded={isBranch ? isExpanded : undefined}
|
|
254
|
+
aria-selected={isBranch ? undefined : isSelected}
|
|
255
|
+
aria-level={level}
|
|
256
|
+
aria-disabled={node.disabled || undefined}
|
|
257
|
+
className="list-none"
|
|
258
|
+
>
|
|
259
|
+
<button
|
|
260
|
+
type="button"
|
|
261
|
+
data-slot="tree-select-item"
|
|
262
|
+
data-level={level}
|
|
263
|
+
data-label={node.label}
|
|
264
|
+
data-selected={isSelected || undefined}
|
|
265
|
+
data-disabled={node.disabled || undefined}
|
|
266
|
+
aria-disabled={node.disabled || undefined}
|
|
267
|
+
tabIndex={level === 1 && nodeIdx === 0 ? 0 : -1}
|
|
268
|
+
onClick={() => handleActivate(node)}
|
|
269
|
+
onKeyDown={(e) => handleItemKeyDown(e, node, level)}
|
|
270
|
+
className={cn(
|
|
271
|
+
"flex w-full items-center gap-1.5 rounded-sm py-1.5 pr-2 text-sm outline-none",
|
|
272
|
+
"transition-colors",
|
|
273
|
+
"focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
274
|
+
"data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
|
275
|
+
isSelected
|
|
276
|
+
? "bg-accent text-accent-foreground"
|
|
277
|
+
: "hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground"
|
|
278
|
+
)}
|
|
279
|
+
style={{
|
|
280
|
+
paddingLeft: `${(level - 1) * 16 + 8}px`,
|
|
281
|
+
transitionDuration: "var(--duration-fast)",
|
|
282
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
283
|
+
}}
|
|
284
|
+
>
|
|
285
|
+
<ChevronRightIcon
|
|
286
|
+
strokeWidth={1.5}
|
|
287
|
+
aria-hidden
|
|
288
|
+
className={cn(
|
|
289
|
+
"size-4 shrink-0 text-muted-foreground transition-transform",
|
|
290
|
+
!isBranch && "pointer-events-none opacity-0",
|
|
291
|
+
isExpanded && "rotate-90"
|
|
292
|
+
)}
|
|
293
|
+
style={{
|
|
294
|
+
transitionDuration: "var(--duration-fast)",
|
|
295
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
296
|
+
}}
|
|
297
|
+
/>
|
|
298
|
+
<span className="min-w-0 flex-1 truncate text-left">{node.label}</span>
|
|
299
|
+
{isSelected && (
|
|
300
|
+
<CheckIcon strokeWidth={1.5} aria-hidden className="size-4 shrink-0" />
|
|
301
|
+
)}
|
|
302
|
+
</button>
|
|
303
|
+
{isBranch && isExpanded && renderNodes(node.children!, level + 1)}
|
|
304
|
+
</li>
|
|
305
|
+
)
|
|
306
|
+
})}
|
|
307
|
+
</ul>
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return (
|
|
312
|
+
<PopoverPrimitive.Root data-slot="tree-select" open={open} onOpenChange={handleOpenChange}>
|
|
313
|
+
<PopoverPrimitive.Trigger asChild>
|
|
314
|
+
<button
|
|
315
|
+
type="button"
|
|
316
|
+
data-slot="tree-select-trigger"
|
|
317
|
+
disabled={disabled}
|
|
318
|
+
aria-haspopup="tree"
|
|
319
|
+
aria-expanded={open}
|
|
320
|
+
aria-label={ariaLabel}
|
|
321
|
+
aria-labelledby={ariaLabelledBy}
|
|
322
|
+
className={cn(
|
|
323
|
+
"flex min-h-11 w-full items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-sm font-medium shadow-xs outline-none",
|
|
324
|
+
"transition-[color,box-shadow]",
|
|
325
|
+
"focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
326
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
327
|
+
"aria-expanded:border-ring aria-expanded:ring-[3px] aria-expanded:ring-ring/50",
|
|
328
|
+
className
|
|
329
|
+
)}
|
|
330
|
+
style={{
|
|
331
|
+
transitionDuration: "var(--duration-fast)",
|
|
332
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
333
|
+
}}
|
|
334
|
+
>
|
|
335
|
+
<span
|
|
336
|
+
title={displayValue ?? undefined}
|
|
337
|
+
className={cn(
|
|
338
|
+
"min-w-0 flex-1 truncate text-left",
|
|
339
|
+
!displayValue && "font-normal text-muted-foreground"
|
|
340
|
+
)}
|
|
341
|
+
>
|
|
342
|
+
{displayValue ?? placeholder}
|
|
343
|
+
</span>
|
|
344
|
+
<ChevronDownIcon
|
|
345
|
+
strokeWidth={1.5}
|
|
346
|
+
aria-hidden
|
|
347
|
+
className={cn(
|
|
348
|
+
"size-4 shrink-0 text-muted-foreground transition-transform",
|
|
349
|
+
open && "rotate-180"
|
|
350
|
+
)}
|
|
351
|
+
style={{
|
|
352
|
+
transitionDuration: "var(--duration-fast)",
|
|
353
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
354
|
+
}}
|
|
355
|
+
/>
|
|
356
|
+
</button>
|
|
357
|
+
</PopoverPrimitive.Trigger>
|
|
358
|
+
|
|
359
|
+
<PopoverPrimitive.Portal>
|
|
360
|
+
<PopoverPrimitive.Content
|
|
361
|
+
ref={contentRef}
|
|
362
|
+
data-slot="tree-select-content"
|
|
363
|
+
align="start"
|
|
364
|
+
sideOffset={4}
|
|
365
|
+
onOpenAutoFocus={(e) => {
|
|
366
|
+
// Redirect Radix's default content focus to the selected (or
|
|
367
|
+
// first) tree item so keyboard users land inside the tree.
|
|
368
|
+
e.preventDefault()
|
|
369
|
+
focusInitialItem()
|
|
370
|
+
}}
|
|
371
|
+
className={cn(
|
|
372
|
+
"z-50 max-h-80 min-w-(--radix-popover-trigger-width) overflow-y-auto rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md",
|
|
373
|
+
"origin-(--radix-popover-content-transform-origin)",
|
|
374
|
+
"data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
|
|
375
|
+
"data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
|
|
376
|
+
portalThemeClass
|
|
377
|
+
)}
|
|
378
|
+
>
|
|
379
|
+
{renderNodes(data, 1)}
|
|
380
|
+
</PopoverPrimitive.Content>
|
|
381
|
+
</PopoverPrimitive.Portal>
|
|
382
|
+
</PopoverPrimitive.Root>
|
|
383
|
+
)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export { TreeSelect }
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { Button } from "./button"
|
|
8
|
+
|
|
9
|
+
export interface YearPickerProps {
|
|
10
|
+
/** Controlled selected year (e.g. 2026). */
|
|
11
|
+
value?: number
|
|
12
|
+
/** Fires with the picked year, or `undefined` when the selected year is re-clicked (clear). */
|
|
13
|
+
onValueChange?: (year: number | undefined) => void
|
|
14
|
+
/** Uncontrolled initial selection. */
|
|
15
|
+
defaultValue?: number
|
|
16
|
+
/**
|
|
17
|
+
* Earliest selectable year (inclusive).
|
|
18
|
+
* @default currentYear - 100
|
|
19
|
+
*/
|
|
20
|
+
minYear?: number
|
|
21
|
+
/**
|
|
22
|
+
* Latest selectable year (inclusive).
|
|
23
|
+
* @default currentYear + 10
|
|
24
|
+
*/
|
|
25
|
+
maxYear?: number
|
|
26
|
+
/** Disables the entire control — decade navigation and every year cell. */
|
|
27
|
+
disabled?: boolean
|
|
28
|
+
className?: string
|
|
29
|
+
/** Forwarded to the root container — pair with an external Label. */
|
|
30
|
+
id?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 4-column × 3-row year grid — the cross-library consensus shape for year pickers
|
|
34
|
+
// (Mantine YearPicker, MUI-X YearCalendar, Ant Design picker="year",
|
|
35
|
+
// Ark UI DatePicker view=year).
|
|
36
|
+
const COLS = 4
|
|
37
|
+
const ROWS = 3
|
|
38
|
+
const PAGE_SIZE = COLS * ROWS // 12 years per window
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Inline year-selection grid with decade navigation — yields a plain year
|
|
42
|
+
* integer without drilling into months or days. Use for graduation years,
|
|
43
|
+
* fiscal-year filters, birth-year selectors, and any surface where the answer
|
|
44
|
+
* is a year, not a date.
|
|
45
|
+
*
|
|
46
|
+
* ARIA grid pattern: grid → row → gridcell buttons with roving tabindex.
|
|
47
|
+
* Arrow keys move ±1 / ±4 years, Home/End jump to first/last enabled year in
|
|
48
|
+
* the view, PageUp/PageDown change the 12-year window.
|
|
49
|
+
*
|
|
50
|
+
* Clicking the already-selected year fires `onValueChange(undefined)` — a
|
|
51
|
+
* clear/deselect signal. To disable this, check your own state and no-op.
|
|
52
|
+
*/
|
|
53
|
+
function YearPicker({
|
|
54
|
+
value,
|
|
55
|
+
onValueChange,
|
|
56
|
+
defaultValue,
|
|
57
|
+
minYear,
|
|
58
|
+
maxYear,
|
|
59
|
+
disabled = false,
|
|
60
|
+
className,
|
|
61
|
+
id,
|
|
62
|
+
}: YearPickerProps) {
|
|
63
|
+
const currentYear = React.useMemo(() => new Date().getFullYear(), [])
|
|
64
|
+
const resolvedMin = minYear ?? currentYear - 100
|
|
65
|
+
const resolvedMax = maxYear ?? currentYear + 10
|
|
66
|
+
|
|
67
|
+
const isControlled = value !== undefined
|
|
68
|
+
const [internalValue, setInternalValue] = React.useState<number | undefined>(
|
|
69
|
+
defaultValue
|
|
70
|
+
)
|
|
71
|
+
const selected = isControlled ? value : internalValue
|
|
72
|
+
|
|
73
|
+
// The 12-year window start: first year of the page that contains the anchor year.
|
|
74
|
+
const [viewStart, setViewStart] = React.useState<number>(() => {
|
|
75
|
+
const anchor = selected ?? currentYear
|
|
76
|
+
return Math.floor(anchor / PAGE_SIZE) * PAGE_SIZE
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
// Roving tabindex: the cell index within the current window the user last focused.
|
|
80
|
+
const [focusedIdx, setFocusedIdx] = React.useState<number | null>(null)
|
|
81
|
+
const cellRefs = React.useRef<Array<HTMLButtonElement | null>>([])
|
|
82
|
+
|
|
83
|
+
// Follow a controlled value into its decade window so the selection stays visible.
|
|
84
|
+
React.useEffect(() => {
|
|
85
|
+
if (value !== undefined) {
|
|
86
|
+
const newStart = Math.floor(value / PAGE_SIZE) * PAGE_SIZE
|
|
87
|
+
setViewStart(newStart)
|
|
88
|
+
}
|
|
89
|
+
}, [value])
|
|
90
|
+
|
|
91
|
+
const years = React.useMemo(
|
|
92
|
+
() => Array.from({ length: PAGE_SIZE }, (_, i) => viewStart + i),
|
|
93
|
+
[viewStart]
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
const isYearDisabled = React.useCallback(
|
|
97
|
+
(year: number) => disabled || year < resolvedMin || year > resolvedMax,
|
|
98
|
+
[disabled, resolvedMin, resolvedMax]
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
// When every cell in the current window is out-of-range / disabled, the grid
|
|
102
|
+
// div itself becomes the keyboard tab stop (no individual button can carry tabIndex=0).
|
|
103
|
+
const allDisabled = React.useMemo(
|
|
104
|
+
() => years.every((y) => isYearDisabled(y)),
|
|
105
|
+
[years, isYearDisabled]
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
const defaultTabbable = React.useMemo(() => {
|
|
109
|
+
// Prefer the selected year, then today's year, then the first enabled year.
|
|
110
|
+
if (selected !== undefined) {
|
|
111
|
+
const idx = years.indexOf(selected)
|
|
112
|
+
if (idx !== -1 && !isYearDisabled(selected)) return idx
|
|
113
|
+
}
|
|
114
|
+
const todayIdx = years.indexOf(currentYear)
|
|
115
|
+
if (todayIdx !== -1 && !isYearDisabled(currentYear)) return todayIdx
|
|
116
|
+
const first = years.findIndex((y) => !isYearDisabled(y))
|
|
117
|
+
return first !== -1 ? first : 0
|
|
118
|
+
}, [selected, currentYear, years, isYearDisabled])
|
|
119
|
+
|
|
120
|
+
const tabbableIdx =
|
|
121
|
+
focusedIdx !== null &&
|
|
122
|
+
focusedIdx < PAGE_SIZE &&
|
|
123
|
+
!isYearDisabled(years[focusedIdx])
|
|
124
|
+
? focusedIdx
|
|
125
|
+
: defaultTabbable
|
|
126
|
+
|
|
127
|
+
function selectYear(year: number) {
|
|
128
|
+
if (isYearDisabled(year)) return
|
|
129
|
+
// Toggle: re-clicking the selected year clears the selection.
|
|
130
|
+
const next = selected === year ? undefined : year
|
|
131
|
+
if (!isControlled) setInternalValue(next)
|
|
132
|
+
onValueChange?.(next)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function focusCell(idx: number) {
|
|
136
|
+
cellRefs.current[idx]?.focus()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function handleGridKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
|
|
140
|
+
const from = focusedIdx ?? tabbableIdx
|
|
141
|
+
let delta = 0
|
|
142
|
+
switch (event.key) {
|
|
143
|
+
case "ArrowLeft":
|
|
144
|
+
delta = -1
|
|
145
|
+
break
|
|
146
|
+
case "ArrowRight":
|
|
147
|
+
delta = 1
|
|
148
|
+
break
|
|
149
|
+
case "ArrowUp":
|
|
150
|
+
// Move one row up = -COLS cells in a 4-column grid.
|
|
151
|
+
delta = -COLS
|
|
152
|
+
break
|
|
153
|
+
case "ArrowDown":
|
|
154
|
+
// Move one row down = +COLS cells.
|
|
155
|
+
delta = COLS
|
|
156
|
+
break
|
|
157
|
+
case "Home": {
|
|
158
|
+
event.preventDefault()
|
|
159
|
+
const first = years.findIndex((y) => !isYearDisabled(y))
|
|
160
|
+
if (first !== -1) focusCell(first)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
case "End": {
|
|
164
|
+
event.preventDefault()
|
|
165
|
+
for (let i = years.length - 1; i >= 0; i--) {
|
|
166
|
+
if (!isYearDisabled(years[i])) {
|
|
167
|
+
focusCell(i)
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
case "PageUp":
|
|
174
|
+
event.preventDefault()
|
|
175
|
+
if (!disabled) setViewStart((s) => s - PAGE_SIZE)
|
|
176
|
+
return
|
|
177
|
+
case "PageDown":
|
|
178
|
+
event.preventDefault()
|
|
179
|
+
if (!disabled) setViewStart((s) => s + PAGE_SIZE)
|
|
180
|
+
return
|
|
181
|
+
default:
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
event.preventDefault()
|
|
185
|
+
// Skip over disabled cells in the direction of travel.
|
|
186
|
+
let next = from + delta
|
|
187
|
+
while (next >= 0 && next < PAGE_SIZE && isYearDisabled(years[next])) {
|
|
188
|
+
next += delta
|
|
189
|
+
}
|
|
190
|
+
if (next < 0 || next >= PAGE_SIZE) return
|
|
191
|
+
focusCell(next)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Slice years into ROWS rows of COLS columns.
|
|
195
|
+
const grid = Array.from({ length: ROWS }, (_, r) =>
|
|
196
|
+
years.slice(r * COLS, r * COLS + COLS)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<div
|
|
201
|
+
id={id}
|
|
202
|
+
data-slot="year-picker"
|
|
203
|
+
data-disabled={disabled || undefined}
|
|
204
|
+
className={cn("w-fit bg-background p-3", className)}
|
|
205
|
+
>
|
|
206
|
+
{/* Header: prev/next decade chevrons + range label */}
|
|
207
|
+
<div
|
|
208
|
+
data-slot="year-picker-header"
|
|
209
|
+
className="mb-2 flex items-center justify-between gap-1"
|
|
210
|
+
>
|
|
211
|
+
<Button
|
|
212
|
+
type="button"
|
|
213
|
+
variant="ghost"
|
|
214
|
+
size="icon-xl"
|
|
215
|
+
aria-label="Previous period"
|
|
216
|
+
disabled={disabled}
|
|
217
|
+
onClick={() => setViewStart((s) => s - PAGE_SIZE)}
|
|
218
|
+
>
|
|
219
|
+
<ChevronLeftIcon strokeWidth={1.5} aria-hidden="true" />
|
|
220
|
+
</Button>
|
|
221
|
+
<span
|
|
222
|
+
aria-live="polite"
|
|
223
|
+
className={cn(
|
|
224
|
+
"text-sm font-medium tabular-nums select-none",
|
|
225
|
+
disabled ? "text-muted-foreground" : "text-foreground"
|
|
226
|
+
)}
|
|
227
|
+
>
|
|
228
|
+
{viewStart} – {viewStart + PAGE_SIZE - 1}
|
|
229
|
+
</span>
|
|
230
|
+
<Button
|
|
231
|
+
type="button"
|
|
232
|
+
variant="ghost"
|
|
233
|
+
size="icon-xl"
|
|
234
|
+
aria-label="Next period"
|
|
235
|
+
disabled={disabled}
|
|
236
|
+
onClick={() => setViewStart((s) => s + PAGE_SIZE)}
|
|
237
|
+
>
|
|
238
|
+
<ChevronRightIcon strokeWidth={1.5} aria-hidden="true" />
|
|
239
|
+
</Button>
|
|
240
|
+
</div>
|
|
241
|
+
|
|
242
|
+
{/* 4 × 3 year grid */}
|
|
243
|
+
<div
|
|
244
|
+
role="grid"
|
|
245
|
+
aria-label="Year picker"
|
|
246
|
+
aria-disabled={disabled || undefined}
|
|
247
|
+
data-slot="year-picker-grid"
|
|
248
|
+
tabIndex={allDisabled ? 0 : -1}
|
|
249
|
+
onKeyDown={handleGridKeyDown}
|
|
250
|
+
className="w-64 space-y-1"
|
|
251
|
+
>
|
|
252
|
+
{grid.map((row, rowIdx) => (
|
|
253
|
+
<div key={rowIdx} role="row" className="grid grid-cols-4 gap-1">
|
|
254
|
+
{row.map((year, colIdx) => {
|
|
255
|
+
const idx = rowIdx * COLS + colIdx
|
|
256
|
+
const isSelected = selected === year
|
|
257
|
+
const isToday = currentYear === year
|
|
258
|
+
const isDisabled = isYearDisabled(year)
|
|
259
|
+
return (
|
|
260
|
+
<button
|
|
261
|
+
key={year}
|
|
262
|
+
ref={(el) => {
|
|
263
|
+
cellRefs.current[idx] = el
|
|
264
|
+
}}
|
|
265
|
+
type="button"
|
|
266
|
+
role="gridcell"
|
|
267
|
+
data-slot="year-picker-cell"
|
|
268
|
+
data-selected={isSelected || undefined}
|
|
269
|
+
data-today={isToday || undefined}
|
|
270
|
+
aria-label={String(year)}
|
|
271
|
+
aria-selected={isSelected}
|
|
272
|
+
aria-disabled={isDisabled || undefined}
|
|
273
|
+
disabled={isDisabled}
|
|
274
|
+
tabIndex={!isDisabled && idx === tabbableIdx ? 0 : -1}
|
|
275
|
+
onClick={() => selectYear(year)}
|
|
276
|
+
onFocus={() => setFocusedIdx(idx)}
|
|
277
|
+
// Motion pattern (c): token-driven one-off transition —
|
|
278
|
+
// never Tailwind duration-* classes (silent failure in v4).
|
|
279
|
+
style={{
|
|
280
|
+
transitionDuration: "var(--duration-fast)",
|
|
281
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
282
|
+
}}
|
|
283
|
+
className={cn(
|
|
284
|
+
"inline-flex min-h-11 items-center justify-center rounded-md px-2 py-2 text-sm select-none",
|
|
285
|
+
"transition-colors outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
286
|
+
"disabled:pointer-events-none disabled:opacity-40",
|
|
287
|
+
isSelected
|
|
288
|
+
? "bg-primary text-primary-foreground hover:bg-primary/90"
|
|
289
|
+
: "text-foreground hover:bg-accent hover:text-accent-foreground",
|
|
290
|
+
// Today's year: underline + weight, never color alone (SC 1.4.1).
|
|
291
|
+
isToday &&
|
|
292
|
+
!isSelected &&
|
|
293
|
+
"font-medium text-primary underline underline-offset-4"
|
|
294
|
+
)}
|
|
295
|
+
>
|
|
296
|
+
{year}
|
|
297
|
+
</button>
|
|
298
|
+
)
|
|
299
|
+
})}
|
|
300
|
+
</div>
|
|
301
|
+
))}
|
|
302
|
+
</div>
|
|
303
|
+
</div>
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export { YearPicker }
|