@behavioral-agentic-design-system/core 0.19.0 → 0.22.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,294 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+
5
+ import { cn } from "../../lib/utils"
6
+
7
+ type SubmitMode = "blur" | "enter" | "both"
8
+
9
+ interface EditableContextValue {
10
+ editing: boolean
11
+ value: string
12
+ inputRef: React.RefObject<HTMLInputElement | null>
13
+ startEditing: () => void
14
+ cancelEditing: () => void
15
+ submitEditing: () => void
16
+ disabled: boolean
17
+ submitMode: SubmitMode
18
+ placeholder: string
19
+ }
20
+
21
+ const EditableContext = React.createContext<EditableContextValue | null>(null)
22
+
23
+ function useEditableContext() {
24
+ const ctx = React.useContext(EditableContext)
25
+ if (!ctx) throw new Error("Editable sub-components must be used inside <Editable>")
26
+ return ctx
27
+ }
28
+
29
+ interface EditableProps {
30
+ value?: string
31
+ defaultValue?: string
32
+ onValueChange?: (value: string) => void
33
+ onEditStart?: () => void
34
+ onEditEnd?: (value: string) => void
35
+ disabled?: boolean
36
+ placeholder?: string
37
+ submitMode?: SubmitMode
38
+ children: React.ReactNode
39
+ className?: string
40
+ }
41
+
42
+ function Editable({
43
+ value: valueProp,
44
+ defaultValue = "",
45
+ onValueChange,
46
+ onEditStart,
47
+ onEditEnd,
48
+ disabled = false,
49
+ placeholder = "Click to edit",
50
+ submitMode = "blur",
51
+ children,
52
+ className,
53
+ }: EditableProps) {
54
+ const [internalValue, setInternalValue] = React.useState(defaultValue)
55
+ const [editing, setEditing] = React.useState(false)
56
+ const [draft, setDraft] = React.useState(defaultValue)
57
+ const inputRef = React.useRef<HTMLInputElement>(null)
58
+
59
+ const isControlled = valueProp !== undefined
60
+ const value = isControlled ? valueProp : internalValue
61
+
62
+ const startEditing = React.useCallback(() => {
63
+ if (disabled) return
64
+ setDraft(value)
65
+ setEditing(true)
66
+ onEditStart?.()
67
+ // Focus in a microtask so the input has rendered
68
+ queueMicrotask(() => inputRef.current?.focus())
69
+ }, [disabled, value, onEditStart])
70
+
71
+ const submitEditing = React.useCallback(() => {
72
+ if (!editing) return
73
+ const trimmed = draft.trim()
74
+ const committed = trimmed || value
75
+ if (!isControlled) setInternalValue(committed)
76
+ onValueChange?.(committed)
77
+ onEditEnd?.(committed)
78
+ setEditing(false)
79
+ }, [editing, draft, value, isControlled, onValueChange, onEditEnd])
80
+
81
+ const cancelEditing = React.useCallback(() => {
82
+ setDraft(value)
83
+ setEditing(false)
84
+ }, [value])
85
+
86
+ return (
87
+ <EditableContext.Provider
88
+ value={{
89
+ editing,
90
+ value,
91
+ inputRef,
92
+ startEditing,
93
+ cancelEditing,
94
+ submitEditing,
95
+ disabled,
96
+ submitMode,
97
+ placeholder,
98
+ }}
99
+ >
100
+ <div
101
+ data-slot="editable"
102
+ data-editing={editing || undefined}
103
+ data-disabled={disabled || undefined}
104
+ className={cn("relative w-full", className)}
105
+ >
106
+ {children}
107
+ {/* Hidden input stores draft and handles keyboard events */}
108
+ <input
109
+ ref={inputRef}
110
+ type="text"
111
+ aria-hidden={!editing}
112
+ tabIndex={editing ? 0 : -1}
113
+ value={draft}
114
+ onChange={(e) => setDraft(e.target.value)}
115
+ onBlur={() => {
116
+ if (submitMode === "blur" || submitMode === "both") {
117
+ submitEditing()
118
+ }
119
+ }}
120
+ onKeyDown={(e) => {
121
+ if (e.key === "Escape") {
122
+ e.preventDefault()
123
+ cancelEditing()
124
+ }
125
+ if (
126
+ e.key === "Enter" &&
127
+ (submitMode === "enter" || submitMode === "both")
128
+ ) {
129
+ e.preventDefault()
130
+ submitEditing()
131
+ }
132
+ }}
133
+ className={cn(
134
+ "absolute inset-0 w-full rounded-md border border-input bg-background px-3 text-sm text-foreground outline-none",
135
+ "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
136
+ "disabled:cursor-not-allowed disabled:opacity-50",
137
+ !editing && "pointer-events-none opacity-0"
138
+ )}
139
+ style={{
140
+ transitionProperty: "opacity",
141
+ transitionDuration: "var(--duration-fast)",
142
+ transitionTimingFunction: "var(--ease-out)",
143
+ }}
144
+ disabled={disabled}
145
+ />
146
+ </div>
147
+ </EditableContext.Provider>
148
+ )
149
+ }
150
+
151
+ interface EditablePreviewProps extends React.HTMLAttributes<HTMLDivElement> {
152
+ className?: string
153
+ }
154
+
155
+ function EditablePreview({ className, ...props }: EditablePreviewProps) {
156
+ const { editing, value, placeholder, startEditing, disabled } =
157
+ useEditableContext()
158
+
159
+ return (
160
+ <div
161
+ data-slot="editable-preview"
162
+ role="button"
163
+ tabIndex={disabled ? -1 : 0}
164
+ aria-label={`Edit: ${value || placeholder}`}
165
+ onClick={() => !disabled && startEditing()}
166
+ onKeyDown={(e) => {
167
+ if ((e.key === "Enter" || e.key === " ") && !disabled) {
168
+ e.preventDefault()
169
+ startEditing()
170
+ }
171
+ }}
172
+ className={cn(
173
+ "min-h-11 w-full cursor-text truncate rounded-md px-3 py-2 text-sm",
174
+ "hover:bg-muted/50 focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50",
175
+ !value && "text-muted-foreground",
176
+ disabled && "cursor-not-allowed opacity-50",
177
+ editing && "pointer-events-none opacity-0",
178
+ className
179
+ )}
180
+ style={{
181
+ transitionProperty: "opacity, background-color",
182
+ transitionDuration: "var(--duration-fast)",
183
+ transitionTimingFunction: "var(--ease-out)",
184
+ }}
185
+ {...props}
186
+ >
187
+ {value || placeholder}
188
+ </div>
189
+ )
190
+ }
191
+
192
+ interface EditableInputProps
193
+ extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange"> {
194
+ className?: string
195
+ }
196
+
197
+ // EditableInput is a visible companion — the real input is controlled by Editable root.
198
+ // This renders as a styled shell that positions on top of the preview when editing.
199
+ // The actual <input> is rendered inside <Editable> and absolutely positioned.
200
+ // This component is kept for API symmetry and potential slot customization.
201
+ function EditableInput({ className, ...props }: EditableInputProps) {
202
+ return (
203
+ <div
204
+ data-slot="editable-input-slot"
205
+ aria-hidden
206
+ className={cn("sr-only", className)}
207
+ {...(props as React.HTMLAttributes<HTMLDivElement>)}
208
+ />
209
+ )
210
+ }
211
+
212
+ interface EditableControlProps extends React.HTMLAttributes<HTMLDivElement> {
213
+ className?: string
214
+ }
215
+
216
+ function EditableControl({ className, ...props }: EditableControlProps) {
217
+ const { editing } = useEditableContext()
218
+ if (!editing) return null
219
+ return (
220
+ <div
221
+ data-slot="editable-control"
222
+ className={cn("flex items-center gap-1 pt-1", className)}
223
+ {...props}
224
+ />
225
+ )
226
+ }
227
+
228
+ interface EditableSubmitProps
229
+ extends React.ButtonHTMLAttributes<HTMLButtonElement> {
230
+ className?: string
231
+ }
232
+
233
+ function EditableSubmit({ className, children, ...props }: EditableSubmitProps) {
234
+ const { submitEditing } = useEditableContext()
235
+ return (
236
+ // allow-text-xs-cta — inline editable chip; full min-h-11 CTA is a separate affordance
237
+ <button
238
+ type="button"
239
+ data-slot="editable-submit"
240
+ onClick={submitEditing}
241
+ className={cn(
242
+ "inline-flex min-h-7 items-center rounded-md bg-primary px-2 text-sm font-medium text-primary-foreground",
243
+ "hover:bg-primary/90 focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50",
244
+ className
245
+ )}
246
+ {...props}
247
+ >
248
+ {children ?? "Save"}
249
+ </button>
250
+ )
251
+ }
252
+
253
+ interface EditableCancelProps
254
+ extends React.ButtonHTMLAttributes<HTMLButtonElement> {
255
+ className?: string
256
+ }
257
+
258
+ function EditableCancel({
259
+ className,
260
+ children,
261
+ ...props
262
+ }: EditableCancelProps) {
263
+ const { cancelEditing } = useEditableContext()
264
+ return (
265
+ // allow-text-xs-cta — inline editable chip; full min-h-11 CTA is a separate affordance
266
+ <button
267
+ type="button"
268
+ data-slot="editable-cancel"
269
+ onClick={cancelEditing}
270
+ className={cn(
271
+ "inline-flex min-h-7 items-center rounded-md px-2 text-sm font-medium text-muted-foreground",
272
+ "hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50",
273
+ className
274
+ )}
275
+ style={{
276
+ transitionProperty: "background-color, color",
277
+ transitionDuration: "var(--duration-fast)",
278
+ transitionTimingFunction: "var(--ease-out)",
279
+ }}
280
+ {...props}
281
+ >
282
+ {children ?? "Cancel"}
283
+ </button>
284
+ )
285
+ }
286
+
287
+ export {
288
+ Editable,
289
+ EditablePreview,
290
+ EditableInput,
291
+ EditableControl,
292
+ EditableSubmit,
293
+ EditableCancel,
294
+ }
@@ -59,3 +59,4 @@ export * from "./toggle-group";
59
59
  export * from "./toolbar";
60
60
  export * from "./tooltip";
61
61
  export * from "./tree-view";
62
+ export * from "./editable";
@@ -58,24 +58,27 @@ export const Variant_Multiple: Story = {
58
58
  render: () => (
59
59
  <Accordion type="multiple" className="w-[400px]">
60
60
  <AccordionItem value="item-1">
61
- <AccordionTrigger>Primary membership</AccordionTrigger>
61
+ <AccordionTrigger>Token sync</AccordionTrigger>
62
62
  <AccordionContent>
63
- Covers the account holder and up to two dependents. Includes all
64
- scheduled group classes and open lap swim.
63
+ Tokens live in one source of truth and publish as CSS variables. When
64
+ the values change, every component that references them updates on the
65
+ next build.
65
66
  </AccordionContent>
66
67
  </AccordionItem>
67
68
  <AccordionItem value="item-2">
68
- <AccordionTrigger>Guest passes</AccordionTrigger>
69
+ <AccordionTrigger>Component adoption</AccordionTrigger>
69
70
  <AccordionContent>
70
- Members can bring one guest per visit at no charge. Guests must
71
- sign a liability waiver at the front desk.
71
+ Install the package, import the primitive you need, and drop it into a
72
+ route. Each component ships with an accessibility contract and a set
73
+ of documented variants.
72
74
  </AccordionContent>
73
75
  </AccordionItem>
74
76
  <AccordionItem value="item-3">
75
- <AccordionTrigger>Facility hours</AccordionTrigger>
77
+ <AccordionTrigger>Versioning and releases</AccordionTrigger>
76
78
  <AccordionContent>
77
- Weekdays 5:30 am – 10:00 pm. Weekends 7:00 am – 8:00 pm. Holiday
78
- hours posted on the bulletin board.
79
+ Releases follow semantic versioning. Breaking changes land in major
80
+ bumps with a migration note, and the changelog records every shipped
81
+ fix and feature.
79
82
  </AccordionContent>
80
83
  </AccordionItem>
81
84
  </Accordion>
@@ -37,10 +37,10 @@ type Story = StoryObj<typeof ButtonGroup>
37
37
 
38
38
  export const Default: Story = {
39
39
  render: () => (
40
- <ButtonGroup aria-label="Lane view controls">
41
- <Button variant="outline">All lanes</Button>
42
- <Button variant="outline">My lanes</Button>
43
- <Button variant="outline">Open only</Button>
40
+ <ButtonGroup aria-label="Component filter">
41
+ <Button variant="outline">All</Button>
42
+ <Button variant="outline">Published</Button>
43
+ <Button variant="outline">Draft</Button>
44
44
  </ButtonGroup>
45
45
  ),
46
46
  }
@@ -63,21 +63,21 @@ export const WithIcons: Story = {
63
63
 
64
64
  export const Vertical: Story = {
65
65
  render: () => (
66
- <ButtonGroup orientation="vertical" aria-label="Stroke options">
67
- <Button variant="outline">Freestyle</Button>
68
- <Button variant="outline">Backstroke</Button>
69
- <Button variant="outline">Breaststroke</Button>
70
- <Button variant="outline">Butterfly</Button>
66
+ <ButtonGroup orientation="vertical" aria-label="Design system views">
67
+ <Button variant="outline">Figma</Button>
68
+ <Button variant="outline">Storybook</Button>
69
+ <Button variant="outline">Code</Button>
70
+ <Button variant="outline">Tokens</Button>
71
71
  </ButtonGroup>
72
72
  ),
73
73
  }
74
74
 
75
75
  export const Variant_Default: Story = {
76
76
  render: () => (
77
- <ButtonGroup aria-label="Season phase">
78
- <Button variant="default">Early season</Button>
79
- <Button variant="default">Championship</Button>
80
- <Button variant="default">Off season</Button>
77
+ <ButtonGroup aria-label="Release channel">
78
+ <Button variant="default">Stable</Button>
79
+ <Button variant="default">Beta</Button>
80
+ <Button variant="default">Alpha</Button>
81
81
  </ButtonGroup>
82
82
  ),
83
83
  }
@@ -155,15 +155,15 @@ export const RealisticUse: Story = {
155
155
 
156
156
  {/* Reaction row */}
157
157
  <div className="space-y-1.5">
158
- <p className="text-xs font-medium text-muted-foreground">Session reactions</p>
159
- <ButtonGroup aria-label="Rate this practice session">
158
+ <p className="text-xs font-medium text-muted-foreground">Feedback</p>
159
+ <ButtonGroup aria-label="Rate this component">
160
160
  <Button variant="outline">
161
161
  <ThumbsUpIcon strokeWidth={1.5} />
162
162
  Helpful
163
163
  </Button>
164
164
  <Button variant="outline">
165
165
  <ThumbsDownIcon strokeWidth={1.5} />
166
- Too easy
166
+ Not helpful
167
167
  </Button>
168
168
  <Button variant="outline">
169
169
  <ShareIcon strokeWidth={1.5} />
@@ -46,33 +46,54 @@ export const WithCustomInput: Story = {
46
46
  ),
47
47
  };
48
48
 
49
- export const Wheel: Story = {
50
- render: function WheelPicker() {
51
- const [color, setColor] = React.useState("#3b82f6");
49
+ // Selection is shown by an offset ring (radio convention), not a checkmark —
50
+ // a check reads as a multi-select checkbox, but swatches are single-select.
51
+ export const Selected: Story = {
52
+ render: () => (
53
+ <ColorPicker
54
+ swatches={BRAND_SWATCHES}
55
+ defaultValue="#8b5cf6"
56
+ aria-label="A color is selected"
57
+ />
58
+ ),
59
+ };
60
+
61
+ export const AddViaWheel: Story = {
62
+ render: function AddViaWheelPicker() {
63
+ const [color, setColor] = React.useState("#8b5cf6");
52
64
  return (
53
65
  <div className="space-y-3">
66
+ <p className="text-xs text-muted-foreground">
67
+ Click the dashed <span className="font-medium text-foreground">+</span> tile to open the
68
+ color wheel and add a custom swatch.
69
+ </p>
54
70
  <ColorPicker
55
71
  value={color}
56
72
  onValueChange={setColor}
57
73
  wheel
58
- aria-label="Pick a color from the wheel"
74
+ swatches={BRAND_SWATCHES}
75
+ aria-label="Pick a swatch or add a custom color"
59
76
  />
60
77
  </div>
61
78
  );
62
79
  },
63
80
  };
64
81
 
65
- export const WheelWithSwatches: Story = {
66
- render: function WheelSwatchesPicker() {
67
- const [color, setColor] = React.useState("#8b5cf6");
82
+ export const AddOnlyWheel: Story = {
83
+ render: function AddOnlyPicker() {
84
+ const [color, setColor] = React.useState("");
68
85
  return (
69
- <ColorPicker
70
- value={color}
71
- onValueChange={setColor}
72
- wheel
73
- swatches={BRAND_SWATCHES}
74
- aria-label="Pick a color from the wheel or a swatch"
75
- />
86
+ <div className="space-y-3">
87
+ <p className="text-xs text-muted-foreground">
88
+ No presets — build the palette from scratch with the <span className="font-medium text-foreground">+</span> tile.
89
+ </p>
90
+ <ColorPicker
91
+ value={color}
92
+ onValueChange={setColor}
93
+ wheel
94
+ aria-label="Add a custom color"
95
+ />
96
+ </div>
76
97
  );
77
98
  },
78
99
  };
@@ -176,7 +197,7 @@ export const RealisticUse: Story = {
176
197
  style={{ backgroundColor: tagTextColor(tagColor) }}
177
198
  aria-hidden
178
199
  />
179
- Breezy Hills
200
+ Tokens
180
201
  </div>
181
202
  </div>
182
203
  </CardContent>
@@ -134,9 +134,9 @@ export const RealisticUse: Story = {
134
134
  return (
135
135
  <div className="w-[320px] space-y-4 rounded-lg border border-border bg-card p-4">
136
136
  <div className="space-y-1">
137
- <p className="text-sm font-semibold">Schedule practice</p>
137
+ <p className="text-sm font-semibold">Schedule a release</p>
138
138
  <p className="text-xs text-muted-foreground">
139
- Choose start and end dates for the practice block
139
+ Choose start and end dates for the release window
140
140
  </p>
141
141
  </div>
142
142
  <div className="space-y-3">
@@ -0,0 +1,143 @@
1
+ import type { Meta, StoryObj } from "@storybook/react"
2
+ import * as React from "react"
3
+
4
+ import {
5
+ Editable,
6
+ EditableCancel,
7
+ EditableControl,
8
+ EditablePreview,
9
+ EditableSubmit,
10
+ } from "../components/ui/editable"
11
+
12
+ const meta: Meta<typeof Editable> = {
13
+ title: "Components/Editable",
14
+ component: Editable,
15
+ parameters: { layout: "centered" },
16
+ tags: ["autodocs"],
17
+ }
18
+
19
+ export default meta
20
+ type Story = StoryObj<typeof Editable>
21
+
22
+ export const Default: Story = {
23
+ render: () => (
24
+ <div className="w-64">
25
+ <Editable defaultValue="Project Alpha">
26
+ <EditablePreview />
27
+ </Editable>
28
+ </div>
29
+ ),
30
+ }
31
+
32
+ export const WithControls: Story = {
33
+ render: () => (
34
+ <div className="w-64">
35
+ <Editable defaultValue="Untitled sprint" submitMode="enter">
36
+ <EditablePreview />
37
+ <EditableControl>
38
+ <EditableSubmit />
39
+ <EditableCancel />
40
+ </EditableControl>
41
+ </Editable>
42
+ </div>
43
+ ),
44
+ }
45
+
46
+ export const SubmitMode_Blur: Story = {
47
+ render: () => (
48
+ <div className="w-64">
49
+ <Editable defaultValue="Click to edit, then blur to save" submitMode="blur">
50
+ <EditablePreview />
51
+ </Editable>
52
+ </div>
53
+ ),
54
+ }
55
+
56
+ export const SubmitMode_Enter: Story = {
57
+ render: () => (
58
+ <div className="w-64">
59
+ <Editable defaultValue="Press Enter to save, Escape to cancel" submitMode="enter">
60
+ <EditablePreview />
61
+ <EditableControl>
62
+ <EditableSubmit>Save</EditableSubmit>
63
+ <EditableCancel>Cancel</EditableCancel>
64
+ </EditableControl>
65
+ </Editable>
66
+ </div>
67
+ ),
68
+ }
69
+
70
+ export const Disabled: Story = {
71
+ render: () => (
72
+ <div className="w-64">
73
+ <Editable defaultValue="Read-only field" disabled>
74
+ <EditablePreview />
75
+ </Editable>
76
+ </div>
77
+ ),
78
+ }
79
+
80
+ export const WithPlaceholder: Story = {
81
+ render: () => (
82
+ <div className="w-64">
83
+ <Editable defaultValue="" placeholder="Add a title...">
84
+ <EditablePreview />
85
+ </Editable>
86
+ </div>
87
+ ),
88
+ }
89
+
90
+ export const Controlled: Story = {
91
+ render: () => {
92
+ const [value, setValue] = React.useState("Maya Chen")
93
+ return (
94
+ <div className="w-64 space-y-2">
95
+ <p className="text-xs text-muted-foreground">
96
+ Controlled: {JSON.stringify(value)}
97
+ </p>
98
+ <Editable value={value} onValueChange={setValue}>
99
+ <EditablePreview />
100
+ </Editable>
101
+ </div>
102
+ )
103
+ },
104
+ }
105
+
106
+ export const RealisticUse: Story = {
107
+ render: () => {
108
+ const [name, setName] = React.useState("Q3 Swim Meet")
109
+ const [coach, setCoach] = React.useState("Coach Rivera")
110
+
111
+ return (
112
+ <div className="w-72 rounded-lg border border-border bg-card p-4 shadow-sm">
113
+ <p className="mb-3 text-xs font-medium uppercase tracking-wide text-muted-foreground">
114
+ Event Details
115
+ </p>
116
+ <div className="space-y-1">
117
+ <label className="text-xs text-muted-foreground">Event name</label>
118
+ <Editable
119
+ value={name}
120
+ onValueChange={setName}
121
+ submitMode="blur"
122
+ >
123
+ <EditablePreview className="text-base font-medium" />
124
+ </Editable>
125
+ </div>
126
+ <div className="mt-3 space-y-1">
127
+ <label className="text-xs text-muted-foreground">Head coach</label>
128
+ <Editable
129
+ value={coach}
130
+ onValueChange={setCoach}
131
+ submitMode="both"
132
+ >
133
+ <EditablePreview />
134
+ <EditableControl>
135
+ <EditableSubmit />
136
+ <EditableCancel />
137
+ </EditableControl>
138
+ </Editable>
139
+ </div>
140
+ </div>
141
+ )
142
+ },
143
+ }