@behavioral-agentic-design-system/core 0.22.0 → 0.23.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,238 @@
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
+ /**
9
+ * Semantic grouping for related form controls. Renders a native
10
+ * <fieldset> + <legend>, so the group gets a real accessible name (announced
11
+ * when focus enters any nested control) and, critically, native
12
+ * <fieldset disabled> cascade-disables EVERY control inside it, something a
13
+ * <div> group (FieldGroup) cannot do.
14
+ *
15
+ * Use Fieldset when several controls answer one question: a RadioGroup of
16
+ * plan tiers, a set of notification checkboxes, an address block. Use Field
17
+ * for a single labelled input, and FieldGroup for pure column/row layout of
18
+ * unrelated fields.
19
+ *
20
+ * <Fieldset invalid={!!error}>
21
+ * <FieldsetLegend>Notification preferences</FieldsetLegend>
22
+ * <FieldsetHelper>Choose how we reach you.</FieldsetHelper>
23
+ * ...controls...
24
+ * <FieldsetError>{error}</FieldsetError>
25
+ * </Fieldset>
26
+ *
27
+ * Place <FieldsetLegend> FIRST, because the browser requires <legend> to be the
28
+ * fieldset's first child to render and be announced correctly.
29
+ */
30
+
31
+ type FieldsetContextValue = {
32
+ helperId: string;
33
+ errorId: string;
34
+ invalid: boolean;
35
+ disabled: boolean;
36
+ registerHelper: (present: boolean) => void;
37
+ registerError: (present: boolean) => void;
38
+ };
39
+
40
+ const FieldsetContext = React.createContext<FieldsetContextValue | null>(null);
41
+
42
+ function useFieldsetContext() {
43
+ const ctx = React.useContext(FieldsetContext);
44
+ if (!ctx) {
45
+ throw new Error(
46
+ "Fieldset subcomponents must be rendered inside a <Fieldset> parent.",
47
+ );
48
+ }
49
+ return ctx;
50
+ }
51
+
52
+ const fieldsetVariants = cva(
53
+ // Column flow so legend → helper → controls → error stack with rhythm.
54
+ // transition-[color,box-shadow] animates the invalid border shift; the
55
+ // duration/ease come from tokens via the inline style below (Tailwind v4 has
56
+ // no --duration-* namespace, so the class form would silently no-op).
57
+ "flex min-w-0 flex-col transition-[color,box-shadow]",
58
+ {
59
+ variants: {
60
+ variant: {
61
+ default: "rounded-lg border border-border bg-transparent",
62
+ unstyled: "border-0 p-0",
63
+ },
64
+ size: {
65
+ sm: "gap-2",
66
+ default: "gap-3",
67
+ lg: "gap-4",
68
+ },
69
+ },
70
+ compoundVariants: [
71
+ { variant: "default", size: "sm", className: "p-4" },
72
+ { variant: "default", size: "default", className: "p-5" },
73
+ { variant: "default", size: "lg", className: "p-6" },
74
+ ],
75
+ defaultVariants: { variant: "default", size: "default" },
76
+ },
77
+ );
78
+
79
+ export interface FieldsetProps
80
+ extends Omit<React.ComponentProps<"fieldset">, "disabled">,
81
+ VariantProps<typeof fieldsetVariants> {
82
+ /**
83
+ * Native cascade-disable: disables the fieldset AND every form control
84
+ * nested inside it, no per-control wiring needed.
85
+ */
86
+ disabled?: boolean;
87
+ /** Paints the destructive border/legend and switches aria-describedby to the error. */
88
+ invalid?: boolean;
89
+ }
90
+
91
+ function Fieldset({
92
+ className,
93
+ variant,
94
+ size,
95
+ disabled = false,
96
+ invalid = false,
97
+ style,
98
+ children,
99
+ ...props
100
+ }: FieldsetProps) {
101
+ const reactId = React.useId();
102
+ const helperId = `fieldset-${reactId}-helper`;
103
+ const errorId = `fieldset-${reactId}-error`;
104
+
105
+ const [hasHelper, setHasHelper] = React.useState(false);
106
+ const [hasError, setHasError] = React.useState(false);
107
+
108
+ const ctx = React.useMemo<FieldsetContextValue>(
109
+ () => ({
110
+ helperId,
111
+ errorId,
112
+ invalid,
113
+ disabled,
114
+ registerHelper: setHasHelper,
115
+ registerError: setHasError,
116
+ }),
117
+ [helperId, errorId, invalid, disabled],
118
+ );
119
+
120
+ // Only reference ids that are actually in the DOM, so aria-describedby never
121
+ // dangles (axe aria-valid-attr-value). When invalid, the error replaces the
122
+ // helper both visually and for screen readers.
123
+ const describedBy = invalid
124
+ ? hasError
125
+ ? errorId
126
+ : undefined
127
+ : hasHelper
128
+ ? helperId
129
+ : undefined;
130
+
131
+ return (
132
+ <FieldsetContext.Provider value={ctx}>
133
+ <fieldset
134
+ data-slot="fieldset"
135
+ data-invalid={invalid || undefined}
136
+ data-disabled={disabled || undefined}
137
+ disabled={disabled}
138
+ aria-describedby={describedBy}
139
+ className={cn(
140
+ "group",
141
+ fieldsetVariants({ variant, size }),
142
+ invalid && variant === "default" && "border-destructive",
143
+ className,
144
+ )}
145
+ style={{
146
+ transitionDuration: "var(--duration-base)",
147
+ transitionTimingFunction: "var(--ease-out)",
148
+ ...style,
149
+ }}
150
+ {...props}
151
+ >
152
+ {children}
153
+ </fieldset>
154
+ </FieldsetContext.Provider>
155
+ );
156
+ }
157
+
158
+ /** The group's accessible name. Render as the FIRST child of Fieldset. */
159
+ function FieldsetLegend({
160
+ className,
161
+ ...props
162
+ }: React.ComponentProps<"legend">) {
163
+ const { invalid } = useFieldsetContext();
164
+ return (
165
+ <legend
166
+ data-slot="fieldset-legend"
167
+ className={cn(
168
+ // float-none + w-full so it plays nicely as the first flex child
169
+ "float-none w-full text-sm font-semibold text-foreground",
170
+ "group-data-[disabled]:text-muted-foreground",
171
+ invalid && "text-destructive",
172
+ className,
173
+ )}
174
+ {...props}
175
+ />
176
+ );
177
+ }
178
+
179
+ /** Group-level description, wired to the fieldset via aria-describedby. */
180
+ function FieldsetHelper({ className, ...props }: React.ComponentProps<"p">) {
181
+ const { helperId, invalid, registerHelper } = useFieldsetContext();
182
+
183
+ React.useEffect(() => {
184
+ registerHelper(true);
185
+ return () => registerHelper(false);
186
+ }, [registerHelper]);
187
+
188
+ return (
189
+ <p
190
+ data-slot="fieldset-helper"
191
+ id={helperId}
192
+ // Hidden when invalid so the error (below) is the single announced
193
+ // description, matching Field's helper/error handoff.
194
+ className={cn(
195
+ "-mt-1 text-sm text-muted-foreground",
196
+ invalid && "hidden",
197
+ className,
198
+ )}
199
+ {...props}
200
+ />
201
+ );
202
+ }
203
+
204
+ /** Group-level error. Renders only when the Fieldset is invalid and has content. */
205
+ function FieldsetError({
206
+ className,
207
+ children,
208
+ ...props
209
+ }: React.ComponentProps<"p">) {
210
+ const { errorId, invalid, registerError } = useFieldsetContext();
211
+ const shown = invalid && !!children;
212
+
213
+ React.useEffect(() => {
214
+ registerError(shown);
215
+ return () => registerError(false);
216
+ }, [registerError, shown]);
217
+
218
+ if (!shown) return null;
219
+ return (
220
+ <p
221
+ data-slot="fieldset-error"
222
+ id={errorId}
223
+ role="alert"
224
+ className={cn("text-sm font-medium text-destructive", className)}
225
+ {...props}
226
+ >
227
+ {children}
228
+ </p>
229
+ );
230
+ }
231
+
232
+ export {
233
+ Fieldset,
234
+ FieldsetLegend,
235
+ FieldsetHelper,
236
+ FieldsetError,
237
+ fieldsetVariants,
238
+ };
@@ -21,9 +21,11 @@ export * from "./command";
21
21
  export * from "./context-menu";
22
22
  export * from "./dialog";
23
23
  export * from "./dropdown-menu";
24
+ export * from "./fieldset";
24
25
  export * from "./file-upload";
25
26
  export * from "./hover-card";
26
27
  export * from "./input";
28
+ export * from "./input-group";
27
29
  export * from "./kbd";
28
30
  export * from "./label";
29
31
  export * from "./menubar";
@@ -0,0 +1,194 @@
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
+ import { Button } from "./button";
8
+
9
+ // ─── Variants ─────────────────────────────────────────────────────────────────
10
+ //
11
+ // The group is the ring owner. The hosted <input> is borderless + outline-none
12
+ // so there is exactly ONE focus indicator (the container's focus-within ring),
13
+ // which keeps it high-contrast-mode safe. Same container-owns-ring contract as
14
+ // PasswordInput, generalized to arbitrary leading/trailing addons.
15
+
16
+ const inputGroupVariants = cva(
17
+ [
18
+ "flex items-center overflow-hidden",
19
+ "rounded-md border border-input bg-transparent shadow-xs",
20
+ "transition-[color,box-shadow]",
21
+ "focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
22
+ "has-[:disabled]:pointer-events-none has-[:disabled]:cursor-not-allowed has-[:disabled]:opacity-50",
23
+ "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40",
24
+ ],
25
+ {
26
+ variants: {
27
+ size: {
28
+ sm: "h-8 text-sm",
29
+ default: "h-9 text-base md:text-sm",
30
+ lg: "h-10 text-base",
31
+ xl: "h-11 text-base",
32
+ "2xl": "h-12 text-lg",
33
+ },
34
+ },
35
+ defaultVariants: { size: "default" },
36
+ }
37
+ );
38
+
39
+ type InputGroupSize = NonNullable<VariantProps<typeof inputGroupVariants>["size"]>;
40
+
41
+ const InputGroupSizeContext = React.createContext<InputGroupSize>("default");
42
+
43
+ // Per-size padding for the hosted input and the flanking addons, so the group
44
+ // reads with the same internal rhythm as a standalone Input at each size.
45
+ const inputPaddingBySize: Record<InputGroupSize, string> = {
46
+ sm: "px-2.5 py-1",
47
+ default: "px-3 py-1",
48
+ lg: "px-4 py-2",
49
+ xl: "px-4 py-2",
50
+ "2xl": "px-5 py-2.5",
51
+ };
52
+
53
+ const addonPaddingBySize: Record<InputGroupSize, string> = {
54
+ sm: "px-2.5",
55
+ default: "px-3",
56
+ lg: "px-3.5",
57
+ xl: "px-3.5",
58
+ "2xl": "px-4",
59
+ };
60
+
61
+ // ─── Root ─────────────────────────────────────────────────────────────────────
62
+
63
+ export interface InputGroupProps
64
+ extends React.ComponentProps<"div">,
65
+ VariantProps<typeof inputGroupVariants> {}
66
+
67
+ function InputGroup({
68
+ className,
69
+ size,
70
+ "aria-invalid": ariaInvalid,
71
+ ...props
72
+ }: InputGroupProps) {
73
+ const resolvedSize: InputGroupSize = size ?? "default";
74
+ return (
75
+ <InputGroupSizeContext.Provider value={resolvedSize}>
76
+ <div
77
+ role="group"
78
+ data-slot="input-group"
79
+ aria-invalid={ariaInvalid}
80
+ className={cn(inputGroupVariants({ size, className }))}
81
+ {...props}
82
+ />
83
+ </InputGroupSizeContext.Provider>
84
+ );
85
+ }
86
+
87
+ // ─── Input ────────────────────────────────────────────────────────────────────
88
+
89
+ function InputGroupInput({
90
+ className,
91
+ type,
92
+ ...props
93
+ }: React.ComponentProps<"input">) {
94
+ const size = React.useContext(InputGroupSizeContext);
95
+ return (
96
+ <input
97
+ type={type}
98
+ data-slot="input-group-input"
99
+ className={cn(
100
+ "min-w-0 flex-1 bg-transparent outline-none",
101
+ "placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground",
102
+ "disabled:cursor-not-allowed dark:bg-input/30",
103
+ inputPaddingBySize[size],
104
+ className
105
+ )}
106
+ {...props}
107
+ />
108
+ );
109
+ }
110
+
111
+ // ─── Addon ────────────────────────────────────────────────────────────────────
112
+ //
113
+ // A non-interactive slot for icons or text pinned to the start/end of the group.
114
+ // It sits as a SIBLING of the input (never wrapping it), so no interactive child
115
+ // it may hold ever nests inside another interactive element (axe nested-interactive).
116
+
117
+ export interface InputGroupAddonProps extends React.ComponentProps<"div"> {
118
+ /** Which edge of the group the addon sits on. */
119
+ align?: "inline-start" | "inline-end";
120
+ }
121
+
122
+ function InputGroupAddon({
123
+ className,
124
+ align = "inline-start",
125
+ ...props
126
+ }: InputGroupAddonProps) {
127
+ const size = React.useContext(InputGroupSizeContext);
128
+ return (
129
+ <div
130
+ data-slot="input-group-addon"
131
+ data-align={align}
132
+ className={cn(
133
+ "flex shrink-0 items-center gap-1.5 text-muted-foreground",
134
+ "[&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:stroke-[1.5]",
135
+ addonPaddingBySize[size],
136
+ align === "inline-start" ? "pr-0" : "pl-0",
137
+ className
138
+ )}
139
+ {...props}
140
+ />
141
+ );
142
+ }
143
+
144
+ // ─── Text ─────────────────────────────────────────────────────────────────────
145
+ //
146
+ // A prefix/suffix label (currency symbol, unit, protocol). Presentation only —
147
+ // aria-hidden so screen readers rely on the field's own label, not the ornament.
148
+
149
+ function InputGroupText({ className, ...props }: React.ComponentProps<"span">) {
150
+ return (
151
+ <span
152
+ data-slot="input-group-text"
153
+ aria-hidden
154
+ className={cn("select-none whitespace-nowrap text-muted-foreground", className)}
155
+ {...props}
156
+ />
157
+ );
158
+ }
159
+
160
+ // ─── Button ───────────────────────────────────────────────────────────────────
161
+ //
162
+ // An inline action (Copy, Send, Search). Wraps the Button primitive so it
163
+ // inherits the system's token-driven hover/focus motion. Renders as a sibling
164
+ // of the input — never a parent of it.
165
+
166
+ export type InputGroupButtonProps = React.ComponentProps<typeof Button>;
167
+
168
+ function InputGroupButton({
169
+ className,
170
+ variant = "ghost",
171
+ size = "sm",
172
+ type = "button",
173
+ ...props
174
+ }: InputGroupButtonProps) {
175
+ return (
176
+ <Button
177
+ data-slot="input-group-button"
178
+ type={type}
179
+ variant={variant}
180
+ size={size}
181
+ className={cn("h-full shrink-0 rounded-none", className)}
182
+ {...props}
183
+ />
184
+ );
185
+ }
186
+
187
+ export {
188
+ InputGroup,
189
+ InputGroupInput,
190
+ InputGroupAddon,
191
+ InputGroupText,
192
+ InputGroupButton,
193
+ inputGroupVariants,
194
+ };
@@ -102,6 +102,7 @@ export const DOCS_INDEX: DocGroup[] = [
102
102
  entries: [
103
103
  { title: "Design rules", path: "DESIGN.md", summary: "Visual system + accessibility baseline." },
104
104
  { title: "Chart color palette", path: "docs/chart-color-palette.md", summary: "Accessible 5-color categorical palette for data viz — Okabe-Ito-derived, reviewed against WCAG 1.4.11 non-text contrast." },
105
+ { title: "Privacy-first design (8 principles)", path: "layer-2-ethics-responsibility/privacy-first-design.md", summary: "Matthew Stephens' 8-principle privacy framework: minimize collection, transparency, expiration, no dark patterns, user control, ethical monetization, sustainable practices, accountability — mapped to BADS enforcement." },
105
106
  { title: "Gamification — responsible vs. manipulative", path: "layer-2-ethics-responsibility/gamification.md", summary: "Sir's 10 Core Drives (Octalysis 8 + Exploration & Discovery + Challenge & Competition) + the test gates that decide when Black Hat ships and when it refuses." },
106
107
  { title: "Corrections log", path: "corrections.md", summary: "Per-fork anti-pattern log. Read before every Path B build." },
107
108
  { title: "Anti-defaults refresh", path: "prompts/anti-defaults-refresh.md", summary: "Quarterly pressure-test of the current anti-defaults." },
@@ -125,6 +126,18 @@ export const DOCS_INDEX: DocGroup[] = [
125
126
  { title: "5 · Component needs", path: "prompts/5-component-needs.md" },
126
127
  { title: "6 · Patterns", path: "prompts/6-patterns.md" },
127
128
  { title: "Chromatic setup", path: "docs/chromatic-setup.md" },
129
+ {
130
+ title: "Handoff runbook",
131
+ path: "docs/handoff-runbook.md",
132
+ summary:
133
+ "Transferring a finished fork to the client's own team — quality pass, repo transfer, secret rotation, knowledge transfer, wind-down.",
134
+ },
135
+ {
136
+ title: "Fork sync migration",
137
+ path: "docs/fork-sync-migration.md",
138
+ summary:
139
+ "workspace:* to npm-consume — the migration every client fork runs so daily component releases actually arrive.",
140
+ },
128
141
  { title: "Chromatic prompt", path: "prompts/chromatic.md" },
129
142
  ],
130
143
  },
@@ -1,4 +1,5 @@
1
1
  import type { Meta, StoryObj } from "@storybook/react";
2
+ import { userEvent, within, expect } from "storybook/test";
2
3
  import {
3
4
  Accordion,
4
5
  AccordionItem,
@@ -110,6 +111,26 @@ export const Disabled: Story = {
110
111
  ),
111
112
  };
112
113
 
114
+ export const Interaction_Opens: Story = {
115
+ tags: ["test"],
116
+ render: () => (
117
+ <Accordion type="single" collapsible className="w-[400px]">
118
+ <AccordionItem value="item-1">
119
+ <AccordionTrigger>What is BADS?</AccordionTrigger>
120
+ <AccordionContent>A portable, agentic design system.</AccordionContent>
121
+ </AccordionItem>
122
+ </Accordion>
123
+ ),
124
+ play: async ({ canvasElement }) => {
125
+ const canvas = within(canvasElement);
126
+ const trigger = canvas.getByRole("button", { name: /what is bads/i });
127
+ expect(trigger).toHaveAttribute("data-state", "closed");
128
+ await userEvent.click(trigger);
129
+ expect(trigger).toHaveAttribute("data-state", "open");
130
+ expect(canvas.getByText("A portable, agentic design system.")).toBeVisible();
131
+ },
132
+ };
133
+
113
134
  export const RealisticUse: Story = {
114
135
  render: () => (
115
136
  <div className="w-[480px] rounded-lg border bg-card p-6">
@@ -1,4 +1,5 @@
1
1
  import { Meta, StoryObj } from "@storybook/react"
2
+ import { userEvent, within, expect, fn } from "storybook/test"
2
3
  import { Button } from "@behavioral-agentic-design-system/core/components/ui/button"
3
4
  import { Heart } from "lucide-react"
4
5
 
@@ -125,6 +126,20 @@ export const RealisticUse: Story = {
125
126
  // base class. Default and the icon sizes used to fall through to rounded-full
126
127
  // (pill / circle) while the rest were rounded-md; this row exists so that
127
128
  // inconsistency can't silently come back.
129
+ export const Interaction_Click: Story = {
130
+ tags: ["test"],
131
+ args: {
132
+ children: "Save Changes",
133
+ onClick: fn(),
134
+ },
135
+ play: async ({ canvasElement, args }) => {
136
+ const canvas = within(canvasElement);
137
+ const button = canvas.getByRole("button", { name: /save changes/i });
138
+ await userEvent.click(button);
139
+ expect(args.onClick).toHaveBeenCalledOnce();
140
+ },
141
+ };
142
+
128
143
  export const AllSizes: Story = {
129
144
  render: () => (
130
145
  <div className="flex flex-wrap items-center gap-3">
@@ -20,7 +20,7 @@ type Story = StoryObj<typeof CopyButton>
20
20
 
21
21
  export const Default: Story = {
22
22
  render: () => (
23
- <CopyButton value="hello@example.com">Copy email</CopyButton>
23
+ <CopyButton value="jordan@reeves.design">Copy email</CopyButton>
24
24
  ),
25
25
  }
26
26
 
@@ -42,7 +42,7 @@ export const Variant_Ghost: Story = {
42
42
 
43
43
  export const Variant_Secondary: Story = {
44
44
  render: () => (
45
- <CopyButton value="https://example.com/share/abc123" variant="secondary">
45
+ <CopyButton value="https://design.bads.com/share/tokens-v2-proposal" variant="secondary">
46
46
  Copy link
47
47
  </CopyButton>
48
48
  ),
@@ -5,6 +5,7 @@
5
5
  // canonical demonstration. Mechanism is identical here; no separate\
6
6
  // story added because the demo would duplicate Sheet's.
7
7
  import { Meta, StoryObj } from "@storybook/react";
8
+ import { userEvent, within, expect, waitFor } from "storybook/test";
8
9
  import {
9
10
  Dialog,
10
11
  DialogTrigger,
@@ -161,6 +162,38 @@ export const Size_Full: Story = {
161
162
  ),
162
163
  };
163
164
 
165
+ export const Interaction_Opens: Story = {
166
+ tags: ["test"],
167
+ render: () => (
168
+ <Dialog>
169
+ <DialogTrigger asChild>
170
+ <Button>Open Dialog</Button>
171
+ </DialogTrigger>
172
+ <DialogContent size="sm">
173
+ <DialogHeader>
174
+ <DialogTitle>Confirm Action</DialogTitle>
175
+ <DialogDescription>This action cannot be undone.</DialogDescription>
176
+ </DialogHeader>
177
+ <DialogFooter>
178
+ <DialogClose asChild>
179
+ <Button variant="outline">Cancel</Button>
180
+ </DialogClose>
181
+ </DialogFooter>
182
+ </DialogContent>
183
+ </Dialog>
184
+ ),
185
+ play: async ({ canvasElement }) => {
186
+ const canvas = within(canvasElement);
187
+ await userEvent.click(canvas.getByRole("button", { name: /open dialog/i }));
188
+ // Dialog portals to document.body — query from there. The content's
189
+ // fade-in-0 entrance starts at opacity 0, so toBeVisible() must wait
190
+ // for the animation to finish rather than assert on mount.
191
+ const dialog = await within(document.body).findByRole("dialog");
192
+ await waitFor(() => expect(dialog).toBeVisible());
193
+ expect(within(dialog).getByText("Confirm Action")).toBeVisible();
194
+ },
195
+ };
196
+
164
197
  export const RealisticUse: Story = {
165
198
  render: () => (
166
199
  <Dialog>
@@ -29,6 +29,9 @@ export const Default: Story = {
29
29
  ),
30
30
  }
31
31
 
32
+ // The overlay input covers only the preview area, so the Save/Cancel controls
33
+ // render below it (visible) once editing begins — not hidden behind the input.
34
+ // Save/Cancel are the BADS Button primitive (default + ghost), spaced with gap-2.
32
35
  export const WithControls: Story = {
33
36
  render: () => (
34
37
  <div className="w-64">
@@ -67,6 +70,8 @@ export const SubmitMode_Enter: Story = {
67
70
  ),
68
71
  }
69
72
 
73
+ // Disabled renders as a single dimmed (opacity-50) preview label — the overlay
74
+ // input stays hidden, so the value is not ghosted over itself.
70
75
  export const Disabled: Story = {
71
76
  render: () => (
72
77
  <div className="w-64">
@@ -87,6 +92,26 @@ export const WithPlaceholder: Story = {
87
92
  ),
88
93
  }
89
94
 
95
+ // Keyboard contract: Enter/Space on the preview opens edit mode (focus moves to
96
+ // the input), Escape cancels and Enter (submitMode enter/both) saves — in both
97
+ // cases focus RETURNS to the preview instead of stranding on the aria-hidden
98
+ // input or an unmounted Save/Cancel button. Blur-submit (clicking elsewhere)
99
+ // deliberately does NOT steal focus back. `label` names the edit input for
100
+ // screen readers; without it the input falls back to the placeholder text.
101
+ export const KeyboardFocusReturn: Story = {
102
+ render: () => (
103
+ <div className="w-64">
104
+ <Editable defaultValue="Tab here, press Enter, then Escape" label="Demo field" submitMode="both">
105
+ <EditablePreview />
106
+ <EditableControl>
107
+ <EditableSubmit />
108
+ <EditableCancel />
109
+ </EditableControl>
110
+ </Editable>
111
+ </div>
112
+ ),
113
+ }
114
+
90
115
  export const Controlled: Story = {
91
116
  render: () => {
92
117
  const [value, setValue] = React.useState("Maya Chen")
@@ -105,29 +130,31 @@ export const Controlled: Story = {
105
130
 
106
131
  export const RealisticUse: Story = {
107
132
  render: () => {
108
- const [name, setName] = React.useState("Q3 Swim Meet")
109
- const [coach, setCoach] = React.useState("Coach Rivera")
133
+ const [name, setName] = React.useState("Brand refresh sprint")
134
+ const [lead, setLead] = React.useState("Maya Chen")
110
135
 
111
136
  return (
112
137
  <div className="w-72 rounded-lg border border-border bg-card p-4 shadow-sm">
113
138
  <p className="mb-3 text-xs font-medium uppercase tracking-wide text-muted-foreground">
114
- Event Details
139
+ Project Details
115
140
  </p>
116
141
  <div className="space-y-1">
117
- <label className="text-xs text-muted-foreground">Event name</label>
142
+ <label className="text-xs text-muted-foreground">Project name</label>
118
143
  <Editable
119
144
  value={name}
120
145
  onValueChange={setName}
146
+ label="Project name"
121
147
  submitMode="blur"
122
148
  >
123
149
  <EditablePreview className="text-base font-medium" />
124
150
  </Editable>
125
151
  </div>
126
152
  <div className="mt-3 space-y-1">
127
- <label className="text-xs text-muted-foreground">Head coach</label>
153
+ <label className="text-xs text-muted-foreground">Design lead</label>
128
154
  <Editable
129
- value={coach}
130
- onValueChange={setCoach}
155
+ value={lead}
156
+ onValueChange={setLead}
157
+ label="Design lead"
131
158
  submitMode="both"
132
159
  >
133
160
  <EditablePreview />