@behavioral-agentic-design-system/core 0.14.0 → 0.16.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 +8 -2
- package/scripts/a11y-lint.cjs +229 -0
- package/scripts/build-tokens.cjs +3 -3
- package/scripts/check-stories.mjs +109 -0
- package/scripts/fix-stories.mjs +785 -0
- package/scripts/generate-stories.cjs +101 -0
- package/scripts/oklch-to-hex.cjs +156 -0
- package/scripts/wcag-audit.cjs +444 -0
- package/src/app/preview/components/page.tsx +1118 -7
- package/src/components/dev-identity-badge.tsx +1 -1
- package/src/components/ui/button-group.tsx +57 -0
- package/src/components/ui/calendar.tsx +4 -0
- package/src/components/ui/circular-progress.tsx +120 -0
- package/src/components/ui/color-picker.tsx +219 -0
- package/src/components/ui/copy-button.tsx +114 -0
- package/src/components/ui/date-range-picker.tsx +174 -0
- package/src/components/ui/empty-state.tsx +115 -0
- package/src/components/ui/index.ts +3 -0
- package/src/components/ui/indicator.tsx +144 -0
- package/src/components/ui/list.tsx +70 -0
- package/src/components/ui/multi-select.tsx +196 -0
- package/src/components/ui/password-input.tsx +150 -0
- package/src/components/ui/time-picker.tsx +363 -0
- package/src/components/ui/tree-view.tsx +405 -0
- package/src/data/docs-index.ts +1 -0
- package/src/hooks/use-global-primary-color.ts +1 -1
- package/src/styles.css +2 -2
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { CheckIcon, ChevronsUpDownIcon, XIcon } from "lucide-react"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
import { Badge } from "./badge"
|
|
7
|
+
import {
|
|
8
|
+
Command,
|
|
9
|
+
CommandEmpty,
|
|
10
|
+
CommandGroup,
|
|
11
|
+
CommandInput,
|
|
12
|
+
CommandItem,
|
|
13
|
+
CommandList,
|
|
14
|
+
} from "./command"
|
|
15
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./popover"
|
|
16
|
+
|
|
17
|
+
export interface MultiSelectOption {
|
|
18
|
+
value: string
|
|
19
|
+
label: string
|
|
20
|
+
disabled?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface MultiSelectProps {
|
|
24
|
+
options: MultiSelectOption[]
|
|
25
|
+
value?: string[]
|
|
26
|
+
onValueChange?: (values: string[]) => void
|
|
27
|
+
placeholder?: string
|
|
28
|
+
searchPlaceholder?: string
|
|
29
|
+
emptyText?: string
|
|
30
|
+
disabled?: boolean
|
|
31
|
+
maxCount?: number
|
|
32
|
+
className?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function MultiSelect({
|
|
36
|
+
options,
|
|
37
|
+
value = [],
|
|
38
|
+
onValueChange,
|
|
39
|
+
placeholder = "Select options...",
|
|
40
|
+
searchPlaceholder = "Search...",
|
|
41
|
+
emptyText = "No results found.",
|
|
42
|
+
disabled = false,
|
|
43
|
+
maxCount,
|
|
44
|
+
className,
|
|
45
|
+
}: MultiSelectProps) {
|
|
46
|
+
const [open, setOpen] = React.useState(false)
|
|
47
|
+
|
|
48
|
+
function toggle(optValue: string) {
|
|
49
|
+
const next = value.includes(optValue)
|
|
50
|
+
? value.filter((v) => v !== optValue)
|
|
51
|
+
: [...value, optValue]
|
|
52
|
+
onValueChange?.(next)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function removeAt(optValue: string, e: React.MouseEvent) {
|
|
56
|
+
e.stopPropagation()
|
|
57
|
+
onValueChange?.(value.filter((v) => v !== optValue))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const selectedOptions = options.filter((o) => value.includes(o.value))
|
|
61
|
+
const visibleSelected = maxCount
|
|
62
|
+
? selectedOptions.slice(0, maxCount)
|
|
63
|
+
: selectedOptions
|
|
64
|
+
const overflowCount =
|
|
65
|
+
maxCount && selectedOptions.length > maxCount
|
|
66
|
+
? selectedOptions.length - maxCount
|
|
67
|
+
: 0
|
|
68
|
+
|
|
69
|
+
const triggerLabel =
|
|
70
|
+
value.length > 0
|
|
71
|
+
? `${value.length} option${value.length === 1 ? "" : "s"} selected`
|
|
72
|
+
: placeholder
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
76
|
+
{/*
|
|
77
|
+
* Architecture: absolutely-positioned transparent <button> is the PopoverTrigger,
|
|
78
|
+
* filling the visual container via inset-0. This satisfies axe because:
|
|
79
|
+
* 1. aria-expanded / aria-controls from Radix land on a <button> — valid per ARIA.
|
|
80
|
+
* 2. X-remove <button> pills live in a sibling z-10 span, NOT nested inside the
|
|
81
|
+
* trigger button — no axe nested-interactive violation.
|
|
82
|
+
* DOM order: trigger button first → Tab lands on the trigger before X buttons.
|
|
83
|
+
* Focus ring: has-[:focus-visible] on the container lights up for both the trigger
|
|
84
|
+
* button and individual X buttons, keeping the whole component visually in focus.
|
|
85
|
+
*/}
|
|
86
|
+
<div
|
|
87
|
+
data-slot="multi-select"
|
|
88
|
+
className={cn(
|
|
89
|
+
"relative flex w-full min-h-9 h-auto items-center justify-between gap-1 rounded-md border border-input bg-background px-3 py-1.5 text-sm font-medium shadow-xs",
|
|
90
|
+
"cursor-default select-none",
|
|
91
|
+
"hover:bg-accent/50",
|
|
92
|
+
"has-[:focus-visible]:outline-none has-[:focus-visible]:ring-[3px] has-[:focus-visible]:ring-ring/50 has-[:focus-visible]:border-ring",
|
|
93
|
+
disabled && "pointer-events-none cursor-not-allowed opacity-50",
|
|
94
|
+
className
|
|
95
|
+
)}
|
|
96
|
+
style={{
|
|
97
|
+
transitionDuration: "var(--duration-fast)",
|
|
98
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
99
|
+
}}
|
|
100
|
+
>
|
|
101
|
+
{/* Trigger: absolute-fill transparent button — first in DOM for correct tab order */}
|
|
102
|
+
<PopoverTrigger asChild>
|
|
103
|
+
<button
|
|
104
|
+
type="button"
|
|
105
|
+
aria-haspopup="listbox"
|
|
106
|
+
aria-label={triggerLabel}
|
|
107
|
+
disabled={disabled}
|
|
108
|
+
tabIndex={disabled ? -1 : 0}
|
|
109
|
+
className="absolute inset-0 z-0 rounded-md focus-visible:outline-none"
|
|
110
|
+
/>
|
|
111
|
+
</PopoverTrigger>
|
|
112
|
+
|
|
113
|
+
{/* Pills + placeholder: z-10 so they render above the trigger button.
|
|
114
|
+
pointer-events-none lets clicks on badge labels fall through to the
|
|
115
|
+
trigger button; X buttons restore pointer-events-auto for themselves. */}
|
|
116
|
+
<span className="relative z-10 flex flex-wrap gap-1 min-w-0 flex-1 pointer-events-none">
|
|
117
|
+
{visibleSelected.length > 0 ? (
|
|
118
|
+
<>
|
|
119
|
+
{visibleSelected.map((opt) => (
|
|
120
|
+
<Badge
|
|
121
|
+
key={opt.value}
|
|
122
|
+
variant="secondary"
|
|
123
|
+
className="gap-1 pr-1 font-normal py-0 min-h-6"
|
|
124
|
+
>
|
|
125
|
+
<span className="truncate max-w-[120px]">{opt.label}</span>
|
|
126
|
+
<button
|
|
127
|
+
type="button"
|
|
128
|
+
aria-label={`Remove ${opt.label}`}
|
|
129
|
+
onClick={(e) => removeAt(opt.value, e)}
|
|
130
|
+
className="pointer-events-auto h-6 w-6 flex items-center justify-center rounded-sm opacity-60 hover:opacity-100 outline-none focus-visible:ring-1 focus-visible:ring-ring transition-opacity"
|
|
131
|
+
style={{
|
|
132
|
+
transitionDuration: "var(--duration-fast)",
|
|
133
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
134
|
+
}}
|
|
135
|
+
>
|
|
136
|
+
<XIcon size={10} strokeWidth={1.5} />
|
|
137
|
+
</button>
|
|
138
|
+
</Badge>
|
|
139
|
+
))}
|
|
140
|
+
{overflowCount > 0 && (
|
|
141
|
+
<Badge variant="secondary" className="font-normal">
|
|
142
|
+
+{overflowCount} more
|
|
143
|
+
</Badge>
|
|
144
|
+
)}
|
|
145
|
+
</>
|
|
146
|
+
) : (
|
|
147
|
+
<span className="text-muted-foreground">{placeholder}</span>
|
|
148
|
+
)}
|
|
149
|
+
</span>
|
|
150
|
+
|
|
151
|
+
{/* Chevron: z-10 and pointer-events-none — the trigger button beneath handles clicks */}
|
|
152
|
+
<ChevronsUpDownIcon
|
|
153
|
+
strokeWidth={1.5}
|
|
154
|
+
className="relative z-10 ml-2 size-4 shrink-0 opacity-50 pointer-events-none transition-[opacity,transform]"
|
|
155
|
+
style={{
|
|
156
|
+
transitionDuration: "var(--duration-fast)",
|
|
157
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
158
|
+
}}
|
|
159
|
+
/>
|
|
160
|
+
</div>
|
|
161
|
+
<PopoverContent className="w-[var(--radix-popover-trigger-width)] min-w-[200px] p-0">
|
|
162
|
+
<Command>
|
|
163
|
+
<CommandInput placeholder={searchPlaceholder} />
|
|
164
|
+
<CommandList>
|
|
165
|
+
<CommandEmpty>{emptyText}</CommandEmpty>
|
|
166
|
+
<CommandGroup>
|
|
167
|
+
{options.map((option) => (
|
|
168
|
+
<CommandItem
|
|
169
|
+
key={option.value}
|
|
170
|
+
value={option.value}
|
|
171
|
+
disabled={option.disabled}
|
|
172
|
+
onSelect={() => toggle(option.value)}
|
|
173
|
+
>
|
|
174
|
+
<CheckIcon
|
|
175
|
+
strokeWidth={1.5}
|
|
176
|
+
className={cn(
|
|
177
|
+
"mr-2 size-4 shrink-0 transition-opacity",
|
|
178
|
+
value.includes(option.value) ? "opacity-100" : "opacity-0"
|
|
179
|
+
)}
|
|
180
|
+
style={{
|
|
181
|
+
transitionDuration: "var(--duration-fast)",
|
|
182
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
183
|
+
}}
|
|
184
|
+
/>
|
|
185
|
+
{option.label}
|
|
186
|
+
</CommandItem>
|
|
187
|
+
))}
|
|
188
|
+
</CommandGroup>
|
|
189
|
+
</CommandList>
|
|
190
|
+
</Command>
|
|
191
|
+
</PopoverContent>
|
|
192
|
+
</Popover>
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export { MultiSelect }
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
5
|
+
import { Eye, EyeOff } from "lucide-react";
|
|
6
|
+
|
|
7
|
+
import { cn } from "../../lib/utils";
|
|
8
|
+
|
|
9
|
+
// ─── Variants ─────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
const passwordInputVariants = cva(
|
|
12
|
+
[
|
|
13
|
+
"flex items-center overflow-hidden",
|
|
14
|
+
"rounded-md border border-input bg-transparent shadow-xs",
|
|
15
|
+
"transition-[color,box-shadow]",
|
|
16
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
17
|
+
"has-[:disabled]:pointer-events-none has-[:disabled]:cursor-not-allowed has-[:disabled]:opacity-50",
|
|
18
|
+
"aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40",
|
|
19
|
+
],
|
|
20
|
+
{
|
|
21
|
+
variants: {
|
|
22
|
+
size: {
|
|
23
|
+
sm: "h-8 text-sm",
|
|
24
|
+
default: "h-9 text-base md:text-sm",
|
|
25
|
+
lg: "h-10 text-base",
|
|
26
|
+
xl: "h-11 text-base",
|
|
27
|
+
"2xl": "h-12 text-lg",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultVariants: { size: "default" },
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
export interface PasswordInputProps
|
|
37
|
+
extends Omit<React.ComponentProps<"input">, "type" | "size">,
|
|
38
|
+
VariantProps<typeof passwordInputVariants> {
|
|
39
|
+
/** Controlled visibility state */
|
|
40
|
+
visible?: boolean;
|
|
41
|
+
/** Uncontrolled default visibility */
|
|
42
|
+
defaultVisible?: boolean;
|
|
43
|
+
/** Fires when visibility toggles */
|
|
44
|
+
onVisibilityChange?: (visible: boolean) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ─── PasswordInput ────────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
function PasswordInput({
|
|
50
|
+
className,
|
|
51
|
+
size,
|
|
52
|
+
visible: visibleProp,
|
|
53
|
+
defaultVisible = false,
|
|
54
|
+
onVisibilityChange,
|
|
55
|
+
id,
|
|
56
|
+
disabled,
|
|
57
|
+
"aria-invalid": ariaInvalid,
|
|
58
|
+
...props
|
|
59
|
+
}: PasswordInputProps) {
|
|
60
|
+
const generatedId = React.useId();
|
|
61
|
+
const inputId = id ?? generatedId;
|
|
62
|
+
|
|
63
|
+
const [visibleState, setVisibleState] = React.useState(defaultVisible);
|
|
64
|
+
const isControlled = visibleProp !== undefined;
|
|
65
|
+
const visible = isControlled ? visibleProp : visibleState;
|
|
66
|
+
|
|
67
|
+
function toggle() {
|
|
68
|
+
const next = !visible;
|
|
69
|
+
if (!isControlled) setVisibleState(next);
|
|
70
|
+
onVisibilityChange?.(next);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const paddingBySize: Record<string, string> = {
|
|
74
|
+
sm: "px-2.5 py-1",
|
|
75
|
+
default: "px-3 py-1",
|
|
76
|
+
lg: "px-4 py-2",
|
|
77
|
+
xl: "px-4 py-2",
|
|
78
|
+
"2xl": "px-5 py-2.5",
|
|
79
|
+
};
|
|
80
|
+
const sizeKey = (size ?? "default") as string;
|
|
81
|
+
const inputPadding = paddingBySize[sizeKey] ?? paddingBySize.default;
|
|
82
|
+
|
|
83
|
+
const iconSize: Record<string, number> = {
|
|
84
|
+
sm: 14,
|
|
85
|
+
default: 16,
|
|
86
|
+
lg: 16,
|
|
87
|
+
xl: 18,
|
|
88
|
+
"2xl": 20,
|
|
89
|
+
};
|
|
90
|
+
const iconPx = iconSize[sizeKey] ?? 16;
|
|
91
|
+
|
|
92
|
+
const togglePadding: Record<string, string> = {
|
|
93
|
+
sm: "px-2",
|
|
94
|
+
default: "px-2.5",
|
|
95
|
+
lg: "px-3",
|
|
96
|
+
xl: "px-3",
|
|
97
|
+
"2xl": "px-3.5",
|
|
98
|
+
};
|
|
99
|
+
const btnPadding = togglePadding[sizeKey] ?? togglePadding.default;
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<div
|
|
103
|
+
data-slot="password-input"
|
|
104
|
+
aria-invalid={ariaInvalid}
|
|
105
|
+
className={cn(passwordInputVariants({ size, className }))}
|
|
106
|
+
>
|
|
107
|
+
<input
|
|
108
|
+
id={inputId}
|
|
109
|
+
type={visible ? "text" : "password"}
|
|
110
|
+
disabled={disabled}
|
|
111
|
+
data-slot="password-input-field"
|
|
112
|
+
className={cn(
|
|
113
|
+
"min-w-0 flex-1 bg-transparent outline-none",
|
|
114
|
+
"placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground",
|
|
115
|
+
"dark:bg-input/30",
|
|
116
|
+
inputPadding
|
|
117
|
+
)}
|
|
118
|
+
{...props}
|
|
119
|
+
/>
|
|
120
|
+
<button
|
|
121
|
+
type="button"
|
|
122
|
+
onClick={toggle}
|
|
123
|
+
disabled={disabled}
|
|
124
|
+
tabIndex={0}
|
|
125
|
+
aria-label={visible ? "Hide password" : "Show password"}
|
|
126
|
+
aria-controls={inputId}
|
|
127
|
+
data-slot="password-input-toggle"
|
|
128
|
+
className={cn(
|
|
129
|
+
"flex shrink-0 items-center text-muted-foreground",
|
|
130
|
+
"hover:text-foreground",
|
|
131
|
+
"disabled:pointer-events-none",
|
|
132
|
+
btnPadding
|
|
133
|
+
)}
|
|
134
|
+
style={{
|
|
135
|
+
transitionProperty: "color",
|
|
136
|
+
transitionDuration: "var(--duration-fast)",
|
|
137
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
138
|
+
}}
|
|
139
|
+
>
|
|
140
|
+
{visible ? (
|
|
141
|
+
<EyeOff size={iconPx} strokeWidth={1.5} aria-hidden />
|
|
142
|
+
) : (
|
|
143
|
+
<Eye size={iconPx} strokeWidth={1.5} aria-hidden />
|
|
144
|
+
)}
|
|
145
|
+
</button>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export { PasswordInput, passwordInputVariants };
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { XIcon } from "lucide-react"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
export interface TimePickerProps {
|
|
8
|
+
value?: string
|
|
9
|
+
onValueChange?: (value: string | undefined) => void
|
|
10
|
+
placeholder?: string
|
|
11
|
+
disabled?: boolean
|
|
12
|
+
clearable?: boolean
|
|
13
|
+
size?: "sm" | "default" | "lg"
|
|
14
|
+
hours12?: boolean
|
|
15
|
+
showSeconds?: boolean
|
|
16
|
+
className?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const sizeMap = {
|
|
20
|
+
sm: { height: "h-8", px: "px-2.5", text: "text-sm", gap: "gap-1", icon: 14 },
|
|
21
|
+
default: { height: "h-9", px: "px-3", text: "text-sm", gap: "gap-1.5", icon: 16 },
|
|
22
|
+
lg: { height: "h-10", px: "px-4", text: "text-base", gap: "gap-2", icon: 16 },
|
|
23
|
+
} as const
|
|
24
|
+
|
|
25
|
+
function pad2(n: number) {
|
|
26
|
+
return String(n).padStart(2, "0")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function parseTime(value?: string): { h: number; m: number; s: number } | null {
|
|
30
|
+
if (!value) return null
|
|
31
|
+
const parts = value.split(":").map(Number)
|
|
32
|
+
if (parts.length < 2 || parts.some(isNaN)) return null
|
|
33
|
+
return { h: parts[0], m: parts[1], s: parts[2] ?? 0 }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatTime(h: number, m: number, s: number, showSeconds: boolean): string {
|
|
37
|
+
return showSeconds ? `${pad2(h)}:${pad2(m)}:${pad2(s)}` : `${pad2(h)}:${pad2(m)}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Segment = "h" | "m" | "s" | "ampm"
|
|
41
|
+
|
|
42
|
+
function TimePicker({
|
|
43
|
+
value,
|
|
44
|
+
onValueChange,
|
|
45
|
+
placeholder = "--:--",
|
|
46
|
+
disabled = false,
|
|
47
|
+
clearable = false,
|
|
48
|
+
size = "default",
|
|
49
|
+
hours12 = false,
|
|
50
|
+
showSeconds = false,
|
|
51
|
+
className,
|
|
52
|
+
}: TimePickerProps) {
|
|
53
|
+
const isControlled = value !== undefined || onValueChange !== undefined
|
|
54
|
+
const [internalValue, setInternalValue] = React.useState<string | undefined>(undefined)
|
|
55
|
+
const resolvedValue = isControlled ? value : internalValue
|
|
56
|
+
|
|
57
|
+
const parsed = parseTime(resolvedValue)
|
|
58
|
+
const [h, setH] = React.useState(parsed?.h ?? 0)
|
|
59
|
+
const [m, setM] = React.useState(parsed?.m ?? 0)
|
|
60
|
+
const [s, setS] = React.useState(parsed?.s ?? 0)
|
|
61
|
+
const [active, setActive] = React.useState<Segment | null>(null)
|
|
62
|
+
const [digitBuffer, setDigitBuffer] = React.useState("")
|
|
63
|
+
const [hasValue, setHasValue] = React.useState(!!parsed)
|
|
64
|
+
|
|
65
|
+
React.useEffect(() => {
|
|
66
|
+
const p = parseTime(resolvedValue)
|
|
67
|
+
if (p) {
|
|
68
|
+
setH(p.h)
|
|
69
|
+
setM(p.m)
|
|
70
|
+
setS(p.s)
|
|
71
|
+
setHasValue(true)
|
|
72
|
+
} else if (resolvedValue === undefined || resolvedValue === "") {
|
|
73
|
+
setHasValue(false)
|
|
74
|
+
}
|
|
75
|
+
}, [resolvedValue])
|
|
76
|
+
|
|
77
|
+
function commit(nh: number, nm: number, ns: number) {
|
|
78
|
+
const str = formatTime(nh, nm, ns, showSeconds)
|
|
79
|
+
setHasValue(true)
|
|
80
|
+
if (!isControlled) setInternalValue(str)
|
|
81
|
+
onValueChange?.(str)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function handleClear(e: React.MouseEvent) {
|
|
85
|
+
e.stopPropagation()
|
|
86
|
+
setHasValue(false)
|
|
87
|
+
setH(0)
|
|
88
|
+
setM(0)
|
|
89
|
+
setS(0)
|
|
90
|
+
if (!isControlled) setInternalValue(undefined)
|
|
91
|
+
onValueChange?.(undefined)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const isPM = h >= 12
|
|
95
|
+
const displayH = hours12 ? (h === 0 ? 12 : h > 12 ? h - 12 : h) : h
|
|
96
|
+
|
|
97
|
+
function adjustH(delta: number) {
|
|
98
|
+
const nh = (h + delta + 24) % 24
|
|
99
|
+
setH(nh)
|
|
100
|
+
commit(nh, m, s)
|
|
101
|
+
}
|
|
102
|
+
function adjustM(delta: number) {
|
|
103
|
+
const nm = (m + delta + 60) % 60
|
|
104
|
+
setM(nm)
|
|
105
|
+
commit(h, nm, s)
|
|
106
|
+
}
|
|
107
|
+
function adjustS(delta: number) {
|
|
108
|
+
const ns = (s + delta + 60) % 60
|
|
109
|
+
setS(ns)
|
|
110
|
+
commit(h, m, ns)
|
|
111
|
+
}
|
|
112
|
+
function toggleAMPM() {
|
|
113
|
+
const nh = h >= 12 ? h - 12 : h + 12
|
|
114
|
+
setH(nh)
|
|
115
|
+
commit(nh, m, s)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function handleSegmentKeyDown(seg: Segment, e: React.KeyboardEvent) {
|
|
119
|
+
if (e.key === "ArrowUp") {
|
|
120
|
+
e.preventDefault()
|
|
121
|
+
if (seg === "h") adjustH(1)
|
|
122
|
+
else if (seg === "m") adjustM(1)
|
|
123
|
+
else if (seg === "s") adjustS(1)
|
|
124
|
+
else if (seg === "ampm") toggleAMPM()
|
|
125
|
+
} else if (e.key === "ArrowDown") {
|
|
126
|
+
e.preventDefault()
|
|
127
|
+
if (seg === "h") adjustH(-1)
|
|
128
|
+
else if (seg === "m") adjustM(-1)
|
|
129
|
+
else if (seg === "s") adjustS(-1)
|
|
130
|
+
else if (seg === "ampm") toggleAMPM()
|
|
131
|
+
} else if (e.key >= "0" && e.key <= "9" && seg !== "ampm") {
|
|
132
|
+
e.preventDefault()
|
|
133
|
+
const digit = Number(e.key)
|
|
134
|
+
const newBuf = digitBuffer + String(digit)
|
|
135
|
+
const val = parseInt(newBuf, 10)
|
|
136
|
+
if (seg === "h") {
|
|
137
|
+
if (hours12) {
|
|
138
|
+
if (newBuf.length === 1 && digit > 1) {
|
|
139
|
+
const nh = isPM ? digit + 12 : digit
|
|
140
|
+
setH(nh)
|
|
141
|
+
commit(nh, m, s)
|
|
142
|
+
setDigitBuffer("")
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
const clamped = Math.min(val, 12)
|
|
146
|
+
const nh =
|
|
147
|
+
clamped === 0
|
|
148
|
+
? isPM ? 12 : 0
|
|
149
|
+
: isPM
|
|
150
|
+
? clamped === 12
|
|
151
|
+
? 12
|
|
152
|
+
: clamped + 12
|
|
153
|
+
: clamped === 12
|
|
154
|
+
? 0
|
|
155
|
+
: clamped
|
|
156
|
+
if (newBuf.length >= 2) {
|
|
157
|
+
setH(nh)
|
|
158
|
+
commit(nh, m, s)
|
|
159
|
+
setDigitBuffer("")
|
|
160
|
+
} else {
|
|
161
|
+
setDigitBuffer(newBuf)
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
if (val > 2 || newBuf.length >= 2) {
|
|
165
|
+
const nh = Math.min(val, 23)
|
|
166
|
+
setH(nh)
|
|
167
|
+
commit(nh, m, s)
|
|
168
|
+
setDigitBuffer("")
|
|
169
|
+
} else {
|
|
170
|
+
setDigitBuffer(newBuf)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} else if (seg === "m" || seg === "s") {
|
|
174
|
+
if (digit > 5 || newBuf.length >= 2) {
|
|
175
|
+
const nv = Math.min(val, 59)
|
|
176
|
+
if (seg === "m") {
|
|
177
|
+
setM(nv)
|
|
178
|
+
commit(h, nv, s)
|
|
179
|
+
} else {
|
|
180
|
+
setS(nv)
|
|
181
|
+
commit(h, m, nv)
|
|
182
|
+
}
|
|
183
|
+
setDigitBuffer("")
|
|
184
|
+
} else {
|
|
185
|
+
setDigitBuffer(newBuf)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
setDigitBuffer("")
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const { height, px, text, gap, icon: iconSize } = sizeMap[size]
|
|
194
|
+
const segClass = cn(
|
|
195
|
+
"inline-flex items-center justify-center rounded px-0.5 outline-none select-none tabular-nums",
|
|
196
|
+
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:bg-accent",
|
|
197
|
+
"transition-colors",
|
|
198
|
+
text
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
return (
|
|
202
|
+
<div
|
|
203
|
+
data-slot="time-picker"
|
|
204
|
+
data-size={size}
|
|
205
|
+
role="group"
|
|
206
|
+
aria-label="Time input"
|
|
207
|
+
aria-disabled={disabled}
|
|
208
|
+
className={cn(
|
|
209
|
+
"inline-flex w-full min-w-0 items-center rounded-md border border-input bg-transparent shadow-xs",
|
|
210
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
211
|
+
disabled && "pointer-events-none cursor-not-allowed opacity-50",
|
|
212
|
+
height,
|
|
213
|
+
px,
|
|
214
|
+
gap,
|
|
215
|
+
className
|
|
216
|
+
)}
|
|
217
|
+
>
|
|
218
|
+
{!hasValue ? (
|
|
219
|
+
<span
|
|
220
|
+
tabIndex={disabled ? -1 : 0}
|
|
221
|
+
className={cn(
|
|
222
|
+
"flex-1 text-muted-foreground cursor-text outline-none",
|
|
223
|
+
text
|
|
224
|
+
)}
|
|
225
|
+
onFocus={() => {
|
|
226
|
+
setActive("h")
|
|
227
|
+
setHasValue(true)
|
|
228
|
+
}}
|
|
229
|
+
onClick={() => {
|
|
230
|
+
setActive("h")
|
|
231
|
+
setHasValue(true)
|
|
232
|
+
}}
|
|
233
|
+
>
|
|
234
|
+
{placeholder}
|
|
235
|
+
</span>
|
|
236
|
+
) : (
|
|
237
|
+
<span className={cn("inline-flex items-center", gap)}>
|
|
238
|
+
<span
|
|
239
|
+
role="spinbutton"
|
|
240
|
+
aria-label="Hours"
|
|
241
|
+
aria-valuenow={displayH}
|
|
242
|
+
aria-valuemin={hours12 ? 1 : 0}
|
|
243
|
+
aria-valuemax={hours12 ? 12 : 23}
|
|
244
|
+
aria-valuetext={pad2(displayH)}
|
|
245
|
+
tabIndex={disabled ? -1 : 0}
|
|
246
|
+
className={cn(segClass, active === "h" && "bg-accent")}
|
|
247
|
+
onFocus={() => setActive("h")}
|
|
248
|
+
onBlur={() => {
|
|
249
|
+
setActive(null)
|
|
250
|
+
setDigitBuffer("")
|
|
251
|
+
}}
|
|
252
|
+
onKeyDown={(e) => handleSegmentKeyDown("h", e)}
|
|
253
|
+
style={{
|
|
254
|
+
transitionDuration: "var(--duration-fast)",
|
|
255
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
256
|
+
}}
|
|
257
|
+
>
|
|
258
|
+
{pad2(displayH)}
|
|
259
|
+
</span>
|
|
260
|
+
<span aria-hidden className={cn("text-muted-foreground", text)}>
|
|
261
|
+
:
|
|
262
|
+
</span>
|
|
263
|
+
<span
|
|
264
|
+
role="spinbutton"
|
|
265
|
+
aria-label="Minutes"
|
|
266
|
+
aria-valuenow={m}
|
|
267
|
+
aria-valuemin={0}
|
|
268
|
+
aria-valuemax={59}
|
|
269
|
+
aria-valuetext={pad2(m)}
|
|
270
|
+
tabIndex={disabled ? -1 : 0}
|
|
271
|
+
className={cn(segClass, active === "m" && "bg-accent")}
|
|
272
|
+
onFocus={() => setActive("m")}
|
|
273
|
+
onBlur={() => {
|
|
274
|
+
setActive(null)
|
|
275
|
+
setDigitBuffer("")
|
|
276
|
+
}}
|
|
277
|
+
onKeyDown={(e) => handleSegmentKeyDown("m", e)}
|
|
278
|
+
style={{
|
|
279
|
+
transitionDuration: "var(--duration-fast)",
|
|
280
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
281
|
+
}}
|
|
282
|
+
>
|
|
283
|
+
{pad2(m)}
|
|
284
|
+
</span>
|
|
285
|
+
{showSeconds && (
|
|
286
|
+
<>
|
|
287
|
+
<span aria-hidden className={cn("text-muted-foreground", text)}>
|
|
288
|
+
:
|
|
289
|
+
</span>
|
|
290
|
+
<span
|
|
291
|
+
role="spinbutton"
|
|
292
|
+
aria-label="Seconds"
|
|
293
|
+
aria-valuenow={s}
|
|
294
|
+
aria-valuemin={0}
|
|
295
|
+
aria-valuemax={59}
|
|
296
|
+
aria-valuetext={pad2(s)}
|
|
297
|
+
tabIndex={disabled ? -1 : 0}
|
|
298
|
+
className={cn(segClass, active === "s" && "bg-accent")}
|
|
299
|
+
onFocus={() => setActive("s")}
|
|
300
|
+
onBlur={() => {
|
|
301
|
+
setActive(null)
|
|
302
|
+
setDigitBuffer("")
|
|
303
|
+
}}
|
|
304
|
+
onKeyDown={(e) => handleSegmentKeyDown("s", e)}
|
|
305
|
+
style={{
|
|
306
|
+
transitionDuration: "var(--duration-fast)",
|
|
307
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
308
|
+
}}
|
|
309
|
+
>
|
|
310
|
+
{pad2(s)}
|
|
311
|
+
</span>
|
|
312
|
+
</>
|
|
313
|
+
)}
|
|
314
|
+
{hours12 && (
|
|
315
|
+
<button
|
|
316
|
+
type="button"
|
|
317
|
+
role="spinbutton"
|
|
318
|
+
aria-label="AM/PM"
|
|
319
|
+
aria-valuenow={isPM ? 1 : 0}
|
|
320
|
+
aria-valuemin={0}
|
|
321
|
+
aria-valuemax={1}
|
|
322
|
+
aria-valuetext={isPM ? "PM" : "AM"}
|
|
323
|
+
tabIndex={disabled ? -1 : 0}
|
|
324
|
+
disabled={disabled}
|
|
325
|
+
onClick={toggleAMPM}
|
|
326
|
+
onFocus={() => setActive("ampm")}
|
|
327
|
+
onBlur={() => setActive(null)}
|
|
328
|
+
onKeyDown={(e) => handleSegmentKeyDown("ampm", e)}
|
|
329
|
+
className={cn(
|
|
330
|
+
segClass,
|
|
331
|
+
"cursor-pointer hover:bg-accent",
|
|
332
|
+
active === "ampm" && "bg-accent"
|
|
333
|
+
)}
|
|
334
|
+
style={{
|
|
335
|
+
transitionDuration: "var(--duration-fast)",
|
|
336
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
337
|
+
}}
|
|
338
|
+
>
|
|
339
|
+
{isPM ? "PM" : "AM"}
|
|
340
|
+
</button>
|
|
341
|
+
)}
|
|
342
|
+
</span>
|
|
343
|
+
)}
|
|
344
|
+
{clearable && hasValue && (
|
|
345
|
+
<button
|
|
346
|
+
type="button"
|
|
347
|
+
aria-label="Clear time"
|
|
348
|
+
onClick={handleClear}
|
|
349
|
+
disabled={disabled}
|
|
350
|
+
className="shrink-0 -mr-1 rounded p-0.5 text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
351
|
+
style={{
|
|
352
|
+
transitionDuration: "var(--duration-fast)",
|
|
353
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
354
|
+
}}
|
|
355
|
+
>
|
|
356
|
+
<XIcon size={iconSize - 2} strokeWidth={1.5} />
|
|
357
|
+
</button>
|
|
358
|
+
)}
|
|
359
|
+
</div>
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export { TimePicker }
|