@behavioral-agentic-design-system/core 0.40.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/README.md +2 -0
- package/package.json +41 -9
- package/scripts/a11y-lint.cjs +66 -3
- package/scripts/check-spec.mjs +73 -0
- package/scripts/deal-directions.mjs +186 -0
- package/scripts/health-report.mjs +23 -3
- 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/lint-slop.mjs +25 -1
- 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/scripts/wcag-audit.cjs +72 -5
- 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/opengraph-image.tsx +5 -0
- package/src/app/preview/canvas/page.tsx +12 -0
- package/src/app/preview/components/page.tsx +2003 -2
- package/src/app/preview/studio/blank-screen.tsx +143 -0
- package/src/app/preview/studio/page.tsx +7 -0
- package/src/app/shell.tsx +168 -49
- package/src/components/Foundations/Motion.stories.tsx +1 -0
- 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/customize/design-controls.tsx +16 -1
- 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 +158 -0
- 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/anchor.tsx +37 -3
- package/src/components/ui/attachment.tsx +211 -0
- package/src/components/ui/autocomplete.tsx +1 -1
- package/src/components/ui/chart.tsx +3 -0
- package/src/components/ui/code.tsx +1 -1
- package/src/components/ui/color-picker.tsx +21 -5
- 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 +252 -0
- package/src/components/ui/heatmap.tsx +256 -0
- package/src/components/ui/index.ts +9 -0
- package/src/components/ui/infinite-scroll.tsx +103 -0
- package/src/components/ui/inline-edit.tsx +175 -0
- package/src/components/ui/json-input.tsx +351 -0
- package/src/components/ui/month-picker.tsx +1 -1
- package/src/components/ui/navigation-progress.tsx +281 -0
- package/src/components/ui/number-input.tsx +27 -7
- package/src/components/ui/otp-input.tsx +208 -0
- package/src/components/ui/password-input.tsx +2 -1
- package/src/components/ui/password-strength-input.tsx +288 -0
- package/src/components/ui/phone-input.tsx +274 -0
- package/src/components/ui/pill.tsx +140 -0
- package/src/components/ui/popover.tsx +27 -4
- package/src/components/ui/scroll-progress.tsx +100 -0
- package/src/components/ui/search-input.tsx +1 -1
- package/src/components/ui/snackbar.tsx +393 -0
- package/src/components/ui/sortable-list.tsx +342 -0
- package/src/components/ui/squircle.tsx +1 -1
- package/src/components/ui/table.tsx +1 -1
- package/src/components/ui/tags-input.tsx +1 -1
- package/src/components/ui/timer.tsx +1 -0
- package/src/components/ui/tour.tsx +1 -3
- 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 +36 -0
- 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/clamp.ts +2 -0
- package/src/lib/spec-parse.ts +4 -0
- package/src/lib/svg-sanitize.ts +19 -9
- package/src/stories/Anchor.stories.tsx +54 -0
- package/src/stories/Attachment.stories.tsx +189 -0
- package/src/stories/Autocomplete.stories.tsx +2 -0
- package/src/stories/Canvas.stories.tsx +45 -0
- package/src/stories/Chart.stories.tsx +4 -0
- package/src/stories/Code.stories.tsx +1 -0
- package/src/stories/ColorPicker.stories.tsx +41 -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 +169 -0
- package/src/stories/Heatmap.stories.tsx +172 -0
- package/src/stories/InfiniteScroll.stories.tsx +225 -0
- package/src/stories/InlineEdit.stories.tsx +106 -0
- package/src/stories/JsonInput.stories.tsx +212 -0
- package/src/stories/MarkdownPreview.stories.tsx +53 -0
- package/src/stories/MonthPicker.stories.tsx +21 -0
- package/src/stories/NavigationProgress.stories.tsx +159 -0
- package/src/stories/NumberInput.stories.tsx +26 -0
- package/src/stories/OtpInput.stories.tsx +110 -0
- package/src/stories/PasswordInput.stories.tsx +4 -0
- package/src/stories/PasswordStrengthInput.stories.tsx +211 -0
- package/src/stories/PhoneInput.stories.tsx +127 -0
- package/src/stories/Pill.stories.tsx +201 -0
- package/src/stories/Popover.stories.tsx +29 -1
- package/src/stories/Portal.stories.tsx +3 -1
- package/src/stories/ScrollProgress.stories.tsx +117 -0
- package/src/stories/SearchInput.stories.tsx +2 -0
- package/src/stories/Snackbar.stories.tsx +315 -0
- package/src/stories/SortableList.stories.tsx +263 -0
- package/src/stories/Squircle.stories.tsx +1 -0
- package/src/stories/Table.stories.tsx +1 -0
- package/src/stories/TagsInput.stories.tsx +2 -0
- package/src/stories/Timer.stories.tsx +26 -0
- package/src/stories/Tour.stories.tsx +1 -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 +33 -16
- package/src/tokens.json +82 -54
|
@@ -5,6 +5,7 @@ import { cva, type VariantProps } from "class-variance-authority";
|
|
|
5
5
|
import { Minus, Plus } from "lucide-react";
|
|
6
6
|
|
|
7
7
|
import { cn } from "../../lib/utils";
|
|
8
|
+
import { clamp } from "../../lib/clamp";
|
|
8
9
|
|
|
9
10
|
// ─── Variants ─────────────────────────────────────────────────────────────────
|
|
10
11
|
|
|
@@ -51,8 +52,15 @@ export interface NumberInputProps
|
|
|
51
52
|
|
|
52
53
|
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
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)));
|
|
56
64
|
}
|
|
57
65
|
|
|
58
66
|
// ─── NumberInput ──────────────────────────────────────────────────────────────
|
|
@@ -69,6 +77,8 @@ function NumberInput({
|
|
|
69
77
|
className,
|
|
70
78
|
"aria-label": ariaLabel = "Number input",
|
|
71
79
|
"aria-labelledby": ariaLabelledBy,
|
|
80
|
+
onBlur: onBlurProp,
|
|
81
|
+
onKeyDown: onKeyDownProp,
|
|
72
82
|
...rest
|
|
73
83
|
}: NumberInputProps) {
|
|
74
84
|
const isControlled = valueProp !== undefined;
|
|
@@ -103,12 +113,21 @@ function NumberInput({
|
|
|
103
113
|
[min, max, isControlled, onChange]
|
|
104
114
|
);
|
|
105
115
|
|
|
106
|
-
|
|
107
|
-
|
|
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);
|
|
108
125
|
|
|
109
126
|
// ── keyboard handling ──────────────────────────────────────────────────────
|
|
110
127
|
|
|
111
128
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
129
|
+
onKeyDownProp?.(e);
|
|
130
|
+
if (e.defaultPrevented) return;
|
|
112
131
|
if (e.key === "ArrowUp") {
|
|
113
132
|
e.preventDefault();
|
|
114
133
|
increment();
|
|
@@ -117,10 +136,10 @@ function NumberInput({
|
|
|
117
136
|
decrement();
|
|
118
137
|
} else if (e.key === "PageUp") {
|
|
119
138
|
e.preventDefault();
|
|
120
|
-
|
|
139
|
+
stepBy(step * 10);
|
|
121
140
|
} else if (e.key === "PageDown") {
|
|
122
141
|
e.preventDefault();
|
|
123
|
-
|
|
142
|
+
stepBy(-step * 10);
|
|
124
143
|
} else if (e.key === "Home" && Number.isFinite(min)) {
|
|
125
144
|
e.preventDefault();
|
|
126
145
|
commit(min);
|
|
@@ -138,7 +157,8 @@ function NumberInput({
|
|
|
138
157
|
|
|
139
158
|
// ── blur: commit typed value or reset ─────────────────────────────────────
|
|
140
159
|
|
|
141
|
-
const handleBlur = () => {
|
|
160
|
+
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
|
|
161
|
+
onBlurProp?.(e);
|
|
142
162
|
const parsed = parseFloat(inputStr);
|
|
143
163
|
if (Number.isFinite(parsed)) {
|
|
144
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 };
|
|
@@ -159,8 +159,9 @@ function PasswordInput({
|
|
|
159
159
|
aria-controls={inputId}
|
|
160
160
|
data-slot="password-input-toggle"
|
|
161
161
|
className={cn(
|
|
162
|
-
"flex shrink-0 items-center text-muted-foreground",
|
|
162
|
+
"flex shrink-0 items-center rounded-sm text-muted-foreground outline-none",
|
|
163
163
|
"hover:text-foreground",
|
|
164
|
+
"focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
164
165
|
"disabled:pointer-events-none",
|
|
165
166
|
btnPadding
|
|
166
167
|
)}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
import { PasswordInput, type PasswordInputProps } from "./password-input"
|
|
7
|
+
|
|
8
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
/** 0 = no input · 1 = weak · 2 = fair · 3 = good · 4 = strong */
|
|
11
|
+
export type PasswordStrength = 0 | 1 | 2 | 3 | 4
|
|
12
|
+
|
|
13
|
+
export interface PasswordStrengthInputProps
|
|
14
|
+
extends Omit<PasswordInputProps, "onChange" | "className"> {
|
|
15
|
+
/** Standard onChange forwarded to the underlying <input>. */
|
|
16
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void
|
|
17
|
+
/**
|
|
18
|
+
* Custom strength calculator. Receives the raw value string and must return
|
|
19
|
+
* 0 (no input) | 1 (weak) | 2 (fair) | 3 (good) | 4 (strong).
|
|
20
|
+
*
|
|
21
|
+
* Default: built-in heuristic keyed off length + character variety.
|
|
22
|
+
* Replace via prop to wire zxcvbn, a backend scorer, or a rules list.
|
|
23
|
+
*/
|
|
24
|
+
strengthFn?: (value: string) => PasswordStrength
|
|
25
|
+
/**
|
|
26
|
+
* Number of visual segments in the strength bar.
|
|
27
|
+
* @default 4
|
|
28
|
+
*/
|
|
29
|
+
segments?: 3 | 4 | 5
|
|
30
|
+
/**
|
|
31
|
+
* Whether to render the strength label ("Weak", "Fair", "Good", "Strong").
|
|
32
|
+
* Label also carries the aria-live region so screen readers hear strength
|
|
33
|
+
* changes regardless of this prop.
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
showLabel?: boolean
|
|
37
|
+
/**
|
|
38
|
+
* Whether to render the requirements hint list below the strength bar.
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
showHints?: boolean
|
|
42
|
+
/** className on the outer wrapper div. */
|
|
43
|
+
className?: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ─── Default strength heuristic ───────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns 0–4 based on length + character-variety heuristics:
|
|
50
|
+
*
|
|
51
|
+
* | Score | Label | Criteria |
|
|
52
|
+
* |-------|--------|------------------------------------------------------|
|
|
53
|
+
* | 0 | — | Empty string |
|
|
54
|
+
* | 1 | Weak | Non-empty, none of the bonuses below |
|
|
55
|
+
* | 2 | Fair | ≥8 chars |
|
|
56
|
+
* | 3 | Good | ≥8 chars + uppercase/lowercase + digit |
|
|
57
|
+
* | 4 | Strong | ≥12 chars + uppercase/lowercase + digit + special |
|
|
58
|
+
*
|
|
59
|
+
* Pass your own `strengthFn` prop to use zxcvbn or a rules-based scorer.
|
|
60
|
+
*/
|
|
61
|
+
export function defaultStrengthFn(value: string): PasswordStrength {
|
|
62
|
+
if (!value.length) return 0
|
|
63
|
+
let score = 0
|
|
64
|
+
if (value.length >= 8) score++
|
|
65
|
+
if (value.length >= 12) score++
|
|
66
|
+
if (/[A-Z]/.test(value) && /[a-z]/.test(value)) score++
|
|
67
|
+
if (/[0-9]/.test(value)) score++
|
|
68
|
+
if (/[^A-Za-z0-9]/.test(value)) score++
|
|
69
|
+
// Floor at 1 (weak) for any non-empty input; cap at 4 (strong).
|
|
70
|
+
return Math.min(4, Math.max(1, score)) as PasswordStrength
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ─── Static config ────────────────────────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
const STRENGTH_LABEL: Record<1 | 2 | 3 | 4, string> = {
|
|
76
|
+
1: "Weak",
|
|
77
|
+
2: "Fair",
|
|
78
|
+
3: "Good",
|
|
79
|
+
4: "Strong",
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Segment and label colors, keyed by strength level.
|
|
84
|
+
*
|
|
85
|
+
* Uses `bg-[var(--token)]` for --success and --warning because those tokens
|
|
86
|
+
* are declared as oklch values; wrapping them in hsl() would silently fail.
|
|
87
|
+
* (Same pattern as Meter and Sonner.)
|
|
88
|
+
*/
|
|
89
|
+
const STRENGTH_SEGMENT_BG: Record<1 | 2 | 3 | 4, string> = {
|
|
90
|
+
1: "bg-destructive",
|
|
91
|
+
2: "bg-[var(--warning)]",
|
|
92
|
+
3: "bg-primary",
|
|
93
|
+
4: "bg-[var(--success)]",
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const STRENGTH_LABEL_COLOR: Record<1 | 2 | 3 | 4, string> = {
|
|
97
|
+
1: "text-destructive",
|
|
98
|
+
2: "text-[var(--warning)]",
|
|
99
|
+
3: "text-primary",
|
|
100
|
+
4: "text-[var(--success)]",
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const PASSWORD_HINTS = [
|
|
104
|
+
"At least 8 characters",
|
|
105
|
+
"Uppercase and lowercase letters",
|
|
106
|
+
"At least one number",
|
|
107
|
+
"At least one special character (!@#$…)",
|
|
108
|
+
"12 or more characters for maximum strength",
|
|
109
|
+
] as const
|
|
110
|
+
|
|
111
|
+
// Segment transition — explicit properties, never transition-all.
|
|
112
|
+
const segmentMotion: React.CSSProperties = {
|
|
113
|
+
transitionProperty: "background-color",
|
|
114
|
+
transitionDuration: "var(--duration-base)",
|
|
115
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ─── PasswordStrengthInput ────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* PasswordInput with an inline segmented strength meter below the field.
|
|
122
|
+
*
|
|
123
|
+
* Composes BADS `PasswordInput` + a row of animated segment bars + a strength
|
|
124
|
+
* label. Strength is calculated from the live value via `strengthFn`; the
|
|
125
|
+
* built-in heuristic covers most signup/change-password surfaces. Override to
|
|
126
|
+
* wire zxcvbn or a backend scorer.
|
|
127
|
+
*
|
|
128
|
+
* ### Accessibility
|
|
129
|
+
* - Strength is conveyed by color **and** text (SC 1.4.1 — use of color).
|
|
130
|
+
* - The label carries `aria-live="polite"` + `aria-atomic="true"` so
|
|
131
|
+
* screen readers announce changes without the user having to query.
|
|
132
|
+
* - The input's `aria-describedby` is extended with the live-region id when
|
|
133
|
+
* there is text to describe.
|
|
134
|
+
*
|
|
135
|
+
* ### Peer parity
|
|
136
|
+
* Mantine `PasswordInput` (strength meter prop), shadcn/ui Auth block
|
|
137
|
+
* (password-strength composition), MUI docs (PasswordStrength example),
|
|
138
|
+
* Ark UI (PasswordInput + Meter composition).
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```tsx
|
|
142
|
+
* // Uncontrolled — simplest usage on a signup form
|
|
143
|
+
* <PasswordStrengthInput
|
|
144
|
+
* placeholder="New password"
|
|
145
|
+
* autoComplete="new-password"
|
|
146
|
+
* showHints
|
|
147
|
+
* />
|
|
148
|
+
*
|
|
149
|
+
* // Controlled
|
|
150
|
+
* const [pw, setPw] = React.useState("")
|
|
151
|
+
* <PasswordStrengthInput
|
|
152
|
+
* value={pw}
|
|
153
|
+
* onChange={(e) => setPw(e.target.value)}
|
|
154
|
+
* autoComplete="new-password"
|
|
155
|
+
* />
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function PasswordStrengthInput({
|
|
159
|
+
value,
|
|
160
|
+
defaultValue = "",
|
|
161
|
+
onChange,
|
|
162
|
+
strengthFn = defaultStrengthFn,
|
|
163
|
+
segments = 4,
|
|
164
|
+
showLabel = true,
|
|
165
|
+
showHints = false,
|
|
166
|
+
id,
|
|
167
|
+
className,
|
|
168
|
+
"aria-describedby": consumerDescribedBy,
|
|
169
|
+
...props
|
|
170
|
+
}: PasswordStrengthInputProps) {
|
|
171
|
+
const generatedId = React.useId()
|
|
172
|
+
const inputId = id ?? generatedId
|
|
173
|
+
const strengthRegionId = `${inputId}-strength`
|
|
174
|
+
|
|
175
|
+
// Uncontrolled path: track the raw value in local state so strength updates
|
|
176
|
+
// on every keystroke. Controlled path: strength is derived from `value` prop.
|
|
177
|
+
const [internalValue, setInternalValue] = React.useState(
|
|
178
|
+
typeof defaultValue === "string" ? defaultValue : ""
|
|
179
|
+
)
|
|
180
|
+
const isControlled = value !== undefined
|
|
181
|
+
const currentValue = isControlled ? String(value ?? "") : internalValue
|
|
182
|
+
const strength = strengthFn(currentValue)
|
|
183
|
+
const hasInput = currentValue.length > 0
|
|
184
|
+
|
|
185
|
+
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
|
|
186
|
+
if (!isControlled) setInternalValue(e.target.value)
|
|
187
|
+
onChange?.(e)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Merge caller's aria-describedby with the strength region id.
|
|
191
|
+
const mergedDescribedBy = [
|
|
192
|
+
consumerDescribedBy,
|
|
193
|
+
hasInput ? strengthRegionId : undefined,
|
|
194
|
+
]
|
|
195
|
+
.filter(Boolean)
|
|
196
|
+
.join(" ") || undefined
|
|
197
|
+
|
|
198
|
+
// How many segments should appear filled at this strength level?
|
|
199
|
+
// round((strength / 4) * N) maps 0–4 evenly onto 0–N for any segment count.
|
|
200
|
+
const filledCount = hasInput ? Math.round((strength / 4) * segments) : 0
|
|
201
|
+
|
|
202
|
+
const strengthKey = strength as 1 | 2 | 3 | 4
|
|
203
|
+
const segmentColor =
|
|
204
|
+
hasInput && strength > 0 ? STRENGTH_SEGMENT_BG[strengthKey] : null
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<div
|
|
208
|
+
className={cn("flex flex-col gap-1.5", className)}
|
|
209
|
+
data-slot="password-strength-input"
|
|
210
|
+
>
|
|
211
|
+
<PasswordInput
|
|
212
|
+
id={inputId}
|
|
213
|
+
value={isControlled ? value : undefined}
|
|
214
|
+
defaultValue={isControlled ? undefined : (defaultValue as string)}
|
|
215
|
+
onChange={handleChange}
|
|
216
|
+
aria-describedby={mergedDescribedBy}
|
|
217
|
+
{...props}
|
|
218
|
+
/>
|
|
219
|
+
|
|
220
|
+
{/* Segment bar — aria-hidden; the live region below carries the a11y signal */}
|
|
221
|
+
<div
|
|
222
|
+
role="presentation"
|
|
223
|
+
aria-hidden="true"
|
|
224
|
+
className="flex gap-1"
|
|
225
|
+
data-slot="strength-segments"
|
|
226
|
+
>
|
|
227
|
+
{Array.from({ length: segments }, (_, i) => {
|
|
228
|
+
const filled = i < filledCount
|
|
229
|
+
return (
|
|
230
|
+
<div
|
|
231
|
+
key={i}
|
|
232
|
+
data-slot="strength-segment"
|
|
233
|
+
data-filled={filled || undefined}
|
|
234
|
+
className={cn(
|
|
235
|
+
"h-1.5 flex-1 rounded-full",
|
|
236
|
+
filled && segmentColor ? segmentColor : "bg-border"
|
|
237
|
+
)}
|
|
238
|
+
style={segmentMotion}
|
|
239
|
+
/>
|
|
240
|
+
)
|
|
241
|
+
})}
|
|
242
|
+
</div>
|
|
243
|
+
|
|
244
|
+
{/* Strength label + aria-live region */}
|
|
245
|
+
{showLabel && (
|
|
246
|
+
<p
|
|
247
|
+
id={strengthRegionId}
|
|
248
|
+
role="status"
|
|
249
|
+
aria-live="polite"
|
|
250
|
+
aria-atomic="true"
|
|
251
|
+
data-slot="strength-label"
|
|
252
|
+
className={cn(
|
|
253
|
+
"h-[1.25rem] text-sm font-medium leading-none",
|
|
254
|
+
!hasInput || strength === 0
|
|
255
|
+
? "text-transparent select-none"
|
|
256
|
+
: STRENGTH_LABEL_COLOR[strengthKey]
|
|
257
|
+
)}
|
|
258
|
+
>
|
|
259
|
+
{hasInput && strength > 0 ? STRENGTH_LABEL[strengthKey] : " "}
|
|
260
|
+
</p>
|
|
261
|
+
)}
|
|
262
|
+
|
|
263
|
+
{/* Optional requirements hints */}
|
|
264
|
+
{showHints && (
|
|
265
|
+
<ul
|
|
266
|
+
data-slot="strength-hints"
|
|
267
|
+
className="mt-0.5 space-y-1"
|
|
268
|
+
aria-label="Password requirements"
|
|
269
|
+
>
|
|
270
|
+
{PASSWORD_HINTS.map((hint) => (
|
|
271
|
+
<li
|
|
272
|
+
key={hint}
|
|
273
|
+
className="flex items-center gap-2 text-sm text-muted-foreground"
|
|
274
|
+
>
|
|
275
|
+
<span
|
|
276
|
+
className="size-1.5 shrink-0 rounded-full bg-current opacity-30"
|
|
277
|
+
aria-hidden="true"
|
|
278
|
+
/>
|
|
279
|
+
{hint}
|
|
280
|
+
</li>
|
|
281
|
+
))}
|
|
282
|
+
</ul>
|
|
283
|
+
)}
|
|
284
|
+
</div>
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
PasswordStrengthInput.displayName = "PasswordStrengthInput"
|