@behavioral-agentic-design-system/core 0.14.0 → 0.15.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 +1 -1
- package/src/app/preview/components/page.tsx +745 -6
- package/src/components/ui/color-picker.tsx +219 -0
- package/src/components/ui/date-range-picker.tsx +174 -0
- package/src/components/ui/index.ts +3 -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
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Check } from "lucide-react";
|
|
5
|
+
import { cn } from "../../lib/utils";
|
|
6
|
+
|
|
7
|
+
/* -------------------------------------------------------------------------- */
|
|
8
|
+
/* Helpers */
|
|
9
|
+
/* -------------------------------------------------------------------------- */
|
|
10
|
+
|
|
11
|
+
function luminance(hex: string): number {
|
|
12
|
+
const h = hex.replace("#", "");
|
|
13
|
+
const full = h.length === 3
|
|
14
|
+
? h.split("").map((c) => parseInt(c + c, 16))
|
|
15
|
+
: [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];
|
|
16
|
+
const [r, g, b] = full.map((v) => {
|
|
17
|
+
const s = v / 255;
|
|
18
|
+
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
19
|
+
});
|
|
20
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function contrastColor(hex: string): string {
|
|
24
|
+
if (!/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(hex)) return "#ffffff";
|
|
25
|
+
return luminance(hex) > 0.179 ? "#000000" : "#ffffff";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isValidHex(v: string): boolean {
|
|
29
|
+
return /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(v);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* -------------------------------------------------------------------------- */
|
|
33
|
+
/* ColorPicker */
|
|
34
|
+
/* -------------------------------------------------------------------------- */
|
|
35
|
+
|
|
36
|
+
export interface ColorPickerProps {
|
|
37
|
+
/** Controlled selected color (hex string, e.g. "#ff0000") */
|
|
38
|
+
value?: string;
|
|
39
|
+
/** Uncontrolled default */
|
|
40
|
+
defaultValue?: string;
|
|
41
|
+
onValueChange?: (value: string) => void;
|
|
42
|
+
/** Preset swatch colors (hex strings) */
|
|
43
|
+
swatches?: string[];
|
|
44
|
+
/** Show a hex text input for custom colors */
|
|
45
|
+
allowCustom?: boolean;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
/** Accessible label for the radiogroup */
|
|
48
|
+
"aria-label"?: string;
|
|
49
|
+
className?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function ColorPicker({
|
|
53
|
+
value: valueProp,
|
|
54
|
+
defaultValue,
|
|
55
|
+
onValueChange,
|
|
56
|
+
swatches = [],
|
|
57
|
+
allowCustom = false,
|
|
58
|
+
disabled = false,
|
|
59
|
+
"aria-label": ariaLabel = "Color picker",
|
|
60
|
+
className,
|
|
61
|
+
}: ColorPickerProps) {
|
|
62
|
+
const isControlled = valueProp !== undefined;
|
|
63
|
+
const [valueState, setValueState] = React.useState<string>(defaultValue ?? "");
|
|
64
|
+
const value = isControlled ? (valueProp ?? "") : valueState;
|
|
65
|
+
|
|
66
|
+
const [customInput, setCustomInput] = React.useState<string>(() =>
|
|
67
|
+
value && !swatches.includes(value) ? value : ""
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const swatchRefs = React.useRef<(HTMLButtonElement | null)[]>([]);
|
|
71
|
+
|
|
72
|
+
const commit = React.useCallback(
|
|
73
|
+
(color: string) => {
|
|
74
|
+
if (disabled) return;
|
|
75
|
+
if (!isControlled) setValueState(color);
|
|
76
|
+
onValueChange?.(color);
|
|
77
|
+
},
|
|
78
|
+
[disabled, isControlled, onValueChange]
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const handleKeyDown = React.useCallback(
|
|
82
|
+
(e: React.KeyboardEvent<HTMLButtonElement>, index: number) => {
|
|
83
|
+
const btns = swatchRefs.current.filter((b): b is HTMLButtonElement => !!b);
|
|
84
|
+
let next = index;
|
|
85
|
+
switch (e.key) {
|
|
86
|
+
case "ArrowRight":
|
|
87
|
+
case "ArrowDown":
|
|
88
|
+
e.preventDefault();
|
|
89
|
+
next = (index + 1) % btns.length;
|
|
90
|
+
break;
|
|
91
|
+
case "ArrowLeft":
|
|
92
|
+
case "ArrowUp":
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
next = (index - 1 + btns.length) % btns.length;
|
|
95
|
+
break;
|
|
96
|
+
case "Home":
|
|
97
|
+
e.preventDefault();
|
|
98
|
+
next = 0;
|
|
99
|
+
break;
|
|
100
|
+
case "End":
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
next = btns.length - 1;
|
|
103
|
+
break;
|
|
104
|
+
default:
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
btns[next]?.focus();
|
|
108
|
+
},
|
|
109
|
+
[]
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const handleCustomInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
113
|
+
const v = e.target.value;
|
|
114
|
+
setCustomInput(v);
|
|
115
|
+
if (isValidHex(v)) commit(v);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const selectedSwatchIdx = swatches.indexOf(value);
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<div className={cn("space-y-2", className)}>
|
|
122
|
+
{swatches.length > 0 && (
|
|
123
|
+
<div
|
|
124
|
+
role="radiogroup"
|
|
125
|
+
aria-label={ariaLabel}
|
|
126
|
+
aria-disabled={disabled || undefined}
|
|
127
|
+
className="flex flex-wrap gap-1.5"
|
|
128
|
+
>
|
|
129
|
+
{swatches.map((swatch, i) => {
|
|
130
|
+
const isSelected = value === swatch;
|
|
131
|
+
const isRovingFocus =
|
|
132
|
+
selectedSwatchIdx >= 0 ? isSelected : i === 0;
|
|
133
|
+
return (
|
|
134
|
+
<button
|
|
135
|
+
key={swatch}
|
|
136
|
+
ref={(el) => {
|
|
137
|
+
swatchRefs.current[i] = el;
|
|
138
|
+
}}
|
|
139
|
+
type="button"
|
|
140
|
+
role="radio"
|
|
141
|
+
aria-checked={isSelected}
|
|
142
|
+
aria-label={swatch}
|
|
143
|
+
tabIndex={isRovingFocus ? 0 : -1}
|
|
144
|
+
disabled={disabled}
|
|
145
|
+
onClick={() => {
|
|
146
|
+
commit(swatch);
|
|
147
|
+
setCustomInput("");
|
|
148
|
+
}}
|
|
149
|
+
onKeyDown={(e) => handleKeyDown(e, i)}
|
|
150
|
+
className={cn(
|
|
151
|
+
"relative size-7 shrink-0 rounded-md border-2 overflow-hidden",
|
|
152
|
+
"focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:ring-offset-1",
|
|
153
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
154
|
+
"transition-[border-color]",
|
|
155
|
+
isSelected
|
|
156
|
+
? "border-foreground"
|
|
157
|
+
: "border-transparent hover:border-foreground/30"
|
|
158
|
+
)}
|
|
159
|
+
style={{
|
|
160
|
+
backgroundColor: swatch,
|
|
161
|
+
transitionDuration: "var(--duration-fast)",
|
|
162
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
163
|
+
}}
|
|
164
|
+
>
|
|
165
|
+
{isSelected && (
|
|
166
|
+
<Check
|
|
167
|
+
className="absolute inset-0 m-auto size-3.5"
|
|
168
|
+
style={{ color: contrastColor(swatch) }}
|
|
169
|
+
strokeWidth={1.5}
|
|
170
|
+
aria-hidden
|
|
171
|
+
/>
|
|
172
|
+
)}
|
|
173
|
+
</button>
|
|
174
|
+
);
|
|
175
|
+
})}
|
|
176
|
+
</div>
|
|
177
|
+
)}
|
|
178
|
+
|
|
179
|
+
{allowCustom && (
|
|
180
|
+
<div className="flex items-center gap-2">
|
|
181
|
+
<div
|
|
182
|
+
role="presentation"
|
|
183
|
+
className={cn(
|
|
184
|
+
"size-7 shrink-0 rounded-md border border-input",
|
|
185
|
+
"transition-colors"
|
|
186
|
+
)}
|
|
187
|
+
style={{
|
|
188
|
+
backgroundColor: value || "transparent",
|
|
189
|
+
transitionDuration: "var(--duration-fast)",
|
|
190
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
191
|
+
}}
|
|
192
|
+
/>
|
|
193
|
+
<input
|
|
194
|
+
type="text"
|
|
195
|
+
value={customInput}
|
|
196
|
+
onChange={handleCustomInput}
|
|
197
|
+
disabled={disabled}
|
|
198
|
+
placeholder="#000000"
|
|
199
|
+
aria-label="Custom hex color"
|
|
200
|
+
spellCheck={false}
|
|
201
|
+
maxLength={7}
|
|
202
|
+
className={cn(
|
|
203
|
+
"h-8 w-28 rounded-md border border-input bg-background px-2 py-1",
|
|
204
|
+
"font-mono text-sm text-foreground",
|
|
205
|
+
"placeholder:text-muted-foreground",
|
|
206
|
+
"focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
207
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
208
|
+
"transition-[border-color,box-shadow]"
|
|
209
|
+
)}
|
|
210
|
+
style={{
|
|
211
|
+
transitionDuration: "var(--duration-fast)",
|
|
212
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
213
|
+
}}
|
|
214
|
+
/>
|
|
215
|
+
</div>
|
|
216
|
+
)}
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { ArrowRightIcon, CalendarIcon, XIcon } from "lucide-react"
|
|
5
|
+
import type { DateRange } from "react-day-picker"
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { Calendar } from "./calendar"
|
|
8
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./popover"
|
|
9
|
+
|
|
10
|
+
export type DateRangeValue = { from?: Date; to?: Date }
|
|
11
|
+
|
|
12
|
+
export interface DateRangePickerProps {
|
|
13
|
+
value?: DateRangeValue
|
|
14
|
+
onValueChange?: (range: DateRangeValue | undefined) => void
|
|
15
|
+
startPlaceholder?: string
|
|
16
|
+
endPlaceholder?: string
|
|
17
|
+
disabled?: boolean
|
|
18
|
+
clearable?: boolean
|
|
19
|
+
size?: "sm" | "default" | "lg"
|
|
20
|
+
className?: string
|
|
21
|
+
fromDate?: Date
|
|
22
|
+
toDate?: Date
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sizeMap = {
|
|
26
|
+
sm: { height: "h-8", px: "px-2.5", text: "text-sm", gap: "gap-1.5", icon: 14 },
|
|
27
|
+
default: { height: "h-9", px: "px-3", text: "text-sm", gap: "gap-2", icon: 16 },
|
|
28
|
+
lg: { height: "h-10", px: "px-4", text: "text-base", gap: "gap-2", icon: 16 },
|
|
29
|
+
} as const
|
|
30
|
+
|
|
31
|
+
function formatDate(date: Date): string {
|
|
32
|
+
return date.toLocaleDateString("en-US", {
|
|
33
|
+
month: "short",
|
|
34
|
+
day: "numeric",
|
|
35
|
+
year: "numeric",
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function DateRangePicker({
|
|
40
|
+
value,
|
|
41
|
+
onValueChange,
|
|
42
|
+
startPlaceholder = "Start date",
|
|
43
|
+
endPlaceholder = "End date",
|
|
44
|
+
disabled = false,
|
|
45
|
+
clearable = false,
|
|
46
|
+
size = "default",
|
|
47
|
+
className,
|
|
48
|
+
fromDate,
|
|
49
|
+
toDate,
|
|
50
|
+
}: DateRangePickerProps) {
|
|
51
|
+
const [open, setOpen] = React.useState(false)
|
|
52
|
+
const [internalRange, setInternalRange] = React.useState<
|
|
53
|
+
DateRangeValue | undefined
|
|
54
|
+
>(undefined)
|
|
55
|
+
|
|
56
|
+
const isControlled = value !== undefined || onValueChange !== undefined
|
|
57
|
+
const selectedRange = isControlled ? value : internalRange
|
|
58
|
+
|
|
59
|
+
function handleSelect(range: DateRange | undefined) {
|
|
60
|
+
const next: DateRangeValue | undefined = range
|
|
61
|
+
? { from: range.from, to: range.to }
|
|
62
|
+
: undefined
|
|
63
|
+
if (!isControlled) setInternalRange(next)
|
|
64
|
+
onValueChange?.(next)
|
|
65
|
+
if (next?.from && next?.to) setOpen(false)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function handleClear(e: React.MouseEvent) {
|
|
69
|
+
e.stopPropagation()
|
|
70
|
+
if (!isControlled) setInternalRange(undefined)
|
|
71
|
+
onValueChange?.(undefined)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const hasFrom = !!selectedRange?.from
|
|
75
|
+
const hasTo = !!selectedRange?.to
|
|
76
|
+
const hasAny = hasFrom || hasTo
|
|
77
|
+
|
|
78
|
+
const { height, px, text, gap, icon: iconSize } = sizeMap[size]
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<Popover open={open} onOpenChange={disabled ? undefined : setOpen}>
|
|
82
|
+
{/*
|
|
83
|
+
Same compound-input pattern as DatePicker: outer div owns border/ring chrome;
|
|
84
|
+
PopoverTrigger button and clear button are siblings (not nested) to satisfy
|
|
85
|
+
the axe-core nested-interactive rule.
|
|
86
|
+
*/}
|
|
87
|
+
<div
|
|
88
|
+
data-slot="date-range-picker-root"
|
|
89
|
+
data-size={size}
|
|
90
|
+
className={cn(
|
|
91
|
+
"inline-flex w-full min-w-0 items-center rounded-md border border-input bg-transparent shadow-xs",
|
|
92
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
93
|
+
disabled && "pointer-events-none cursor-not-allowed opacity-50",
|
|
94
|
+
height,
|
|
95
|
+
px,
|
|
96
|
+
gap,
|
|
97
|
+
className
|
|
98
|
+
)}
|
|
99
|
+
>
|
|
100
|
+
<PopoverTrigger asChild>
|
|
101
|
+
<button
|
|
102
|
+
type="button"
|
|
103
|
+
data-slot="date-range-picker-trigger"
|
|
104
|
+
disabled={disabled}
|
|
105
|
+
aria-haspopup="dialog"
|
|
106
|
+
aria-expanded={open}
|
|
107
|
+
aria-label={
|
|
108
|
+
hasFrom && hasTo
|
|
109
|
+
? `${formatDate(selectedRange!.from!)} to ${formatDate(selectedRange!.to!)}`
|
|
110
|
+
: hasFrom
|
|
111
|
+
? `From ${formatDate(selectedRange!.from!)}`
|
|
112
|
+
: "Select date range"
|
|
113
|
+
}
|
|
114
|
+
className={cn(
|
|
115
|
+
"inline-flex flex-1 min-w-0 h-full items-center text-left font-normal bg-transparent outline-none",
|
|
116
|
+
"transition-[color]",
|
|
117
|
+
gap,
|
|
118
|
+
text
|
|
119
|
+
)}
|
|
120
|
+
style={{
|
|
121
|
+
transitionDuration: "var(--duration-fast)",
|
|
122
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
<CalendarIcon
|
|
126
|
+
size={iconSize}
|
|
127
|
+
strokeWidth={1.5}
|
|
128
|
+
className="shrink-0 text-muted-foreground"
|
|
129
|
+
/>
|
|
130
|
+
<span className={cn("truncate", !hasFrom && "text-muted-foreground")}>
|
|
131
|
+
{hasFrom ? formatDate(selectedRange!.from!) : startPlaceholder}
|
|
132
|
+
</span>
|
|
133
|
+
<ArrowRightIcon
|
|
134
|
+
size={iconSize - 2}
|
|
135
|
+
strokeWidth={1.5}
|
|
136
|
+
className="shrink-0 text-muted-foreground mx-1"
|
|
137
|
+
/>
|
|
138
|
+
<span className={cn("truncate", !hasTo && "text-muted-foreground")}>
|
|
139
|
+
{hasTo ? formatDate(selectedRange!.to!) : endPlaceholder}
|
|
140
|
+
</span>
|
|
141
|
+
</button>
|
|
142
|
+
</PopoverTrigger>
|
|
143
|
+
{clearable && hasAny ? (
|
|
144
|
+
<button
|
|
145
|
+
type="button"
|
|
146
|
+
aria-label="Clear date range"
|
|
147
|
+
onClick={handleClear}
|
|
148
|
+
disabled={disabled}
|
|
149
|
+
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"
|
|
150
|
+
style={{
|
|
151
|
+
transitionDuration: "var(--duration-fast)",
|
|
152
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
153
|
+
}}
|
|
154
|
+
>
|
|
155
|
+
<XIcon size={iconSize - 2} strokeWidth={1.5} />
|
|
156
|
+
</button>
|
|
157
|
+
) : null}
|
|
158
|
+
</div>
|
|
159
|
+
<PopoverContent className="w-auto p-0" align="start">
|
|
160
|
+
<Calendar
|
|
161
|
+
mode="range"
|
|
162
|
+
selected={selectedRange as DateRange | undefined}
|
|
163
|
+
onSelect={handleSelect}
|
|
164
|
+
fromDate={fromDate}
|
|
165
|
+
toDate={toDate}
|
|
166
|
+
numberOfMonths={2}
|
|
167
|
+
initialFocus
|
|
168
|
+
/>
|
|
169
|
+
</PopoverContent>
|
|
170
|
+
</Popover>
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export { DateRangePicker }
|
|
@@ -16,6 +16,7 @@ export * from "./date-picker";
|
|
|
16
16
|
export * from "./chart";
|
|
17
17
|
export * from "./checkbox";
|
|
18
18
|
export * from "./collapsible";
|
|
19
|
+
export * from "./color-picker";
|
|
19
20
|
export * from "./command";
|
|
20
21
|
export * from "./context-menu";
|
|
21
22
|
export * from "./dialog";
|
|
@@ -29,6 +30,7 @@ export * from "./menubar";
|
|
|
29
30
|
export * from "./navigation-menu";
|
|
30
31
|
export * from "./number-input";
|
|
31
32
|
export * from "./pagination";
|
|
33
|
+
export * from "./password-input";
|
|
32
34
|
export * from "./popover";
|
|
33
35
|
export * from "./progress";
|
|
34
36
|
export * from "./radio-group";
|
|
@@ -57,3 +59,4 @@ export * from "./toggle";
|
|
|
57
59
|
export * from "./toggle-group";
|
|
58
60
|
export * from "./toolbar";
|
|
59
61
|
export * from "./tooltip";
|
|
62
|
+
export * from "./tree-view";
|
|
@@ -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 };
|