@behavioral-agentic-design-system/core 0.21.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@behavioral-agentic-design-system/core",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "BADS core — tokens, shadcn/ui components, and the BADS template shell (app routes, customize tab, theme hooks). Forks consume this and re-export, keeping client repos to ~30 lines of glue.",
5
5
  "license": "MIT",
6
6
  "author": "Matthew Stephens <work@matthewstephens.com> (https://github.com/matthewlarn)",
@@ -264,6 +264,13 @@ import {
264
264
  } from "@behavioral-agentic-design-system/core/components/ui/tree-view";
265
265
  import { ColorPicker } from "@behavioral-agentic-design-system/core/components/ui/color-picker";
266
266
  import { NativeSelect } from "@behavioral-agentic-design-system/core/components/ui/native-select";
267
+ import {
268
+ Editable,
269
+ EditableCancel,
270
+ EditableControl,
271
+ EditablePreview,
272
+ EditableSubmit,
273
+ } from "@behavioral-agentic-design-system/core/components/ui/editable";
267
274
 
268
275
  // Stable, deterministic data for ScrollArea — never use Math.random in
269
276
  // SSR-rendered output, it causes hydration mismatches.
@@ -2983,6 +2990,39 @@ export default function ComponentsPreview() {
2983
2990
  </div>
2984
2991
  </div>
2985
2992
  </Section>
2993
+
2994
+ <Section title="Editable">
2995
+ <div className="grid gap-6 sm:grid-cols-2">
2996
+ <div className="space-y-2">
2997
+ <p className="text-sm font-medium text-foreground">Default (blur to save)</p>
2998
+ <Editable defaultValue="Project Alpha">
2999
+ <EditablePreview />
3000
+ </Editable>
3001
+ </div>
3002
+ <div className="space-y-2">
3003
+ <p className="text-sm font-medium text-foreground">With save/cancel controls</p>
3004
+ <Editable defaultValue="Untitled sprint" submitMode="enter">
3005
+ <EditablePreview />
3006
+ <EditableControl>
3007
+ <EditableSubmit />
3008
+ <EditableCancel />
3009
+ </EditableControl>
3010
+ </Editable>
3011
+ </div>
3012
+ <div className="space-y-2">
3013
+ <p className="text-sm font-medium text-foreground">Empty with placeholder</p>
3014
+ <Editable defaultValue="" placeholder="Add a title…">
3015
+ <EditablePreview />
3016
+ </Editable>
3017
+ </div>
3018
+ <div className="space-y-2">
3019
+ <p className="text-sm font-medium text-foreground">Disabled</p>
3020
+ <Editable defaultValue="Read-only field" disabled>
3021
+ <EditablePreview />
3022
+ </Editable>
3023
+ </div>
3024
+ </div>
3025
+ </Section>
2986
3026
  </div>
2987
3027
  </div>
2988
3028
  );
@@ -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";
@@ -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
+ }