@behavioral-agentic-design-system/core 0.13.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.
@@ -0,0 +1,213 @@
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
+ // ─── Context ──────────────────────────────────────────────────────────────────
8
+
9
+ interface SegmentedControlContextValue {
10
+ value: string;
11
+ onValueChange: (value: string) => void;
12
+ disabled: boolean;
13
+ size: "sm" | "default" | "lg";
14
+ }
15
+
16
+ const SegmentedControlContext = React.createContext<SegmentedControlContextValue>({
17
+ value: "",
18
+ onValueChange: () => {},
19
+ disabled: false,
20
+ size: "default",
21
+ });
22
+
23
+ // ─── Variants ─────────────────────────────────────────────────────────────────
24
+
25
+ const rootVariants = cva(
26
+ [
27
+ "relative inline-grid rounded-lg bg-muted p-1",
28
+ ],
29
+ {
30
+ variants: {
31
+ size: {
32
+ sm: "gap-0.5",
33
+ default: "gap-0.5",
34
+ lg: "gap-0.5",
35
+ },
36
+ },
37
+ defaultVariants: { size: "default" },
38
+ }
39
+ );
40
+
41
+ const itemVariants = cva(
42
+ [
43
+ "relative z-10 inline-flex cursor-pointer select-none items-center justify-center whitespace-nowrap rounded-md",
44
+ "font-medium text-muted-foreground",
45
+ "transition-colors",
46
+ "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1",
47
+ "disabled:pointer-events-none disabled:opacity-50",
48
+ "aria-checked:text-foreground",
49
+ "hover:text-foreground",
50
+ ],
51
+ {
52
+ variants: {
53
+ size: {
54
+ sm: "px-2.5 min-h-8 text-xs",
55
+ default: "px-3 min-h-11 text-sm",
56
+ lg: "px-4 min-h-12 text-sm",
57
+ },
58
+ },
59
+ defaultVariants: { size: "default" },
60
+ }
61
+ );
62
+
63
+ // ─── Root ─────────────────────────────────────────────────────────────────────
64
+
65
+ export interface SegmentedControlProps
66
+ extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
67
+ /** The currently selected value. */
68
+ value: string;
69
+ /** Callback fired when the selected value changes. */
70
+ onValueChange: (value: string) => void;
71
+ /** Visual + size variant. */
72
+ size?: "sm" | "default" | "lg";
73
+ /** Disables all items. */
74
+ disabled?: boolean;
75
+ /** Accessible label for the radio group. */
76
+ label?: string;
77
+ }
78
+
79
+ export function SegmentedControl({
80
+ value,
81
+ onValueChange,
82
+ size = "default",
83
+ disabled = false,
84
+ label,
85
+ className,
86
+ children,
87
+ ...props
88
+ }: SegmentedControlProps) {
89
+ // Count valid SegmentedControlItem children to calculate indicator geometry
90
+ const items = React.Children.toArray(children).filter(
91
+ (child): child is React.ReactElement<SegmentedControlItemProps> =>
92
+ React.isValidElement(child)
93
+ );
94
+ const count = items.length;
95
+ const selectedIndex = items.findIndex((item) => item.props.value === value);
96
+
97
+ // Roving tabindex: track focused item index so ArrowLeft/Right works across items
98
+ const rootRef = React.useRef<HTMLDivElement>(null);
99
+
100
+ const handleKeyDown = React.useCallback(
101
+ (e: React.KeyboardEvent<HTMLDivElement>) => {
102
+ if (!rootRef.current) return;
103
+ const buttons = Array.from(
104
+ rootRef.current.querySelectorAll<HTMLButtonElement>("[role=radio]:not([disabled])")
105
+ );
106
+ const focusedIdx = buttons.findIndex((b) => b === document.activeElement);
107
+ if (focusedIdx === -1) return;
108
+
109
+ let next = focusedIdx;
110
+ if (e.key === "ArrowRight" || e.key === "ArrowDown") {
111
+ next = (focusedIdx + 1) % buttons.length;
112
+ } else if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
113
+ next = (focusedIdx - 1 + buttons.length) % buttons.length;
114
+ } else if (e.key === "Home") {
115
+ next = 0;
116
+ } else if (e.key === "End") {
117
+ next = buttons.length - 1;
118
+ } else {
119
+ return;
120
+ }
121
+ e.preventDefault();
122
+ buttons[next]?.focus();
123
+ const nextValue = items.find((_, i) => {
124
+ const enabledItems = items.filter((item) => !item.props.disabled);
125
+ return enabledItems[next] === items[i];
126
+ });
127
+ const targetItem = items.filter((item) => !item.props.disabled)[next];
128
+ if (targetItem) onValueChange(targetItem.props.value);
129
+ },
130
+ [items, onValueChange]
131
+ );
132
+
133
+ return (
134
+ <SegmentedControlContext.Provider value={{ value, onValueChange, disabled, size }}>
135
+ <div
136
+ ref={rootRef}
137
+ role="radiogroup"
138
+ aria-label={label}
139
+ data-slot="segmented-control"
140
+ data-disabled={disabled || undefined}
141
+ onKeyDown={handleKeyDown}
142
+ className={cn(rootVariants({ size }), className)}
143
+ style={{ gridTemplateColumns: `repeat(${count}, 1fr)` }}
144
+ {...props}
145
+ >
146
+ {/* Sliding indicator — absolutely positioned, moves via left transition */}
147
+ {selectedIndex >= 0 && count > 0 && (
148
+ <div
149
+ aria-hidden
150
+ data-slot="segmented-control-indicator"
151
+ className="absolute inset-y-1 rounded-md bg-background shadow-sm pointer-events-none"
152
+ style={{
153
+ // width = (containerWidth - 2*p-1 gap) / count
154
+ width: `calc((100% - 8px) / ${count})`,
155
+ // left = p-1(4px) + selectedIndex * (itemWidth + gap)
156
+ left: `calc(${selectedIndex} * (100% - 8px) / ${count} + 4px)`,
157
+ transitionProperty: "left",
158
+ transitionDuration: "var(--duration-fast)",
159
+ transitionTimingFunction: "var(--ease-out)",
160
+ }}
161
+ />
162
+ )}
163
+ {children}
164
+ </div>
165
+ </SegmentedControlContext.Provider>
166
+ );
167
+ }
168
+
169
+ // ─── Item ─────────────────────────────────────────────────────────────────────
170
+
171
+ export interface SegmentedControlItemProps
172
+ extends React.ButtonHTMLAttributes<HTMLButtonElement> {
173
+ /** The value this item represents. */
174
+ value: string;
175
+ /** Icon to render before the label. Must be a Lucide icon with strokeWidth={1.5}. */
176
+ icon?: React.ReactNode;
177
+ }
178
+
179
+ export function SegmentedControlItem({
180
+ value,
181
+ icon,
182
+ className,
183
+ disabled,
184
+ children,
185
+ ...props
186
+ }: SegmentedControlItemProps) {
187
+ const { value: selectedValue, onValueChange, disabled: groupDisabled, size } =
188
+ React.useContext(SegmentedControlContext);
189
+ const isSelected = selectedValue === value;
190
+ const isDisabled = groupDisabled || disabled;
191
+
192
+ return (
193
+ <button
194
+ type="button"
195
+ role="radio"
196
+ aria-checked={isSelected}
197
+ data-slot="segmented-control-item"
198
+ data-state={isSelected ? "on" : "off"}
199
+ disabled={isDisabled}
200
+ tabIndex={isSelected ? 0 : -1}
201
+ onClick={() => {
202
+ if (!isDisabled) onValueChange(value);
203
+ }}
204
+ className={cn(itemVariants({ size }), className)}
205
+ {...props}
206
+ >
207
+ {icon && (
208
+ <span className="mr-1.5 shrink-0 [&>svg]:size-4 [&>svg]:stroke-[1.5]">{icon}</span>
209
+ )}
210
+ {children}
211
+ </button>
212
+ );
213
+ }
@@ -165,6 +165,12 @@ function StepperIndicator({
165
165
  ...(state === "active" && {
166
166
  borderColor: "var(--primary)",
167
167
  }),
168
+ // Error matches the active/completed pattern: inline so the
169
+ // destructive color resolves regardless of the @theme cascade
170
+ // (the `border-destructive` class alone falls back to gray).
171
+ ...(state === "error" && {
172
+ borderColor: "var(--destructive)",
173
+ }),
168
174
  }}
169
175
  {...props}
170
176
  >
@@ -193,9 +199,11 @@ function StepperTitle({
193
199
  data-state={state}
194
200
  className={cn(
195
201
  "text-sm font-medium leading-tight",
196
- state === "active" || state === "completed"
197
- ? "text-foreground"
198
- : "text-muted-foreground",
202
+ state === "error"
203
+ ? "text-destructive"
204
+ : state === "active" || state === "completed"
205
+ ? "text-foreground"
206
+ : "text-muted-foreground",
199
207
  className
200
208
  )}
201
209
  {...props}
@@ -106,7 +106,7 @@ function TabsList({
106
106
  className={cn(
107
107
  tabsListVariants({ variant }),
108
108
  scrollable &&
109
- "overflow-x-auto [scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden data-[overflowing]:[mask-image:linear-gradient(to_right,#000_calc(100%-2rem),transparent)]",
109
+ "overflow-x-auto scrollbar-none data-[overflowing]:[mask-image:linear-gradient(to_right,#000_calc(100%-2rem),transparent)]",
110
110
  className
111
111
  )}
112
112
  {...props}
@@ -0,0 +1,199 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import { XIcon } from "lucide-react";
5
+ import { cn } from "../../lib/utils";
6
+
7
+ // ─── Types ────────────────────────────────────────────────────────────────────
8
+
9
+ export interface TagsInputProps
10
+ extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
11
+ /** The current list of tag values. */
12
+ value: string[];
13
+ /** Callback fired when the tag list changes. */
14
+ onValueChange: (value: string[]) => void;
15
+ /** Placeholder shown when no tags are present. */
16
+ placeholder?: string;
17
+ /** Disables the entire input. */
18
+ disabled?: boolean;
19
+ /** Maximum number of tags allowed. Input is blocked once reached. */
20
+ maxTags?: number;
21
+ /** Allow duplicate tag values. Defaults to false. */
22
+ allowDuplicates?: boolean;
23
+ /** Characters that trigger tag creation in addition to Enter. Default: [","]. */
24
+ delimiters?: string[];
25
+ }
26
+
27
+ // ─── Component ────────────────────────────────────────────────────────────────
28
+
29
+ export function TagsInput({
30
+ value,
31
+ onValueChange,
32
+ placeholder,
33
+ disabled = false,
34
+ maxTags,
35
+ allowDuplicates = false,
36
+ delimiters = [","],
37
+ className,
38
+ ...props
39
+ }: TagsInputProps) {
40
+ const [inputValue, setInputValue] = React.useState("");
41
+ const [focusedTagIndex, setFocusedTagIndex] = React.useState<number | null>(null);
42
+ const inputRef = React.useRef<HTMLInputElement>(null);
43
+
44
+ const atMax = maxTags !== undefined && value.length >= maxTags;
45
+
46
+ const addTag = React.useCallback(
47
+ (raw: string) => {
48
+ const trimmed = raw.trim();
49
+ if (!trimmed) return;
50
+ if (atMax) return;
51
+ if (!allowDuplicates && value.includes(trimmed)) return;
52
+ onValueChange([...value, trimmed]);
53
+ setInputValue("");
54
+ },
55
+ [atMax, allowDuplicates, value, onValueChange]
56
+ );
57
+
58
+ const removeTag = React.useCallback(
59
+ (index: number) => {
60
+ onValueChange(value.filter((_, i) => i !== index));
61
+ setFocusedTagIndex(null);
62
+ inputRef.current?.focus();
63
+ },
64
+ [value, onValueChange]
65
+ );
66
+
67
+ const handleKeyDown = React.useCallback(
68
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
69
+ if (e.key === "Enter") {
70
+ e.preventDefault();
71
+ addTag(inputValue);
72
+ } else if (e.key === "Backspace" && inputValue === "") {
73
+ e.preventDefault();
74
+ if (focusedTagIndex !== null) {
75
+ removeTag(focusedTagIndex);
76
+ } else if (value.length > 0) {
77
+ setFocusedTagIndex(value.length - 1);
78
+ }
79
+ } else if (e.key === "Delete" && inputValue === "" && focusedTagIndex !== null) {
80
+ e.preventDefault();
81
+ removeTag(focusedTagIndex);
82
+ } else if (e.key === "ArrowLeft" && inputValue === "") {
83
+ if (focusedTagIndex === null && value.length > 0) {
84
+ setFocusedTagIndex(value.length - 1);
85
+ } else if (focusedTagIndex !== null && focusedTagIndex > 0) {
86
+ setFocusedTagIndex(focusedTagIndex - 1);
87
+ }
88
+ } else if (e.key === "ArrowRight" && inputValue === "") {
89
+ if (focusedTagIndex !== null) {
90
+ if (focusedTagIndex < value.length - 1) {
91
+ setFocusedTagIndex(focusedTagIndex + 1);
92
+ } else {
93
+ setFocusedTagIndex(null);
94
+ }
95
+ }
96
+ } else if (e.key === "Escape") {
97
+ setFocusedTagIndex(null);
98
+ } else {
99
+ setFocusedTagIndex(null);
100
+ }
101
+ },
102
+ [inputValue, focusedTagIndex, value, addTag, removeTag]
103
+ );
104
+
105
+ const handleInputChange = React.useCallback(
106
+ (e: React.ChangeEvent<HTMLInputElement>) => {
107
+ const val = e.target.value;
108
+ const lastChar = val[val.length - 1];
109
+ if (lastChar !== undefined && delimiters.includes(lastChar)) {
110
+ addTag(val.slice(0, -1));
111
+ } else {
112
+ setInputValue(val);
113
+ }
114
+ setFocusedTagIndex(null);
115
+ },
116
+ [delimiters, addTag]
117
+ );
118
+
119
+ const inputPlaceholder =
120
+ value.length === 0
121
+ ? placeholder
122
+ : atMax
123
+ ? `Max ${maxTags} tags reached`
124
+ : undefined;
125
+
126
+ return (
127
+ <div
128
+ data-slot="tags-input"
129
+ data-disabled={disabled || undefined}
130
+ aria-disabled={disabled || undefined}
131
+ onClick={() => !disabled && inputRef.current?.focus()}
132
+ className={cn(
133
+ "flex min-h-11 flex-wrap items-center gap-1.5 rounded-md border border-input bg-background px-3 py-2",
134
+ "cursor-text",
135
+ "focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2",
136
+ disabled && "cursor-not-allowed border-border/50 bg-muted/30 pointer-events-none",
137
+ className
138
+ )}
139
+ {...props}
140
+ >
141
+ {value.map((tag, index) => (
142
+ <span
143
+ key={`${tag}-${index}`}
144
+ data-slot="tags-input-item"
145
+ data-focused={focusedTagIndex === index || undefined}
146
+ className={cn(
147
+ "inline-flex max-w-[160px] items-center gap-1 rounded-md px-2 py-0.5",
148
+ "text-sm font-medium transition-colors",
149
+ disabled
150
+ ? "bg-muted text-muted-foreground"
151
+ : "bg-secondary text-secondary-foreground",
152
+ !disabled && focusedTagIndex === index && "ring-2 ring-ring ring-offset-1 ring-offset-background"
153
+ )}
154
+ >
155
+ <span className="truncate">{tag}</span>
156
+ {!disabled && (
157
+ <button
158
+ type="button"
159
+ data-slot="tags-input-item-delete"
160
+ aria-label={`Remove ${tag}`}
161
+ tabIndex={-1}
162
+ onClick={(e) => {
163
+ e.stopPropagation();
164
+ removeTag(index);
165
+ }}
166
+ className={cn(
167
+ "shrink-0 rounded-sm p-0.5",
168
+ "text-secondary-foreground/70 hover:text-secondary-foreground",
169
+ "transition-colors",
170
+ "focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
171
+ )}
172
+ style={{
173
+ transitionDuration: "var(--duration-fast)",
174
+ transitionTimingFunction: "var(--ease-out)",
175
+ }}
176
+ >
177
+ <XIcon className="size-3" strokeWidth={1.5} aria-hidden />
178
+ </button>
179
+ )}
180
+ </span>
181
+ ))}
182
+ <input
183
+ ref={inputRef}
184
+ type="text"
185
+ value={inputValue}
186
+ onChange={handleInputChange}
187
+ onKeyDown={handleKeyDown}
188
+ disabled={disabled || atMax}
189
+ placeholder={inputPlaceholder}
190
+ aria-label={placeholder ?? "Add tags, press Enter or comma to confirm"}
191
+ className={cn(
192
+ "min-w-[80px] flex-1 bg-transparent text-sm outline-none",
193
+ "placeholder:text-muted-foreground",
194
+ "disabled:cursor-not-allowed disabled:opacity-0"
195
+ )}
196
+ />
197
+ </div>
198
+ );
199
+ }