Component
Copy button
A button that copies a value to the clipboard and confirms it.
- #arc
- #component
- #clipboard
- #button
- #copy
- #feedback
1280px
Install
One command installs the files and packages this item needs.
TerminalTerminalTerminalbash
npx shadcn@latest add https://dsui.vercel.app/r/copy-button.jsonFiles
components/arc/copy-button/copy-button.module.csscomponents/arc/copy-button/copy-button.tsx
npm packages
motion@^14.2.0
Registry items it pulls in
Source
View as Markdowncopy-button.module.csscopy-button.module.csscopy-button.module.csscss
/* Width never changes: the label cell reserves its widest state while icon and letters crossfade inside it. Press releases on the spring curve. */
.button { display: inline-flex; width: max-content; max-width: 100%; min-height: var(--control-height-sm); align-items: center; justify-content: center; gap: var(--space-2); border: 1px solid var(--border); border-radius: var(--radius-control); padding: 0 var(--space-3); background: var(--surface); color: var(--foreground); font: inherit; font-size: var(--text-sm); font-weight: 500; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: background-color var(--duration-fast) var(--ease-standard), border-color var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard), transform var(--duration-spring) var(--ease-spring); }
.button:active:not(:disabled) { transform: scale(.97); transition-duration: var(--duration-fast), var(--duration-fast), var(--duration-fast), var(--duration-instant); transition-timing-function: var(--ease-standard); }
.iconOnly:active:not(:disabled) { transform: scale(.96); }
@media (hover: hover) and (pointer: fine) { .button:hover:not(:disabled) { background: var(--surface-muted); border-color: var(--border-strong); } .plain:hover:not(:disabled) { border-color: transparent; background: var(--surface-muted); color: var(--foreground); } }
.button:disabled { cursor: not-allowed; opacity: .5; }
/* Each icon carries its own color, so the outgoing glyph never tints on its way out. */
.icon { position: relative; display: grid; width: 16px; height: 16px; flex: none; }
.iconInner { position: absolute; inset: 0; display: grid; place-items: center; }
.iconInner[data-state="copied"] { color: var(--success); }
.iconInner[data-state="error"] { color: var(--danger); }
/* The inline padding gives blurred edge letters room before the horizontal clip; the negative margin keeps the box the same size. */
.label { display: grid; min-width: 0; margin-inline: calc(var(--space-1) * -1); padding-inline: var(--space-1); overflow-x: clip; text-align: left; white-space: nowrap; }
.measure, .glyphs { grid-area: 1 / 1; }
.measure { visibility: hidden; }
.glyphs { position: relative; display: inline-flex; justify-self: start; white-space: pre; }
.glyph { display: inline-block; }
.iconOnly { width: var(--control-height-sm); padding: 0; }
.plain { border-color: transparent; background: transparent; color: var(--text-secondary); }
.srOnly { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
@media (prefers-reduced-motion: reduce) { .button { transition: none; } .button:active:not(:disabled) { transform: none; } }
copy-button.tsxcopy-button.tsxcopy-button.tsxtsx
"use client";
import { useState } from "react";
import { AnimatePresence, motion, type TargetAndTransition, type Transition } from "motion/react";
import { useReducedMotion } from "../lib/use-reduced-motion";
import { motionTokens } from "../lib/motion-tokens";
import { useCopyFeedback } from "../lib/use-copy-feedback";
import styles from "./copy-button.module.css";
import { UiIcon } from "../theme/ui-icon";
import { playUiSound } from "../sound/play";
export interface CopyButtonProps {
value: string;
label?: string;
/** Shown after a successful copy. */
copiedLabel?: string;
/** Shown when the clipboard refused the copy. */
errorLabel?: string;
className?: string;
iconOnly?: boolean;
variant?: "outline" | "plain";
disabled?: boolean;
onCopied?: () => void;
}
/** Copy feedback is deliberately unhurried: a slow, almost critically damped spring and long, soft crossfades read as calm, never busy.
* The same motion plays in reverse when the confirmation hands back to idle, so nothing ever snaps. */
const settle = { type: "spring", visualDuration: .5, bounce: .06 } as const;
const enter = { duration: motionTokens.duration.moderate, ease: [...motionTokens.ease.enter] } as const;
const leave = { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.standard] } as const;
const instant = { duration: motionTokens.duration.instant } as const;
const soft = `blur(${motionTokens.blur.soft}px)`;
const rest: TargetAndTransition = { opacity: 1, y: 0, scale: 1, filter: "blur(0px)" };
const fadeIn: TargetAndTransition = { ...rest, opacity: 0 };
const fadeOut: TargetAndTransition = { opacity: 0, transition: instant };
/** Icons trade places in one soft breath: the old glyph shrinks into a blur while the new one grows out of it on the slow spring. */
const iconIn: TargetAndTransition = { opacity: 0, scale: .6, filter: soft };
const iconOut: TargetAndTransition = { opacity: 0, scale: .6, filter: soft, transition: { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.standard] } };
const iconEnter: Transition = { scale: settle, opacity: { ...enter, delay: .03 }, filter: { ...enter, delay: .03 } };
/** Letters rise about .3em out of a soft blur; the outgoing ones lift away a little faster. */
const glyphIn: TargetAndTransition = { opacity: 0, y: 4, filter: soft };
const glyphOut: TargetAndTransition = { opacity: 0, y: -3, filter: soft, transition: leave };
/** The success tick draws itself from its short stroke, the way a hand would write it: quick to start, then easing into place while the icon settles. */
function DrawnCheck({ reduced }: { reduced: boolean }) {
return <svg width={16} height={16} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.75} strokeLinecap="round" strokeLinejoin="round">
<motion.path d="M4 12l5 5L20 6" initial={reduced ? false : { pathLength: 0, opacity: 0 }} animate={{ pathLength: 1, opacity: 1 }} transition={{ pathLength: { duration: motionTokens.duration.considered, ease: [...motionTokens.ease.standard], delay: motionTokens.delay.micro }, opacity: { duration: .01, delay: motionTokens.delay.micro } }} />
</svg>;
}
type Glyph = { id: string; char: string; order: number };
const toGlyphs = (chars: string[], seq: number): Glyph[] => chars.map((char, order) => ({ id: `${seq}:${order}`, char, order }));
/** Shared leading and trailing characters keep their identity, so "복사하기" to "복사했어요" only replaces the changed letters. */
function useGlyphs(text: string) {
const [state, setState] = useState(() => ({ text, seq: 0, glyphs: toGlyphs([...text], 0) }));
if (state.text === text) return state.glyphs;
const prev = [...state.text], next = [...text];
let start = 0, end = 0;
while (start < prev.length && start < next.length && prev[start] === next[start]) start++;
while (end < prev.length - start && end < next.length - start && prev[prev.length - 1 - end] === next[next.length - 1 - end]) end++;
if (start < 2) start = 0;
if (end < 2) end = 0;
const seq = state.seq + 1;
const glyphs = [...state.glyphs.slice(0, start), ...toGlyphs(next.slice(start, next.length - end), seq), ...state.glyphs.slice(state.glyphs.length - end)];
setState({ text, seq, glyphs });
return glyphs;
}
/**
* The label cell already reserves its widest state, so the row stays put and only the letters move. It is a layout root:
* the letters measure their glide inside the label only, so when the button itself moves (a page entering, a panel
* sliding, a list reflowing) they ride along instead of flying in from where the button used to be.
*/
function MorphText({ text, reduced }: { text: string; reduced: boolean }) {
const glyphs = useGlyphs(text);
return <motion.span className={styles.glyphs} layoutRoot>
<AnimatePresence mode="popLayout" initial={false}>
{glyphs.map(glyph => <motion.span key={glyph.id} className={styles.glyph} layout={reduced ? false : "position"} layoutDependency={text} initial={reduced ? fadeIn : glyphIn} animate={rest} exit={reduced ? fadeOut : glyphOut} transition={reduced ? instant : { ...enter, delay: Math.min(glyph.order * .02, .12), layout: settle }}>{glyph.char}</motion.span>)}
</AnimatePresence>
</motion.span>;
}
export function CopyButton({ value, label = "복사하기", copiedLabel = "복사했어요", errorLabel = "복사하지 못했어요", className, iconOnly = false, variant = "outline", disabled, onCopied }: CopyButtonProps) {
const { state, copy } = useCopyFeedback();
const reduced = useReducedMotion() ?? false;
const text = state === "copied" ? copiedLabel : state === "error" ? errorLabel : label;
async function handleCopy() {
const copied = await copy(value);
playUiSound(copied ? "success" : "error");
if (copied) onCopied?.();
}
return <><button
type="button"
className={[styles.button, iconOnly && styles.iconOnly, variant === "plain" && styles.plain, className].filter(Boolean).join(" ")}
onClick={() => void handleCopy()}
aria-label={label}
data-copy-state={state}
disabled={disabled}
>
<span className={styles.icon} aria-hidden="true">
<AnimatePresence initial={false}>
<motion.span key={state} className={styles.iconInner} data-state={state} initial={reduced ? fadeIn : iconIn} animate={rest} exit={reduced ? fadeOut : iconOut} transition={reduced ? instant : iconEnter}>
{state === "copied" ? <DrawnCheck reduced={reduced} /> : state === "error" ? <UiIcon iconRole="alert" size={16} strokeWidth={1.75} /> : <UiIcon iconRole="copy" size={16} strokeWidth={1.75} />}
</motion.span>
</AnimatePresence>
</span>
{!iconOnly && <span className={styles.label} aria-hidden="true">
<span className={styles.measure}>{label}</span><span className={styles.measure}>{copiedLabel}</span><span className={styles.measure}>{errorLabel}</span>
<MorphText text={text} reduced={reduced} />
</span>}
</button><span className={styles.srOnly} role="status" aria-live="polite">{state === "idle" ? "" : state === "error" ? `${label}: ${errorLabel}` : `${label}: ${copiedLabel}`}</span></>;
}
Used by
- Changelog feedBlock
A monthly changelog feed that separates new features, improvements, and fixes.
- Code blockComponent
A code snippet with an optional filename and line-limited collapse.
- Contact sectionBlock
A contact section offering a validated form, channel list, or offices with live local time.
- Hero sectionBlock
A full-screen hero in centered, split, and minimal variants with animated visuals.