@behavioral-agentic-design-system/core 0.42.0 → 0.45.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/LICENSE +21 -0
- package/package.json +25 -9
- package/scripts/a11y-lint.cjs +25 -1
- package/scripts/check-spec.mjs +73 -0
- package/scripts/deal-directions.mjs +186 -0
- package/scripts/health-report.mjs +20 -14
- package/scripts/lib/counted-lint.mjs +100 -0
- package/scripts/lib/marketing-library.mjs +129 -0
- package/scripts/lib/on-system.mjs +249 -0
- package/scripts/lib/random.mjs +22 -0
- package/scripts/lib/spec-parse.d.mts +25 -0
- package/scripts/lib/spec-parse.mjs +188 -0
- package/scripts/lib/tells.mjs +128 -0
- package/scripts/lint-css-vars.mjs +175 -0
- package/scripts/lint-html-nesting.mjs +116 -0
- package/scripts/lint-live-ctas.mjs +138 -0
- package/scripts/on-system-score.mjs +95 -0
- package/scripts/slop-rules.mjs +1 -1
- package/scripts/source-images.mjs +180 -0
- package/scripts/ux-tells.mjs +78 -0
- package/src/api/brand-colors.ts +3 -0
- package/src/api/brand-fonts.ts +3 -0
- package/src/api/brand-mark.ts +6 -1
- package/src/api/dev-write-guard.ts +53 -0
- package/src/api/layers/file.ts +7 -4
- package/src/api/onboarding-extract.ts +85 -30
- package/src/api/onboarding.ts +3 -0
- package/src/api/save-tokens.ts +3 -0
- package/src/app/layout.tsx +6 -0
- package/src/app/preview/canvas/page.tsx +12 -0
- package/src/app/preview/components/page.tsx +734 -0
- package/src/app/preview/studio/blank-screen.tsx +143 -0
- package/src/app/preview/studio/page.tsx +7 -0
- package/src/app/shell.tsx +160 -44
- package/src/components/canvas/canvas.tsx +458 -0
- package/src/components/canvas/layout.ts +72 -0
- package/src/components/canvas/parse-canvas.ts +32 -0
- package/src/components/canvas/sample.ts +90 -0
- package/src/components/canvas/spec-frame.tsx +43 -0
- package/src/components/canvas/types.ts +33 -0
- package/src/components/docs-tab.tsx +7 -7
- package/src/components/home-recent.tsx +73 -0
- package/src/components/home-tab.tsx +13 -13
- package/src/components/layers-tab.tsx +2 -2
- package/src/components/markdown-preview.tsx +60 -12
- package/src/components/studio/control-panel.tsx +299 -0
- package/src/components/studio/control-row.tsx +77 -0
- package/src/components/studio/preview-frame.tsx +165 -0
- package/src/components/studio/preview-pane.tsx +247 -0
- package/src/components/studio/studio.tsx +133 -128
- package/src/components/studio/types.ts +218 -0
- package/src/components/studio/use-studio-tweaks.ts +128 -0
- package/src/components/studio/use-studio-view-state.ts +72 -0
- package/src/components/ui/autocomplete.tsx +1 -1
- package/src/components/ui/color-picker.tsx +20 -4
- package/src/components/ui/data-table.tsx +64 -3
- package/src/components/ui/date-field.tsx +1 -1
- package/src/components/ui/editable.tsx +1 -0
- package/src/components/ui/faceted-filter.tsx +187 -0
- package/src/components/ui/gauge-chart.tsx +18 -4
- package/src/components/ui/inline-edit.tsx +175 -0
- package/src/components/ui/month-picker.tsx +1 -1
- package/src/components/ui/number-input.tsx +28 -5
- package/src/components/ui/otp-input.tsx +208 -0
- package/src/components/ui/phone-input.tsx +274 -0
- package/src/components/ui/search-input.tsx +1 -1
- package/src/components/ui/tags-input.tsx +1 -1
- package/src/components/ui/timer.tsx +1 -0
- package/src/components/ui/transfer-list.tsx +13 -26
- package/src/components/ui/week-picker.tsx +1 -1
- package/src/components/ui/year-picker.tsx +1 -1
- package/src/data/component-added.json +958 -0
- package/src/data/docs-index.ts +13 -1
- package/src/hooks/use-global-mono-font.ts +1 -0
- package/src/hooks/use-global-primary-color.ts +1 -0
- package/src/hooks/use-global-sans-font.ts +1 -0
- package/src/hooks/use-global-serif-font.ts +1 -0
- package/src/hooks/use-global-type-scale.ts +17 -2
- package/src/hooks/use-mobile.ts +2 -0
- package/src/hooks/use-sync-type-scale.ts +7 -11
- package/src/lib/spec-parse.ts +4 -0
- package/src/lib/svg-sanitize.ts +19 -9
- package/src/stories/Autocomplete.stories.tsx +2 -0
- package/src/stories/Canvas.stories.tsx +45 -0
- package/src/stories/ColorPicker.stories.tsx +40 -0
- package/src/stories/DataTable.stories.tsx +30 -0
- package/src/stories/DateField.stories.tsx +21 -0
- package/src/stories/Editable.stories.tsx +2 -0
- package/src/stories/FacetedFilter.stories.tsx +197 -0
- package/src/stories/GaugeChart.stories.tsx +9 -3
- package/src/stories/InlineEdit.stories.tsx +106 -0
- package/src/stories/MarkdownPreview.stories.tsx +53 -0
- package/src/stories/MonthPicker.stories.tsx +21 -0
- package/src/stories/NumberInput.stories.tsx +25 -0
- package/src/stories/OtpInput.stories.tsx +110 -0
- package/src/stories/PhoneInput.stories.tsx +127 -0
- package/src/stories/SearchInput.stories.tsx +2 -0
- package/src/stories/TagsInput.stories.tsx +2 -0
- package/src/stories/Timer.stories.tsx +26 -0
- package/src/stories/TransferList.stories.tsx +4 -0
- package/src/stories/WeekPicker.stories.tsx +21 -0
- package/src/stories/YearPicker.stories.tsx +21 -0
- package/src/stories/story-groups.json +22 -16
- package/src/tokens.json +82 -54
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { PlusCircleIcon, XCircleIcon } from "lucide-react"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { Badge } from "./badge"
|
|
8
|
+
import { Button } from "./button"
|
|
9
|
+
import { Checkbox } from "./checkbox"
|
|
10
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./popover"
|
|
11
|
+
import { Separator } from "./separator"
|
|
12
|
+
import {
|
|
13
|
+
Command,
|
|
14
|
+
CommandEmpty,
|
|
15
|
+
CommandGroup,
|
|
16
|
+
CommandInput,
|
|
17
|
+
CommandItem,
|
|
18
|
+
CommandList,
|
|
19
|
+
CommandSeparator,
|
|
20
|
+
} from "./command"
|
|
21
|
+
|
|
22
|
+
export interface FacetedFilterOption {
|
|
23
|
+
label: string
|
|
24
|
+
value: string
|
|
25
|
+
icon?: React.ComponentType<{ className?: string }>
|
|
26
|
+
count?: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FacetedFilterProps {
|
|
30
|
+
title: string
|
|
31
|
+
options: FacetedFilterOption[]
|
|
32
|
+
selectedValues?: Set<string>
|
|
33
|
+
onSelectionChange?: (values: Set<string>) => void
|
|
34
|
+
className?: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function FacetedFilter({
|
|
38
|
+
title,
|
|
39
|
+
options,
|
|
40
|
+
selectedValues = new Set(),
|
|
41
|
+
onSelectionChange,
|
|
42
|
+
className,
|
|
43
|
+
}: FacetedFilterProps) {
|
|
44
|
+
const selectedCount = selectedValues.size
|
|
45
|
+
|
|
46
|
+
function toggleValue(value: string) {
|
|
47
|
+
const next = new Set(selectedValues)
|
|
48
|
+
if (next.has(value)) {
|
|
49
|
+
next.delete(value)
|
|
50
|
+
} else {
|
|
51
|
+
next.add(value)
|
|
52
|
+
}
|
|
53
|
+
onSelectionChange?.(next)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function clearSelection() {
|
|
57
|
+
onSelectionChange?.(new Set())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<Popover>
|
|
62
|
+
<PopoverTrigger asChild>
|
|
63
|
+
<Button
|
|
64
|
+
variant="outline"
|
|
65
|
+
size="sm"
|
|
66
|
+
aria-label={
|
|
67
|
+
selectedCount > 0
|
|
68
|
+
? `${title}: ${selectedCount} selected`
|
|
69
|
+
: `Filter by ${title}`
|
|
70
|
+
}
|
|
71
|
+
className={cn("h-8 gap-1.5 border-dashed", className)}
|
|
72
|
+
>
|
|
73
|
+
<PlusCircleIcon strokeWidth={1.5} className="size-4 shrink-0" />
|
|
74
|
+
<span>{title}</span>
|
|
75
|
+
{selectedCount > 0 && (
|
|
76
|
+
<>
|
|
77
|
+
<Separator orientation="vertical" className="h-4" />
|
|
78
|
+
{selectedCount > 2 ? (
|
|
79
|
+
<Badge
|
|
80
|
+
variant="secondary"
|
|
81
|
+
size="sm"
|
|
82
|
+
className="rounded-sm px-1 font-normal"
|
|
83
|
+
>
|
|
84
|
+
{selectedCount} selected
|
|
85
|
+
</Badge>
|
|
86
|
+
) : (
|
|
87
|
+
<div className="flex gap-1">
|
|
88
|
+
{Array.from(selectedValues).map((val) => (
|
|
89
|
+
<Badge
|
|
90
|
+
key={val}
|
|
91
|
+
variant="secondary"
|
|
92
|
+
size="sm"
|
|
93
|
+
className="rounded-sm px-1 font-normal"
|
|
94
|
+
>
|
|
95
|
+
{options.find((o) => o.value === val)?.label ?? val}
|
|
96
|
+
</Badge>
|
|
97
|
+
))}
|
|
98
|
+
</div>
|
|
99
|
+
)}
|
|
100
|
+
</>
|
|
101
|
+
)}
|
|
102
|
+
</Button>
|
|
103
|
+
</PopoverTrigger>
|
|
104
|
+
<PopoverContent
|
|
105
|
+
className="w-[220px] p-0"
|
|
106
|
+
align="start"
|
|
107
|
+
sideOffset={6}
|
|
108
|
+
>
|
|
109
|
+
<Command aria-label={`${title} filter options`}>
|
|
110
|
+
<CommandInput placeholder={`Search ${title.toLowerCase()}…`} />
|
|
111
|
+
<CommandList>
|
|
112
|
+
<CommandEmpty>No results.</CommandEmpty>
|
|
113
|
+
<CommandGroup>
|
|
114
|
+
{options.map((option) => {
|
|
115
|
+
const isSelected = selectedValues.has(option.value)
|
|
116
|
+
const Icon = option.icon
|
|
117
|
+
return (
|
|
118
|
+
<CommandItem
|
|
119
|
+
key={option.value}
|
|
120
|
+
value={option.value}
|
|
121
|
+
onSelect={() => toggleValue(option.value)}
|
|
122
|
+
className="gap-2"
|
|
123
|
+
style={{
|
|
124
|
+
transitionProperty: "color, background-color",
|
|
125
|
+
transitionDuration: "var(--duration-fast)",
|
|
126
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
127
|
+
}}
|
|
128
|
+
>
|
|
129
|
+
{/* Decorative, not the real control: CommandItem's onSelect above is
|
|
130
|
+
what toggles selection and is what gets the roving cmdk focus. A
|
|
131
|
+
second focusable/interactive checkbox nested in the same row would
|
|
132
|
+
double the controls a screen reader announces for one choice. This
|
|
133
|
+
still has to be the real Checkbox primitive, not a hand-rolled
|
|
134
|
+
lookalike (see CLAUDE.md -> "Primitive visual parity") — tabIndex,
|
|
135
|
+
pointer-events, and aria-hidden are what make an otherwise-real,
|
|
136
|
+
otherwise-interactive component safe to use purely as a visual. */}
|
|
137
|
+
<Checkbox
|
|
138
|
+
size="sm"
|
|
139
|
+
checked={isSelected}
|
|
140
|
+
tabIndex={-1}
|
|
141
|
+
aria-hidden
|
|
142
|
+
className={cn(
|
|
143
|
+
"pointer-events-none",
|
|
144
|
+
!isSelected && "opacity-50 [.group_&]:opacity-100"
|
|
145
|
+
)}
|
|
146
|
+
/>
|
|
147
|
+
{Icon && (
|
|
148
|
+
<Icon className="size-4 shrink-0 text-muted-foreground" />
|
|
149
|
+
)}
|
|
150
|
+
<span className="flex-1 truncate">{option.label}</span>
|
|
151
|
+
{option.count !== undefined && (
|
|
152
|
+
<span
|
|
153
|
+
className="ml-auto font-mono text-xs tabular-nums text-muted-foreground"
|
|
154
|
+
aria-label={`${option.count} items`}
|
|
155
|
+
>
|
|
156
|
+
{option.count}
|
|
157
|
+
</span>
|
|
158
|
+
)}
|
|
159
|
+
</CommandItem>
|
|
160
|
+
)
|
|
161
|
+
})}
|
|
162
|
+
</CommandGroup>
|
|
163
|
+
{selectedCount > 0 && (
|
|
164
|
+
<>
|
|
165
|
+
<CommandSeparator />
|
|
166
|
+
<CommandGroup>
|
|
167
|
+
<CommandItem
|
|
168
|
+
onSelect={clearSelection}
|
|
169
|
+
className="justify-center gap-1.5 text-muted-foreground"
|
|
170
|
+
style={{
|
|
171
|
+
transitionProperty: "color, background-color",
|
|
172
|
+
transitionDuration: "var(--duration-fast)",
|
|
173
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
174
|
+
}}
|
|
175
|
+
>
|
|
176
|
+
<XCircleIcon strokeWidth={1.5} className="size-4" />
|
|
177
|
+
Clear filters
|
|
178
|
+
</CommandItem>
|
|
179
|
+
</CommandGroup>
|
|
180
|
+
</>
|
|
181
|
+
)}
|
|
182
|
+
</CommandList>
|
|
183
|
+
</Command>
|
|
184
|
+
</PopoverContent>
|
|
185
|
+
</Popover>
|
|
186
|
+
)
|
|
187
|
+
}
|
|
@@ -4,7 +4,7 @@ import * as React from "react"
|
|
|
4
4
|
import { ResponsiveContainer, PieChart, Pie } from "recharts"
|
|
5
5
|
|
|
6
6
|
import { cn } from "../../lib/utils"
|
|
7
|
-
import {
|
|
7
|
+
import { prefersReducedMotion } from "../../hooks/use-reduced-motion"
|
|
8
8
|
|
|
9
9
|
// Arc geometry. Recharts measures angles counter-clockwise from 3 o'clock, so
|
|
10
10
|
// starting at 225° (lower-left) and sweeping down to -45° (lower-right) draws
|
|
@@ -115,13 +115,27 @@ function GaugeChart({
|
|
|
115
115
|
"aria-label": ariaLabel,
|
|
116
116
|
...props
|
|
117
117
|
}: GaugeChartProps) {
|
|
118
|
-
const prefersReducedMotion = useReducedMotion()
|
|
119
|
-
const animate = !prefersReducedMotion
|
|
120
|
-
|
|
121
118
|
// ResizeObserver gate — same pattern as ChartContainer and Sparkline.
|
|
122
119
|
const containerRef = React.useRef<HTMLDivElement | null>(null)
|
|
123
120
|
const [hasSize, setHasSize] = React.useState(false)
|
|
124
121
|
|
|
122
|
+
// Read reduced-motion synchronously at mount rather than via the reactive
|
|
123
|
+
// `useReducedMotion()` hook. That hook intentionally starts `false` on
|
|
124
|
+
// every render (SSR-safe) and corrects itself a tick later via its own
|
|
125
|
+
// effect — fine for components whose SSR output doesn't depend on it, but
|
|
126
|
+
// this chart's entire Pie subtree is already gated behind `hasSize` (which
|
|
127
|
+
// is `false` during SSR and the first client paint), so there is no
|
|
128
|
+
// server/client mismatch risk in reading the real preference immediately.
|
|
129
|
+
// Without this, `isAnimationActive` starts `true`, react-smooth begins
|
|
130
|
+
// tweening the value arc from a zero-width sector, and when the hook's
|
|
131
|
+
// effect flips it to `false` a tick later — mid-tween — Recharts freezes
|
|
132
|
+
// on whatever near-zero angle it had already interpolated to instead of
|
|
133
|
+
// jumping to the final one, so the arc never recovers to its correct
|
|
134
|
+
// angle. Reading the preference up front means `isAnimationActive` is
|
|
135
|
+
// right from the component's very first animation frame, so this race
|
|
136
|
+
// never opens. (Confirmed via repeated headless capture — see PR.)
|
|
137
|
+
const [animate] = React.useState(() => !prefersReducedMotion())
|
|
138
|
+
|
|
125
139
|
React.useEffect(() => {
|
|
126
140
|
const el = containerRef.current
|
|
127
141
|
if (!el) return
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
const inlineEditViewVariants = cva(
|
|
8
|
+
"inline-flex w-full min-w-0 cursor-text items-center rounded-md outline-none transition-colors select-none",
|
|
9
|
+
{
|
|
10
|
+
variants: {
|
|
11
|
+
variant: {
|
|
12
|
+
ghost:
|
|
13
|
+
"px-0 hover:bg-muted/60 data-[empty=true]:text-muted-foreground",
|
|
14
|
+
outline:
|
|
15
|
+
"border border-transparent px-3 hover:border-input data-[empty=true]:text-muted-foreground",
|
|
16
|
+
},
|
|
17
|
+
size: {
|
|
18
|
+
sm: "h-8 text-sm",
|
|
19
|
+
default: "h-9 text-sm md:text-sm",
|
|
20
|
+
lg: "h-10 text-base",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
defaultVariants: { variant: "ghost", size: "default" },
|
|
24
|
+
}
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
const inlineEditInputVariants = cva(
|
|
28
|
+
"w-full min-w-0 rounded-md border bg-transparent outline-none transition-[color,box-shadow] placeholder:text-muted-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50",
|
|
29
|
+
{
|
|
30
|
+
variants: {
|
|
31
|
+
variant: {
|
|
32
|
+
ghost:
|
|
33
|
+
"rounded-none border-0 border-b border-input focus-visible:border-ring focus-visible:ring-0",
|
|
34
|
+
outline:
|
|
35
|
+
"border-input shadow-xs focus-visible:border-ring dark:bg-input/30",
|
|
36
|
+
},
|
|
37
|
+
size: {
|
|
38
|
+
sm: "h-8 px-2.5 text-sm",
|
|
39
|
+
default: "h-9 px-3 text-sm md:text-sm",
|
|
40
|
+
lg: "h-10 px-4 text-base",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
defaultVariants: { variant: "ghost", size: "default" },
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
export interface InlineEditProps
|
|
48
|
+
extends Omit<React.ComponentProps<"input">, "size" | "onChange">,
|
|
49
|
+
VariantProps<typeof inlineEditViewVariants> {
|
|
50
|
+
/** Controlled value. Use with onConfirm / onCancel. */
|
|
51
|
+
value?: string
|
|
52
|
+
/** Uncontrolled initial value. */
|
|
53
|
+
defaultValue?: string
|
|
54
|
+
/** Called with the new value when the user confirms (Enter or blur). */
|
|
55
|
+
onConfirm?: (value: string) => void
|
|
56
|
+
/** Called when the user cancels with Escape; value reverts to before-edit state. */
|
|
57
|
+
onCancel?: (value: string) => void
|
|
58
|
+
/** Placeholder shown in view mode when value is empty. */
|
|
59
|
+
placeholder?: string
|
|
60
|
+
/** When true the input renders directly without the view-mode toggle. */
|
|
61
|
+
alwaysEditing?: boolean
|
|
62
|
+
/** Wrapper className */
|
|
63
|
+
className?: string
|
|
64
|
+
/** Input element className override */
|
|
65
|
+
inputClassName?: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function InlineEdit({
|
|
69
|
+
value: controlledValue,
|
|
70
|
+
defaultValue = "",
|
|
71
|
+
onConfirm,
|
|
72
|
+
onCancel,
|
|
73
|
+
placeholder = "Click to edit",
|
|
74
|
+
alwaysEditing = false,
|
|
75
|
+
disabled,
|
|
76
|
+
variant,
|
|
77
|
+
size,
|
|
78
|
+
className,
|
|
79
|
+
inputClassName,
|
|
80
|
+
...inputProps
|
|
81
|
+
}: InlineEditProps) {
|
|
82
|
+
const [editing, setEditing] = React.useState(alwaysEditing)
|
|
83
|
+
const [draft, setDraft] = React.useState(controlledValue ?? defaultValue)
|
|
84
|
+
const [committed, setCommitted] = React.useState(controlledValue ?? defaultValue)
|
|
85
|
+
const inputRef = React.useRef<HTMLInputElement>(null)
|
|
86
|
+
|
|
87
|
+
// Sync when controlled value changes from outside
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
if (controlledValue !== undefined) {
|
|
90
|
+
setDraft(controlledValue)
|
|
91
|
+
setCommitted(controlledValue)
|
|
92
|
+
}
|
|
93
|
+
}, [controlledValue])
|
|
94
|
+
|
|
95
|
+
const startEdit = () => {
|
|
96
|
+
if (disabled || alwaysEditing) return
|
|
97
|
+
setEditing(true)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const confirm = () => {
|
|
101
|
+
if (!editing) return
|
|
102
|
+
setCommitted(draft)
|
|
103
|
+
setEditing(false)
|
|
104
|
+
onConfirm?.(draft)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const cancel = () => {
|
|
108
|
+
if (!editing) return
|
|
109
|
+
setDraft(committed)
|
|
110
|
+
setEditing(false)
|
|
111
|
+
onCancel?.(committed)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
React.useEffect(() => {
|
|
115
|
+
if (editing) {
|
|
116
|
+
inputRef.current?.focus()
|
|
117
|
+
inputRef.current?.select()
|
|
118
|
+
}
|
|
119
|
+
}, [editing])
|
|
120
|
+
|
|
121
|
+
const displayValue = controlledValue ?? committed
|
|
122
|
+
|
|
123
|
+
if (!editing && !alwaysEditing) {
|
|
124
|
+
return (
|
|
125
|
+
<span
|
|
126
|
+
role="button"
|
|
127
|
+
tabIndex={disabled ? -1 : 0}
|
|
128
|
+
data-slot="inline-edit-view"
|
|
129
|
+
data-empty={!displayValue ? "true" : undefined}
|
|
130
|
+
aria-label={displayValue || placeholder}
|
|
131
|
+
onClick={startEdit}
|
|
132
|
+
onKeyDown={(e) => {
|
|
133
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
134
|
+
e.preventDefault()
|
|
135
|
+
startEdit()
|
|
136
|
+
}
|
|
137
|
+
}}
|
|
138
|
+
className={cn(
|
|
139
|
+
inlineEditViewVariants({ variant, size }),
|
|
140
|
+
disabled && "cursor-not-allowed opacity-50",
|
|
141
|
+
className
|
|
142
|
+
)}
|
|
143
|
+
>
|
|
144
|
+
{displayValue || (
|
|
145
|
+
<span className="text-muted-foreground">{placeholder}</span>
|
|
146
|
+
)}
|
|
147
|
+
</span>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<input
|
|
153
|
+
ref={inputRef}
|
|
154
|
+
data-slot="inline-edit-input"
|
|
155
|
+
value={draft}
|
|
156
|
+
onChange={(e) => setDraft(e.target.value)}
|
|
157
|
+
onBlur={confirm}
|
|
158
|
+
onKeyDown={(e) => {
|
|
159
|
+
if (e.key === "Enter") {
|
|
160
|
+
e.preventDefault()
|
|
161
|
+
confirm()
|
|
162
|
+
} else if (e.key === "Escape") {
|
|
163
|
+
e.preventDefault()
|
|
164
|
+
cancel()
|
|
165
|
+
}
|
|
166
|
+
}}
|
|
167
|
+
disabled={disabled}
|
|
168
|
+
placeholder={placeholder}
|
|
169
|
+
{...inputProps}
|
|
170
|
+
className={cn(inlineEditInputVariants({ variant, size }), inputClassName)}
|
|
171
|
+
/>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export { InlineEdit, inlineEditViewVariants, inlineEditInputVariants }
|
|
@@ -62,7 +62,7 @@ function MonthPicker({
|
|
|
62
62
|
className,
|
|
63
63
|
id,
|
|
64
64
|
}: MonthPickerProps) {
|
|
65
|
-
const isControlled = value !== undefined
|
|
65
|
+
const isControlled = value !== undefined || onChange !== undefined
|
|
66
66
|
const [internalValue, setInternalValue] = React.useState<Date | undefined>(
|
|
67
67
|
defaultValue
|
|
68
68
|
)
|
|
@@ -52,6 +52,17 @@ export interface NumberInputProps
|
|
|
52
52
|
|
|
53
53
|
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
54
54
|
|
|
55
|
+
/** Digits after the decimal point, so 0.1 + 0.2 can be rounded back to 0.3. */
|
|
56
|
+
function decimalsOf(n: number): number {
|
|
57
|
+
if (!Number.isFinite(n)) return 0;
|
|
58
|
+
const [, frac = ""] = String(n).split(".");
|
|
59
|
+
return frac.length;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function roundTo(n: number, decimals: number): number {
|
|
63
|
+
return Number(n.toFixed(Math.min(decimals, 12)));
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
// ─── NumberInput ──────────────────────────────────────────────────────────────
|
|
56
67
|
|
|
57
68
|
function NumberInput({
|
|
@@ -66,6 +77,8 @@ function NumberInput({
|
|
|
66
77
|
className,
|
|
67
78
|
"aria-label": ariaLabel = "Number input",
|
|
68
79
|
"aria-labelledby": ariaLabelledBy,
|
|
80
|
+
onBlur: onBlurProp,
|
|
81
|
+
onKeyDown: onKeyDownProp,
|
|
69
82
|
...rest
|
|
70
83
|
}: NumberInputProps) {
|
|
71
84
|
const isControlled = valueProp !== undefined;
|
|
@@ -100,12 +113,21 @@ function NumberInput({
|
|
|
100
113
|
[min, max, isControlled, onChange]
|
|
101
114
|
);
|
|
102
115
|
|
|
103
|
-
|
|
104
|
-
|
|
116
|
+
// Step from what is in the box, not the last committed value: with 3 committed, typing 7 and pressing
|
|
117
|
+
// ArrowUp must give 8. Round to the finer of the two precisions so step={0.1} never shows 0.30000000000000004.
|
|
118
|
+
const stepBy = (delta: number) => {
|
|
119
|
+
const typed = parseFloat(inputStr);
|
|
120
|
+
const base = Number.isFinite(typed) ? typed : value;
|
|
121
|
+
commit(roundTo(base + delta, Math.max(decimalsOf(step), decimalsOf(base))));
|
|
122
|
+
};
|
|
123
|
+
const increment = () => stepBy(step);
|
|
124
|
+
const decrement = () => stepBy(-step);
|
|
105
125
|
|
|
106
126
|
// ── keyboard handling ──────────────────────────────────────────────────────
|
|
107
127
|
|
|
108
128
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
129
|
+
onKeyDownProp?.(e);
|
|
130
|
+
if (e.defaultPrevented) return;
|
|
109
131
|
if (e.key === "ArrowUp") {
|
|
110
132
|
e.preventDefault();
|
|
111
133
|
increment();
|
|
@@ -114,10 +136,10 @@ function NumberInput({
|
|
|
114
136
|
decrement();
|
|
115
137
|
} else if (e.key === "PageUp") {
|
|
116
138
|
e.preventDefault();
|
|
117
|
-
|
|
139
|
+
stepBy(step * 10);
|
|
118
140
|
} else if (e.key === "PageDown") {
|
|
119
141
|
e.preventDefault();
|
|
120
|
-
|
|
142
|
+
stepBy(-step * 10);
|
|
121
143
|
} else if (e.key === "Home" && Number.isFinite(min)) {
|
|
122
144
|
e.preventDefault();
|
|
123
145
|
commit(min);
|
|
@@ -135,7 +157,8 @@ function NumberInput({
|
|
|
135
157
|
|
|
136
158
|
// ── blur: commit typed value or reset ─────────────────────────────────────
|
|
137
159
|
|
|
138
|
-
const handleBlur = () => {
|
|
160
|
+
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
|
|
161
|
+
onBlurProp?.(e);
|
|
139
162
|
const parsed = parseFloat(inputStr);
|
|
140
163
|
if (Number.isFinite(parsed)) {
|
|
141
164
|
commit(parsed);
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
5
|
+
import { cn } from "../../lib/utils";
|
|
6
|
+
|
|
7
|
+
/* -------------------------------------------------------------------------- */
|
|
8
|
+
/* Variants */
|
|
9
|
+
/* -------------------------------------------------------------------------- */
|
|
10
|
+
|
|
11
|
+
const otpSlotVariants = cva(
|
|
12
|
+
[
|
|
13
|
+
"relative flex h-11 w-10 items-center justify-center",
|
|
14
|
+
"rounded-md border border-input bg-background",
|
|
15
|
+
"text-sm font-medium text-foreground caret-transparent",
|
|
16
|
+
"outline-none ring-offset-background",
|
|
17
|
+
"transition-[color,box-shadow]",
|
|
18
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
19
|
+
"aria-[invalid=true]:border-destructive aria-[invalid=true]:ring-destructive/20",
|
|
20
|
+
],
|
|
21
|
+
{
|
|
22
|
+
variants: {
|
|
23
|
+
size: {
|
|
24
|
+
sm: "h-9 w-9 text-sm",
|
|
25
|
+
default: "h-11 w-10 text-sm",
|
|
26
|
+
lg: "h-12 w-11 text-base",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
defaultVariants: { size: "default" },
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/* -------------------------------------------------------------------------- */
|
|
34
|
+
/* OTPInput */
|
|
35
|
+
/* -------------------------------------------------------------------------- */
|
|
36
|
+
|
|
37
|
+
export interface OTPInputProps
|
|
38
|
+
extends Omit<React.ComponentProps<"div">, "onChange"> {
|
|
39
|
+
/** Number of OTP slots. Defaults to 6. */
|
|
40
|
+
length?: number;
|
|
41
|
+
/** Controlled value — a string of up to `length` characters. */
|
|
42
|
+
value?: string;
|
|
43
|
+
/** Called whenever the composed value changes. */
|
|
44
|
+
onChange?: (value: string) => void;
|
|
45
|
+
/** Called when every slot is filled and the value is complete. */
|
|
46
|
+
onComplete?: (value: string) => void;
|
|
47
|
+
/** Accept only numeric input. Defaults to true. */
|
|
48
|
+
numeric?: boolean;
|
|
49
|
+
/** Disable all slots. */
|
|
50
|
+
disabled?: boolean;
|
|
51
|
+
/** Mark all slots as invalid (aria-invalid). */
|
|
52
|
+
invalid?: boolean;
|
|
53
|
+
/** Render a separator element after this slot index (0-based). */
|
|
54
|
+
separatorAfter?: number;
|
|
55
|
+
/** aria-label for the group. */
|
|
56
|
+
"aria-label"?: string;
|
|
57
|
+
size?: VariantProps<typeof otpSlotVariants>["size"];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function OTPInput({
|
|
61
|
+
length = 6,
|
|
62
|
+
value: valueProp,
|
|
63
|
+
onChange,
|
|
64
|
+
onComplete,
|
|
65
|
+
numeric = true,
|
|
66
|
+
disabled = false,
|
|
67
|
+
invalid = false,
|
|
68
|
+
separatorAfter,
|
|
69
|
+
className,
|
|
70
|
+
size,
|
|
71
|
+
"aria-label": ariaLabel = "One-time password",
|
|
72
|
+
...props
|
|
73
|
+
}: OTPInputProps) {
|
|
74
|
+
const [internalValue, setInternalValue] = React.useState("");
|
|
75
|
+
const isControlled = valueProp !== undefined;
|
|
76
|
+
const value = isControlled ? valueProp : internalValue;
|
|
77
|
+
|
|
78
|
+
const refs = React.useRef<Array<HTMLInputElement | null>>([]);
|
|
79
|
+
|
|
80
|
+
const setRefs = React.useCallback(
|
|
81
|
+
(index: number) => (el: HTMLInputElement | null) => {
|
|
82
|
+
refs.current[index] = el;
|
|
83
|
+
},
|
|
84
|
+
[]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
function updateValue(next: string) {
|
|
88
|
+
if (!isControlled) setInternalValue(next);
|
|
89
|
+
onChange?.(next);
|
|
90
|
+
if (next.length === length) onComplete?.(next);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function handleKeyDown(index: number, e: React.KeyboardEvent<HTMLInputElement>) {
|
|
94
|
+
if (e.key === "ArrowLeft") {
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
refs.current[index - 1]?.focus();
|
|
97
|
+
} else if (e.key === "ArrowRight") {
|
|
98
|
+
e.preventDefault();
|
|
99
|
+
refs.current[index + 1]?.focus();
|
|
100
|
+
} else if (e.key === "Backspace") {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
if (value[index]) {
|
|
103
|
+
// Clear current slot
|
|
104
|
+
const next = value.slice(0, index) + value.slice(index + 1);
|
|
105
|
+
updateValue(next.padEnd(index, "").slice(0, length));
|
|
106
|
+
} else {
|
|
107
|
+
// Already empty — retreat and clear previous
|
|
108
|
+
const prev = index - 1;
|
|
109
|
+
if (prev >= 0) {
|
|
110
|
+
refs.current[prev]?.focus();
|
|
111
|
+
const next = value.slice(0, prev) + value.slice(prev + 1);
|
|
112
|
+
updateValue(next.padEnd(prev, "").slice(0, length));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} else if (e.key === "Delete") {
|
|
116
|
+
e.preventDefault();
|
|
117
|
+
const next = value.slice(0, index) + value.slice(index + 1);
|
|
118
|
+
updateValue(next.padEnd(index, "").slice(0, length));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function handleInput(index: number, e: React.FormEvent<HTMLInputElement>) {
|
|
123
|
+
const raw = (e.target as HTMLInputElement).value;
|
|
124
|
+
const char = raw.slice(-1);
|
|
125
|
+
if (!char) return;
|
|
126
|
+
if (numeric && !/^\d$/.test(char)) return;
|
|
127
|
+
|
|
128
|
+
const chars = value.split("").concat(Array(length).fill("")).slice(0, length);
|
|
129
|
+
chars[index] = char;
|
|
130
|
+
const next = chars.join("").trimEnd();
|
|
131
|
+
updateValue(next);
|
|
132
|
+
refs.current[Math.min(index + 1, length - 1)]?.focus();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function handlePaste(e: React.ClipboardEvent<HTMLInputElement>) {
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
const pasted = e.clipboardData.getData("text/plain");
|
|
138
|
+
const filtered = numeric ? pasted.replace(/\D/g, "") : pasted;
|
|
139
|
+
const next = filtered.slice(0, length);
|
|
140
|
+
updateValue(next);
|
|
141
|
+
refs.current[Math.min(next.length, length - 1)]?.focus();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function handleFocus(index: number) {
|
|
145
|
+
// Move focus to the first unfilled slot when clicking a filled slot
|
|
146
|
+
// (only when clicking past the filled region)
|
|
147
|
+
const firstEmpty = value.length;
|
|
148
|
+
if (index > firstEmpty) {
|
|
149
|
+
refs.current[firstEmpty]?.focus();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return (
|
|
154
|
+
<div
|
|
155
|
+
role="group"
|
|
156
|
+
aria-label={ariaLabel}
|
|
157
|
+
className={cn("flex items-center gap-2", className)}
|
|
158
|
+
{...props}
|
|
159
|
+
>
|
|
160
|
+
{Array.from({ length }, (_, i) => (
|
|
161
|
+
<React.Fragment key={i}>
|
|
162
|
+
<div
|
|
163
|
+
className={cn(otpSlotVariants({ size }))}
|
|
164
|
+
aria-invalid={invalid || undefined}
|
|
165
|
+
>
|
|
166
|
+
<input
|
|
167
|
+
ref={setRefs(i)}
|
|
168
|
+
type="text"
|
|
169
|
+
inputMode={numeric ? "numeric" : "text"}
|
|
170
|
+
pattern={numeric ? "[0-9]*" : undefined}
|
|
171
|
+
maxLength={2}
|
|
172
|
+
value={value[i] ?? ""}
|
|
173
|
+
autoComplete={i === 0 ? "one-time-code" : "off"}
|
|
174
|
+
disabled={disabled}
|
|
175
|
+
aria-label={`Digit ${i + 1} of ${length}`}
|
|
176
|
+
className={cn(
|
|
177
|
+
"absolute inset-0 h-full w-full cursor-text select-none",
|
|
178
|
+
"bg-transparent text-center text-[inherit] font-[inherit]",
|
|
179
|
+
"outline-none border-0 ring-0",
|
|
180
|
+
"caret-transparent",
|
|
181
|
+
disabled && "cursor-not-allowed opacity-50"
|
|
182
|
+
)}
|
|
183
|
+
style={{
|
|
184
|
+
transitionDuration: "var(--duration-fast)",
|
|
185
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
186
|
+
}}
|
|
187
|
+
onKeyDown={(e) => handleKeyDown(i, e)}
|
|
188
|
+
onInput={(e) => handleInput(i, e)}
|
|
189
|
+
onPaste={handlePaste}
|
|
190
|
+
onFocus={() => handleFocus(i)}
|
|
191
|
+
onChange={() => {}} // suppress React uncontrolled warning
|
|
192
|
+
/>
|
|
193
|
+
</div>
|
|
194
|
+
{separatorAfter === i && (
|
|
195
|
+
<span
|
|
196
|
+
aria-hidden
|
|
197
|
+
className="text-muted-foreground select-none text-sm font-medium"
|
|
198
|
+
>
|
|
199
|
+
–
|
|
200
|
+
</span>
|
|
201
|
+
)}
|
|
202
|
+
</React.Fragment>
|
|
203
|
+
))}
|
|
204
|
+
</div>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { OTPInput, otpSlotVariants };
|