@behavioral-agentic-design-system/core 0.24.0 → 0.27.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 (137) hide show
  1. package/package.json +5 -2
  2. package/scripts/build-tokens.cjs +87 -5
  3. package/src/app/preview/components/page.tsx +4745 -244
  4. package/src/app/shell.tsx +18 -6
  5. package/src/components/customize/brand-mark-section.tsx +1 -1
  6. package/src/components/customize-tab.tsx +2 -2
  7. package/src/components/home-tab.tsx +2 -2
  8. package/src/components/ui/action-bar.tsx +142 -0
  9. package/src/components/ui/affix.tsx +109 -0
  10. package/src/components/ui/alert.tsx +37 -3
  11. package/src/components/ui/anchor.tsx +136 -0
  12. package/src/components/ui/angle-slider.tsx +265 -0
  13. package/src/components/ui/autocomplete.tsx +223 -0
  14. package/src/components/ui/avatar.tsx +8 -2
  15. package/src/components/ui/blockquote.tsx +112 -0
  16. package/src/components/ui/bottom-navigation.tsx +144 -0
  17. package/src/components/ui/burger.tsx +117 -0
  18. package/src/components/ui/button.tsx +101 -3
  19. package/src/components/ui/calendar.tsx +1 -1
  20. package/src/components/ui/card.tsx +43 -5
  21. package/src/components/ui/carousel.tsx +10 -5
  22. package/src/components/ui/cascader.tsx +253 -0
  23. package/src/components/ui/checkbox-card.tsx +176 -0
  24. package/src/components/ui/checkbox-group.tsx +231 -0
  25. package/src/components/ui/checkbox.tsx +14 -3
  26. package/src/components/ui/chip.tsx +143 -0
  27. package/src/components/ui/circular-progress.tsx +2 -2
  28. package/src/components/ui/color-swatch.tsx +73 -0
  29. package/src/components/ui/copy-button.tsx +21 -4
  30. package/src/components/ui/date-picker.tsx +6 -3
  31. package/src/components/ui/date-range-picker.tsx +6 -3
  32. package/src/components/ui/float-label.tsx +114 -0
  33. package/src/components/ui/floating-action-button.tsx +179 -0
  34. package/src/components/ui/floating-panel.tsx +129 -0
  35. package/src/components/ui/format-number.tsx +122 -0
  36. package/src/components/ui/highlight.tsx +85 -0
  37. package/src/components/ui/image.tsx +195 -0
  38. package/src/components/ui/index.ts +51 -0
  39. package/src/components/ui/indicator.tsx +2 -2
  40. package/src/components/ui/input.tsx +48 -4
  41. package/src/components/ui/kbd.tsx +1 -1
  42. package/src/components/ui/link.tsx +158 -0
  43. package/src/components/ui/listbox.tsx +426 -0
  44. package/src/components/ui/loading-overlay.tsx +80 -0
  45. package/src/components/ui/marquee.tsx +193 -0
  46. package/src/components/ui/masonry.tsx +164 -0
  47. package/src/components/ui/meter.tsx +186 -0
  48. package/src/components/ui/motion-toggle.tsx +170 -0
  49. package/src/components/ui/notification.tsx +175 -0
  50. package/src/components/ui/overlay.tsx +63 -0
  51. package/src/components/ui/progress.tsx +95 -4
  52. package/src/components/ui/rating-group.tsx +1 -0
  53. package/src/components/ui/separator.tsx +57 -1
  54. package/src/components/ui/sidebar.tsx +1 -1
  55. package/src/components/ui/signature-pad.tsx +381 -0
  56. package/src/components/ui/skeleton.tsx +59 -2
  57. package/src/components/ui/skip-link.tsx +117 -0
  58. package/src/components/ui/slider.tsx +86 -1
  59. package/src/components/ui/spoiler.tsx +131 -0
  60. package/src/components/ui/tabs.tsx +1 -1
  61. package/src/components/ui/text.tsx +135 -0
  62. package/src/components/ui/textarea.tsx +66 -2
  63. package/src/components/ui/timeline.tsx +71 -14
  64. package/src/components/ui/timer.tsx +183 -0
  65. package/src/components/ui/tour.tsx +332 -0
  66. package/src/components/ui/transfer-list.tsx +343 -0
  67. package/src/components/ui/typing-indicator.tsx +96 -0
  68. package/src/components/ui/visually-hidden.tsx +42 -0
  69. package/src/components/ui/watermark.tsx +0 -0
  70. package/src/components/ui/word-rotate.tsx +82 -0
  71. package/src/data/docs-index.ts +10 -2
  72. package/src/stories/ActionBar.stories.tsx +201 -0
  73. package/src/stories/Affix.stories.tsx +165 -0
  74. package/src/stories/Alert.stories.tsx +58 -0
  75. package/src/stories/Anchor.stories.tsx +236 -0
  76. package/src/stories/AngleSlider.stories.tsx +128 -0
  77. package/src/stories/Autocomplete.stories.tsx +216 -0
  78. package/src/stories/Avatar.stories.tsx +73 -1
  79. package/src/stories/Blockquote.stories.tsx +148 -0
  80. package/src/stories/BottomNavigation.stories.tsx +195 -0
  81. package/src/stories/Burger.stories.tsx +79 -0
  82. package/src/stories/Button.stories.tsx +103 -2
  83. package/src/stories/Calendar.stories.tsx +4 -0
  84. package/src/stories/Card.stories.tsx +30 -0
  85. package/src/stories/Carousel.stories.tsx +2 -1
  86. package/src/stories/Cascader.stories.tsx +285 -0
  87. package/src/stories/Checkbox.stories.tsx +13 -0
  88. package/src/stories/CheckboxCard.stories.tsx +210 -0
  89. package/src/stories/CheckboxGroup.stories.tsx +186 -0
  90. package/src/stories/Chip.stories.tsx +173 -0
  91. package/src/stories/CircularProgress.stories.tsx +3 -0
  92. package/src/stories/ColorSwatch.stories.tsx +81 -0
  93. package/src/stories/CopyButton.stories.tsx +3 -3
  94. package/src/stories/DatePicker.stories.tsx +3 -0
  95. package/src/stories/DateRangePicker.stories.tsx +3 -0
  96. package/src/stories/Editable.stories.tsx +14 -0
  97. package/src/stories/FloatLabel.stories.tsx +148 -0
  98. package/src/stories/FloatingActionButton.stories.tsx +215 -0
  99. package/src/stories/FloatingPanel.stories.tsx +176 -0
  100. package/src/stories/FormatNumber.stories.tsx +308 -0
  101. package/src/stories/Highlight.stories.tsx +140 -0
  102. package/src/stories/Image.stories.tsx +141 -0
  103. package/src/stories/Indicator.stories.tsx +3 -0
  104. package/src/stories/Input.stories.tsx +33 -0
  105. package/src/stories/Kbd.stories.tsx +3 -0
  106. package/src/stories/Link.stories.tsx +198 -0
  107. package/src/stories/Listbox.stories.tsx +240 -0
  108. package/src/stories/LoadingOverlay.stories.tsx +148 -0
  109. package/src/stories/Marquee.stories.tsx +170 -0
  110. package/src/stories/Masonry.stories.tsx +227 -0
  111. package/src/stories/Meter.stories.tsx +150 -0
  112. package/src/stories/MotionToggle.stories.tsx +85 -0
  113. package/src/stories/Notification.stories.tsx +264 -0
  114. package/src/stories/Overlay.stories.tsx +169 -0
  115. package/src/stories/PasswordInput.stories.tsx +4 -0
  116. package/src/stories/Progress.stories.tsx +70 -27
  117. package/src/stories/RatingGroup.stories.tsx +5 -0
  118. package/src/stories/Separator.stories.tsx +41 -5
  119. package/src/stories/Sidebar.stories.tsx +2 -1
  120. package/src/stories/SignaturePad.stories.tsx +112 -0
  121. package/src/stories/Skeleton.stories.tsx +23 -4
  122. package/src/stories/SkipLink.stories.tsx +137 -0
  123. package/src/stories/Slider.stories.tsx +28 -0
  124. package/src/stories/Spoiler.stories.tsx +122 -0
  125. package/src/stories/Tabs.stories.tsx +2 -1
  126. package/src/stories/Text.stories.tsx +191 -0
  127. package/src/stories/Textarea.stories.tsx +58 -6
  128. package/src/stories/Timeline.stories.tsx +82 -0
  129. package/src/stories/Timer.stories.tsx +160 -0
  130. package/src/stories/Tour.stories.tsx +191 -0
  131. package/src/stories/TransferList.stories.tsx +190 -0
  132. package/src/stories/TypingIndicator.stories.tsx +139 -0
  133. package/src/stories/VisuallyHidden.stories.tsx +105 -0
  134. package/src/stories/Watermark.stories.tsx +212 -0
  135. package/src/stories/WordRotate.stories.tsx +101 -0
  136. package/src/styles.css +88 -6
  137. package/src/tokens.json +3 -3
@@ -0,0 +1,332 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import * as DialogPrimitive from "@radix-ui/react-dialog"
5
+ import { XIcon } from "lucide-react"
6
+
7
+ import { cn } from "../../lib/utils"
8
+ import { usePortalThemeClass } from "../../hooks/use-portal-theme-class"
9
+ import { Button } from "./button"
10
+
11
+ /**
12
+ * Tour — guided product tour overlay with step progression.
13
+ *
14
+ * A sequenced, selector-anchored walkthrough: each step points at an existing
15
+ * element on the page (by CSS selector), highlights it with a primary ring,
16
+ * and shows a popover-styled panel with title, description, text progress
17
+ * ("Step N of M"), and Back / Next / Finish controls.
18
+ *
19
+ * Built on Radix Dialog internally, so the focus trap, Escape-to-close,
20
+ * scroll lock, and focus-return-to-opener all come from the most
21
+ * a11y-correct base. The page behind the tour is intentionally inert while
22
+ * the tour is open — a tour narrates chrome, it doesn't operate it.
23
+ *
24
+ * Steps with `target: null` (or a selector that matches nothing — e.g.
25
+ * responsive chrome that isn't rendered) render centered, like a welcome or
26
+ * wrap-up step. The panel and highlight glide between steps via token-driven
27
+ * transitions (pattern c); open/close uses the same animate-in/out vocabulary
28
+ * as Dialog (pattern a).
29
+ */
30
+
31
+ interface TourStep {
32
+ /** Step heading, rendered as the dialog title. */
33
+ title: React.ReactNode
34
+ /** Supporting copy for the step. Optional. */
35
+ description?: React.ReactNode
36
+ /**
37
+ * CSS selector for the element this step points at
38
+ * (e.g. `"#save-button"`, `"[data-tour='filters']"`).
39
+ * `null` / `undefined` / no match ⇒ the step renders centered.
40
+ */
41
+ target?: string | null
42
+ /** Which side of the target the panel sits on. Default `"bottom"`. */
43
+ placement?: "top" | "bottom" | "left" | "right"
44
+ }
45
+
46
+ interface TourLabels {
47
+ back?: string
48
+ next?: string
49
+ finish?: string
50
+ close?: string
51
+ /** Formats the text progress indicator. Default: `Step ${current} of ${total}`. */
52
+ progress?: (current: number, total: number) => string
53
+ }
54
+
55
+ interface TourProps {
56
+ /** The ordered steps of the tour. */
57
+ steps: TourStep[]
58
+ /** Whether the tour is open. Fully controlled. */
59
+ open: boolean
60
+ /** Called when the tour requests open/close (Escape, close button, Finish). */
61
+ onOpenChange: (open: boolean) => void
62
+ /** Controlled 0-based step index. Omit for uncontrolled stepping. */
63
+ step?: number
64
+ /** Uncontrolled initial step index. Default 0. */
65
+ defaultStep?: number
66
+ /** Called whenever the active step changes (Back/Next/arrow keys). */
67
+ onStepChange?: (step: number) => void
68
+ /** Called when Finish is pressed on the last step (the tour also closes). */
69
+ onComplete?: () => void
70
+ /** Override the built-in button / progress strings (i18n). */
71
+ labels?: TourLabels
72
+ /** Accessible name for the tour dialog. Default "Product tour". */
73
+ "aria-label"?: string
74
+ }
75
+
76
+ type TargetRect = { top: number; left: number; width: number; height: number }
77
+
78
+ const HIGHLIGHT_PADDING = 6
79
+ const PANEL_GAP = 12
80
+ const VIEWPORT_MARGIN = 16
81
+
82
+ function measureTarget(selector: string | null | undefined): TargetRect | null {
83
+ if (!selector || typeof document === "undefined") return null
84
+ const el = document.querySelector(selector)
85
+ if (!el) return null
86
+ el.scrollIntoView({ block: "nearest", inline: "nearest" })
87
+ const r = el.getBoundingClientRect()
88
+ return { top: r.top, left: r.left, width: r.width, height: r.height }
89
+ }
90
+
91
+ function clamp(value: number, min: number, max: number) {
92
+ return Math.min(Math.max(value, min), Math.max(min, max))
93
+ }
94
+
95
+ function computePanelPosition(
96
+ target: TargetRect,
97
+ placement: NonNullable<TourStep["placement"]>,
98
+ panel: { width: number; height: number }
99
+ ): { top: number; left: number } {
100
+ const centerX = target.left + target.width / 2 - panel.width / 2
101
+ const centerY = target.top + target.height / 2 - panel.height / 2
102
+ let top: number
103
+ let left: number
104
+ switch (placement) {
105
+ case "top":
106
+ top = target.top - HIGHLIGHT_PADDING - PANEL_GAP - panel.height
107
+ left = centerX
108
+ break
109
+ case "left":
110
+ top = centerY
111
+ left = target.left - HIGHLIGHT_PADDING - PANEL_GAP - panel.width
112
+ break
113
+ case "right":
114
+ top = centerY
115
+ left = target.left + target.width + HIGHLIGHT_PADDING + PANEL_GAP
116
+ break
117
+ case "bottom":
118
+ default:
119
+ top = target.top + target.height + HIGHLIGHT_PADDING + PANEL_GAP
120
+ left = centerX
121
+ break
122
+ }
123
+ if (typeof window !== "undefined") {
124
+ top = clamp(top, VIEWPORT_MARGIN, window.innerHeight - panel.height - VIEWPORT_MARGIN)
125
+ left = clamp(left, VIEWPORT_MARGIN, window.innerWidth - panel.width - VIEWPORT_MARGIN)
126
+ }
127
+ return { top, left }
128
+ }
129
+
130
+ function Tour({
131
+ steps,
132
+ open,
133
+ onOpenChange,
134
+ step,
135
+ defaultStep = 0,
136
+ onStepChange,
137
+ onComplete,
138
+ labels,
139
+ "aria-label": ariaLabel = "Product tour",
140
+ }: TourProps) {
141
+ const [uncontrolledStep, setUncontrolledStep] = React.useState(defaultStep)
142
+ const activeIndex = clamp(step ?? uncontrolledStep, 0, Math.max(steps.length - 1, 0))
143
+ const activeStep = steps[activeIndex]
144
+ const total = steps.length
145
+ const isFirst = activeIndex === 0
146
+ const isLast = activeIndex === total - 1
147
+
148
+ const [targetRect, setTargetRect] = React.useState<TargetRect | null>(null)
149
+ const [panelPos, setPanelPos] = React.useState<{ top: number; left: number } | null>(null)
150
+ const panelRef = React.useRef<HTMLDivElement | null>(null)
151
+ const portalThemeClass = usePortalThemeClass()
152
+
153
+ const setStep = React.useCallback(
154
+ (next: number) => {
155
+ const clamped = clamp(next, 0, total - 1)
156
+ if (step === undefined) setUncontrolledStep(clamped)
157
+ onStepChange?.(clamped)
158
+ },
159
+ [step, total, onStepChange]
160
+ )
161
+
162
+ const reposition = React.useCallback(() => {
163
+ const rect = measureTarget(activeStep?.target)
164
+ setTargetRect(rect)
165
+ if (rect && panelRef.current) {
166
+ const { offsetWidth, offsetHeight } = panelRef.current
167
+ setPanelPos(
168
+ computePanelPosition(rect, activeStep?.placement ?? "bottom", {
169
+ width: offsetWidth,
170
+ height: offsetHeight,
171
+ })
172
+ )
173
+ } else {
174
+ setPanelPos(null)
175
+ }
176
+ }, [activeStep])
177
+
178
+ // Measure after the panel renders (its size feeds the position), and track
179
+ // viewport changes while open. Scroll uses capture so nested scroll
180
+ // containers reposition the highlight too.
181
+ React.useLayoutEffect(() => {
182
+ if (!open) return
183
+ reposition()
184
+ window.addEventListener("resize", reposition)
185
+ window.addEventListener("scroll", reposition, true)
186
+ return () => {
187
+ window.removeEventListener("resize", reposition)
188
+ window.removeEventListener("scroll", reposition, true)
189
+ }
190
+ }, [open, reposition])
191
+
192
+ // Reset to the first step whenever the tour reopens (uncontrolled mode).
193
+ React.useEffect(() => {
194
+ if (open && step === undefined) setUncontrolledStep(defaultStep)
195
+ // eslint-disable-next-line react-hooks/exhaustive-deps
196
+ }, [open])
197
+
198
+ if (total === 0) return null
199
+
200
+ const handleNext = () => {
201
+ if (isLast) {
202
+ onComplete?.()
203
+ onOpenChange(false)
204
+ } else {
205
+ setStep(activeIndex + 1)
206
+ }
207
+ }
208
+
209
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
210
+ if (event.key === "ArrowRight" && !isLast) {
211
+ event.preventDefault()
212
+ setStep(activeIndex + 1)
213
+ } else if (event.key === "ArrowLeft" && !isFirst) {
214
+ event.preventDefault()
215
+ setStep(activeIndex - 1)
216
+ }
217
+ }
218
+
219
+ const anchored = targetRect !== null && panelPos !== null
220
+ const progressText =
221
+ labels?.progress?.(activeIndex + 1, total) ?? `Step ${activeIndex + 1} of ${total}`
222
+ const hasDescription = activeStep.description != null
223
+
224
+ return (
225
+ <DialogPrimitive.Root data-slot="tour" open={open} onOpenChange={onOpenChange}>
226
+ <DialogPrimitive.Portal>
227
+ <DialogPrimitive.Overlay
228
+ data-slot="tour-overlay"
229
+ // No backdrop blur — the page being toured must stay readable.
230
+ className="fixed inset-0 z-50 bg-black/40 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0"
231
+ />
232
+ {targetRect !== null && (
233
+ <div
234
+ data-slot="tour-highlight"
235
+ aria-hidden="true"
236
+ className="pointer-events-none fixed z-50 rounded-md ring-2 ring-primary transition-[top,left,width,height]"
237
+ style={{
238
+ top: targetRect.top - HIGHLIGHT_PADDING,
239
+ left: targetRect.left - HIGHLIGHT_PADDING,
240
+ width: targetRect.width + HIGHLIGHT_PADDING * 2,
241
+ height: targetRect.height + HIGHLIGHT_PADDING * 2,
242
+ transitionDuration: "var(--duration-base)",
243
+ transitionTimingFunction: "var(--ease-out)",
244
+ }}
245
+ />
246
+ )}
247
+ <DialogPrimitive.Content
248
+ ref={panelRef}
249
+ data-slot="tour-content"
250
+ data-anchored={anchored ? "" : undefined}
251
+ aria-label={ariaLabel}
252
+ // Steps without a description silence Radix's missing-Description
253
+ // warning; steps with one keep the automatic aria-describedby wiring.
254
+ {...(hasDescription ? {} : { "aria-describedby": undefined })}
255
+ onKeyDown={handleKeyDown}
256
+ className={cn(
257
+ "fixed z-50 flex w-80 max-w-[calc(100vw-2rem)] flex-col gap-3 rounded-lg border bg-popover p-5 text-popover-foreground shadow-md outline-none",
258
+ "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
259
+ // Anchored panels glide between step targets (pattern c, inline
260
+ // vars below); centered panels use the Dialog translate trick.
261
+ anchored ? "transition-[top,left]" : "left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2",
262
+ portalThemeClass
263
+ )}
264
+ style={
265
+ anchored
266
+ ? {
267
+ top: panelPos.top,
268
+ left: panelPos.left,
269
+ transitionDuration: "var(--duration-base)",
270
+ transitionTimingFunction: "var(--ease-out)",
271
+ }
272
+ : undefined
273
+ }
274
+ >
275
+ <div className="flex flex-col gap-1 pr-10">
276
+ <DialogPrimitive.Title
277
+ data-slot="tour-title"
278
+ className="text-sm leading-snug font-semibold"
279
+ >
280
+ {activeStep.title}
281
+ </DialogPrimitive.Title>
282
+ {hasDescription && (
283
+ <DialogPrimitive.Description
284
+ data-slot="tour-description"
285
+ className="text-sm text-muted-foreground"
286
+ >
287
+ {activeStep.description}
288
+ </DialogPrimitive.Description>
289
+ )}
290
+ </div>
291
+ <div className="flex items-center justify-between gap-3">
292
+ <p
293
+ data-slot="tour-progress"
294
+ aria-live="polite"
295
+ className="text-xs text-muted-foreground tabular-nums"
296
+ >
297
+ {progressText}
298
+ </p>
299
+ <div data-slot="tour-footer" className="flex items-center gap-2">
300
+ <Button
301
+ variant="outline"
302
+ size="xl"
303
+ className="px-4"
304
+ disabled={isFirst}
305
+ onClick={() => setStep(activeIndex - 1)}
306
+ >
307
+ {labels?.back ?? "Back"}
308
+ </Button>
309
+ <Button size="xl" className="px-4" onClick={handleNext}>
310
+ {isLast ? (labels?.finish ?? "Finish") : (labels?.next ?? "Next")}
311
+ </Button>
312
+ </div>
313
+ </div>
314
+ <DialogPrimitive.Close
315
+ data-slot="tour-close"
316
+ aria-label={labels?.close ?? "Close tour"}
317
+ className="absolute top-1 right-1 inline-flex size-11 items-center justify-center rounded-md text-muted-foreground opacity-70 transition-opacity hover:opacity-100 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:outline-none"
318
+ style={{
319
+ transitionDuration: "var(--duration-fast)",
320
+ transitionTimingFunction: "var(--ease-out)",
321
+ }}
322
+ >
323
+ <XIcon strokeWidth={1.5} className="size-4" aria-hidden="true" />
324
+ </DialogPrimitive.Close>
325
+ </DialogPrimitive.Content>
326
+ </DialogPrimitive.Portal>
327
+ </DialogPrimitive.Root>
328
+ )
329
+ }
330
+
331
+ export { Tour }
332
+ export type { TourProps, TourStep, TourLabels }
@@ -0,0 +1,343 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import {
5
+ ChevronRight,
6
+ ChevronLeft,
7
+ ChevronsRight,
8
+ ChevronsLeft,
9
+ } from "lucide-react"
10
+
11
+ import { cn } from "../../lib/utils"
12
+ import { Button } from "../ui/button"
13
+ import { Badge } from "../ui/badge"
14
+ import { Input } from "../ui/input"
15
+
16
+ // ────────────────────────────────────────────────────────────────────────────
17
+ // Types
18
+ // ────────────────────────────────────────────────────────────────────────────
19
+
20
+ export interface TransferListProps {
21
+ /** Items in the left (available) panel */
22
+ available: string[]
23
+ /** Items in the right (selected) panel */
24
+ selected: string[]
25
+ /** Callback when the left panel changes */
26
+ onAvailableChange: (items: string[]) => void
27
+ /** Callback when the right panel changes */
28
+ onSelectedChange: (items: string[]) => void
29
+ /** Label for the left panel (default: "Available") */
30
+ availableLabel?: string
31
+ /** Label for the right panel (default: "Selected") */
32
+ selectedLabel?: string
33
+ /** Show a filter input in each panel (default: true) */
34
+ searchable?: boolean
35
+ /** Additional class name for the root element */
36
+ className?: string
37
+ }
38
+
39
+ // ────────────────────────────────────────────────────────────────────────────
40
+ // Panel sub-component
41
+ // ────────────────────────────────────────────────────────────────────────────
42
+
43
+ interface PanelProps {
44
+ label: string
45
+ items: string[]
46
+ checked: Set<string>
47
+ onToggle: (item: string) => void
48
+ searchable: boolean
49
+ }
50
+
51
+ function Panel({ label, items, checked, onToggle, searchable }: PanelProps) {
52
+ const [filter, setFilter] = React.useState("")
53
+ const [activeIdx, setActiveIdx] = React.useState(-1)
54
+ const listboxId = React.useId()
55
+
56
+ const visible = filter
57
+ ? items.filter((it) => it.toLowerCase().includes(filter.toLowerCase()))
58
+ : items
59
+
60
+ // Reset active index when visible list changes (filter or items update)
61
+ React.useEffect(() => {
62
+ setActiveIdx(-1)
63
+ }, [filter, items])
64
+
65
+ function handleKeyDown(e: React.KeyboardEvent<HTMLUListElement>) {
66
+ if (visible.length === 0) return
67
+ if (e.key === "ArrowDown") {
68
+ e.preventDefault()
69
+ setActiveIdx((prev) => (prev + 1) % visible.length)
70
+ } else if (e.key === "ArrowUp") {
71
+ e.preventDefault()
72
+ setActiveIdx((prev) => (prev <= 0 ? visible.length - 1 : prev - 1))
73
+ } else if (e.key === " " || e.key === "Enter") {
74
+ e.preventDefault()
75
+ if (activeIdx >= 0 && activeIdx < visible.length) {
76
+ onToggle(visible[activeIdx])
77
+ }
78
+ }
79
+ }
80
+
81
+ const activeItemId =
82
+ activeIdx >= 0 && activeIdx < visible.length
83
+ ? `${listboxId}-opt-${activeIdx}`
84
+ : undefined
85
+
86
+ return (
87
+ <div className="w-56 min-h-48 max-h-72 rounded-md border border-border bg-background overflow-hidden flex flex-col">
88
+ {/* Panel header */}
89
+ <div className="flex items-center justify-between px-3 py-2 border-b border-border shrink-0">
90
+ <span className="text-sm font-medium text-foreground">{label}</span>
91
+ <Badge variant="secondary">{items.length}</Badge>
92
+ </div>
93
+
94
+ {/* Search filter */}
95
+ {searchable && (
96
+ <div className="px-2 py-1.5 border-b border-border shrink-0">
97
+ <Input
98
+ size="sm"
99
+ type="search"
100
+ placeholder="Filter…"
101
+ value={filter}
102
+ onChange={(e) => setFilter(e.target.value)}
103
+ aria-label={`Filter ${label} items`}
104
+ />
105
+ </div>
106
+ )}
107
+
108
+ {/* Item list — container is the sole focusable target; items use
109
+ aria-activedescendant so screen readers track the active option
110
+ without each item needing its own tabIndex (nested-interactive). */}
111
+ <ul
112
+ id={listboxId}
113
+ role="listbox"
114
+ aria-multiselectable="true"
115
+ aria-label={label}
116
+ aria-activedescendant={activeItemId}
117
+ tabIndex={0}
118
+ onKeyDown={handleKeyDown}
119
+ className="overflow-y-auto flex-1 py-1 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
120
+ >
121
+ {visible.length === 0 ? (
122
+ <li
123
+ role="option"
124
+ aria-disabled="true"
125
+ aria-selected="false"
126
+ className="px-3 py-2 text-sm text-muted-foreground select-none"
127
+ >
128
+ No items
129
+ </li>
130
+ ) : (
131
+ visible.map((item, idx) => {
132
+ const isChecked = checked.has(item)
133
+ const isActive = idx === activeIdx
134
+ return (
135
+ <li
136
+ key={item}
137
+ id={`${listboxId}-opt-${idx}`}
138
+ role="option"
139
+ aria-selected={isChecked}
140
+ onClick={() => {
141
+ setActiveIdx(idx)
142
+ onToggle(item)
143
+ }}
144
+ className={cn(
145
+ "flex items-center gap-2 px-2 py-1.5 cursor-pointer select-none text-sm",
146
+ "transition-colors",
147
+ isChecked
148
+ ? "bg-accent text-accent-foreground"
149
+ : "hover:bg-muted",
150
+ isActive && "ring-1 ring-ring ring-inset"
151
+ )}
152
+ style={{
153
+ transitionDuration: "var(--duration-fast)",
154
+ transitionTimingFunction: "var(--ease-out)",
155
+ }}
156
+ >
157
+ {/* Visual checkbox indicator */}
158
+ <span
159
+ aria-hidden="true"
160
+ className={cn(
161
+ "inline-flex shrink-0 size-4 items-center justify-center rounded-sm border",
162
+ isChecked
163
+ ? "border-primary bg-primary text-primary-foreground"
164
+ : "border-muted-foreground/60 bg-background"
165
+ )}
166
+ >
167
+ {isChecked && (
168
+ <svg
169
+ width="10"
170
+ height="8"
171
+ viewBox="0 0 10 8"
172
+ fill="none"
173
+ stroke="currentColor"
174
+ strokeWidth="1.5"
175
+ strokeLinecap="round"
176
+ strokeLinejoin="round"
177
+ aria-hidden="true"
178
+ >
179
+ <polyline points="1 4 3.5 6.5 9 1" />
180
+ </svg>
181
+ )}
182
+ </span>
183
+ <span className="min-w-0 truncate" title={item}>
184
+ {item}
185
+ </span>
186
+ </li>
187
+ )
188
+ })
189
+ )}
190
+ </ul>
191
+ </div>
192
+ )
193
+ }
194
+
195
+ // ────────────────────────────────────────────────────────────────────────────
196
+ // TransferList
197
+ // ────────────────────────────────────────────────────────────────────────────
198
+
199
+ function TransferList({
200
+ available,
201
+ selected,
202
+ onAvailableChange,
203
+ onSelectedChange,
204
+ availableLabel = "Available",
205
+ selectedLabel = "Selected",
206
+ searchable = true,
207
+ className,
208
+ }: TransferListProps) {
209
+ // Independently track which items are checked in each panel
210
+ const [leftChecked, setLeftChecked] = React.useState<Set<string>>(new Set())
211
+ const [rightChecked, setRightChecked] = React.useState<Set<string>>(new Set())
212
+
213
+ // Toggle an item's checked state in the given panel
214
+ function toggleLeft(item: string) {
215
+ setLeftChecked((prev) => {
216
+ const next = new Set(prev)
217
+ if (next.has(item)) next.delete(item)
218
+ else next.add(item)
219
+ return next
220
+ })
221
+ }
222
+
223
+ function toggleRight(item: string) {
224
+ setRightChecked((prev) => {
225
+ const next = new Set(prev)
226
+ if (next.has(item)) next.delete(item)
227
+ else next.add(item)
228
+ return next
229
+ })
230
+ }
231
+
232
+ // Move checked items from available → selected
233
+ function moveCheckedRight() {
234
+ const toMove = available.filter((it) => leftChecked.has(it))
235
+ if (toMove.length === 0) return
236
+ onAvailableChange(available.filter((it) => !leftChecked.has(it)))
237
+ onSelectedChange([...selected, ...toMove])
238
+ setLeftChecked(new Set())
239
+ }
240
+
241
+ // Move ALL items from available → selected
242
+ function moveAllRight() {
243
+ if (available.length === 0) return
244
+ onSelectedChange([...selected, ...available])
245
+ onAvailableChange([])
246
+ setLeftChecked(new Set())
247
+ }
248
+
249
+ // Move checked items from selected → available
250
+ function moveCheckedLeft() {
251
+ const toMove = selected.filter((it) => rightChecked.has(it))
252
+ if (toMove.length === 0) return
253
+ onSelectedChange(selected.filter((it) => !rightChecked.has(it)))
254
+ onAvailableChange([...available, ...toMove])
255
+ setRightChecked(new Set())
256
+ }
257
+
258
+ // Move ALL items from selected → available
259
+ function moveAllLeft() {
260
+ if (selected.length === 0) return
261
+ onAvailableChange([...available, ...selected])
262
+ onSelectedChange([])
263
+ setRightChecked(new Set())
264
+ }
265
+
266
+ const hasLeftChecked = leftChecked.size > 0
267
+ const hasRightChecked = rightChecked.size > 0
268
+
269
+ return (
270
+ <div
271
+ role="group"
272
+ aria-label="Transfer list"
273
+ className={cn("flex items-center gap-3", className)}
274
+ >
275
+ {/* Left panel — Available */}
276
+ <Panel
277
+ label={availableLabel}
278
+ items={available}
279
+ checked={leftChecked}
280
+ onToggle={toggleLeft}
281
+ searchable={searchable}
282
+ />
283
+
284
+ {/* Transfer controls */}
285
+ <div className="flex flex-col items-center gap-2" aria-label="Transfer controls">
286
+ <Button
287
+ variant="outline"
288
+ size="icon-sm"
289
+ onClick={moveCheckedRight}
290
+ disabled={!hasLeftChecked}
291
+ aria-label={`Move selected to ${selectedLabel}`}
292
+ title={`Move selected to ${selectedLabel}`}
293
+ >
294
+ <ChevronRight strokeWidth={1.5} />
295
+ </Button>
296
+
297
+ <Button
298
+ variant="outline"
299
+ size="icon-sm"
300
+ onClick={moveAllRight}
301
+ disabled={available.length === 0}
302
+ aria-label={`Move all to ${selectedLabel}`}
303
+ title={`Move all to ${selectedLabel}`}
304
+ >
305
+ <ChevronsRight strokeWidth={1.5} />
306
+ </Button>
307
+
308
+ <Button
309
+ variant="outline"
310
+ size="icon-sm"
311
+ onClick={moveCheckedLeft}
312
+ disabled={!hasRightChecked}
313
+ aria-label={`Move selected to ${availableLabel}`}
314
+ title={`Move selected to ${availableLabel}`}
315
+ >
316
+ <ChevronLeft strokeWidth={1.5} />
317
+ </Button>
318
+
319
+ <Button
320
+ variant="outline"
321
+ size="icon-sm"
322
+ onClick={moveAllLeft}
323
+ disabled={selected.length === 0}
324
+ aria-label={`Move all to ${availableLabel}`}
325
+ title={`Move all to ${availableLabel}`}
326
+ >
327
+ <ChevronsLeft strokeWidth={1.5} />
328
+ </Button>
329
+ </div>
330
+
331
+ {/* Right panel — Selected */}
332
+ <Panel
333
+ label={selectedLabel}
334
+ items={selected}
335
+ checked={rightChecked}
336
+ onToggle={toggleRight}
337
+ searchable={searchable}
338
+ />
339
+ </div>
340
+ )
341
+ }
342
+
343
+ export { TransferList }