@behavioral-agentic-design-system/core 0.39.1 → 0.42.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/README.md +2 -0
- package/package.json +26 -5
- package/scripts/a11y-lint.cjs +88 -8
- package/scripts/bads-components.mjs +118 -0
- package/scripts/build-tokens.cjs +133 -12
- package/scripts/check-stories.mjs +1 -1
- package/scripts/health-report.mjs +16 -2
- package/scripts/lint-component-source.mjs +100 -0
- package/scripts/lint-microanimation.mjs +267 -0
- package/scripts/lint-slop.mjs +25 -1
- package/scripts/size-audit.cjs +79 -0
- package/scripts/source-of-truth.cjs +58 -0
- package/scripts/ux-smoke.mjs +438 -0
- package/scripts/wcag-audit.cjs +121 -6
- package/src/api/layers/file.ts +1 -0
- package/src/app/opengraph-image.tsx +5 -0
- package/src/app/preview/components/page.tsx +2237 -2
- package/src/app/shell.tsx +17 -16
- package/src/components/Foundations/Motion.stories.tsx +1 -0
- package/src/components/customize/brand-mark-section.tsx +4 -2
- package/src/components/customize/design-controls.tsx +17 -2
- package/src/components/customize/discipline-section.tsx +3 -6
- package/src/components/customize/font-pickers.tsx +1 -1
- package/src/components/customize/letter-spacing.tsx +1 -1
- package/src/components/customize/save-bar.tsx +2 -2
- package/src/components/customize/theme-io.tsx +7 -7
- package/src/components/customize/type-scale.tsx +1 -1
- package/src/components/customize-tab.tsx +7 -4
- package/src/components/home-tab.tsx +3 -0
- package/src/components/studio/studio.tsx +153 -0
- package/src/components/ui/accordion.tsx +7 -0
- package/src/components/ui/alert.tsx +1 -0
- package/src/components/ui/anchor.tsx +37 -3
- package/src/components/ui/announcement-bar.tsx +256 -0
- package/src/components/ui/attachment.tsx +211 -0
- package/src/components/ui/badge.tsx +1 -0
- package/src/components/ui/callout.tsx +218 -0
- package/src/components/ui/chart.tsx +5 -14
- package/src/components/ui/code.tsx +1 -1
- package/src/components/ui/color-picker.tsx +2 -2
- package/src/components/ui/format-relative-time.tsx +139 -0
- package/src/components/ui/gauge-chart.tsx +238 -0
- package/src/components/ui/grid.tsx +255 -0
- package/src/components/ui/heatmap.tsx +256 -0
- package/src/components/ui/icon.tsx +108 -0
- package/src/components/ui/index.ts +17 -0
- package/src/components/ui/infinite-scroll.tsx +103 -0
- package/src/components/ui/json-input.tsx +351 -0
- package/src/components/ui/navigation-progress.tsx +281 -0
- package/src/components/ui/number-input.tsx +1 -4
- package/src/components/ui/page-header.tsx +63 -0
- package/src/components/ui/password-input.tsx +2 -1
- package/src/components/ui/password-strength-input.tsx +288 -0
- package/src/components/ui/pill.tsx +140 -0
- package/src/components/ui/popover.tsx +27 -4
- package/src/components/ui/portal.tsx +111 -0
- package/src/components/ui/presence.tsx +172 -0
- package/src/components/ui/progress-stack.tsx +258 -0
- package/src/components/ui/scroll-progress.tsx +100 -0
- package/src/components/ui/select.tsx +89 -4
- package/src/components/ui/separator.tsx +1 -0
- package/src/components/ui/slider.tsx +83 -3
- package/src/components/ui/snackbar.tsx +393 -0
- package/src/components/ui/sortable-list.tsx +342 -0
- package/src/components/ui/sparkline.tsx +2 -14
- package/src/components/ui/squircle.tsx +1 -1
- package/src/components/ui/table.tsx +1 -1
- package/src/components/ui/textarea.tsx +28 -1
- package/src/components/ui/tour.tsx +1 -3
- package/src/data/docs-index.ts +25 -0
- package/src/hooks/use-reduced-motion.ts +6 -0
- package/src/lib/clamp.ts +2 -0
- package/src/stories/Accordion.stories.tsx +59 -0
- package/src/stories/Alert.stories.tsx +3 -1
- package/src/stories/Anchor.stories.tsx +54 -0
- package/src/stories/AnnouncementBar.stories.tsx +261 -0
- package/src/stories/Attachment.stories.tsx +189 -0
- package/src/stories/Badge.stories.tsx +3 -0
- package/src/stories/Calendar.stories.tsx +1 -1
- package/src/stories/Callout.stories.tsx +219 -0
- package/src/stories/Chart.stories.tsx +8 -0
- package/src/stories/Code.stories.tsx +1 -0
- package/src/stories/ColorPicker.stories.tsx +24 -0
- package/src/stories/FormatRelativeTime.stories.tsx +116 -0
- package/src/stories/GaugeChart.stories.tsx +163 -0
- package/src/stories/Grid.stories.tsx +197 -0
- package/src/stories/Heatmap.stories.tsx +172 -0
- package/src/stories/Icon.stories.tsx +175 -0
- package/src/stories/InfiniteScroll.stories.tsx +225 -0
- package/src/stories/JsonInput.stories.tsx +212 -0
- package/src/stories/MultiSelect.stories.tsx +1 -1
- package/src/stories/NavigationProgress.stories.tsx +159 -0
- package/src/stories/NumberInput.stories.tsx +1 -0
- package/src/stories/PageHeader.stories.tsx +102 -0
- package/src/stories/PasswordInput.stories.tsx +4 -0
- package/src/stories/PasswordStrengthInput.stories.tsx +211 -0
- package/src/stories/Pill.stories.tsx +201 -0
- package/src/stories/Popover.stories.tsx +29 -1
- package/src/stories/Portal.stories.tsx +191 -0
- package/src/stories/Presence.stories.tsx +228 -0
- package/src/stories/ProgressStack.stories.tsx +287 -0
- package/src/stories/ScrollProgress.stories.tsx +117 -0
- package/src/stories/Select.stories.tsx +25 -0
- package/src/stories/Separator.stories.tsx +3 -0
- package/src/stories/Slider.stories.tsx +54 -5
- package/src/stories/Snackbar.stories.tsx +315 -0
- package/src/stories/SortableList.stories.tsx +263 -0
- package/src/stories/Sparkline.stories.tsx +3 -0
- package/src/stories/Squircle.stories.tsx +1 -0
- package/src/stories/Table.stories.tsx +1 -0
- package/src/stories/Textarea.stories.tsx +18 -0
- package/src/stories/Tour.stories.tsx +1 -0
- package/src/stories/story-groups.json +26 -6
- package/src/styles.css +133 -12
- package/LICENSE +0 -21
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cn } from "../../lib/utils"
|
|
5
|
+
import { Spinner } from "./spinner"
|
|
6
|
+
|
|
7
|
+
export interface InfiniteScrollProps {
|
|
8
|
+
/** Fired when the sentinel enters the viewport and `hasMore` is true. */
|
|
9
|
+
onLoadMore: () => void
|
|
10
|
+
/** Whether more pages are available. When false the sentinel is removed. */
|
|
11
|
+
hasMore: boolean
|
|
12
|
+
/** Override the default spinner. Pass `null` to suppress the loader entirely. */
|
|
13
|
+
loader?: React.ReactNode
|
|
14
|
+
/** Shown below the list when `hasMore` is false. */
|
|
15
|
+
endMessage?: React.ReactNode
|
|
16
|
+
/**
|
|
17
|
+
* IntersectionObserver threshold (0–1). Default 0.
|
|
18
|
+
* Higher values trigger earlier (before the sentinel fully enters view).
|
|
19
|
+
*/
|
|
20
|
+
threshold?: number
|
|
21
|
+
/**
|
|
22
|
+
* IntersectionObserver rootMargin. Default "0px 0px 200px 0px" (triggers
|
|
23
|
+
* 200px before the sentinel reaches the viewport bottom).
|
|
24
|
+
*/
|
|
25
|
+
rootMargin?: string
|
|
26
|
+
children: React.ReactNode
|
|
27
|
+
className?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Wraps a list with an IntersectionObserver-based load-more trigger.
|
|
32
|
+
* The sentinel sits below `children`; when it enters the viewport and
|
|
33
|
+
* `hasMore` is true, `onLoadMore` fires once per intersection.
|
|
34
|
+
*
|
|
35
|
+
* Sources: Mantine InfiniteScroll (Embla fork), TanStack Query infinite
|
|
36
|
+
* scroll recipe, react-infinite-scroll-component (npm), shadcn community
|
|
37
|
+
* infinite-scroll recipe (2026-Q3).
|
|
38
|
+
*/
|
|
39
|
+
function InfiniteScroll({
|
|
40
|
+
onLoadMore,
|
|
41
|
+
hasMore,
|
|
42
|
+
loader,
|
|
43
|
+
endMessage,
|
|
44
|
+
threshold = 0,
|
|
45
|
+
rootMargin = "0px 0px 200px 0px",
|
|
46
|
+
children,
|
|
47
|
+
className,
|
|
48
|
+
}: InfiniteScrollProps) {
|
|
49
|
+
const sentinelRef = React.useRef<HTMLDivElement>(null)
|
|
50
|
+
const loadingRef = React.useRef(false)
|
|
51
|
+
|
|
52
|
+
React.useEffect(() => {
|
|
53
|
+
const sentinel = sentinelRef.current
|
|
54
|
+
if (!sentinel || !hasMore) return
|
|
55
|
+
|
|
56
|
+
const observer = new IntersectionObserver(
|
|
57
|
+
(entries) => {
|
|
58
|
+
const entry = entries[0]
|
|
59
|
+
if (!entry.isIntersecting) {
|
|
60
|
+
// The sentinel left the viewport: re-arm for the next entry. Before
|
|
61
|
+
// this, the guard only reset on a `hasMore` transition, so a list
|
|
62
|
+
// whose `hasMore` stayed true loaded exactly one page and went inert.
|
|
63
|
+
loadingRef.current = false
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
if (!loadingRef.current) {
|
|
67
|
+
loadingRef.current = true
|
|
68
|
+
onLoadMore()
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
{ threshold, rootMargin }
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
observer.observe(sentinel)
|
|
75
|
+
return () => observer.disconnect()
|
|
76
|
+
}, [hasMore, onLoadMore, threshold, rootMargin])
|
|
77
|
+
|
|
78
|
+
// Reset loading guard when hasMore changes (i.e. a new page loaded).
|
|
79
|
+
React.useEffect(() => {
|
|
80
|
+
loadingRef.current = false
|
|
81
|
+
}, [hasMore])
|
|
82
|
+
|
|
83
|
+
const defaultLoader = (
|
|
84
|
+
<div className="flex justify-center py-4" aria-label="Loading more items">
|
|
85
|
+
<Spinner size="sm" />
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div data-slot="infinite-scroll" className={cn("w-full", className)}>
|
|
91
|
+
{children}
|
|
92
|
+
{hasMore ? (
|
|
93
|
+
<div ref={sentinelRef} aria-hidden="true">
|
|
94
|
+
{loader === null ? null : loader ?? defaultLoader}
|
|
95
|
+
</div>
|
|
96
|
+
) : (
|
|
97
|
+
endMessage
|
|
98
|
+
)}
|
|
99
|
+
</div>
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { InfiniteScroll }
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
// JsonInput needs useState/useCallback for parse state, so it cannot be a
|
|
4
|
+
// server component. See the note in button.tsx on why the simpler primitives
|
|
5
|
+
// that only forward callbacks can stay server-renderable.
|
|
6
|
+
import * as React from "react"
|
|
7
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
8
|
+
import { AlertCircle, CheckCircle2, WrapText } from "lucide-react"
|
|
9
|
+
|
|
10
|
+
import { cn } from "../../lib/utils"
|
|
11
|
+
import { Textarea } from "./textarea"
|
|
12
|
+
import { Button } from "./button"
|
|
13
|
+
|
|
14
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
export interface JsonInputProps
|
|
17
|
+
extends Omit<
|
|
18
|
+
React.ComponentProps<typeof Textarea>,
|
|
19
|
+
"aria-invalid" | "value" | "defaultValue" | "onChange" | "onError"
|
|
20
|
+
> {
|
|
21
|
+
/** Controlled string value (raw JSON text). */
|
|
22
|
+
value?: string
|
|
23
|
+
/** Uncontrolled default value. */
|
|
24
|
+
defaultValue?: string
|
|
25
|
+
/** Fires on every keystroke with the raw text — same contract as Textarea onChange. */
|
|
26
|
+
onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void
|
|
27
|
+
/**
|
|
28
|
+
* Fires whenever the value is valid JSON, with the parsed result.
|
|
29
|
+
* Not called when the field is empty or contains invalid JSON.
|
|
30
|
+
*/
|
|
31
|
+
onValidChange?: (parsed: unknown) => void
|
|
32
|
+
/**
|
|
33
|
+
* Fires whenever the validation state changes.
|
|
34
|
+
* Receives the error message (string) when invalid, or null when valid/empty.
|
|
35
|
+
*/
|
|
36
|
+
onError?: (error: string | null) => void
|
|
37
|
+
/**
|
|
38
|
+
* When to run validation.
|
|
39
|
+
* - `"change"` (default) — on every keystroke; shows errors as the user types
|
|
40
|
+
* - `"blur"` — validates only when the field loses focus
|
|
41
|
+
*/
|
|
42
|
+
validateOn?: "change" | "blur"
|
|
43
|
+
/**
|
|
44
|
+
* Show a "Format" button that auto-pretty-prints the JSON on click.
|
|
45
|
+
* The button is inside the field area and calls JSON.stringify(parse, null, 2).
|
|
46
|
+
* Default `true`.
|
|
47
|
+
*/
|
|
48
|
+
showFormatButton?: boolean
|
|
49
|
+
/**
|
|
50
|
+
* Number of spaces to use when formatting JSON. Default 2.
|
|
51
|
+
* Passed to `JSON.stringify(parsed, null, indent)`.
|
|
52
|
+
*/
|
|
53
|
+
formatIndent?: number
|
|
54
|
+
/**
|
|
55
|
+
* Error message to display. When provided, this overrides the built-in
|
|
56
|
+
* JSON.parse error message. Useful for server-side validation feedback.
|
|
57
|
+
* Pass `null` to clear an external error.
|
|
58
|
+
*/
|
|
59
|
+
externalError?: string | null
|
|
60
|
+
/** Size variant — matches Textarea sizes. */
|
|
61
|
+
size?: "sm" | "default" | "lg" | "xl"
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Try to parse a string as JSON. Returns {ok, parsed, error}.
|
|
68
|
+
* Empty string is treated as "not entered yet" — ok is undefined (neutral).
|
|
69
|
+
*/
|
|
70
|
+
function parseJson(
|
|
71
|
+
text: string
|
|
72
|
+
): { ok: true; parsed: unknown } | { ok: false; error: string } | { ok: undefined } {
|
|
73
|
+
if (text.trim() === "") return { ok: undefined }
|
|
74
|
+
try {
|
|
75
|
+
return { ok: true, parsed: JSON.parse(text) }
|
|
76
|
+
} catch (err) {
|
|
77
|
+
const raw = err instanceof Error ? err.message : "Invalid JSON"
|
|
78
|
+
// Trim the "JSON Parse error:" / "SyntaxError: " prefix noise some engines add.
|
|
79
|
+
const cleaned = raw.replace(/^JSON Parse error:\s*/i, "").replace(/^SyntaxError:\s*/i, "")
|
|
80
|
+
return { ok: false, error: cleaned }
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function formatJson(text: string, indent: number): string {
|
|
85
|
+
try {
|
|
86
|
+
return JSON.stringify(JSON.parse(text), null, indent)
|
|
87
|
+
} catch {
|
|
88
|
+
return text
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ─── JsonInput ────────────────────────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* JsonInput — a `<Textarea>` specialized for JSON text entry. Validates on
|
|
96
|
+
* every keystroke (or on blur) and shows an inline error when the text is not
|
|
97
|
+
* valid JSON. Fires `onValidChange(parsed)` whenever valid JSON is present.
|
|
98
|
+
* An optional "Format" button auto-indents the content.
|
|
99
|
+
*
|
|
100
|
+
* Peer parity: Mantine JsonInput (textarea + JSON.parse validation + error msg
|
|
101
|
+
* + optional formatOnBlur prop), Ant Design's config-panel pattern, shadcn
|
|
102
|
+
* JSON config examples. T1 tier.
|
|
103
|
+
*
|
|
104
|
+
* Accessibility
|
|
105
|
+
* -------------
|
|
106
|
+
* - `aria-invalid="true"` is set on the `<textarea>` when the value contains
|
|
107
|
+
* invalid JSON. The Textarea component uses this for its destructive ring.
|
|
108
|
+
* - The error message is a `role="status"` region (aria-live="polite") so screen readers announce it
|
|
109
|
+
* when it appears. The alert id is added to the textarea's
|
|
110
|
+
* `aria-describedby` so the validation message is associated with the field.
|
|
111
|
+
* - The optional "Format" button has a descriptive aria-label.
|
|
112
|
+
*
|
|
113
|
+
* Motion
|
|
114
|
+
* ------
|
|
115
|
+
* Pattern (c) — the Textarea already carries `transition-[color,box-shadow]`
|
|
116
|
+
* on the ring; the status icon fades in/out via `transition-opacity` +
|
|
117
|
+
* `var(--duration-fast)` / `var(--ease-out)`. The error message slides down
|
|
118
|
+
* with `data-[visible=true]:animate-in data-[visible=true]:slide-in-from-top-1`
|
|
119
|
+
* (existing BADS keyframes, pattern a).
|
|
120
|
+
*/
|
|
121
|
+
export function JsonInput({
|
|
122
|
+
value: controlledValue,
|
|
123
|
+
defaultValue,
|
|
124
|
+
onChange,
|
|
125
|
+
onValidChange,
|
|
126
|
+
onError,
|
|
127
|
+
validateOn = "change",
|
|
128
|
+
showFormatButton = true,
|
|
129
|
+
formatIndent = 2,
|
|
130
|
+
externalError,
|
|
131
|
+
size,
|
|
132
|
+
className,
|
|
133
|
+
disabled,
|
|
134
|
+
placeholder = '{"key": "value"}',
|
|
135
|
+
...props
|
|
136
|
+
}: JsonInputProps) {
|
|
137
|
+
const errorId = React.useId()
|
|
138
|
+
|
|
139
|
+
// ── Controlled / uncontrolled state ───────────────────────────────────────
|
|
140
|
+
const isControlled = controlledValue !== undefined
|
|
141
|
+
const [internalValue, setInternalValue] = React.useState<string>(defaultValue ?? "")
|
|
142
|
+
const text = isControlled ? controlledValue : internalValue
|
|
143
|
+
|
|
144
|
+
// ── Parse state ───────────────────────────────────────────────────────────
|
|
145
|
+
// "dirty" tracks whether the user has ever typed (don't show errors on first
|
|
146
|
+
// paint of an empty field or an already-valid initial value).
|
|
147
|
+
const [dirty, setDirty] = React.useState<boolean>(
|
|
148
|
+
() => !!defaultValue || !!controlledValue
|
|
149
|
+
)
|
|
150
|
+
const [parseResult, setParseResult] = React.useState<
|
|
151
|
+
ReturnType<typeof parseJson>
|
|
152
|
+
>(() => parseJson(text ?? ""))
|
|
153
|
+
|
|
154
|
+
// ── Callbacks ─────────────────────────────────────────────────────────────
|
|
155
|
+
const prevErrorRef = React.useRef<string | null | undefined>(undefined)
|
|
156
|
+
const prevValidRef = React.useRef<unknown>(undefined)
|
|
157
|
+
|
|
158
|
+
function runValidation(raw: string) {
|
|
159
|
+
const result = parseJson(raw)
|
|
160
|
+
setParseResult(result)
|
|
161
|
+
|
|
162
|
+
// Fire onValidChange only when the parsed value actually changes.
|
|
163
|
+
if (result.ok === true) {
|
|
164
|
+
if (result.parsed !== prevValidRef.current) {
|
|
165
|
+
prevValidRef.current = result.parsed
|
|
166
|
+
onValidChange?.(result.parsed)
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
prevValidRef.current = undefined
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Fire onError when error state changes.
|
|
173
|
+
const error = result.ok === false ? result.error : null
|
|
174
|
+
if (error !== prevErrorRef.current) {
|
|
175
|
+
prevErrorRef.current = error
|
|
176
|
+
onError?.(error)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
|
|
181
|
+
const raw = e.target.value
|
|
182
|
+
if (!isControlled) setInternalValue(raw)
|
|
183
|
+
setDirty(true)
|
|
184
|
+
if (validateOn === "change") runValidation(raw)
|
|
185
|
+
onChange?.(e)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function handleBlur(e: React.FocusEvent<HTMLTextAreaElement>) {
|
|
189
|
+
if (validateOn === "blur") {
|
|
190
|
+
setDirty(true)
|
|
191
|
+
runValidation(text ?? "")
|
|
192
|
+
}
|
|
193
|
+
props.onBlur?.(e)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function handleFormat() {
|
|
197
|
+
if (!text) return
|
|
198
|
+
const formatted = formatJson(text, formatIndent)
|
|
199
|
+
if (!isControlled) setInternalValue(formatted)
|
|
200
|
+
// Synthesize a change event so the controlled consumer can update too.
|
|
201
|
+
const nativeInput = textareaRef.current
|
|
202
|
+
if (nativeInput) {
|
|
203
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
204
|
+
HTMLTextAreaElement.prototype,
|
|
205
|
+
"value"
|
|
206
|
+
)
|
|
207
|
+
descriptor?.set?.call(nativeInput, formatted)
|
|
208
|
+
nativeInput.dispatchEvent(new Event("input", { bubbles: true }))
|
|
209
|
+
// Also call onChange directly to match normal input behaviour.
|
|
210
|
+
const syntheticEvent = {
|
|
211
|
+
target: nativeInput,
|
|
212
|
+
currentTarget: nativeInput,
|
|
213
|
+
} as React.ChangeEvent<HTMLTextAreaElement>
|
|
214
|
+
onChange?.(syntheticEvent)
|
|
215
|
+
}
|
|
216
|
+
runValidation(formatted)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const textareaRef = React.useRef<HTMLTextAreaElement>(null)
|
|
220
|
+
|
|
221
|
+
// Sync parse state when the controlled value changes externally.
|
|
222
|
+
React.useEffect(() => {
|
|
223
|
+
if (isControlled) {
|
|
224
|
+
runValidation(controlledValue ?? "")
|
|
225
|
+
}
|
|
226
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
227
|
+
}, [controlledValue])
|
|
228
|
+
|
|
229
|
+
// ── Derived ────────────────────────────────────────────────────────────────
|
|
230
|
+
// Resolve effective error (external prop overrides internal parse result).
|
|
231
|
+
const internalError = parseResult.ok === false ? parseResult.error : null
|
|
232
|
+
const effectiveError = externalError !== undefined ? externalError : internalError
|
|
233
|
+
const showError = dirty && !!effectiveError
|
|
234
|
+
const showValid = dirty && parseResult.ok === true && !externalError
|
|
235
|
+
const isEmpty = !text || text.trim() === ""
|
|
236
|
+
|
|
237
|
+
// ─── Render ───────────────────────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
<div
|
|
241
|
+
data-slot="json-input-root"
|
|
242
|
+
className={cn("relative flex flex-col gap-1.5", className)}
|
|
243
|
+
>
|
|
244
|
+
{/* Textarea + optional Format button wrapper */}
|
|
245
|
+
<div className="relative">
|
|
246
|
+
<Textarea
|
|
247
|
+
{...props}
|
|
248
|
+
ref={textareaRef}
|
|
249
|
+
size={size}
|
|
250
|
+
value={isControlled ? controlledValue : internalValue}
|
|
251
|
+
onChange={handleChange}
|
|
252
|
+
onBlur={handleBlur}
|
|
253
|
+
disabled={disabled}
|
|
254
|
+
placeholder={placeholder}
|
|
255
|
+
aria-invalid={showError ? "true" : undefined}
|
|
256
|
+
aria-describedby={
|
|
257
|
+
[props["aria-describedby"], showError ? errorId : undefined]
|
|
258
|
+
.filter(Boolean)
|
|
259
|
+
.join(" ") || undefined
|
|
260
|
+
}
|
|
261
|
+
spellCheck={false}
|
|
262
|
+
autoCapitalize="none"
|
|
263
|
+
autoCorrect="off"
|
|
264
|
+
data-slot="json-input-textarea"
|
|
265
|
+
// Right-side padding so text never runs under the status icon.
|
|
266
|
+
className={cn(
|
|
267
|
+
"font-mono",
|
|
268
|
+
(showFormatButton || !isEmpty) && "pr-9"
|
|
269
|
+
)}
|
|
270
|
+
/>
|
|
271
|
+
|
|
272
|
+
{/* Status icon — valid or empty show nothing, invalid shows AlertCircle */}
|
|
273
|
+
<div
|
|
274
|
+
aria-hidden="true"
|
|
275
|
+
data-slot="json-input-status"
|
|
276
|
+
className={cn(
|
|
277
|
+
"pointer-events-none absolute right-3 top-3",
|
|
278
|
+
"transition-opacity",
|
|
279
|
+
"size-4 shrink-0"
|
|
280
|
+
)}
|
|
281
|
+
style={{
|
|
282
|
+
transitionDuration: "var(--duration-fast)",
|
|
283
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
284
|
+
opacity: showError || showValid ? 1 : 0,
|
|
285
|
+
}}
|
|
286
|
+
>
|
|
287
|
+
{showValid && (
|
|
288
|
+
<CheckCircle2
|
|
289
|
+
className="size-4 text-[var(--color-success,oklch(0.65_0.15_150))]"
|
|
290
|
+
strokeWidth={1.5}
|
|
291
|
+
/>
|
|
292
|
+
)}
|
|
293
|
+
{showError && (
|
|
294
|
+
<AlertCircle className="size-4 text-destructive" strokeWidth={1.5} />
|
|
295
|
+
)}
|
|
296
|
+
</div>
|
|
297
|
+
</div>
|
|
298
|
+
|
|
299
|
+
{/* Bottom row: error message + Format button */}
|
|
300
|
+
<div className="flex min-h-0 items-start justify-between gap-2">
|
|
301
|
+
{/* Error message — announces on appearance via role=alert */}
|
|
302
|
+
<p
|
|
303
|
+
id={errorId}
|
|
304
|
+
role="status"
|
|
305
|
+
aria-live="polite"
|
|
306
|
+
aria-atomic="true"
|
|
307
|
+
data-slot="json-input-error"
|
|
308
|
+
className={cn(
|
|
309
|
+
"flex min-w-0 items-start gap-1 text-xs text-destructive",
|
|
310
|
+
"transition-opacity",
|
|
311
|
+
showError ? "opacity-100" : "opacity-0 pointer-events-none select-none"
|
|
312
|
+
)}
|
|
313
|
+
style={{
|
|
314
|
+
transitionDuration: "var(--duration-fast)",
|
|
315
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
316
|
+
}}
|
|
317
|
+
>
|
|
318
|
+
{showError && (
|
|
319
|
+
<>
|
|
320
|
+
<AlertCircle
|
|
321
|
+
aria-hidden="true"
|
|
322
|
+
className="mt-px size-3.5 shrink-0 text-destructive"
|
|
323
|
+
strokeWidth={1.5}
|
|
324
|
+
/>
|
|
325
|
+
<span className="break-words">{effectiveError}</span>
|
|
326
|
+
</>
|
|
327
|
+
)}
|
|
328
|
+
</p>
|
|
329
|
+
|
|
330
|
+
{/* Format button */}
|
|
331
|
+
{showFormatButton && (
|
|
332
|
+
<Button
|
|
333
|
+
type="button"
|
|
334
|
+
variant="ghost"
|
|
335
|
+
size="sm"
|
|
336
|
+
aria-label="Format JSON"
|
|
337
|
+
disabled={disabled || isEmpty || parseResult.ok === false}
|
|
338
|
+
onClick={handleFormat}
|
|
339
|
+
data-slot="json-input-format-button"
|
|
340
|
+
className="h-auto shrink-0 px-2 py-0.5 text-xs text-muted-foreground hover:text-foreground"
|
|
341
|
+
>
|
|
342
|
+
<WrapText aria-hidden="true" className="size-3.5" strokeWidth={1.5} />
|
|
343
|
+
Format
|
|
344
|
+
</Button>
|
|
345
|
+
)}
|
|
346
|
+
</div>
|
|
347
|
+
</div>
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
JsonInput.displayName = "JsonInput"
|