@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.
- package/package.json +5 -2
- package/scripts/build-tokens.cjs +87 -5
- package/src/app/preview/components/page.tsx +4745 -244
- package/src/app/shell.tsx +18 -6
- package/src/components/customize/brand-mark-section.tsx +1 -1
- package/src/components/customize-tab.tsx +2 -2
- package/src/components/home-tab.tsx +2 -2
- package/src/components/ui/action-bar.tsx +142 -0
- package/src/components/ui/affix.tsx +109 -0
- package/src/components/ui/alert.tsx +37 -3
- package/src/components/ui/anchor.tsx +136 -0
- package/src/components/ui/angle-slider.tsx +265 -0
- package/src/components/ui/autocomplete.tsx +223 -0
- package/src/components/ui/avatar.tsx +8 -2
- package/src/components/ui/blockquote.tsx +112 -0
- package/src/components/ui/bottom-navigation.tsx +144 -0
- package/src/components/ui/burger.tsx +117 -0
- package/src/components/ui/button.tsx +101 -3
- package/src/components/ui/calendar.tsx +1 -1
- package/src/components/ui/card.tsx +43 -5
- package/src/components/ui/carousel.tsx +10 -5
- package/src/components/ui/cascader.tsx +253 -0
- package/src/components/ui/checkbox-card.tsx +176 -0
- package/src/components/ui/checkbox-group.tsx +231 -0
- package/src/components/ui/checkbox.tsx +14 -3
- package/src/components/ui/chip.tsx +143 -0
- package/src/components/ui/circular-progress.tsx +2 -2
- package/src/components/ui/color-swatch.tsx +73 -0
- package/src/components/ui/copy-button.tsx +21 -4
- package/src/components/ui/date-picker.tsx +6 -3
- package/src/components/ui/date-range-picker.tsx +6 -3
- package/src/components/ui/float-label.tsx +114 -0
- package/src/components/ui/floating-action-button.tsx +179 -0
- package/src/components/ui/floating-panel.tsx +129 -0
- package/src/components/ui/format-number.tsx +122 -0
- package/src/components/ui/highlight.tsx +85 -0
- package/src/components/ui/image.tsx +195 -0
- package/src/components/ui/index.ts +51 -0
- package/src/components/ui/indicator.tsx +2 -2
- package/src/components/ui/input.tsx +48 -4
- package/src/components/ui/kbd.tsx +1 -1
- package/src/components/ui/link.tsx +158 -0
- package/src/components/ui/listbox.tsx +426 -0
- package/src/components/ui/loading-overlay.tsx +80 -0
- package/src/components/ui/marquee.tsx +193 -0
- package/src/components/ui/masonry.tsx +164 -0
- package/src/components/ui/meter.tsx +186 -0
- package/src/components/ui/motion-toggle.tsx +170 -0
- package/src/components/ui/notification.tsx +175 -0
- package/src/components/ui/overlay.tsx +63 -0
- package/src/components/ui/progress.tsx +95 -4
- package/src/components/ui/rating-group.tsx +1 -0
- package/src/components/ui/separator.tsx +57 -1
- package/src/components/ui/sidebar.tsx +1 -1
- package/src/components/ui/signature-pad.tsx +381 -0
- package/src/components/ui/skeleton.tsx +59 -2
- package/src/components/ui/skip-link.tsx +117 -0
- package/src/components/ui/slider.tsx +86 -1
- package/src/components/ui/spoiler.tsx +131 -0
- package/src/components/ui/tabs.tsx +1 -1
- package/src/components/ui/text.tsx +135 -0
- package/src/components/ui/textarea.tsx +66 -2
- package/src/components/ui/timeline.tsx +71 -14
- package/src/components/ui/timer.tsx +183 -0
- package/src/components/ui/tour.tsx +332 -0
- package/src/components/ui/transfer-list.tsx +343 -0
- package/src/components/ui/typing-indicator.tsx +96 -0
- package/src/components/ui/visually-hidden.tsx +42 -0
- package/src/components/ui/watermark.tsx +0 -0
- package/src/components/ui/word-rotate.tsx +82 -0
- package/src/data/docs-index.ts +10 -2
- package/src/stories/ActionBar.stories.tsx +201 -0
- package/src/stories/Affix.stories.tsx +165 -0
- package/src/stories/Alert.stories.tsx +58 -0
- package/src/stories/Anchor.stories.tsx +236 -0
- package/src/stories/AngleSlider.stories.tsx +128 -0
- package/src/stories/Autocomplete.stories.tsx +216 -0
- package/src/stories/Avatar.stories.tsx +73 -1
- package/src/stories/Blockquote.stories.tsx +148 -0
- package/src/stories/BottomNavigation.stories.tsx +195 -0
- package/src/stories/Burger.stories.tsx +79 -0
- package/src/stories/Button.stories.tsx +103 -2
- package/src/stories/Calendar.stories.tsx +4 -0
- package/src/stories/Card.stories.tsx +30 -0
- package/src/stories/Carousel.stories.tsx +2 -1
- package/src/stories/Cascader.stories.tsx +285 -0
- package/src/stories/Checkbox.stories.tsx +13 -0
- package/src/stories/CheckboxCard.stories.tsx +210 -0
- package/src/stories/CheckboxGroup.stories.tsx +186 -0
- package/src/stories/Chip.stories.tsx +173 -0
- package/src/stories/CircularProgress.stories.tsx +3 -0
- package/src/stories/ColorSwatch.stories.tsx +81 -0
- package/src/stories/CopyButton.stories.tsx +3 -3
- package/src/stories/DatePicker.stories.tsx +3 -0
- package/src/stories/DateRangePicker.stories.tsx +3 -0
- package/src/stories/Editable.stories.tsx +14 -0
- package/src/stories/FloatLabel.stories.tsx +148 -0
- package/src/stories/FloatingActionButton.stories.tsx +215 -0
- package/src/stories/FloatingPanel.stories.tsx +176 -0
- package/src/stories/FormatNumber.stories.tsx +308 -0
- package/src/stories/Highlight.stories.tsx +140 -0
- package/src/stories/Image.stories.tsx +141 -0
- package/src/stories/Indicator.stories.tsx +3 -0
- package/src/stories/Input.stories.tsx +33 -0
- package/src/stories/Kbd.stories.tsx +3 -0
- package/src/stories/Link.stories.tsx +198 -0
- package/src/stories/Listbox.stories.tsx +240 -0
- package/src/stories/LoadingOverlay.stories.tsx +148 -0
- package/src/stories/Marquee.stories.tsx +170 -0
- package/src/stories/Masonry.stories.tsx +227 -0
- package/src/stories/Meter.stories.tsx +150 -0
- package/src/stories/MotionToggle.stories.tsx +85 -0
- package/src/stories/Notification.stories.tsx +264 -0
- package/src/stories/Overlay.stories.tsx +169 -0
- package/src/stories/PasswordInput.stories.tsx +4 -0
- package/src/stories/Progress.stories.tsx +70 -27
- package/src/stories/RatingGroup.stories.tsx +5 -0
- package/src/stories/Separator.stories.tsx +41 -5
- package/src/stories/Sidebar.stories.tsx +2 -1
- package/src/stories/SignaturePad.stories.tsx +112 -0
- package/src/stories/Skeleton.stories.tsx +23 -4
- package/src/stories/SkipLink.stories.tsx +137 -0
- package/src/stories/Slider.stories.tsx +28 -0
- package/src/stories/Spoiler.stories.tsx +122 -0
- package/src/stories/Tabs.stories.tsx +2 -1
- package/src/stories/Text.stories.tsx +191 -0
- package/src/stories/Textarea.stories.tsx +58 -6
- package/src/stories/Timeline.stories.tsx +82 -0
- package/src/stories/Timer.stories.tsx +160 -0
- package/src/stories/Tour.stories.tsx +191 -0
- package/src/stories/TransferList.stories.tsx +190 -0
- package/src/stories/TypingIndicator.stories.tsx +139 -0
- package/src/stories/VisuallyHidden.stories.tsx +105 -0
- package/src/stories/Watermark.stories.tsx +212 -0
- package/src/stories/WordRotate.stories.tsx +101 -0
- package/src/styles.css +88 -6
- package/src/tokens.json +3 -3
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { ChevronRightIcon, CheckIcon, ChevronDownIcon } 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
|
+
export interface CascaderOption {
|
|
11
|
+
value: string
|
|
12
|
+
label: string
|
|
13
|
+
disabled?: boolean
|
|
14
|
+
children?: CascaderOption[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface CascaderProps {
|
|
18
|
+
options: CascaderOption[]
|
|
19
|
+
value?: string[]
|
|
20
|
+
onValueChange?: (value: string[], labels: string[]) => void
|
|
21
|
+
placeholder?: string
|
|
22
|
+
disabled?: boolean
|
|
23
|
+
className?: string
|
|
24
|
+
/** aria-label for the trigger button when no visual label is provided */
|
|
25
|
+
"aria-label"?: string
|
|
26
|
+
/** aria-labelledby pointing to an external label element */
|
|
27
|
+
"aria-labelledby"?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function Cascader({
|
|
31
|
+
options,
|
|
32
|
+
value,
|
|
33
|
+
onValueChange,
|
|
34
|
+
placeholder = "Select…",
|
|
35
|
+
disabled,
|
|
36
|
+
className,
|
|
37
|
+
"aria-label": ariaLabel,
|
|
38
|
+
"aria-labelledby": ariaLabelledBy,
|
|
39
|
+
}: CascaderProps) {
|
|
40
|
+
const [open, setOpen] = React.useState(false)
|
|
41
|
+
// Each entry in activePath is the selected value at that column index
|
|
42
|
+
const [activePath, setActivePath] = React.useState<string[]>(value ?? [])
|
|
43
|
+
const portalThemeClass = usePortalThemeClass()
|
|
44
|
+
|
|
45
|
+
// Sync activePath when controlled value changes
|
|
46
|
+
React.useEffect(() => {
|
|
47
|
+
if (value) setActivePath(value)
|
|
48
|
+
}, [value])
|
|
49
|
+
|
|
50
|
+
// Build the label string from selected path
|
|
51
|
+
function buildLabels(path: string[], opts: CascaderOption[]): string[] {
|
|
52
|
+
const labels: string[] = []
|
|
53
|
+
let current = opts
|
|
54
|
+
for (const val of path) {
|
|
55
|
+
const found = current.find((o) => o.value === val)
|
|
56
|
+
if (!found) break
|
|
57
|
+
labels.push(found.label)
|
|
58
|
+
current = found.children ?? []
|
|
59
|
+
}
|
|
60
|
+
return labels
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const selectedLabels = buildLabels(activePath, options)
|
|
64
|
+
const displayValue = selectedLabels.length > 0 ? selectedLabels.join(" / ") : null
|
|
65
|
+
|
|
66
|
+
// Determine columns to render: options at each depth given the active path
|
|
67
|
+
function buildColumns(path: string[], opts: CascaderOption[]): CascaderOption[][] {
|
|
68
|
+
const columns: CascaderOption[][] = [opts]
|
|
69
|
+
let current = opts
|
|
70
|
+
for (const val of path) {
|
|
71
|
+
const found = current.find((o) => o.value === val)
|
|
72
|
+
if (!found || !found.children?.length) break
|
|
73
|
+
columns.push(found.children)
|
|
74
|
+
current = found.children
|
|
75
|
+
}
|
|
76
|
+
return columns
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const columns = buildColumns(activePath, options)
|
|
80
|
+
|
|
81
|
+
function handleSelect(colIndex: number, option: CascaderOption) {
|
|
82
|
+
if (option.disabled) return
|
|
83
|
+
const newPath = [...activePath.slice(0, colIndex), option.value]
|
|
84
|
+
setActivePath(newPath)
|
|
85
|
+
// If option has no children, it's a leaf — commit selection
|
|
86
|
+
if (!option.children?.length) {
|
|
87
|
+
const labels = buildLabels(newPath, options)
|
|
88
|
+
onValueChange?.(newPath, labels)
|
|
89
|
+
setOpen(false)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function handleKeyDown(
|
|
94
|
+
e: React.KeyboardEvent<HTMLButtonElement>,
|
|
95
|
+
colIndex: number,
|
|
96
|
+
optIndex: number,
|
|
97
|
+
option: CascaderOption
|
|
98
|
+
) {
|
|
99
|
+
const colEl = (e.currentTarget.closest("[data-cascader-column]") as HTMLElement | null)
|
|
100
|
+
const allCols = colEl?.parentElement?.querySelectorAll<HTMLElement>("[data-cascader-column]")
|
|
101
|
+
|
|
102
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
103
|
+
e.preventDefault()
|
|
104
|
+
const buttons = colEl?.querySelectorAll<HTMLButtonElement>("[data-cascader-option]") ?? []
|
|
105
|
+
const nextIdx = e.key === "ArrowDown" ? optIndex + 1 : optIndex - 1
|
|
106
|
+
if (nextIdx >= 0 && nextIdx < buttons.length) buttons[nextIdx].focus()
|
|
107
|
+
} else if (e.key === "ArrowRight" && option.children?.length) {
|
|
108
|
+
e.preventDefault()
|
|
109
|
+
handleSelect(colIndex, option)
|
|
110
|
+
// Focus first item in the next column
|
|
111
|
+
setTimeout(() => {
|
|
112
|
+
if (allCols) {
|
|
113
|
+
const nextCol = allCols[colIndex + 1]
|
|
114
|
+
nextCol?.querySelector<HTMLButtonElement>("[data-cascader-option]")?.focus()
|
|
115
|
+
}
|
|
116
|
+
}, 0)
|
|
117
|
+
} else if (e.key === "ArrowLeft" && colIndex > 0) {
|
|
118
|
+
e.preventDefault()
|
|
119
|
+
if (allCols) {
|
|
120
|
+
const prevCol = allCols[colIndex - 1]
|
|
121
|
+
// Focus the active item in prev column
|
|
122
|
+
const active = prevCol?.querySelector<HTMLButtonElement>("[data-cascader-option][data-active]")
|
|
123
|
+
const first = prevCol?.querySelector<HTMLButtonElement>("[data-cascader-option]")
|
|
124
|
+
;(active ?? first)?.focus()
|
|
125
|
+
}
|
|
126
|
+
} else if (e.key === "Enter" || e.key === " ") {
|
|
127
|
+
e.preventDefault()
|
|
128
|
+
handleSelect(colIndex, option)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<PopoverPrimitive.Root open={open} onOpenChange={setOpen}>
|
|
134
|
+
<PopoverPrimitive.Trigger asChild>
|
|
135
|
+
<button
|
|
136
|
+
data-slot="cascader-trigger"
|
|
137
|
+
disabled={disabled}
|
|
138
|
+
aria-haspopup="dialog"
|
|
139
|
+
aria-expanded={open}
|
|
140
|
+
aria-label={ariaLabel}
|
|
141
|
+
aria-labelledby={ariaLabelledBy}
|
|
142
|
+
className={cn(
|
|
143
|
+
"flex min-h-9 w-full items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-xs outline-none",
|
|
144
|
+
"transition-[color,box-shadow]",
|
|
145
|
+
"focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
146
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
147
|
+
"aria-expanded:border-ring aria-expanded:ring-[3px] aria-expanded:ring-ring/50",
|
|
148
|
+
className
|
|
149
|
+
)}
|
|
150
|
+
>
|
|
151
|
+
<span
|
|
152
|
+
className={cn(
|
|
153
|
+
"min-w-0 flex-1 truncate text-left",
|
|
154
|
+
!displayValue && "text-muted-foreground"
|
|
155
|
+
)}
|
|
156
|
+
>
|
|
157
|
+
{displayValue ?? placeholder}
|
|
158
|
+
</span>
|
|
159
|
+
<ChevronDownIcon
|
|
160
|
+
strokeWidth={1.5}
|
|
161
|
+
aria-hidden
|
|
162
|
+
className={cn(
|
|
163
|
+
"size-4 shrink-0 text-muted-foreground",
|
|
164
|
+
"transition-transform",
|
|
165
|
+
{ "rotate-180": open }
|
|
166
|
+
)}
|
|
167
|
+
style={{
|
|
168
|
+
transitionDuration: "var(--duration-fast)",
|
|
169
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
170
|
+
}}
|
|
171
|
+
/>
|
|
172
|
+
</button>
|
|
173
|
+
</PopoverPrimitive.Trigger>
|
|
174
|
+
|
|
175
|
+
<PopoverPrimitive.Portal>
|
|
176
|
+
<PopoverPrimitive.Content
|
|
177
|
+
data-slot="cascader-content"
|
|
178
|
+
align="start"
|
|
179
|
+
sideOffset={4}
|
|
180
|
+
className={cn(
|
|
181
|
+
"z-50 flex overflow-hidden rounded-md border border-border bg-popover text-popover-foreground shadow-md",
|
|
182
|
+
"origin-(--radix-popover-content-transform-origin)",
|
|
183
|
+
"data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
|
|
184
|
+
"data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
|
|
185
|
+
portalThemeClass
|
|
186
|
+
)}
|
|
187
|
+
>
|
|
188
|
+
{columns.map((col, colIndex) => (
|
|
189
|
+
<ul
|
|
190
|
+
key={colIndex}
|
|
191
|
+
role="listbox"
|
|
192
|
+
aria-label={colIndex === 0 ? "Select category" : `Level ${colIndex + 1}`}
|
|
193
|
+
data-cascader-column
|
|
194
|
+
className={cn(
|
|
195
|
+
"min-w-[160px] overflow-y-auto py-1",
|
|
196
|
+
colIndex > 0 && "border-l border-border"
|
|
197
|
+
)}
|
|
198
|
+
style={{ maxHeight: "280px" }}
|
|
199
|
+
>
|
|
200
|
+
{col.map((option, optIndex) => {
|
|
201
|
+
const isActive = activePath[colIndex] === option.value
|
|
202
|
+
const hasChildren = !!option.children?.length
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<li key={option.value} role="presentation">
|
|
206
|
+
<button
|
|
207
|
+
role="option"
|
|
208
|
+
data-cascader-option
|
|
209
|
+
data-active={isActive ? "" : undefined}
|
|
210
|
+
aria-selected={isActive}
|
|
211
|
+
aria-disabled={option.disabled}
|
|
212
|
+
disabled={option.disabled}
|
|
213
|
+
tabIndex={optIndex === 0 && colIndex === 0 ? 0 : -1}
|
|
214
|
+
onClick={() => handleSelect(colIndex, option)}
|
|
215
|
+
onKeyDown={(e) => handleKeyDown(e, colIndex, optIndex, option)}
|
|
216
|
+
className={cn(
|
|
217
|
+
"flex w-full items-center gap-2 px-3 py-1.5 text-sm outline-none",
|
|
218
|
+
"transition-colors",
|
|
219
|
+
"focus-visible:bg-accent focus-visible:text-accent-foreground",
|
|
220
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
221
|
+
isActive
|
|
222
|
+
? "bg-accent text-accent-foreground"
|
|
223
|
+
: "hover:bg-accent hover:text-accent-foreground"
|
|
224
|
+
)}
|
|
225
|
+
style={{
|
|
226
|
+
transitionDuration: "var(--duration-fast)",
|
|
227
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
228
|
+
}}
|
|
229
|
+
>
|
|
230
|
+
<span className="min-w-0 flex-1 truncate text-left">{option.label}</span>
|
|
231
|
+
{isActive && !hasChildren && (
|
|
232
|
+
<CheckIcon strokeWidth={1.5} aria-hidden className="size-4 shrink-0" />
|
|
233
|
+
)}
|
|
234
|
+
{hasChildren && (
|
|
235
|
+
<ChevronRightIcon
|
|
236
|
+
strokeWidth={1.5}
|
|
237
|
+
aria-hidden
|
|
238
|
+
className="size-4 shrink-0 text-muted-foreground"
|
|
239
|
+
/>
|
|
240
|
+
)}
|
|
241
|
+
</button>
|
|
242
|
+
</li>
|
|
243
|
+
)
|
|
244
|
+
})}
|
|
245
|
+
</ul>
|
|
246
|
+
))}
|
|
247
|
+
</PopoverPrimitive.Content>
|
|
248
|
+
</PopoverPrimitive.Portal>
|
|
249
|
+
</PopoverPrimitive.Root>
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export { Cascader }
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* CheckboxCard / RadioCard — card-shaped selection controls where the entire
|
|
9
|
+
* card surface is the hit area. A visually-hidden native input inside the
|
|
10
|
+
* <label> carries all keyboard and assistive-technology behavior (implicit
|
|
11
|
+
* labeling — no htmlFor plumbing), so Space toggles a CheckboxCard and arrow
|
|
12
|
+
* keys move selection across RadioCards sharing a `name`.
|
|
13
|
+
*
|
|
14
|
+
* Checked visuals are keyed off CSS truth (`:checked` via has-[]) rather than
|
|
15
|
+
* React state alone, so uncontrolled native radio groups — where selecting a
|
|
16
|
+
* sibling deselects this card without React knowing — always paint correctly.
|
|
17
|
+
* `data-checked` mirrors the known React state as an external CSS/test hook.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const cardClasses = cn(
|
|
21
|
+
"relative flex min-h-11 cursor-pointer flex-col gap-1 rounded-lg border border-border bg-card p-4 shadow-sm transition-colors",
|
|
22
|
+
"hover:bg-accent/40",
|
|
23
|
+
"has-[:checked]:border-primary has-[:checked]:bg-primary/10"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const disabledClasses = "pointer-events-none cursor-not-allowed opacity-50"
|
|
27
|
+
|
|
28
|
+
const motionStyle: React.CSSProperties = {
|
|
29
|
+
transitionDuration: "var(--duration-fast)",
|
|
30
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Focus indicator lives on an inset overlay sibling of the sr-only input so
|
|
35
|
+
* the ring renders on the card surface. Ring is box-shadow — deliberately not
|
|
36
|
+
* in the transition-colors list, so it appears instantly (a11y: focus must
|
|
37
|
+
* not fade in).
|
|
38
|
+
*/
|
|
39
|
+
function FocusRing() {
|
|
40
|
+
return (
|
|
41
|
+
<span
|
|
42
|
+
aria-hidden="true"
|
|
43
|
+
className="pointer-events-none absolute inset-0 rounded-lg ring-ring ring-offset-background peer-focus-visible:ring-2 peer-focus-visible:ring-ring peer-focus-visible:ring-offset-2"
|
|
44
|
+
/>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface CheckboxCardProps
|
|
49
|
+
extends Omit<React.ComponentPropsWithoutRef<"label">, "htmlFor"> {
|
|
50
|
+
checked?: boolean
|
|
51
|
+
defaultChecked?: boolean
|
|
52
|
+
onCheckedChange?: (checked: boolean) => void
|
|
53
|
+
disabled?: boolean
|
|
54
|
+
value?: string
|
|
55
|
+
children?: React.ReactNode
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const CheckboxCard = React.forwardRef<HTMLLabelElement, CheckboxCardProps>(
|
|
59
|
+
(
|
|
60
|
+
{
|
|
61
|
+
className,
|
|
62
|
+
style,
|
|
63
|
+
checked,
|
|
64
|
+
defaultChecked,
|
|
65
|
+
onCheckedChange,
|
|
66
|
+
disabled,
|
|
67
|
+
value,
|
|
68
|
+
children,
|
|
69
|
+
...props
|
|
70
|
+
},
|
|
71
|
+
ref
|
|
72
|
+
) => {
|
|
73
|
+
const [internalChecked, setInternalChecked] = React.useState(
|
|
74
|
+
defaultChecked ?? false
|
|
75
|
+
)
|
|
76
|
+
const isControlled = checked !== undefined
|
|
77
|
+
const isChecked = isControlled ? checked : internalChecked
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<label
|
|
81
|
+
ref={ref}
|
|
82
|
+
data-slot="checkbox-card"
|
|
83
|
+
data-checked={isChecked || undefined}
|
|
84
|
+
data-disabled={disabled || undefined}
|
|
85
|
+
className={cn(cardClasses, disabled && disabledClasses, className)}
|
|
86
|
+
style={{ ...motionStyle, ...style }}
|
|
87
|
+
{...props}
|
|
88
|
+
>
|
|
89
|
+
<input
|
|
90
|
+
type="checkbox"
|
|
91
|
+
className="peer sr-only"
|
|
92
|
+
checked={isChecked}
|
|
93
|
+
onChange={(e) => {
|
|
94
|
+
if (!isControlled) setInternalChecked(e.target.checked)
|
|
95
|
+
onCheckedChange?.(e.target.checked)
|
|
96
|
+
}}
|
|
97
|
+
disabled={disabled}
|
|
98
|
+
value={value}
|
|
99
|
+
/>
|
|
100
|
+
<FocusRing />
|
|
101
|
+
{children}
|
|
102
|
+
</label>
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
CheckboxCard.displayName = "CheckboxCard"
|
|
107
|
+
|
|
108
|
+
interface RadioCardProps
|
|
109
|
+
extends Omit<
|
|
110
|
+
React.ComponentPropsWithoutRef<"label">,
|
|
111
|
+
"htmlFor" | "onChange"
|
|
112
|
+
> {
|
|
113
|
+
checked?: boolean
|
|
114
|
+
defaultChecked?: boolean
|
|
115
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void
|
|
116
|
+
disabled?: boolean
|
|
117
|
+
value?: string
|
|
118
|
+
name?: string
|
|
119
|
+
children?: React.ReactNode
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const RadioCard = React.forwardRef<HTMLLabelElement, RadioCardProps>(
|
|
123
|
+
(
|
|
124
|
+
{
|
|
125
|
+
className,
|
|
126
|
+
style,
|
|
127
|
+
checked,
|
|
128
|
+
defaultChecked,
|
|
129
|
+
onChange,
|
|
130
|
+
disabled,
|
|
131
|
+
value,
|
|
132
|
+
name,
|
|
133
|
+
children,
|
|
134
|
+
...props
|
|
135
|
+
},
|
|
136
|
+
ref
|
|
137
|
+
) => {
|
|
138
|
+
const [internalChecked, setInternalChecked] = React.useState(
|
|
139
|
+
defaultChecked ?? false
|
|
140
|
+
)
|
|
141
|
+
const isControlled = checked !== undefined
|
|
142
|
+
const isChecked = isControlled ? checked : internalChecked
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<label
|
|
146
|
+
ref={ref}
|
|
147
|
+
data-slot="radio-card"
|
|
148
|
+
data-checked={isChecked || undefined}
|
|
149
|
+
data-disabled={disabled || undefined}
|
|
150
|
+
className={cn(cardClasses, disabled && disabledClasses, className)}
|
|
151
|
+
style={{ ...motionStyle, ...style }}
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
<input
|
|
155
|
+
type="radio"
|
|
156
|
+
className="peer sr-only"
|
|
157
|
+
checked={isControlled ? checked : undefined}
|
|
158
|
+
defaultChecked={isControlled ? undefined : defaultChecked}
|
|
159
|
+
onChange={(e) => {
|
|
160
|
+
if (!isControlled) setInternalChecked(e.target.checked)
|
|
161
|
+
onChange?.(e)
|
|
162
|
+
}}
|
|
163
|
+
disabled={disabled}
|
|
164
|
+
value={value}
|
|
165
|
+
name={name}
|
|
166
|
+
/>
|
|
167
|
+
<FocusRing />
|
|
168
|
+
{children}
|
|
169
|
+
</label>
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
RadioCard.displayName = "RadioCard"
|
|
174
|
+
|
|
175
|
+
export { CheckboxCard, RadioCard }
|
|
176
|
+
export type { CheckboxCardProps, RadioCardProps }
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { Checkbox } from "./checkbox";
|
|
6
|
+
import { cn } from "../../lib/utils";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Group orchestration for Checkbox — the multi-select sibling of RadioGroup.
|
|
10
|
+
* Owns the shared `string[]` value (controlled or uncontrolled), propagates
|
|
11
|
+
* disabled + form `name` to every item, and derives a select-all parent's
|
|
12
|
+
* tri-state (checked / indeterminate / unchecked) from `allValues`.
|
|
13
|
+
*
|
|
14
|
+
* Rendering is delegated entirely to the existing BADS Checkbox — this file
|
|
15
|
+
* ships state logic, not a new box.
|
|
16
|
+
*
|
|
17
|
+
* <CheckboxGroup
|
|
18
|
+
* aria-label="Notification preferences"
|
|
19
|
+
* allValues={["email", "push", "sms"]}
|
|
20
|
+
* defaultValue={["email"]}
|
|
21
|
+
* >
|
|
22
|
+
* <CheckboxGroupSelectAll id="all" /> <Label htmlFor="all">Select all</Label>
|
|
23
|
+
* <CheckboxGroupItem value="email" id="email" /> <Label htmlFor="email">Email</Label>
|
|
24
|
+
* <CheckboxGroupItem value="push" id="push" /> <Label htmlFor="push">Push</Label>
|
|
25
|
+
* <CheckboxGroupItem value="sms" id="sms" /> <Label htmlFor="sms">SMS</Label>
|
|
26
|
+
* </CheckboxGroup>
|
|
27
|
+
*
|
|
28
|
+
* The group needs an accessible name: pass `aria-label` / `aria-labelledby`,
|
|
29
|
+
* or wrap the group in <Fieldset> + <FieldsetLegend> for a native one.
|
|
30
|
+
*
|
|
31
|
+
* `allValues` (API shape from Base UI's Checkbox Group parent checkbox) lists
|
|
32
|
+
* the values select-all governs — include only the values a user could toggle
|
|
33
|
+
* themselves. Leave disabled items' values out and select-all will never
|
|
34
|
+
* touch them: toggling off removes ONLY the allValues entries, so a
|
|
35
|
+
* disabled-but-checked value survives a "clear all" (the user couldn't have
|
|
36
|
+
* unchecked it by hand either — matches Ant Design's check-all behavior).
|
|
37
|
+
* Deriving from a prop instead of item registration also means the server
|
|
38
|
+
* render already shows the correct tri-state — no post-hydration flicker.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type CheckboxGroupContextValue = {
|
|
42
|
+
value: string[];
|
|
43
|
+
toggle: (itemValue: string, checked: boolean) => void;
|
|
44
|
+
setValue: (next: string[]) => void;
|
|
45
|
+
disabled: boolean;
|
|
46
|
+
name?: string;
|
|
47
|
+
allValues?: string[];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const CheckboxGroupContext =
|
|
51
|
+
React.createContext<CheckboxGroupContextValue | null>(null);
|
|
52
|
+
|
|
53
|
+
function useCheckboxGroupContext(caller: string) {
|
|
54
|
+
const ctx = React.useContext(CheckboxGroupContext);
|
|
55
|
+
if (!ctx) {
|
|
56
|
+
throw new Error(`${caller} must be rendered inside a <CheckboxGroup>.`);
|
|
57
|
+
}
|
|
58
|
+
return ctx;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface CheckboxGroupProps
|
|
62
|
+
extends Omit<React.ComponentProps<"div">, "defaultValue" | "onChange"> {
|
|
63
|
+
/** Controlled selected values. */
|
|
64
|
+
value?: string[];
|
|
65
|
+
/** Uncontrolled initial selected values. */
|
|
66
|
+
defaultValue?: string[];
|
|
67
|
+
/** Called with the full next value array on every change. */
|
|
68
|
+
onValueChange?: (value: string[]) => void;
|
|
69
|
+
/** Disables every item (and select-all) in the group. */
|
|
70
|
+
disabled?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Form field name forwarded to each item's hidden input (Radix renders one
|
|
73
|
+
* per checked item inside a <form>), so the group submits like a native
|
|
74
|
+
* checkbox list: name=value for every checked item.
|
|
75
|
+
*/
|
|
76
|
+
name?: string;
|
|
77
|
+
/**
|
|
78
|
+
* The item values CheckboxGroupSelectAll governs (required when a
|
|
79
|
+
* SelectAll is rendered). List only user-toggleable values — leave
|
|
80
|
+
* disabled items out and select-all will never change them.
|
|
81
|
+
*/
|
|
82
|
+
allValues?: string[];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function CheckboxGroup({
|
|
86
|
+
className,
|
|
87
|
+
value: valueProp,
|
|
88
|
+
defaultValue,
|
|
89
|
+
onValueChange,
|
|
90
|
+
disabled = false,
|
|
91
|
+
name,
|
|
92
|
+
allValues,
|
|
93
|
+
children,
|
|
94
|
+
...props
|
|
95
|
+
}: CheckboxGroupProps) {
|
|
96
|
+
const [internalValue, setInternalValue] = React.useState<string[]>(
|
|
97
|
+
() => defaultValue ?? [],
|
|
98
|
+
);
|
|
99
|
+
const isControlled = valueProp !== undefined;
|
|
100
|
+
const value = isControlled ? valueProp : internalValue;
|
|
101
|
+
|
|
102
|
+
const onValueChangeRef = React.useRef(onValueChange);
|
|
103
|
+
React.useEffect(() => {
|
|
104
|
+
onValueChangeRef.current = onValueChange;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const setValue = React.useCallback(
|
|
108
|
+
(next: string[]) => {
|
|
109
|
+
if (!isControlled) setInternalValue(next);
|
|
110
|
+
onValueChangeRef.current?.(next);
|
|
111
|
+
},
|
|
112
|
+
[isControlled],
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const valueRef = React.useRef(value);
|
|
116
|
+
valueRef.current = value;
|
|
117
|
+
|
|
118
|
+
const toggle = React.useCallback(
|
|
119
|
+
(itemValue: string, checked: boolean) => {
|
|
120
|
+
const current = valueRef.current;
|
|
121
|
+
const next = checked
|
|
122
|
+
? current.includes(itemValue)
|
|
123
|
+
? current
|
|
124
|
+
: [...current, itemValue]
|
|
125
|
+
: current.filter((v) => v !== itemValue);
|
|
126
|
+
setValue(next);
|
|
127
|
+
},
|
|
128
|
+
[setValue],
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const ctx = React.useMemo<CheckboxGroupContextValue>(
|
|
132
|
+
() => ({ value, toggle, setValue, disabled, name, allValues }),
|
|
133
|
+
[value, toggle, setValue, disabled, name, allValues],
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<CheckboxGroupContext.Provider value={ctx}>
|
|
138
|
+
<div
|
|
139
|
+
role="group"
|
|
140
|
+
data-slot="checkbox-group"
|
|
141
|
+
data-disabled={disabled || undefined}
|
|
142
|
+
className={cn("grid gap-3", className)}
|
|
143
|
+
{...props}
|
|
144
|
+
>
|
|
145
|
+
{children}
|
|
146
|
+
</div>
|
|
147
|
+
</CheckboxGroupContext.Provider>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface CheckboxGroupItemProps
|
|
152
|
+
extends Omit<
|
|
153
|
+
React.ComponentProps<typeof Checkbox>,
|
|
154
|
+
"checked" | "defaultChecked" | "onCheckedChange" | "name" | "value"
|
|
155
|
+
> {
|
|
156
|
+
/** The value this item contributes to the group's value array. */
|
|
157
|
+
value: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function CheckboxGroupItem({
|
|
161
|
+
value: itemValue,
|
|
162
|
+
disabled: itemDisabled,
|
|
163
|
+
...props
|
|
164
|
+
}: CheckboxGroupItemProps) {
|
|
165
|
+
const ctx = useCheckboxGroupContext("CheckboxGroupItem");
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
<Checkbox
|
|
169
|
+
data-slot="checkbox-group-item"
|
|
170
|
+
checked={ctx.value.includes(itemValue)}
|
|
171
|
+
onCheckedChange={(checked) => ctx.toggle(itemValue, checked === true)}
|
|
172
|
+
disabled={ctx.disabled || itemDisabled}
|
|
173
|
+
name={ctx.name}
|
|
174
|
+
value={itemValue}
|
|
175
|
+
{...props}
|
|
176
|
+
/>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type CheckboxGroupSelectAllProps = Omit<
|
|
181
|
+
React.ComponentProps<typeof Checkbox>,
|
|
182
|
+
"checked" | "defaultChecked" | "onCheckedChange" | "name" | "value"
|
|
183
|
+
>;
|
|
184
|
+
|
|
185
|
+
function CheckboxGroupSelectAll({
|
|
186
|
+
disabled: selfDisabled,
|
|
187
|
+
...props
|
|
188
|
+
}: CheckboxGroupSelectAllProps) {
|
|
189
|
+
const ctx = useCheckboxGroupContext("CheckboxGroupSelectAll");
|
|
190
|
+
const allValues = ctx.allValues ?? [];
|
|
191
|
+
|
|
192
|
+
if (process.env.NODE_ENV !== "production" && ctx.allValues === undefined) {
|
|
193
|
+
console.warn(
|
|
194
|
+
"CheckboxGroupSelectAll: pass `allValues` on the parent <CheckboxGroup> — without it, select-all has nothing to govern and renders disabled.",
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const selectedCount = allValues.filter((v) => ctx.value.includes(v)).length;
|
|
199
|
+
|
|
200
|
+
const state: boolean | "indeterminate" =
|
|
201
|
+
allValues.length > 0 && selectedCount === allValues.length
|
|
202
|
+
? true
|
|
203
|
+
: selectedCount > 0
|
|
204
|
+
? "indeterminate"
|
|
205
|
+
: false;
|
|
206
|
+
|
|
207
|
+
const onCheckedChange = () => {
|
|
208
|
+
if (state === true) {
|
|
209
|
+
// Clear only the governed values — anything outside allValues (e.g. a
|
|
210
|
+
// disabled-but-checked item) survives; the user couldn't have unchecked
|
|
211
|
+
// it by hand either.
|
|
212
|
+
ctx.setValue(ctx.value.filter((v) => !allValues.includes(v)));
|
|
213
|
+
} else {
|
|
214
|
+
const next = [...ctx.value];
|
|
215
|
+
for (const v of allValues) if (!next.includes(v)) next.push(v);
|
|
216
|
+
ctx.setValue(next);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<Checkbox
|
|
222
|
+
data-slot="checkbox-group-select-all"
|
|
223
|
+
checked={state}
|
|
224
|
+
onCheckedChange={onCheckedChange}
|
|
225
|
+
disabled={ctx.disabled || selfDisabled || allValues.length === 0}
|
|
226
|
+
{...props}
|
|
227
|
+
/>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export { CheckboxGroup, CheckboxGroupItem, CheckboxGroupSelectAll };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
3
|
import * as React from "react"
|
|
4
|
-
import { CheckIcon } from "lucide-react"
|
|
4
|
+
import { CheckIcon, MinusIcon } from "lucide-react"
|
|
5
5
|
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
|
|
6
6
|
|
|
7
7
|
import { cn } from "../../lib/utils"
|
|
@@ -22,9 +22,20 @@ function Checkbox({
|
|
|
22
22
|
<CheckboxPrimitive.Indicator
|
|
23
23
|
forceMount
|
|
24
24
|
data-slot="checkbox-indicator"
|
|
25
|
-
className="grid place-content-center text-current opacity-0 scale-75 transition-[opacity,transform] duration-[120ms] ease-out data-[state=checked]:opacity-100 data-[state=checked]:scale-100 data-[state=indeterminate]:opacity-100 data-[state=indeterminate]:scale-100"
|
|
25
|
+
className="group/indicator grid place-content-center text-current opacity-0 scale-75 transition-[opacity,transform] duration-[120ms] ease-out data-[state=checked]:opacity-100 data-[state=checked]:scale-100 data-[state=indeterminate]:opacity-100 data-[state=indeterminate]:scale-100"
|
|
26
26
|
>
|
|
27
|
-
|
|
27
|
+
{/* Indeterminate renders a minus, not a check — partial selection must
|
|
28
|
+
be visually distinct from full selection (matches aria-checked="mixed",
|
|
29
|
+
which Radix already announces). Icons keep the CheckIcon's stroke
|
|
30
|
+
weight so the glyphs read identically inside the same indicator. */}
|
|
31
|
+
<CheckIcon
|
|
32
|
+
className="hidden size-6 group-data-[state=checked]/indicator:block"
|
|
33
|
+
strokeWidth={3}
|
|
34
|
+
/>
|
|
35
|
+
<MinusIcon
|
|
36
|
+
className="hidden size-6 group-data-[state=indeterminate]/indicator:block"
|
|
37
|
+
strokeWidth={3}
|
|
38
|
+
/>
|
|
28
39
|
</CheckboxPrimitive.Indicator>
|
|
29
40
|
</CheckboxPrimitive.Root>
|
|
30
41
|
)
|