@behavioral-agentic-design-system/core 0.27.0 → 0.28.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 +4 -4
- package/src/app/preview/components/page.tsx +771 -24
- package/src/app/shell.tsx +17 -0
- package/src/components/ui/ai-badge.tsx +74 -0
- package/src/components/ui/chat-bubble.tsx +298 -0
- package/src/components/ui/clipboard.tsx +291 -0
- package/src/components/ui/copy-button.tsx +5 -1
- package/src/components/ui/data-table.tsx +388 -0
- package/src/components/ui/focus-trap.tsx +184 -0
- package/src/components/ui/index.ts +7 -0
- package/src/components/ui/mark.tsx +69 -0
- package/src/components/ui/rolling-number.tsx +200 -0
- package/src/components/ui/typing-indicator.tsx +2 -2
- package/src/components/ui/word-rotate.tsx +6 -1
- package/src/stories/AiBadge.stories.tsx +67 -0
- package/src/stories/ChatBubble.stories.tsx +222 -0
- package/src/stories/Clipboard.stories.tsx +136 -0
- package/src/stories/CopyButton.stories.tsx +2 -0
- package/src/stories/DataTable.stories.tsx +271 -0
- package/src/stories/FocusTrap.stories.tsx +218 -0
- package/src/stories/Mark.stories.tsx +84 -0
- package/src/stories/RollingNumber.stories.tsx +226 -0
- package/src/stories/TypingIndicator.stories.tsx +1 -0
- package/src/stories/WordRotate.stories.tsx +1 -0
package/src/app/shell.tsx
CHANGED
|
@@ -250,6 +250,23 @@ export function Shell({ tabs, defaultTab }: ShellProps) {
|
|
|
250
250
|
url.searchParams.set("tab", value);
|
|
251
251
|
window.history.pushState({ shellTab: value }, "", url);
|
|
252
252
|
} catch {}
|
|
253
|
+
// Snap the (re)selected preview tab's iframe back to the top. The tabs are
|
|
254
|
+
// sticky-mounted, so returning to Components would otherwise land wherever
|
|
255
|
+
// it was last scrolled — clicking a preview tab should show it from the
|
|
256
|
+
// start. Same-origin iframe, so contentWindow is reachable (the theme-sync
|
|
257
|
+
// effect uses it too). rAF waits for Radix to flip data-state on the new
|
|
258
|
+
// panel; native tabs (no iframe) match nothing → their scroll is preserved.
|
|
259
|
+
// On a tab's first-ever selection the iframe hasn't mounted yet, so this
|
|
260
|
+
// no-ops — a cold load already starts at the top.
|
|
261
|
+
requestAnimationFrame(() => {
|
|
262
|
+
try {
|
|
263
|
+
document
|
|
264
|
+
.querySelector<HTMLIFrameElement>(
|
|
265
|
+
'[role="tabpanel"][data-state="active"] iframe',
|
|
266
|
+
)
|
|
267
|
+
?.contentWindow?.scrollTo({ top: 0 });
|
|
268
|
+
} catch {}
|
|
269
|
+
});
|
|
253
270
|
}, []);
|
|
254
271
|
|
|
255
272
|
// Restore the active tab once on mount, preferring an explicit ?tab= in the
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
3
|
+
import { Slot } from "@radix-ui/react-slot"
|
|
4
|
+
import { Sparkles } from "lucide-react"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
|
|
8
|
+
// AiBadge — the canonical BADS disclosure for AI-generated content. It exists
|
|
9
|
+
// so the Transparency floor's "AI-generated content is marked [AI]" rule is a
|
|
10
|
+
// real, accessible component rather than a hand-typed string: the disclosure
|
|
11
|
+
// announces itself fully to assistive tech (aria-label "AI-generated content")
|
|
12
|
+
// even when the visible text is just "AI", and it can't be typo'd out of
|
|
13
|
+
// existence. `lint:ai-label` accepts <AiBadge> as a valid disclosure token.
|
|
14
|
+
const aiBadgeVariants = cva(
|
|
15
|
+
"inline-flex w-fit shrink-0 items-center justify-center gap-1 rounded-full border font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 [&>svg]:pointer-events-none [&>svg]:shrink-0",
|
|
16
|
+
{
|
|
17
|
+
variants: {
|
|
18
|
+
variant: {
|
|
19
|
+
// Quiet by default — a disclosure should be legible, not loud, sitting
|
|
20
|
+
// beside the content it labels.
|
|
21
|
+
subtle: "border-transparent bg-muted text-muted-foreground",
|
|
22
|
+
solid: "border-transparent bg-primary text-primary-foreground",
|
|
23
|
+
outline: "border-border text-foreground",
|
|
24
|
+
},
|
|
25
|
+
size: {
|
|
26
|
+
sm: "gap-0.5 px-1.5 py-0.5 text-xs [&>svg]:size-2.5",
|
|
27
|
+
default: "px-2 py-0.5 text-xs [&>svg]:size-3",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultVariants: {
|
|
31
|
+
variant: "subtle",
|
|
32
|
+
size: "default",
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
function AiBadge({
|
|
38
|
+
className,
|
|
39
|
+
variant,
|
|
40
|
+
size,
|
|
41
|
+
showIcon = true,
|
|
42
|
+
label = "AI-generated content",
|
|
43
|
+
children,
|
|
44
|
+
asChild = false,
|
|
45
|
+
...props
|
|
46
|
+
}: React.ComponentProps<"span"> &
|
|
47
|
+
VariantProps<typeof aiBadgeVariants> & {
|
|
48
|
+
/** Render as the child element (e.g. wrap an anchor) via Radix Slot. */
|
|
49
|
+
asChild?: boolean
|
|
50
|
+
/** Show the leading AI glyph. Default true. */
|
|
51
|
+
showIcon?: boolean
|
|
52
|
+
/**
|
|
53
|
+
* The full phrase announced to assistive tech. Overrides the compact
|
|
54
|
+
* visible text so a screen reader always hears the complete disclosure,
|
|
55
|
+
* not just "AI". Default "AI-generated content".
|
|
56
|
+
*/
|
|
57
|
+
label?: string
|
|
58
|
+
}) {
|
|
59
|
+
const Comp = asChild ? Slot : "span"
|
|
60
|
+
return (
|
|
61
|
+
<Comp
|
|
62
|
+
data-slot="ai-badge"
|
|
63
|
+
data-variant={variant ?? "subtle"}
|
|
64
|
+
aria-label={label}
|
|
65
|
+
className={cn(aiBadgeVariants({ variant, size }), className)}
|
|
66
|
+
{...props}
|
|
67
|
+
>
|
|
68
|
+
{showIcon ? <Sparkles strokeWidth={1.5} aria-hidden="true" /> : null}
|
|
69
|
+
{children ?? "AI"}
|
|
70
|
+
</Comp>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { AiBadge, aiBadgeVariants }
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ChatBubble — styled message bubble for chat/messaging surfaces.
|
|
5
|
+
*
|
|
6
|
+
* Sources: Base UI / shadcn community chat message pattern (June 2026);
|
|
7
|
+
* 21st.dev community registry (multiple independent authors).
|
|
8
|
+
*
|
|
9
|
+
* Compound component: ChatBubble (row) + ChatBubbleAvatar + ChatBubbleMessage
|
|
10
|
+
* (sent / received / typing) + ChatBubbleTimestamp + ChatBubbleReadReceipt +
|
|
11
|
+
* ChatBubbleActions / ChatBubbleAction.
|
|
12
|
+
*
|
|
13
|
+
* Semantics split: each ChatBubble row is role="group"; the conversation
|
|
14
|
+
* CONTAINER is the consumer's responsibility (role="log", scrollable) — see
|
|
15
|
+
* the registry a11yContract. Message text is read naturally by screen
|
|
16
|
+
* readers (no per-message aria-label — that would double-announce every
|
|
17
|
+
* message); only the typing variant carries an accessible name because it
|
|
18
|
+
* has no visible text.
|
|
19
|
+
*
|
|
20
|
+
* Motion (spec §4):
|
|
21
|
+
* - entrance: tw-animate-css `animate-in fade-in-0 slide-in-from-{left,right}-2`
|
|
22
|
+
* (pattern a — same vocabulary as tooltip/menubar/dialog)
|
|
23
|
+
* - typing dots: `chat-typing-bounce` keyframe keyed off
|
|
24
|
+
* [data-slot="chat-bubble-typing"] in build-tokens.cjs (pattern b)
|
|
25
|
+
* - hover-action reveal: transition-opacity + inline var(--duration-fast) /
|
|
26
|
+
* var(--ease-out) (pattern c), with focus-within reveal for keyboard parity
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import * as React from "react"
|
|
30
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
31
|
+
import { Check, CheckCheck } from "lucide-react"
|
|
32
|
+
|
|
33
|
+
import { cn } from "../../lib/utils"
|
|
34
|
+
import { Avatar, AvatarFallback, AvatarImage } from "./avatar"
|
|
35
|
+
import { Button } from "./button"
|
|
36
|
+
|
|
37
|
+
type ChatBubblePosition = "sent" | "received"
|
|
38
|
+
|
|
39
|
+
const ChatBubblePositionContext =
|
|
40
|
+
React.createContext<ChatBubblePosition>("received")
|
|
41
|
+
|
|
42
|
+
function ChatBubble({
|
|
43
|
+
position,
|
|
44
|
+
className,
|
|
45
|
+
children,
|
|
46
|
+
...props
|
|
47
|
+
}: React.ComponentProps<"div"> & {
|
|
48
|
+
/** Which side of the conversation this row aligns to. */
|
|
49
|
+
position: ChatBubblePosition
|
|
50
|
+
}) {
|
|
51
|
+
return (
|
|
52
|
+
<ChatBubblePositionContext.Provider value={position}>
|
|
53
|
+
<div
|
|
54
|
+
role="group"
|
|
55
|
+
data-slot="chat-bubble"
|
|
56
|
+
data-position={position}
|
|
57
|
+
className={cn(
|
|
58
|
+
"group/chat-bubble relative flex w-max max-w-[75%] items-end gap-2",
|
|
59
|
+
"animate-in fade-in-0",
|
|
60
|
+
position === "sent"
|
|
61
|
+
? "ml-auto flex-row-reverse slide-in-from-right-2"
|
|
62
|
+
: "mr-auto slide-in-from-left-2",
|
|
63
|
+
className
|
|
64
|
+
)}
|
|
65
|
+
{...props}
|
|
66
|
+
>
|
|
67
|
+
{children}
|
|
68
|
+
</div>
|
|
69
|
+
</ChatBubblePositionContext.Provider>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function ChatBubbleAvatar({
|
|
74
|
+
src,
|
|
75
|
+
alt = "",
|
|
76
|
+
fallback,
|
|
77
|
+
className,
|
|
78
|
+
children,
|
|
79
|
+
...props
|
|
80
|
+
}: React.ComponentProps<typeof Avatar> & {
|
|
81
|
+
/** Image source for the sender's avatar. */
|
|
82
|
+
src?: string
|
|
83
|
+
/** Alt text for the avatar image. Empty (decorative) when the sender's name is visible next to the message. */
|
|
84
|
+
alt?: string
|
|
85
|
+
/** Fallback initials shown while the image loads or when no src is given. */
|
|
86
|
+
fallback?: React.ReactNode
|
|
87
|
+
}) {
|
|
88
|
+
return (
|
|
89
|
+
<Avatar
|
|
90
|
+
data-slot="chat-bubble-avatar"
|
|
91
|
+
className={cn("shrink-0 self-end", className)}
|
|
92
|
+
{...props}
|
|
93
|
+
>
|
|
94
|
+
{src ? <AvatarImage src={src} alt={alt} /> : null}
|
|
95
|
+
{fallback != null ? <AvatarFallback>{fallback}</AvatarFallback> : null}
|
|
96
|
+
{children}
|
|
97
|
+
</Avatar>
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const chatBubbleMessageVariants = cva(
|
|
102
|
+
// min-w-0 + break-words: long-label overflow contract — URLs, identifiers,
|
|
103
|
+
// and non-Latin strings wrap instead of bleeding past the bubble.
|
|
104
|
+
"min-w-0 break-words px-4 py-2.5 text-sm leading-relaxed",
|
|
105
|
+
{
|
|
106
|
+
variants: {
|
|
107
|
+
variant: {
|
|
108
|
+
sent: "rounded-tl-2xl rounded-tr-2xl rounded-bl-2xl rounded-br-sm bg-primary text-primary-foreground",
|
|
109
|
+
received:
|
|
110
|
+
"rounded-tl-2xl rounded-tr-2xl rounded-br-2xl rounded-bl-sm bg-muted text-foreground",
|
|
111
|
+
typing:
|
|
112
|
+
"rounded-tl-2xl rounded-tr-2xl rounded-br-2xl rounded-bl-sm bg-muted text-foreground",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
defaultVariants: {
|
|
116
|
+
variant: "received",
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
function ChatBubbleMessage({
|
|
122
|
+
variant,
|
|
123
|
+
className,
|
|
124
|
+
children,
|
|
125
|
+
...props
|
|
126
|
+
}: React.ComponentProps<"div"> &
|
|
127
|
+
VariantProps<typeof chatBubbleMessageVariants>) {
|
|
128
|
+
const position = React.useContext(ChatBubblePositionContext)
|
|
129
|
+
// Variant defaults to the parent row's position so the common case is
|
|
130
|
+
// just <ChatBubbleMessage>text</ChatBubbleMessage>.
|
|
131
|
+
const resolved = variant ?? position
|
|
132
|
+
|
|
133
|
+
if (resolved === "typing") {
|
|
134
|
+
return (
|
|
135
|
+
<div
|
|
136
|
+
data-slot="chat-bubble-typing"
|
|
137
|
+
role="status"
|
|
138
|
+
aria-live="polite"
|
|
139
|
+
aria-label="Typing…"
|
|
140
|
+
className={cn(
|
|
141
|
+
chatBubbleMessageVariants({ variant: "typing" }),
|
|
142
|
+
"flex w-max items-center gap-1",
|
|
143
|
+
className
|
|
144
|
+
)}
|
|
145
|
+
{...props}
|
|
146
|
+
>
|
|
147
|
+
{/* Dots are decorative — the accessible name above carries the meaning.
|
|
148
|
+
Bounce keyframe (chat-typing-bounce) is applied from the design-
|
|
149
|
+
system CSS via the data-slot selectors; stagger is 0 / 150 / 300ms
|
|
150
|
+
driven by var(--duration-fast) steps. */}
|
|
151
|
+
<span
|
|
152
|
+
aria-hidden="true"
|
|
153
|
+
data-slot="chat-bubble-typing-dot"
|
|
154
|
+
className="size-1.5 rounded-full bg-muted-foreground"
|
|
155
|
+
/>
|
|
156
|
+
<span
|
|
157
|
+
aria-hidden="true"
|
|
158
|
+
data-slot="chat-bubble-typing-dot"
|
|
159
|
+
className="size-1.5 rounded-full bg-muted-foreground"
|
|
160
|
+
/>
|
|
161
|
+
<span
|
|
162
|
+
aria-hidden="true"
|
|
163
|
+
data-slot="chat-bubble-typing-dot"
|
|
164
|
+
className="size-1.5 rounded-full bg-muted-foreground"
|
|
165
|
+
/>
|
|
166
|
+
</div>
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<div
|
|
172
|
+
data-slot="chat-bubble-message"
|
|
173
|
+
className={cn(chatBubbleMessageVariants({ variant: resolved }), className)}
|
|
174
|
+
{...props}
|
|
175
|
+
>
|
|
176
|
+
{children}
|
|
177
|
+
</div>
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function ChatBubbleTimestamp({
|
|
182
|
+
timestamp,
|
|
183
|
+
className,
|
|
184
|
+
...props
|
|
185
|
+
}: React.ComponentProps<"span"> & {
|
|
186
|
+
/** Pre-formatted display string ("10:42 AM"). Formatting is the consumer's job. */
|
|
187
|
+
timestamp: string
|
|
188
|
+
}) {
|
|
189
|
+
return (
|
|
190
|
+
<span
|
|
191
|
+
data-slot="chat-bubble-timestamp"
|
|
192
|
+
className={cn("text-xs text-muted-foreground select-none", className)}
|
|
193
|
+
{...props}
|
|
194
|
+
>
|
|
195
|
+
{timestamp}
|
|
196
|
+
</span>
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const READ_RECEIPT_LABEL: Record<"sent" | "delivered" | "read", string> = {
|
|
201
|
+
sent: "Sent",
|
|
202
|
+
delivered: "Delivered",
|
|
203
|
+
read: "Read",
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function ChatBubbleReadReceipt({
|
|
207
|
+
status,
|
|
208
|
+
className,
|
|
209
|
+
...props
|
|
210
|
+
}: React.ComponentProps<"span"> & {
|
|
211
|
+
/** Delivery state: sent (single tick), delivered (double tick), read (double tick, primary). */
|
|
212
|
+
status: "sent" | "delivered" | "read"
|
|
213
|
+
}) {
|
|
214
|
+
const Icon = status === "sent" ? Check : CheckCheck
|
|
215
|
+
return (
|
|
216
|
+
<span
|
|
217
|
+
data-slot="chat-bubble-read-receipt"
|
|
218
|
+
data-status={status}
|
|
219
|
+
className={cn(
|
|
220
|
+
"inline-flex items-center",
|
|
221
|
+
// No --color-info utility is mapped in the theme; "read" uses the
|
|
222
|
+
// brand primary so state is still triple-encoded (icon shape + color
|
|
223
|
+
// + sr-only text), never color alone.
|
|
224
|
+
status === "read" ? "text-primary" : "text-muted-foreground",
|
|
225
|
+
className
|
|
226
|
+
)}
|
|
227
|
+
{...props}
|
|
228
|
+
>
|
|
229
|
+
<Icon strokeWidth={1.5} className="size-4" aria-hidden="true" />
|
|
230
|
+
<span className="sr-only">{READ_RECEIPT_LABEL[status]}</span>
|
|
231
|
+
</span>
|
|
232
|
+
)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function ChatBubbleActions({
|
|
236
|
+
className,
|
|
237
|
+
style,
|
|
238
|
+
children,
|
|
239
|
+
...props
|
|
240
|
+
}: React.ComponentProps<"div">) {
|
|
241
|
+
return (
|
|
242
|
+
<div
|
|
243
|
+
data-slot="chat-bubble-actions"
|
|
244
|
+
className={cn(
|
|
245
|
+
// Hidden until the row is hovered OR any action has keyboard focus —
|
|
246
|
+
// focus-within keeps hover-revealed actions reachable without a mouse.
|
|
247
|
+
"flex items-center gap-1 self-center opacity-0 transition-opacity",
|
|
248
|
+
"group-hover/chat-bubble:opacity-100 focus-within:opacity-100",
|
|
249
|
+
className
|
|
250
|
+
)}
|
|
251
|
+
style={{
|
|
252
|
+
transitionDuration: "var(--duration-fast)",
|
|
253
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
254
|
+
...style,
|
|
255
|
+
}}
|
|
256
|
+
{...props}
|
|
257
|
+
>
|
|
258
|
+
{children}
|
|
259
|
+
</div>
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function ChatBubbleAction({
|
|
264
|
+
icon,
|
|
265
|
+
label,
|
|
266
|
+
className,
|
|
267
|
+
...props
|
|
268
|
+
}: Omit<React.ComponentProps<typeof Button>, "children" | "aria-label"> & {
|
|
269
|
+
/** Icon node — pass lucide icons with strokeWidth={1.5}. */
|
|
270
|
+
icon: React.ReactNode
|
|
271
|
+
/** Accessible name for the icon-only button (required). */
|
|
272
|
+
label: string
|
|
273
|
+
}) {
|
|
274
|
+
return (
|
|
275
|
+
<Button
|
|
276
|
+
type="button"
|
|
277
|
+
variant="ghost"
|
|
278
|
+
size="icon-xl"
|
|
279
|
+
data-slot="chat-bubble-action"
|
|
280
|
+
aria-label={label}
|
|
281
|
+
className={className}
|
|
282
|
+
{...props}
|
|
283
|
+
>
|
|
284
|
+
{icon}
|
|
285
|
+
</Button>
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export {
|
|
290
|
+
ChatBubble,
|
|
291
|
+
ChatBubbleAvatar,
|
|
292
|
+
ChatBubbleMessage,
|
|
293
|
+
ChatBubbleTimestamp,
|
|
294
|
+
ChatBubbleReadReceipt,
|
|
295
|
+
ChatBubbleActions,
|
|
296
|
+
ChatBubbleAction,
|
|
297
|
+
chatBubbleMessageVariants,
|
|
298
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
// Clipboard — compound widget that renders the copyable VALUE inline (as a
|
|
4
|
+
// read-only input) alongside a copy trigger with the CopyButton success
|
|
5
|
+
// animation. The expected UI for API key panels, invite links, embed codes,
|
|
6
|
+
// webhook URLs, CLI snippets, and share links. Distinct from CopyButton,
|
|
7
|
+
// which is a standalone button that copies a string prop without showing it.
|
|
8
|
+
//
|
|
9
|
+
// The trigger composes the CopyButton primitive — it owns the clipboard
|
|
10
|
+
// write, the icon-swap success animation, and the aria-live announcement
|
|
11
|
+
// (registry antiPattern: never hand-roll a copy state; a visually-dead Copy
|
|
12
|
+
// button shipped once, 2026-07-06).
|
|
13
|
+
|
|
14
|
+
import * as React from "react"
|
|
15
|
+
|
|
16
|
+
import { cn } from "../../lib/utils"
|
|
17
|
+
import { CopyButton, type CopyButtonProps } from "./copy-button"
|
|
18
|
+
import { Label } from "./label"
|
|
19
|
+
|
|
20
|
+
// ─── Context ──────────────────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
type ClipboardContextValue = {
|
|
23
|
+
value: string
|
|
24
|
+
copied: boolean
|
|
25
|
+
disabled: boolean
|
|
26
|
+
inputId: string
|
|
27
|
+
copiedDuration: number
|
|
28
|
+
onCopied: (value: string) => void
|
|
29
|
+
/** True when a ClipboardLabel is mounted — the input drops its fallback aria-label. */
|
|
30
|
+
hasLabel: boolean
|
|
31
|
+
registerLabel: () => () => void
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ClipboardContext = React.createContext<ClipboardContextValue | null>(null)
|
|
35
|
+
|
|
36
|
+
function useClipboardContext(part: string): ClipboardContextValue {
|
|
37
|
+
const ctx = React.useContext(ClipboardContext)
|
|
38
|
+
if (!ctx) {
|
|
39
|
+
throw new Error(`<${part}> must be used within <Clipboard>`)
|
|
40
|
+
}
|
|
41
|
+
return ctx
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ─── Root ─────────────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
// Omit the DOM clipboard event handlers: React.ComponentProps<"div"> already
|
|
47
|
+
// types `onCopy` as ClipboardEventHandler, which conflicts with our
|
|
48
|
+
// (value: string) => void callback (same TS2430 class as correction #004,
|
|
49
|
+
// where `title?: ReactNode` collided with HTMLAttributes' `title: string`).
|
|
50
|
+
export type ClipboardProps = Omit<
|
|
51
|
+
React.ComponentProps<"div">,
|
|
52
|
+
"onCopy" | "onCopyCapture"
|
|
53
|
+
> & {
|
|
54
|
+
/** The text value shown in the input and written to the clipboard. Required. */
|
|
55
|
+
value: string
|
|
56
|
+
/** Milliseconds the copied state is held before resetting. Default 2000. */
|
|
57
|
+
copiedDuration?: number
|
|
58
|
+
/** Called after a successful clipboard write. */
|
|
59
|
+
onCopy?: (value: string) => void
|
|
60
|
+
/** Disables both the input and the trigger. */
|
|
61
|
+
disabled?: boolean
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function Clipboard({
|
|
65
|
+
value,
|
|
66
|
+
copiedDuration = 2000,
|
|
67
|
+
onCopy,
|
|
68
|
+
disabled = false,
|
|
69
|
+
className,
|
|
70
|
+
children,
|
|
71
|
+
...props
|
|
72
|
+
}: ClipboardProps) {
|
|
73
|
+
const inputId = React.useId()
|
|
74
|
+
const [copied, setCopied] = React.useState(false)
|
|
75
|
+
const [labelCount, setLabelCount] = React.useState(0)
|
|
76
|
+
const resetTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
77
|
+
|
|
78
|
+
React.useEffect(() => {
|
|
79
|
+
return () => {
|
|
80
|
+
if (resetTimer.current) clearTimeout(resetTimer.current)
|
|
81
|
+
}
|
|
82
|
+
}, [])
|
|
83
|
+
|
|
84
|
+
const onCopied = React.useCallback(
|
|
85
|
+
(copiedValue: string) => {
|
|
86
|
+
setCopied(true)
|
|
87
|
+
onCopy?.(copiedValue)
|
|
88
|
+
if (resetTimer.current) clearTimeout(resetTimer.current)
|
|
89
|
+
resetTimer.current = setTimeout(() => setCopied(false), copiedDuration)
|
|
90
|
+
},
|
|
91
|
+
[onCopy, copiedDuration]
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
const registerLabel = React.useCallback(() => {
|
|
95
|
+
setLabelCount((n) => n + 1)
|
|
96
|
+
return () => setLabelCount((n) => n - 1)
|
|
97
|
+
}, [])
|
|
98
|
+
|
|
99
|
+
const ctx = React.useMemo<ClipboardContextValue>(
|
|
100
|
+
() => ({
|
|
101
|
+
value,
|
|
102
|
+
copied,
|
|
103
|
+
disabled,
|
|
104
|
+
inputId,
|
|
105
|
+
copiedDuration,
|
|
106
|
+
onCopied,
|
|
107
|
+
hasLabel: labelCount > 0,
|
|
108
|
+
registerLabel,
|
|
109
|
+
}),
|
|
110
|
+
[value, copied, disabled, inputId, copiedDuration, onCopied, labelCount, registerLabel]
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<ClipboardContext.Provider value={ctx}>
|
|
115
|
+
<div
|
|
116
|
+
role="group"
|
|
117
|
+
data-slot="clipboard"
|
|
118
|
+
data-copied={copied ? "true" : undefined}
|
|
119
|
+
data-disabled={disabled ? "true" : undefined}
|
|
120
|
+
className={cn("flex w-full min-w-0 flex-col gap-3", className)}
|
|
121
|
+
{...props}
|
|
122
|
+
>
|
|
123
|
+
{children}
|
|
124
|
+
</div>
|
|
125
|
+
</ClipboardContext.Provider>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ─── Label ────────────────────────────────────────────────────────────────────
|
|
130
|
+
|
|
131
|
+
function ClipboardLabel({
|
|
132
|
+
className,
|
|
133
|
+
...props
|
|
134
|
+
}: React.ComponentProps<typeof Label>) {
|
|
135
|
+
const { inputId, registerLabel } = useClipboardContext("ClipboardLabel")
|
|
136
|
+
|
|
137
|
+
// Registering tells the input a visible label exists, so it drops its
|
|
138
|
+
// fallback aria-label instead of overriding the visible name.
|
|
139
|
+
React.useEffect(() => registerLabel(), [registerLabel])
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<Label
|
|
143
|
+
data-slot="clipboard-label"
|
|
144
|
+
htmlFor={inputId}
|
|
145
|
+
className={className}
|
|
146
|
+
{...props}
|
|
147
|
+
/>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ─── Control ──────────────────────────────────────────────────────────────────
|
|
152
|
+
//
|
|
153
|
+
// The row holding input + trigger. min-w-0 + overflow-hidden so the truncating
|
|
154
|
+
// input can actually shrink below its content width (correction #001), and so
|
|
155
|
+
// the pair never stretches oddly as a lone flex-column child (correction #005).
|
|
156
|
+
|
|
157
|
+
function ClipboardControl({
|
|
158
|
+
className,
|
|
159
|
+
...props
|
|
160
|
+
}: React.ComponentProps<"div">) {
|
|
161
|
+
return (
|
|
162
|
+
<div
|
|
163
|
+
data-slot="clipboard-control"
|
|
164
|
+
className={cn(
|
|
165
|
+
"flex w-full min-w-0 items-center gap-2 overflow-hidden",
|
|
166
|
+
className
|
|
167
|
+
)}
|
|
168
|
+
{...props}
|
|
169
|
+
/>
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ─── Input ────────────────────────────────────────────────────────────────────
|
|
174
|
+
//
|
|
175
|
+
// Read-only display of the copyable value. Focusable so keyboard users can
|
|
176
|
+
// select-all + ⌘C manually; select-on-focus makes that a single keystroke.
|
|
177
|
+
// h-11 matches the 44px trigger so the pair reads as one control row.
|
|
178
|
+
|
|
179
|
+
export type ClipboardInputProps = Omit<
|
|
180
|
+
React.ComponentProps<"input">,
|
|
181
|
+
"value" | "defaultValue" | "readOnly" | "type"
|
|
182
|
+
>
|
|
183
|
+
|
|
184
|
+
function ClipboardInput({
|
|
185
|
+
className,
|
|
186
|
+
onFocus,
|
|
187
|
+
"aria-label": ariaLabel,
|
|
188
|
+
disabled,
|
|
189
|
+
...props
|
|
190
|
+
}: ClipboardInputProps) {
|
|
191
|
+
const { value, copied, disabled: rootDisabled, inputId, hasLabel } =
|
|
192
|
+
useClipboardContext("ClipboardInput")
|
|
193
|
+
const isDisabled = disabled ?? rootDisabled
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<input
|
|
197
|
+
type="text"
|
|
198
|
+
readOnly
|
|
199
|
+
id={inputId}
|
|
200
|
+
value={value}
|
|
201
|
+
data-slot="clipboard-input"
|
|
202
|
+
data-copied={copied ? "true" : undefined}
|
|
203
|
+
// Full value survives truncation on hover / long-press (correction #001).
|
|
204
|
+
title={value}
|
|
205
|
+
// Only fall back to an aria-label when no visible ClipboardLabel exists —
|
|
206
|
+
// an aria-label would otherwise override the visible label's name.
|
|
207
|
+
aria-label={ariaLabel ?? (hasLabel ? undefined : "Copyable value")}
|
|
208
|
+
disabled={isDisabled}
|
|
209
|
+
onFocus={(event) => {
|
|
210
|
+
// Select-on-focus: one Tab puts the whole value in the selection so
|
|
211
|
+
// ⌘C / Ctrl+C works without the trigger.
|
|
212
|
+
event.currentTarget.select()
|
|
213
|
+
onFocus?.(event)
|
|
214
|
+
}}
|
|
215
|
+
className={cn(
|
|
216
|
+
"h-11 flex-1 truncate rounded-md border px-3 py-2 font-mono text-sm",
|
|
217
|
+
"min-w-0 bg-transparent shadow-xs outline-none dark:bg-input/30",
|
|
218
|
+
"text-foreground selection:bg-primary selection:text-primary-foreground",
|
|
219
|
+
"transition-[color,box-shadow,border-color]",
|
|
220
|
+
"focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
221
|
+
// Copied feedback on the value itself: border tints to the ring token
|
|
222
|
+
// for the copied window, then eases back.
|
|
223
|
+
copied ? "border-ring" : "border-input",
|
|
224
|
+
// Disabled uses explicit muted tokens, never opacity-50 on a text
|
|
225
|
+
// container (correction #003 — blended opacity breaks the 7:1 floor).
|
|
226
|
+
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:border-border/50 disabled:bg-muted/30 disabled:text-muted-foreground",
|
|
227
|
+
className
|
|
228
|
+
)}
|
|
229
|
+
style={{
|
|
230
|
+
transitionDuration: "var(--duration-fast)",
|
|
231
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
232
|
+
}}
|
|
233
|
+
{...props}
|
|
234
|
+
/>
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ─── Trigger ──────────────────────────────────────────────────────────────────
|
|
239
|
+
//
|
|
240
|
+
// Composes CopyButton: it owns the clipboard write (with execCommand
|
|
241
|
+
// fallback), the Copy→Check swap animation, and the aria-live announcement.
|
|
242
|
+
// Defaults to the 44px CTA size (size="xl") per the CTA size contract.
|
|
243
|
+
|
|
244
|
+
export type ClipboardTriggerProps = Omit<CopyButtonProps, "value"> & {
|
|
245
|
+
/** Rarely needed — copies a different string than the one displayed. */
|
|
246
|
+
value?: string
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function ClipboardTrigger({
|
|
250
|
+
value: valueOverride,
|
|
251
|
+
onCopy,
|
|
252
|
+
children,
|
|
253
|
+
variant = "outline",
|
|
254
|
+
size,
|
|
255
|
+
className,
|
|
256
|
+
disabled,
|
|
257
|
+
"aria-label": ariaLabel,
|
|
258
|
+
...props
|
|
259
|
+
}: ClipboardTriggerProps) {
|
|
260
|
+
const { value, disabled: rootDisabled, copiedDuration, onCopied } =
|
|
261
|
+
useClipboardContext("ClipboardTrigger")
|
|
262
|
+
|
|
263
|
+
// Default to labeled "Copy" at the 44px CTA size. Icon-only mode: pass an
|
|
264
|
+
// icon-* size and no children — CopyButton then supplies its own
|
|
265
|
+
// aria-label ("Copy" / "Copied") unless one is passed here.
|
|
266
|
+
const resolvedSize = size ?? "xl"
|
|
267
|
+
|
|
268
|
+
return (
|
|
269
|
+
<CopyButton
|
|
270
|
+
data-slot="clipboard-trigger"
|
|
271
|
+
value={valueOverride ?? value}
|
|
272
|
+
copiedDuration={copiedDuration}
|
|
273
|
+
variant={variant}
|
|
274
|
+
size={resolvedSize}
|
|
275
|
+
disabled={disabled ?? rootDisabled}
|
|
276
|
+
aria-label={ariaLabel}
|
|
277
|
+
className={cn("shrink-0", className)}
|
|
278
|
+
onCopy={(copiedValue) => {
|
|
279
|
+
onCopied(copiedValue)
|
|
280
|
+
onCopy?.(copiedValue)
|
|
281
|
+
}}
|
|
282
|
+
{...props}
|
|
283
|
+
>
|
|
284
|
+
{children === undefined && !String(resolvedSize).startsWith("icon")
|
|
285
|
+
? "Copy"
|
|
286
|
+
: children}
|
|
287
|
+
</CopyButton>
|
|
288
|
+
)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export { Clipboard, ClipboardLabel, ClipboardControl, ClipboardInput, ClipboardTrigger }
|
|
@@ -8,7 +8,11 @@ import type { VariantProps } from "class-variance-authority"
|
|
|
8
8
|
|
|
9
9
|
export type CopyButtonProps = Omit<
|
|
10
10
|
React.ComponentPropsWithoutRef<"button">,
|
|
11
|
-
"
|
|
11
|
+
// "onCopy" is also omitted: the DOM ClipboardEventHandler would otherwise
|
|
12
|
+
// intersect with our (value: string) => void callback, giving consumers a
|
|
13
|
+
// string | ClipboardEvent union parameter (correction #004 class — surfaced
|
|
14
|
+
// by ClipboardTrigger's composition, 2026-08-06).
|
|
15
|
+
"onClick" | "value" | "children" | "onCopy"
|
|
12
16
|
> &
|
|
13
17
|
VariantProps<typeof buttonVariants> & {
|
|
14
18
|
/** The text value to copy to the clipboard. Required. */
|