@behavioral-agentic-design-system/core 0.8.3 → 0.9.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.
@@ -0,0 +1,257 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import { cva, type VariantProps } from "class-variance-authority";
5
+
6
+ import { cn } from "../../lib/utils";
7
+
8
+ // ─── Variants ─────────────────────────────────────────────────────────────────
9
+
10
+ const pinSlotVariants = cva(
11
+ [
12
+ "relative flex items-center justify-center",
13
+ "rounded-md border border-input bg-transparent",
14
+ "text-center font-sans font-semibold text-foreground caret-transparent",
15
+ "shadow-xs outline-none",
16
+ "transition-[color,box-shadow]",
17
+ "disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
18
+ "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40",
19
+ // Active-slot ring lights up via data-active OR :focus-visible — caret is
20
+ // hidden by convention (OTP slots show one digit centered), so the ring
21
+ // IS the cursor. Either signal lights it up so click-to-focus works.
22
+ "data-[active=true]:border-ring data-[active=true]:ring-[3px] data-[active=true]:ring-ring/50",
23
+ "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
24
+ ],
25
+ {
26
+ variants: {
27
+ size: {
28
+ sm: "size-9 text-sm",
29
+ default: "size-11 text-base",
30
+ lg: "size-14 text-xl",
31
+ },
32
+ },
33
+ defaultVariants: { size: "default" },
34
+ }
35
+ );
36
+
37
+ // ─── Types ────────────────────────────────────────────────────────────────────
38
+
39
+ export interface PinInputProps extends VariantProps<typeof pinSlotVariants> {
40
+ /** Number of character slots */
41
+ length?: number;
42
+ /** Controlled value — string up to `length` characters */
43
+ value?: string;
44
+ /** Uncontrolled initial value. Ignored if `value` is provided. */
45
+ defaultValue?: string;
46
+ /** Fires with the new string whenever the value changes */
47
+ onChange?: (value: string) => void;
48
+ /** Fires when all slots are filled */
49
+ onComplete?: (value: string) => void;
50
+ /** Sets all slots as disabled */
51
+ disabled?: boolean;
52
+ /** Propagate aria-invalid to each slot */
53
+ "aria-invalid"?: boolean;
54
+ /** Propagate aria-label on the group */
55
+ "aria-label"?: string;
56
+ className?: string;
57
+ /** Input type — "text" (default) or "password" */
58
+ type?: "text" | "password";
59
+ }
60
+
61
+ // ─── PinInput ─────────────────────────────────────────────────────────────────
62
+
63
+ function PinInput({
64
+ length = 6,
65
+ value: valueProp,
66
+ defaultValue = "",
67
+ onChange,
68
+ onComplete,
69
+ disabled = false,
70
+ size,
71
+ className,
72
+ type = "text",
73
+ "aria-invalid": ariaInvalid,
74
+ "aria-label": ariaLabel = "PIN input",
75
+ }: PinInputProps) {
76
+ const slots = Array.from({ length });
77
+ const inputRefs = React.useRef<Array<HTMLInputElement | null>>([]);
78
+ // Controlled-or-uncontrolled: when the caller doesn't pass `value`, we keep
79
+ // state internally so the showcase (and any consumer that just wants a
80
+ // working OTP input) gets click + type + arrow-key + paste behavior for free.
81
+ const isControlled = valueProp !== undefined;
82
+ const [internalValue, setInternalValue] = React.useState(defaultValue);
83
+ const value = isControlled ? (valueProp ?? "") : internalValue;
84
+ // Which slot the user is currently focused on. Lights the active ring even
85
+ // before the user has typed anything — that's the click-feedback Sir flagged
86
+ // as missing.
87
+ const [focusedIdx, setFocusedIdx] = React.useState<number | null>(null);
88
+
89
+ const commit = React.useCallback(
90
+ (next: string) => {
91
+ if (!isControlled) setInternalValue(next);
92
+ onChange?.(next);
93
+ if (next.length === length) {
94
+ onComplete?.(next);
95
+ }
96
+ },
97
+ [isControlled, length, onChange, onComplete]
98
+ );
99
+
100
+ // Focus the first empty slot, or the last one if full
101
+ const focusSlot = React.useCallback(
102
+ (idx: number) => {
103
+ const clamped = Math.max(0, Math.min(idx, length - 1));
104
+ inputRefs.current[clamped]?.focus();
105
+ },
106
+ [length]
107
+ );
108
+
109
+ // ── keyboard handling ──────────────────────────────────────────────────────
110
+
111
+ const handleKeyDown = (
112
+ e: React.KeyboardEvent<HTMLInputElement>,
113
+ idx: number
114
+ ) => {
115
+ if (e.key === "ArrowLeft") {
116
+ e.preventDefault();
117
+ focusSlot(idx - 1);
118
+ } else if (e.key === "ArrowRight") {
119
+ e.preventDefault();
120
+ focusSlot(idx + 1);
121
+ } else if (e.key === "Backspace") {
122
+ e.preventDefault();
123
+ const currentChar = value[idx];
124
+ if (currentChar) {
125
+ // clear current slot
126
+ const trimmed = (value.slice(0, idx) + value.slice(idx + 1)).slice(
127
+ 0,
128
+ length
129
+ );
130
+ commit(trimmed);
131
+ } else if (idx > 0) {
132
+ // already empty — clear previous and move back
133
+ const trimmed = (
134
+ value.slice(0, idx - 1) + value.slice(idx)
135
+ ).slice(0, length);
136
+ commit(trimmed);
137
+ focusSlot(idx - 1);
138
+ }
139
+ } else if (e.key === "Delete") {
140
+ e.preventDefault();
141
+ const trimmed = (value.slice(0, idx) + value.slice(idx + 1)).slice(
142
+ 0,
143
+ length
144
+ );
145
+ commit(trimmed);
146
+ } else if (e.key === "Home") {
147
+ e.preventDefault();
148
+ focusSlot(0);
149
+ } else if (e.key === "End") {
150
+ e.preventDefault();
151
+ focusSlot(length - 1);
152
+ }
153
+ };
154
+
155
+ // ── input handling ─────────────────────────────────────────────────────────
156
+
157
+ const handleInput = (
158
+ e: React.FormEvent<HTMLInputElement>,
159
+ idx: number
160
+ ) => {
161
+ const raw = (e.target as HTMLInputElement).value;
162
+ // Only use the last typed character (the input may show previous char + new)
163
+ const char = raw.replace(value[idx] ?? "", "").slice(-1);
164
+ if (!char) return;
165
+ const next = (value.slice(0, idx) + char + value.slice(idx + 1)).slice(
166
+ 0,
167
+ length
168
+ );
169
+ commit(next);
170
+ if (idx < length - 1) {
171
+ focusSlot(idx + 1);
172
+ }
173
+ };
174
+
175
+ // ── paste handling ─────────────────────────────────────────────────────────
176
+
177
+ const handlePaste = (
178
+ e: React.ClipboardEvent<HTMLInputElement>,
179
+ idx: number
180
+ ) => {
181
+ e.preventDefault();
182
+ const raw = e.clipboardData.getData("text");
183
+ // For numeric input mode strip non-digits; otherwise keep verbatim.
184
+ const cleaned = type === "text" && /^\d+$/.test(raw.replace(/\D/g, ""))
185
+ ? raw.replace(/\D/g, "")
186
+ : raw;
187
+ const filled = (value.slice(0, idx) + cleaned).slice(0, length);
188
+ commit(filled);
189
+ const nextFocus = Math.min(idx + cleaned.length, length - 1);
190
+ focusSlot(nextFocus);
191
+ };
192
+
193
+ // ── click: focus the first empty slot (prevents gaps) ─────────────────────
194
+
195
+ const handleClick = (_e: React.MouseEvent, idx: number) => {
196
+ // If clicking a slot after the current fill boundary, focus the boundary
197
+ const boundary = Math.min(value.length, length - 1);
198
+ if (idx > value.length) {
199
+ focusSlot(boundary);
200
+ }
201
+ };
202
+
203
+ return (
204
+ <div
205
+ data-slot="pin-input"
206
+ role="group"
207
+ aria-label={ariaLabel}
208
+ className={cn("flex items-center gap-2", className)}
209
+ >
210
+ {slots.map((_, idx) => {
211
+ const char = value[idx] ?? "";
212
+ // Light the ring when (a) the user has clicked into this slot, OR (b)
213
+ // no slot has focus yet and this is the next-empty slot (the default
214
+ // landing spot). Either way the showcase + click-driven flows get a
215
+ // visible cursor surrogate.
216
+ const isActive =
217
+ focusedIdx === idx ||
218
+ (focusedIdx === null &&
219
+ (idx === value.length || (value.length === length && idx === length - 1)));
220
+ return (
221
+ <input
222
+ key={idx}
223
+ ref={(el) => {
224
+ inputRefs.current[idx] = el;
225
+ }}
226
+ data-slot="pin-input-slot"
227
+ data-active={isActive ? "true" : undefined}
228
+ type={type}
229
+ inputMode="numeric"
230
+ maxLength={2}
231
+ value={char}
232
+ disabled={disabled}
233
+ aria-label={`Digit ${idx + 1} of ${length}`}
234
+ aria-invalid={ariaInvalid}
235
+ autoComplete={idx === 0 ? "one-time-code" : "off"}
236
+ className={cn(pinSlotVariants({ size }))}
237
+ style={{
238
+ transitionDuration: "var(--duration-fast)",
239
+ transitionTimingFunction: "var(--ease-out)",
240
+ }}
241
+ onKeyDown={(e) => handleKeyDown(e, idx)}
242
+ onInput={(e) => handleInput(e, idx)}
243
+ onPaste={(e) => handlePaste(e, idx)}
244
+ onClick={(e) => handleClick(e, idx)}
245
+ onFocus={() => setFocusedIdx(idx)}
246
+ onBlur={() => setFocusedIdx(null)}
247
+ onChange={() => {
248
+ /* controlled via onInput */
249
+ }}
250
+ />
251
+ );
252
+ })}
253
+ </div>
254
+ );
255
+ }
256
+
257
+ export { PinInput, pinSlotVariants };
@@ -84,7 +84,13 @@ function SheetContent({
84
84
  {showCloseButton && (
85
85
  <SheetPrimitive.Close
86
86
  data-slot="sheet-close"
87
- className="absolute top-3 right-3 inline-flex size-9 items-center justify-center rounded-md text-foreground ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
87
+ // Matches the Dialog close button: top-4 right-4, rounded-xs, a 32px
88
+ // (size-8) X glyph, opacity-70→100 on hover. Shared by Sheet AND
89
+ // BottomSheet (both render SheetContent), so they read as one family
90
+ // with Dialog. The one intentional difference from Dialog: a 44px
91
+ // (size-11) hit target wrapping the 32px glyph, so it clears BADS's
92
+ // touch-target floor — invisible since there's no box fill.
93
+ className="absolute top-4 right-4 inline-flex size-11 items-center justify-center rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-8"
88
94
  style={{ strokeWidth: '1.5px' }}
89
95
  >
90
96
  <XIcon />
@@ -100,9 +106,9 @@ function SheetHeader({ className, ...props }: React.ComponentProps<"div">) {
100
106
  return (
101
107
  <div
102
108
  data-slot="sheet-header"
103
- // pr-12 reserves space for the absolutely-positioned close button (top-3
104
- // right-3, size-9) so the title/description can never run underneath it.
105
- className={cn("flex flex-col gap-1.5 p-4 pr-12", className)}
109
+ // pr-16 reserves space for the absolutely-positioned close button (top-4
110
+ // right-4, size-11) so the title/description can never run underneath it.
111
+ className={cn("flex flex-col gap-1.5 p-4 pr-16", className)}
106
112
  {...props}
107
113
  />
108
114
  )
@@ -0,0 +1,70 @@
1
+ import * as React from "react"
2
+ import { cva, type VariantProps } from "class-variance-authority"
3
+ import { cn } from "../../lib/utils"
4
+
5
+ const spinnerVariants = cva("animate-spin", {
6
+ variants: {
7
+ size: {
8
+ xs: "size-4",
9
+ sm: "size-5",
10
+ default: "size-6",
11
+ lg: "size-8",
12
+ },
13
+ variant: {
14
+ default: "text-foreground",
15
+ muted: "text-muted-foreground",
16
+ primary: "text-primary",
17
+ },
18
+ },
19
+ defaultVariants: {
20
+ size: "default",
21
+ variant: "default",
22
+ },
23
+ })
24
+
25
+ type SpinnerProps = React.HTMLAttributes<HTMLSpanElement> &
26
+ VariantProps<typeof spinnerVariants> & {
27
+ label?: string
28
+ }
29
+
30
+ function Spinner({
31
+ className,
32
+ size,
33
+ variant,
34
+ label = "Loading",
35
+ ...props
36
+ }: SpinnerProps) {
37
+ return (
38
+ <span
39
+ data-slot="spinner"
40
+ role="status"
41
+ aria-label={label}
42
+ className={cn("inline-flex items-center justify-center", className)}
43
+ {...props}
44
+ >
45
+ <svg
46
+ className={spinnerVariants({ size, variant })}
47
+ xmlns="http://www.w3.org/2000/svg"
48
+ fill="none"
49
+ viewBox="0 0 24 24"
50
+ aria-hidden="true"
51
+ >
52
+ <circle
53
+ className="opacity-25"
54
+ cx="12"
55
+ cy="12"
56
+ r="10"
57
+ stroke="currentColor"
58
+ strokeWidth="1.5"
59
+ />
60
+ <path
61
+ className="opacity-75"
62
+ fill="currentColor"
63
+ d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
64
+ />
65
+ </svg>
66
+ </span>
67
+ )
68
+ }
69
+
70
+ export { Spinner, spinnerVariants }
@@ -0,0 +1,272 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import { CheckIcon, XIcon } from "lucide-react";
5
+
6
+ import { cn } from "@behavioral-agentic-design-system/core/lib/utils";
7
+
8
+ // ─── Types ────────────────────────────────────────────────────────────────────
9
+
10
+ type Orientation = "horizontal" | "vertical";
11
+ type StepState = "pending" | "active" | "completed" | "error";
12
+
13
+ // ─── Stepper context ──────────────────────────────────────────────────────────
14
+
15
+ type StepperContextValue = {
16
+ value: number;
17
+ orientation: Orientation;
18
+ totalSteps: number;
19
+ };
20
+
21
+ const StepperContext = React.createContext<StepperContextValue | null>(null);
22
+
23
+ function useStepper() {
24
+ const ctx = React.useContext(StepperContext);
25
+ if (!ctx) throw new Error("Stepper sub-components must be used inside <Stepper>");
26
+ return ctx;
27
+ }
28
+
29
+ // ─── StepperItem context ──────────────────────────────────────────────────────
30
+
31
+ type StepperItemContextValue = {
32
+ step: number;
33
+ state: StepState;
34
+ };
35
+
36
+ const StepperItemContext = React.createContext<StepperItemContextValue | null>(null);
37
+
38
+ // ─── Root ─────────────────────────────────────────────────────────────────────
39
+
40
+ type StepperProps = {
41
+ /** 1-based index of the currently active step */
42
+ value: number;
43
+ orientation?: Orientation;
44
+ "aria-label"?: string;
45
+ } & React.ComponentPropsWithoutRef<"ol">;
46
+
47
+ function Stepper({
48
+ value,
49
+ orientation = "horizontal",
50
+ children,
51
+ className,
52
+ "aria-label": ariaLabel = "Progress steps",
53
+ ...props
54
+ }: StepperProps) {
55
+ const totalSteps = React.Children.toArray(children).filter(
56
+ (c) => React.isValidElement(c) && (c.type as React.FC) === StepperItem
57
+ ).length;
58
+
59
+ return (
60
+ <StepperContext.Provider value={{ value, orientation, totalSteps }}>
61
+ <ol
62
+ data-slot="stepper"
63
+ aria-label={ariaLabel}
64
+ className={cn(
65
+ "flex",
66
+ orientation === "horizontal" ? "flex-row items-start" : "flex-col",
67
+ className
68
+ )}
69
+ {...props}
70
+ >
71
+ {children}
72
+ </ol>
73
+ </StepperContext.Provider>
74
+ );
75
+ }
76
+
77
+ // ─── Item ─────────────────────────────────────────────────────────────────────
78
+
79
+ type StepperItemProps = {
80
+ /** 1-based index of this step */
81
+ step: number;
82
+ /** Flag the step as errored regardless of value comparison */
83
+ error?: boolean;
84
+ } & React.ComponentPropsWithoutRef<"li">;
85
+
86
+ function StepperItem({
87
+ step,
88
+ error = false,
89
+ children,
90
+ className,
91
+ ...props
92
+ }: StepperItemProps) {
93
+ const { value, orientation } = useStepper();
94
+
95
+ const state: StepState = error
96
+ ? "error"
97
+ : step < value
98
+ ? "completed"
99
+ : step === value
100
+ ? "active"
101
+ : "pending";
102
+
103
+ return (
104
+ <StepperItemContext.Provider value={{ step, state }}>
105
+ <li
106
+ data-slot="stepper-item"
107
+ data-state={state}
108
+ aria-current={state === "active" ? "step" : undefined}
109
+ className={cn(
110
+ "group/step flex",
111
+ orientation === "horizontal"
112
+ ? "flex-col items-center flex-1"
113
+ : "flex-row items-start gap-3",
114
+ className
115
+ )}
116
+ {...props}
117
+ >
118
+ {children}
119
+ </li>
120
+ </StepperItemContext.Provider>
121
+ );
122
+ }
123
+
124
+ // ─── Indicator ────────────────────────────────────────────────────────────────
125
+
126
+ function StepperIndicator({
127
+ className,
128
+ ...props
129
+ }: React.ComponentPropsWithoutRef<"div">) {
130
+ const { totalSteps } = useStepper();
131
+ const ctx = React.useContext(StepperItemContext);
132
+ if (!ctx) throw new Error("StepperIndicator must be used inside <StepperItem>");
133
+ const { step, state } = ctx;
134
+
135
+ return (
136
+ <div
137
+ data-slot="stepper-indicator"
138
+ data-state={state}
139
+ role="img"
140
+ aria-label={`Step ${step} of ${totalSteps}${
141
+ state === "completed" ? ", completed" : state === "error" ? ", error" : ""
142
+ }`}
143
+ className={cn(
144
+ // 44×44px touch target (BADS SC 2.5.5 contract)
145
+ "flex shrink-0 items-center justify-center size-11 rounded-full border-2",
146
+ "text-sm font-semibold",
147
+ "transition-colors",
148
+ state === "completed" && "border-primary bg-primary text-primary-foreground",
149
+ state === "active" && "border-primary bg-background text-primary",
150
+ state === "pending" && "border-input bg-background text-muted-foreground",
151
+ state === "error" && "border-destructive bg-background text-destructive",
152
+ className
153
+ )}
154
+ style={{
155
+ transitionDuration: "var(--duration-base)",
156
+ transitionTimingFunction: "var(--ease-out)",
157
+ }}
158
+ {...props}
159
+ >
160
+ {state === "completed" ? (
161
+ <CheckIcon size={22} strokeWidth={1.5} aria-hidden="true" />
162
+ ) : state === "error" ? (
163
+ <XIcon size={22} strokeWidth={1.5} aria-hidden="true" />
164
+ ) : (
165
+ <span aria-hidden="true">{step}</span>
166
+ )}
167
+ </div>
168
+ );
169
+ }
170
+
171
+ // ─── Title ────────────────────────────────────────────────────────────────────
172
+
173
+ function StepperTitle({
174
+ className,
175
+ ...props
176
+ }: React.ComponentPropsWithoutRef<"p">) {
177
+ const ctx = React.useContext(StepperItemContext);
178
+ const state = ctx?.state ?? "pending";
179
+ return (
180
+ <p
181
+ data-slot="stepper-title"
182
+ data-state={state}
183
+ className={cn(
184
+ "text-sm font-medium leading-tight",
185
+ state === "active" || state === "completed"
186
+ ? "text-foreground"
187
+ : "text-muted-foreground",
188
+ className
189
+ )}
190
+ {...props}
191
+ />
192
+ );
193
+ }
194
+
195
+ // ─── Description ─────────────────────────────────────────────────────────────
196
+
197
+ function StepperDescription({
198
+ className,
199
+ ...props
200
+ }: React.ComponentPropsWithoutRef<"p">) {
201
+ return (
202
+ <p
203
+ data-slot="stepper-description"
204
+ className={cn("text-xs text-muted-foreground mt-0.5", className)}
205
+ {...props}
206
+ />
207
+ );
208
+ }
209
+
210
+ // ─── Separator ────────────────────────────────────────────────────────────────
211
+ //
212
+ // Horizontal mode: place between <StepperItem>s in the <Stepper> — pass the
213
+ // `step` it follows (e.g. step={1} after StepperItem step={1}) so it knows
214
+ // whether to render as completed.
215
+ //
216
+ // Vertical mode: place inside a <StepperItem>, inside the indicator column
217
+ // (a div.flex.flex-col.items-center wrapping the indicator + separator) so
218
+ // the bar extends downward from the indicator.
219
+ // In this case the state is derived from StepperItemContext automatically.
220
+
221
+ type StepperSeparatorProps = {
222
+ /** The step number this separator follows — required when placed between items */
223
+ step?: number;
224
+ } & React.ComponentPropsWithoutRef<"div">;
225
+
226
+ function StepperSeparator({
227
+ step: stepProp,
228
+ className,
229
+ ...props
230
+ }: StepperSeparatorProps) {
231
+ const { value, orientation } = useStepper();
232
+ const itemCtx = React.useContext(StepperItemContext);
233
+
234
+ // Separator is "colored" when the step it follows is completed
235
+ const followStep = stepProp ?? itemCtx?.step;
236
+ const colored =
237
+ followStep !== undefined
238
+ ? followStep < value
239
+ : itemCtx?.state === "completed";
240
+
241
+ return (
242
+ <div
243
+ data-slot="stepper-separator"
244
+ aria-hidden="true"
245
+ className={cn(
246
+ "shrink-0 rounded-full transition-colors",
247
+ orientation === "horizontal"
248
+ // Between items in horizontal mode: thin horizontal bar.
249
+ // mt aligns bar with indicator center (size-11 = 44 px → center = 22 px = 1.375rem).
250
+ ? "h-0.5 flex-1 self-start mt-[1.375rem] mx-1"
251
+ // Inside indicator column in vertical mode: thin vertical bar.
252
+ : "w-0.5 flex-1 min-h-4 mx-auto mt-1",
253
+ colored ? "bg-primary" : "bg-input",
254
+ className
255
+ )}
256
+ style={{
257
+ transitionDuration: "var(--duration-base)",
258
+ transitionTimingFunction: "var(--ease-out)",
259
+ }}
260
+ {...props}
261
+ />
262
+ );
263
+ }
264
+
265
+ export {
266
+ Stepper,
267
+ StepperItem,
268
+ StepperIndicator,
269
+ StepperTitle,
270
+ StepperDescription,
271
+ StepperSeparator,
272
+ };