@behavioral-agentic-design-system/core 0.14.0 → 0.16.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 +8 -2
- package/scripts/a11y-lint.cjs +229 -0
- package/scripts/build-tokens.cjs +3 -3
- package/scripts/check-stories.mjs +109 -0
- package/scripts/fix-stories.mjs +785 -0
- package/scripts/generate-stories.cjs +101 -0
- package/scripts/oklch-to-hex.cjs +156 -0
- package/scripts/wcag-audit.cjs +444 -0
- package/src/app/preview/components/page.tsx +1118 -7
- package/src/components/dev-identity-badge.tsx +1 -1
- package/src/components/ui/button-group.tsx +57 -0
- package/src/components/ui/calendar.tsx +4 -0
- package/src/components/ui/circular-progress.tsx +120 -0
- package/src/components/ui/color-picker.tsx +219 -0
- package/src/components/ui/copy-button.tsx +114 -0
- package/src/components/ui/date-range-picker.tsx +174 -0
- package/src/components/ui/empty-state.tsx +115 -0
- package/src/components/ui/index.ts +3 -0
- package/src/components/ui/indicator.tsx +144 -0
- package/src/components/ui/list.tsx +70 -0
- package/src/components/ui/multi-select.tsx +196 -0
- package/src/components/ui/password-input.tsx +150 -0
- package/src/components/ui/time-picker.tsx +363 -0
- package/src/components/ui/tree-view.tsx +405 -0
- package/src/data/docs-index.ts +1 -0
- package/src/hooks/use-global-primary-color.ts +1 -1
- package/src/styles.css +2 -2
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import { cn } from "../../lib/utils"
|
|
4
|
+
|
|
5
|
+
// ─── ButtonGroup ──────────────────────────────────────────────────────────────
|
|
6
|
+
// Groups adjacent Button primitives with shared borders and no interior radii.
|
|
7
|
+
// Targets [data-slot="button"] so it composes with Button's asChild mode too.
|
|
8
|
+
|
|
9
|
+
export interface ButtonGroupProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
10
|
+
/** "horizontal" (default) | "vertical" */
|
|
11
|
+
orientation?: "horizontal" | "vertical"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function ButtonGroup({
|
|
15
|
+
className,
|
|
16
|
+
orientation = "horizontal",
|
|
17
|
+
children,
|
|
18
|
+
...props
|
|
19
|
+
}: ButtonGroupProps) {
|
|
20
|
+
return (
|
|
21
|
+
<div
|
|
22
|
+
role="group"
|
|
23
|
+
data-slot="button-group"
|
|
24
|
+
data-orientation={orientation}
|
|
25
|
+
className={cn(
|
|
26
|
+
"inline-flex",
|
|
27
|
+
orientation === "vertical" ? "flex-col" : "flex-row",
|
|
28
|
+
// Strip radii from every direct Button child
|
|
29
|
+
"[&>[data-slot=button]]:rounded-none",
|
|
30
|
+
orientation === "horizontal"
|
|
31
|
+
? [
|
|
32
|
+
// Restore outer corners
|
|
33
|
+
"[&>[data-slot=button]:first-child]:rounded-l-md",
|
|
34
|
+
"[&>[data-slot=button]:last-child]:rounded-r-md",
|
|
35
|
+
// Collapse shared border between siblings
|
|
36
|
+
"[&>[data-slot=button]:not(:first-child)]:-ml-px",
|
|
37
|
+
// Lift hovered / focused button so its border isn't clipped by sibling
|
|
38
|
+
"[&>[data-slot=button]:hover]:z-10",
|
|
39
|
+
"[&>[data-slot=button]:focus-visible]:z-10",
|
|
40
|
+
]
|
|
41
|
+
: [
|
|
42
|
+
"[&>[data-slot=button]:first-child]:rounded-t-md",
|
|
43
|
+
"[&>[data-slot=button]:last-child]:rounded-b-md",
|
|
44
|
+
"[&>[data-slot=button]:not(:first-child)]:-mt-px",
|
|
45
|
+
"[&>[data-slot=button]:hover]:z-10",
|
|
46
|
+
"[&>[data-slot=button]:focus-visible]:z-10",
|
|
47
|
+
],
|
|
48
|
+
className
|
|
49
|
+
)}
|
|
50
|
+
{...props}
|
|
51
|
+
>
|
|
52
|
+
{children}
|
|
53
|
+
</div>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { ButtonGroup }
|
|
@@ -205,6 +205,10 @@ function CalendarDayButton({
|
|
|
205
205
|
data-range-end={modifiers.range_end}
|
|
206
206
|
data-range-middle={modifiers.range_middle}
|
|
207
207
|
className={cn(
|
|
208
|
+
// shadcn calendar day-cell default weight; the nested [&>span] day-number
|
|
209
|
+
// is text-xs. Bumping day numbers to font-medium is a visual change to this
|
|
210
|
+
// shared primitive — deferred to a design decision, not a mechanical fix.
|
|
211
|
+
// allow-font-normal-small
|
|
208
212
|
"flex aspect-square size-auto w-full min-w-(--cell-size) flex-col gap-1 leading-none font-normal group-data-[focused=true]/day:relative group-data-[focused=true]/day:z-10 group-data-[focused=true]/day:border-ring group-data-[focused=true]/day:ring-[3px] group-data-[focused=true]/day:ring-ring/50 data-[range-end=true]:rounded-md data-[range-end=true]:rounded-r-md data-[range-end=true]:bg-primary data-[range-end=true]:text-primary-foreground data-[range-middle=true]:rounded-none data-[range-middle=true]:bg-accent data-[range-middle=true]:text-foreground data-[range-start=true]:rounded-md data-[range-start=true]:rounded-l-md data-[range-start=true]:bg-primary data-[range-start=true]:text-primary-foreground data-[selected-single=true]:bg-primary data-[selected-single=true]:text-primary-foreground dark:hover:text-accent-foreground [&>span]:text-xs [&>span]:opacity-70",
|
|
209
213
|
defaultClassNames.day,
|
|
210
214
|
className
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
const SIZES = {
|
|
8
|
+
sm: { size: 32, strokeWidth: 3 },
|
|
9
|
+
default: { size: 48, strokeWidth: 4 },
|
|
10
|
+
lg: { size: 64, strokeWidth: 5 },
|
|
11
|
+
} as const
|
|
12
|
+
|
|
13
|
+
type SizeKey = keyof typeof SIZES
|
|
14
|
+
|
|
15
|
+
const circularProgressVariants = cva(
|
|
16
|
+
"inline-flex items-center justify-center relative",
|
|
17
|
+
{
|
|
18
|
+
variants: {
|
|
19
|
+
variant: {
|
|
20
|
+
default: "text-primary",
|
|
21
|
+
muted: "text-muted-foreground",
|
|
22
|
+
success: "text-[hsl(var(--success))]",
|
|
23
|
+
warning: "text-[hsl(var(--warning))]",
|
|
24
|
+
destructive: "text-destructive",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
defaultVariants: {
|
|
28
|
+
variant: "default",
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
export interface CircularProgressProps
|
|
34
|
+
extends React.HTMLAttributes<HTMLDivElement>,
|
|
35
|
+
VariantProps<typeof circularProgressVariants> {
|
|
36
|
+
/** 0–100. Omit for indeterminate (spinning) mode. */
|
|
37
|
+
value?: number
|
|
38
|
+
/** Required: describes what is progressing for screen readers */
|
|
39
|
+
label: string
|
|
40
|
+
size?: SizeKey
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function CircularProgress({
|
|
44
|
+
value,
|
|
45
|
+
label,
|
|
46
|
+
variant = "default",
|
|
47
|
+
size = "default",
|
|
48
|
+
className,
|
|
49
|
+
children,
|
|
50
|
+
...props
|
|
51
|
+
}: CircularProgressProps) {
|
|
52
|
+
const determinate = value !== undefined
|
|
53
|
+
const clampedValue = determinate ? Math.min(100, Math.max(0, value!)) : 0
|
|
54
|
+
const { size: svgSize, strokeWidth } = SIZES[size ?? "default"]
|
|
55
|
+
const cx = svgSize / 2
|
|
56
|
+
const cy = svgSize / 2
|
|
57
|
+
// Radius keeps entire stroke inside the viewBox
|
|
58
|
+
const radius = svgSize / 2 - strokeWidth
|
|
59
|
+
const circumference = 2 * Math.PI * radius
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<div
|
|
63
|
+
role="progressbar"
|
|
64
|
+
aria-valuenow={determinate ? clampedValue : undefined}
|
|
65
|
+
aria-valuemin={0}
|
|
66
|
+
aria-valuemax={100}
|
|
67
|
+
aria-label={label}
|
|
68
|
+
data-slot="circular-progress"
|
|
69
|
+
data-size={size}
|
|
70
|
+
data-variant={variant}
|
|
71
|
+
className={cn(circularProgressVariants({ variant }), className)}
|
|
72
|
+
{...props}
|
|
73
|
+
>
|
|
74
|
+
<svg
|
|
75
|
+
width={svgSize}
|
|
76
|
+
height={svgSize}
|
|
77
|
+
viewBox={`0 0 ${svgSize} ${svgSize}`}
|
|
78
|
+
fill="none"
|
|
79
|
+
className={cn(
|
|
80
|
+
"-rotate-90",
|
|
81
|
+
!determinate && "animate-spin"
|
|
82
|
+
)}
|
|
83
|
+
aria-hidden="true"
|
|
84
|
+
>
|
|
85
|
+
{/* Track */}
|
|
86
|
+
<circle
|
|
87
|
+
cx={cx}
|
|
88
|
+
cy={cy}
|
|
89
|
+
r={radius}
|
|
90
|
+
strokeWidth={strokeWidth}
|
|
91
|
+
className="stroke-current opacity-20"
|
|
92
|
+
/>
|
|
93
|
+
{/* Fill */}
|
|
94
|
+
<circle
|
|
95
|
+
cx={cx}
|
|
96
|
+
cy={cy}
|
|
97
|
+
r={radius}
|
|
98
|
+
strokeWidth={strokeWidth}
|
|
99
|
+
strokeLinecap="round"
|
|
100
|
+
className="stroke-current transition-[stroke-dashoffset]"
|
|
101
|
+
style={{
|
|
102
|
+
strokeDasharray: determinate ? circumference : circumference * 0.75,
|
|
103
|
+
strokeDashoffset: determinate
|
|
104
|
+
? circumference * (1 - clampedValue / 100)
|
|
105
|
+
: 0,
|
|
106
|
+
transitionDuration: "var(--duration-base)",
|
|
107
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
108
|
+
}}
|
|
109
|
+
/>
|
|
110
|
+
</svg>
|
|
111
|
+
{children !== undefined && (
|
|
112
|
+
<span className="absolute" aria-hidden="true">
|
|
113
|
+
{children}
|
|
114
|
+
</span>
|
|
115
|
+
)}
|
|
116
|
+
</div>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { CircularProgress, circularProgressVariants }
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Check } from "lucide-react";
|
|
5
|
+
import { cn } from "../../lib/utils";
|
|
6
|
+
|
|
7
|
+
/* -------------------------------------------------------------------------- */
|
|
8
|
+
/* Helpers */
|
|
9
|
+
/* -------------------------------------------------------------------------- */
|
|
10
|
+
|
|
11
|
+
function luminance(hex: string): number {
|
|
12
|
+
const h = hex.replace("#", "");
|
|
13
|
+
const full = h.length === 3
|
|
14
|
+
? h.split("").map((c) => parseInt(c + c, 16))
|
|
15
|
+
: [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];
|
|
16
|
+
const [r, g, b] = full.map((v) => {
|
|
17
|
+
const s = v / 255;
|
|
18
|
+
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
19
|
+
});
|
|
20
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function contrastColor(hex: string): string {
|
|
24
|
+
if (!/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(hex)) return "#ffffff";
|
|
25
|
+
return luminance(hex) > 0.179 ? "#000000" : "#ffffff";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isValidHex(v: string): boolean {
|
|
29
|
+
return /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(v);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* -------------------------------------------------------------------------- */
|
|
33
|
+
/* ColorPicker */
|
|
34
|
+
/* -------------------------------------------------------------------------- */
|
|
35
|
+
|
|
36
|
+
export interface ColorPickerProps {
|
|
37
|
+
/** Controlled selected color (hex string, e.g. "#ff0000") */
|
|
38
|
+
value?: string;
|
|
39
|
+
/** Uncontrolled default */
|
|
40
|
+
defaultValue?: string;
|
|
41
|
+
onValueChange?: (value: string) => void;
|
|
42
|
+
/** Preset swatch colors (hex strings) */
|
|
43
|
+
swatches?: string[];
|
|
44
|
+
/** Show a hex text input for custom colors */
|
|
45
|
+
allowCustom?: boolean;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
/** Accessible label for the radiogroup */
|
|
48
|
+
"aria-label"?: string;
|
|
49
|
+
className?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function ColorPicker({
|
|
53
|
+
value: valueProp,
|
|
54
|
+
defaultValue,
|
|
55
|
+
onValueChange,
|
|
56
|
+
swatches = [],
|
|
57
|
+
allowCustom = false,
|
|
58
|
+
disabled = false,
|
|
59
|
+
"aria-label": ariaLabel = "Color picker",
|
|
60
|
+
className,
|
|
61
|
+
}: ColorPickerProps) {
|
|
62
|
+
const isControlled = valueProp !== undefined;
|
|
63
|
+
const [valueState, setValueState] = React.useState<string>(defaultValue ?? "");
|
|
64
|
+
const value = isControlled ? (valueProp ?? "") : valueState;
|
|
65
|
+
|
|
66
|
+
const [customInput, setCustomInput] = React.useState<string>(() =>
|
|
67
|
+
value && !swatches.includes(value) ? value : ""
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const swatchRefs = React.useRef<(HTMLButtonElement | null)[]>([]);
|
|
71
|
+
|
|
72
|
+
const commit = React.useCallback(
|
|
73
|
+
(color: string) => {
|
|
74
|
+
if (disabled) return;
|
|
75
|
+
if (!isControlled) setValueState(color);
|
|
76
|
+
onValueChange?.(color);
|
|
77
|
+
},
|
|
78
|
+
[disabled, isControlled, onValueChange]
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const handleKeyDown = React.useCallback(
|
|
82
|
+
(e: React.KeyboardEvent<HTMLButtonElement>, index: number) => {
|
|
83
|
+
const btns = swatchRefs.current.filter((b): b is HTMLButtonElement => !!b);
|
|
84
|
+
let next = index;
|
|
85
|
+
switch (e.key) {
|
|
86
|
+
case "ArrowRight":
|
|
87
|
+
case "ArrowDown":
|
|
88
|
+
e.preventDefault();
|
|
89
|
+
next = (index + 1) % btns.length;
|
|
90
|
+
break;
|
|
91
|
+
case "ArrowLeft":
|
|
92
|
+
case "ArrowUp":
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
next = (index - 1 + btns.length) % btns.length;
|
|
95
|
+
break;
|
|
96
|
+
case "Home":
|
|
97
|
+
e.preventDefault();
|
|
98
|
+
next = 0;
|
|
99
|
+
break;
|
|
100
|
+
case "End":
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
next = btns.length - 1;
|
|
103
|
+
break;
|
|
104
|
+
default:
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
btns[next]?.focus();
|
|
108
|
+
},
|
|
109
|
+
[]
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const handleCustomInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
113
|
+
const v = e.target.value;
|
|
114
|
+
setCustomInput(v);
|
|
115
|
+
if (isValidHex(v)) commit(v);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const selectedSwatchIdx = swatches.indexOf(value);
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<div className={cn("space-y-2", className)}>
|
|
122
|
+
{swatches.length > 0 && (
|
|
123
|
+
<div
|
|
124
|
+
role="radiogroup"
|
|
125
|
+
aria-label={ariaLabel}
|
|
126
|
+
aria-disabled={disabled || undefined}
|
|
127
|
+
className="flex flex-wrap gap-1.5"
|
|
128
|
+
>
|
|
129
|
+
{swatches.map((swatch, i) => {
|
|
130
|
+
const isSelected = value === swatch;
|
|
131
|
+
const isRovingFocus =
|
|
132
|
+
selectedSwatchIdx >= 0 ? isSelected : i === 0;
|
|
133
|
+
return (
|
|
134
|
+
<button
|
|
135
|
+
key={swatch}
|
|
136
|
+
ref={(el) => {
|
|
137
|
+
swatchRefs.current[i] = el;
|
|
138
|
+
}}
|
|
139
|
+
type="button"
|
|
140
|
+
role="radio"
|
|
141
|
+
aria-checked={isSelected}
|
|
142
|
+
aria-label={swatch}
|
|
143
|
+
tabIndex={isRovingFocus ? 0 : -1}
|
|
144
|
+
disabled={disabled}
|
|
145
|
+
onClick={() => {
|
|
146
|
+
commit(swatch);
|
|
147
|
+
setCustomInput("");
|
|
148
|
+
}}
|
|
149
|
+
onKeyDown={(e) => handleKeyDown(e, i)}
|
|
150
|
+
className={cn(
|
|
151
|
+
"relative size-7 shrink-0 rounded-md border-2 overflow-hidden",
|
|
152
|
+
"focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:ring-offset-1",
|
|
153
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
154
|
+
"transition-[border-color]",
|
|
155
|
+
isSelected
|
|
156
|
+
? "border-foreground"
|
|
157
|
+
: "border-transparent hover:border-foreground/30"
|
|
158
|
+
)}
|
|
159
|
+
style={{
|
|
160
|
+
backgroundColor: swatch,
|
|
161
|
+
transitionDuration: "var(--duration-fast)",
|
|
162
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
163
|
+
}}
|
|
164
|
+
>
|
|
165
|
+
{isSelected && (
|
|
166
|
+
<Check
|
|
167
|
+
className="absolute inset-0 m-auto size-3.5"
|
|
168
|
+
style={{ color: contrastColor(swatch) }}
|
|
169
|
+
strokeWidth={1.5}
|
|
170
|
+
aria-hidden
|
|
171
|
+
/>
|
|
172
|
+
)}
|
|
173
|
+
</button>
|
|
174
|
+
);
|
|
175
|
+
})}
|
|
176
|
+
</div>
|
|
177
|
+
)}
|
|
178
|
+
|
|
179
|
+
{allowCustom && (
|
|
180
|
+
<div className="flex items-center gap-2">
|
|
181
|
+
<div
|
|
182
|
+
role="presentation"
|
|
183
|
+
className={cn(
|
|
184
|
+
"size-7 shrink-0 rounded-md border border-input",
|
|
185
|
+
"transition-colors"
|
|
186
|
+
)}
|
|
187
|
+
style={{
|
|
188
|
+
backgroundColor: value || "transparent",
|
|
189
|
+
transitionDuration: "var(--duration-fast)",
|
|
190
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
191
|
+
}}
|
|
192
|
+
/>
|
|
193
|
+
<input
|
|
194
|
+
type="text"
|
|
195
|
+
value={customInput}
|
|
196
|
+
onChange={handleCustomInput}
|
|
197
|
+
disabled={disabled}
|
|
198
|
+
placeholder="#000000"
|
|
199
|
+
aria-label="Custom hex color"
|
|
200
|
+
spellCheck={false}
|
|
201
|
+
maxLength={7}
|
|
202
|
+
className={cn(
|
|
203
|
+
"h-8 w-28 rounded-md border border-input bg-background px-2 py-1",
|
|
204
|
+
"font-mono text-sm text-foreground",
|
|
205
|
+
"placeholder:text-muted-foreground",
|
|
206
|
+
"focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
207
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
208
|
+
"transition-[border-color,box-shadow]"
|
|
209
|
+
)}
|
|
210
|
+
style={{
|
|
211
|
+
transitionDuration: "var(--duration-fast)",
|
|
212
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
213
|
+
}}
|
|
214
|
+
/>
|
|
215
|
+
</div>
|
|
216
|
+
)}
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { CheckIcon, CopyIcon } from "lucide-react"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
import { Button, buttonVariants } from "./button"
|
|
7
|
+
import type { VariantProps } from "class-variance-authority"
|
|
8
|
+
|
|
9
|
+
export type CopyButtonProps = Omit<
|
|
10
|
+
React.ComponentPropsWithoutRef<"button">,
|
|
11
|
+
"onClick" | "value" | "children"
|
|
12
|
+
> &
|
|
13
|
+
VariantProps<typeof buttonVariants> & {
|
|
14
|
+
/** The text value to copy to the clipboard. Required. */
|
|
15
|
+
value: string
|
|
16
|
+
/** Milliseconds the "copied" state is held before resetting. Default 2000. */
|
|
17
|
+
copiedDuration?: number
|
|
18
|
+
/** Called after a successful clipboard write. */
|
|
19
|
+
onCopy?: (value: string) => void
|
|
20
|
+
/** Label shown at rest. Omit for icon-only mode. */
|
|
21
|
+
children?: React.ReactNode
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function CopyButton({
|
|
25
|
+
value,
|
|
26
|
+
copiedDuration = 2000,
|
|
27
|
+
onCopy,
|
|
28
|
+
children,
|
|
29
|
+
variant = "outline",
|
|
30
|
+
size = "default",
|
|
31
|
+
className,
|
|
32
|
+
disabled,
|
|
33
|
+
"aria-label": ariaLabel,
|
|
34
|
+
...props
|
|
35
|
+
}: CopyButtonProps) {
|
|
36
|
+
const [copied, setCopied] = React.useState(false)
|
|
37
|
+
|
|
38
|
+
const handleCopy = React.useCallback(async () => {
|
|
39
|
+
if (copied) return
|
|
40
|
+
try {
|
|
41
|
+
await navigator.clipboard.writeText(value)
|
|
42
|
+
} catch {
|
|
43
|
+
// Fallback for environments without clipboard API
|
|
44
|
+
const el = document.createElement("textarea")
|
|
45
|
+
el.value = value
|
|
46
|
+
el.style.position = "fixed"
|
|
47
|
+
el.style.opacity = "0"
|
|
48
|
+
document.body.appendChild(el)
|
|
49
|
+
el.focus()
|
|
50
|
+
el.select()
|
|
51
|
+
document.execCommand("copy")
|
|
52
|
+
document.body.removeChild(el)
|
|
53
|
+
}
|
|
54
|
+
setCopied(true)
|
|
55
|
+
onCopy?.(value)
|
|
56
|
+
const timer = setTimeout(() => setCopied(false), copiedDuration)
|
|
57
|
+
return () => clearTimeout(timer)
|
|
58
|
+
}, [value, copied, copiedDuration, onCopy])
|
|
59
|
+
|
|
60
|
+
const iconOnly = children === undefined
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<>
|
|
64
|
+
<Button
|
|
65
|
+
data-slot="copy-button"
|
|
66
|
+
data-copied={copied ? "true" : undefined}
|
|
67
|
+
variant={variant}
|
|
68
|
+
size={size}
|
|
69
|
+
disabled={disabled || copied}
|
|
70
|
+
onClick={handleCopy}
|
|
71
|
+
aria-label={ariaLabel ?? (iconOnly ? (copied ? "Copied" : "Copy") : undefined)}
|
|
72
|
+
className={cn("relative overflow-hidden", className)}
|
|
73
|
+
{...props}
|
|
74
|
+
>
|
|
75
|
+
{/* In labeled mode this span provides the button's accessible name — not aria-hidden */}
|
|
76
|
+
<span
|
|
77
|
+
className={cn(
|
|
78
|
+
"inline-flex items-center gap-2 transition-all",
|
|
79
|
+
copied ? "opacity-0 scale-75" : "opacity-100 scale-100"
|
|
80
|
+
)}
|
|
81
|
+
style={{
|
|
82
|
+
transitionDuration: "var(--duration-fast)",
|
|
83
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
84
|
+
}}
|
|
85
|
+
aria-hidden={iconOnly ? "true" : undefined}
|
|
86
|
+
>
|
|
87
|
+
<CopyIcon strokeWidth={1.5} aria-hidden="true" />
|
|
88
|
+
{!iconOnly && (children ?? "Copy")}
|
|
89
|
+
</span>
|
|
90
|
+
{/* Always hidden — state announced by the aria-live region */}
|
|
91
|
+
<span
|
|
92
|
+
className={cn(
|
|
93
|
+
"absolute inset-0 inline-flex items-center justify-center gap-2 transition-all",
|
|
94
|
+
copied ? "opacity-100 scale-100" : "opacity-0 scale-75"
|
|
95
|
+
)}
|
|
96
|
+
style={{
|
|
97
|
+
transitionDuration: "var(--duration-fast)",
|
|
98
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
99
|
+
}}
|
|
100
|
+
aria-hidden="true"
|
|
101
|
+
>
|
|
102
|
+
<CheckIcon strokeWidth={1.5} />
|
|
103
|
+
{!iconOnly && "Copied!"}
|
|
104
|
+
</span>
|
|
105
|
+
</Button>
|
|
106
|
+
{/* Screen-reader live region — announces state change without focus move */}
|
|
107
|
+
<span className="sr-only" aria-live="polite" aria-atomic="true">
|
|
108
|
+
{copied ? "Copied to clipboard" : ""}
|
|
109
|
+
</span>
|
|
110
|
+
</>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { CopyButton }
|