"use client";
import * as React from "react";
import {CheckIcon,CopyIcon} from "lucide-react";
import { cn } from "@/lib/utils";
type SizeVariant = "sm" | "default" | "lg";
interface CopyButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
value?: string;
size?: SizeVariant;
}
const sizeMap: Record<SizeVariant, { button: string; icon: number }> = {
sm: { button: "h-8 w-8", icon: 14 },
default: { button: "h-9 w-9", icon: 16 },
lg: { button: "h-12 w-12", icon: 20 },
};
const CopyButton = React.forwardRef<HTMLButtonElement, CopyButtonProps>(
(
{
value,
size = "default",
className,
onClick,
...props
},
ref,
) => {
const [copied, setCopied] = React.useState<boolean>(false);
const handleCopy = (event: React.MouseEvent<HTMLButtonElement>) => {
if (value) {
navigator.clipboard.writeText(value).catch(() => {});
}
setCopied(true);
setTimeout(() => setCopied(false), 1500);
onClick?.(event);
};
const { button: buttonSize, icon: iconSize } = sizeMap[size];
return (
<button
ref={ref}
type="button"
onClick={handleCopy}
aria-label={copied ? "Copied" : "Copy to clipboard"}
disabled={copied}
className={cn(
"relative cursor-pointer active:scale-[0.97] transition-all ease-out duration-200 inline-flex items-center justify-center rounded-md text-neutral-900 disabled:pointer-events-none disabled:opacity-100 dark:text-neutral-50",
buttonSize,
className,
)}
{...props}
>
<div
className={cn(
"transition-all duration-200",
copied
? "scale-100 opacity-100 blur-none"
: "scale-70 opacity-0 blur-[2px]",
)}
>
<CheckIcon
size={iconSize}
strokeWidth={2}
aria-hidden="true"
/>
</div>
<div
className={cn(
"absolute transition-all duration-200",
copied
? "scale-0 opacity-0 blur-[2px]"
: "scale-100 opacity-100 blur-none",
)}
>
<CopyIcon size={iconSize} strokeWidth={2} aria-hidden="true" />
</div>
</button>
);
},
);
CopyButton.displayName = "CopyButton";
export { CopyButton };
export type { CopyButtonProps };
Copy Button
By tom
Try the interaction, copy the React source files, or download the complete example.
About Copy Button
Copy Button is an animated React and Tailwind CSS button by tom. Try the interaction, copy the React source, or download the example with creator credit.
- Effects and categories
- Hover effects, Animated
- Using this example
- Copy a TSX file or download the original source and standalone demo. Test keyboard focus, motion and contrast in your own project.
Usage, Tailwind and licensing questions
Read the source files
docs/copy-button/demo.tsx
import { CopyButton } from "@/registry/spell-ui/copy-button";
export function Demo() {
return <CopyButton value="Spell 🪄" />;
}
registry/spell-ui/copy-button.tsx
"use client";
import * as React from "react";
import {CheckIcon,CopyIcon} from "lucide-react";
import { cn } from "@/lib/utils";
type SizeVariant = "sm" | "default" | "lg";
interface CopyButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
value?: string;
size?: SizeVariant;
}
const sizeMap: Record<SizeVariant, { button: string; icon: number }> = {
sm: { button: "h-8 w-8", icon: 14 },
default: { button: "h-9 w-9", icon: 16 },
lg: { button: "h-12 w-12", icon: 20 },
};
const CopyButton = React.forwardRef<HTMLButtonElement, CopyButtonProps>(
(
{
value,
size = "default",
className,
onClick,
...props
},
ref,
) => {
const [copied, setCopied] = React.useState<boolean>(false);
const handleCopy = (event: React.MouseEvent<HTMLButtonElement>) => {
if (value) {
navigator.clipboard.writeText(value).catch(() => {});
}
setCopied(true);
setTimeout(() => setCopied(false), 1500);
onClick?.(event);
};
const { button: buttonSize, icon: iconSize } = sizeMap[size];
return (
<button
ref={ref}
type="button"
onClick={handleCopy}
aria-label={copied ? "Copied" : "Copy to clipboard"}
disabled={copied}
className={cn(
"relative cursor-pointer active:scale-[0.97] transition-all ease-out duration-200 inline-flex items-center justify-center rounded-md text-neutral-900 disabled:pointer-events-none disabled:opacity-100 dark:text-neutral-50",
buttonSize,
className,
)}
{...props}
>
<div
className={cn(
"transition-all duration-200",
copied
? "scale-100 opacity-100 blur-none"
: "scale-70 opacity-0 blur-[2px]",
)}
>
<CheckIcon
size={iconSize}
strokeWidth={2}
aria-hidden="true"
/>
</div>
<div
className={cn(
"absolute transition-all duration-200",
copied
? "scale-0 opacity-0 blur-[2px]"
: "scale-100 opacity-100 blur-none",
)}
>
<CopyIcon size={iconSize} strokeWidth={2} aria-hidden="true" />
</div>
</button>
);
},
);
CopyButton.displayName = "CopyButton";
export { CopyButton };
export type { CopyButtonProps };
lib/utils.ts
import type { Metadata } from "next";
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { siteConfig } from "@/lib/config";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function absoluteUrl(path: string) {
const base = process.env.NEXT_PUBLIC_APP_URL ?? siteConfig.url;
return `${base.replace(/\/$/, "")}${path.startsWith("/") ? path : `/${path}`}`;
}
export function buildOgUrl(params?: { title?: string; description?: string }) {
const url = new URL(absoluteUrl("/og"));
if (params?.title) url.searchParams.set("title", params.title);
if (params?.description) url.searchParams.set("description", params.description);
return url.toString();
}
export function constructMetadata({
title = siteConfig.name,
description = siteConfig.description,
image = absoluteUrl("/og"),
...props
}: {
title?: string;
description?: string;
image?: string;
} & Partial<Metadata> = {}): Metadata {
return {
title,
description,
keywords: siteConfig.keywords,
openGraph: {
title,
description,
type: "website",
url: siteConfig.url,
siteName: siteConfig.name,
images: [
{
url: image,
width: 1200,
height: 628,
alt: title,
},
],
},
twitter: {
card: "summary_large_image",
title,
description,
images: [image],
creator: siteConfig.links.tom.replace("https://x.com/", "@"),
},
metadataBase: new URL(siteConfig.url),
icons: "/icon.svg",
...props,
};
}
lib/config.ts
export const siteConfig = {
name: "Spell UI",
url: "https://spell.sh",
ogImage: "https://spell.sh/og",
description:
"Beautiful, sophisticated UI components designed for modern React and Tailwind CSS applications.",
carbon: {
// Setting serve to "" disables ads entirely.
serve: "CWBDE53E",
placement: "spellsh",
},
links: {
tom: "https://x.com/sushidrip",
x: "https://x.com/intent/follow?screen_name=sushidrip",
discord: "https://discord.gg/CxzqwQ2EAa",
github: "https://github.com/xxtomm/spell-ui",
// instagram: "https://instagram.com/spellui/",
},
keywords: [
"React",
"Tailwind CSS",
"Motion",
"Landing Page",
"Components",
"Next.js",
],
};
export type SiteConfig = typeof siteConfig;
demo.tsx
export { Demo as default } from "@/docs/copy-button/demo";CSS
/*! tailwindcss v4.3.3 | MIT License | https://tailwindcss.com */
@layer properties;
@layer theme, base, components, utilities;
@layer theme {
:root, :host {
--color-neutral-50: oklch(98.5% 0 none);
--color-neutral-200: oklch(92.2% 0 none);
--color-neutral-900: oklch(20.5% 0 none);
--spacing: 0.25rem;
--ease-out: cubic-bezier(0, 0, 0.2, 1);
--default-transition-duration: 150ms;
--default-transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
--default-font-family: var(--font-geist-sans);
--default-mono-font-family: var(--font-geist-mono);
}
}
@layer base {
*, ::after, ::before, ::backdrop, ::file-selector-button {
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0 solid;
}
html, :host {
line-height: 1.5;
-webkit-text-size-adjust: 100%;
tab-size: 4;
font-family: var(--default-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji");
font-feature-settings: var(--default-font-feature-settings, normal);
font-variation-settings: var(--default-font-variation-settings, normal);
-webkit-tap-highlight-color: transparent;
}
hr {
height: 0;
color: inherit;
border-top-width: 1px;
}
abbr:where([title]) {
-webkit-text-decoration: underline dotted;
text-decoration: underline dotted;
}
h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: inherit;
}
a {
color: inherit;
-webkit-text-decoration: inherit;
text-decoration: inherit;
}
b, strong {
font-weight: bolder;
}
code, kbd, samp, pre {
font-family: var(--default-mono-font-family, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace);
font-feature-settings: var(--default-mono-font-feature-settings, normal);
font-variation-settings: var(--default-mono-font-variation-settings, normal);
font-size: 1em;
}
small {
font-size: 80%;
}
sub, sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
table {
text-indent: 0;
border-color: inherit;
border-collapse: collapse;
}
:-moz-focusring:where(:not(iframe)) {
outline: auto;
}
progress {
vertical-align: baseline;
}
summary {
display: list-item;
}
ol, ul, menu {
list-style: none;
}
img, svg, video, canvas, audio, iframe, embed, object {
display: block;
vertical-align: middle;
}
img, video {
max-width: 100%;
height: auto;
}
button, input, select, optgroup, textarea, ::file-selector-button {
font: inherit;
font-feature-settings: inherit;
font-variation-settings: inherit;
letter-spacing: inherit;
color: inherit;
border-radius: 0;
background-color: transparent;
opacity: 1;
}
:where(select:is([multiple], [size])) optgroup {
font-weight: bolder;
}
:where(select:is([multiple], [size])) optgroup option {
padding-inline-start: 20px;
}
::file-selector-button {
margin-inline-end: 4px;
}
::placeholder {
opacity: 1;
}
@supports (not (-webkit-appearance: -apple-pay-button)) or (contain-intrinsic-size: 1px) {
::placeholder {
color: currentcolor;
@supports (color: color-mix(in lab, red, red)) {
color: color-mix(in oklab, currentcolor 50%, transparent);
}
}
}
textarea {
resize: vertical;
}
::-webkit-search-decoration {
-webkit-appearance: none;
}
::-webkit-date-and-time-value {
min-height: 1lh;
text-align: inherit;
}
::-webkit-datetime-edit {
display: inline-flex;
}
::-webkit-datetime-edit-fields-wrapper {
padding: 0;
}
::-webkit-datetime-edit, ::-webkit-datetime-edit-year-field, ::-webkit-datetime-edit-month-field, ::-webkit-datetime-edit-day-field, ::-webkit-datetime-edit-hour-field, ::-webkit-datetime-edit-minute-field, ::-webkit-datetime-edit-second-field, ::-webkit-datetime-edit-millisecond-field, ::-webkit-datetime-edit-meridiem-field {
padding-block: 0;
}
::-webkit-calendar-picker-indicator {
line-height: 1;
}
:-moz-ui-invalid {
box-shadow: none;
}
button, input:where([type="button"], [type="reset"], [type="submit"]), ::file-selector-button {
appearance: button;
}
::-webkit-inner-spin-button, ::-webkit-outer-spin-button {
height: auto;
}
[hidden]:where(:not([hidden="until-found"])) {
display: none !important;
}
}
@layer utilities {
.absolute {
position: absolute;
}
.relative {
position: relative;
}
.inline-flex {
display: inline-flex;
}
.h-8 {
height: calc(var(--spacing) * 8);
}
.h-9 {
height: calc(var(--spacing) * 9);
}
.h-12 {
height: calc(var(--spacing) * 12);
}
.w-8 {
width: calc(var(--spacing) * 8);
}
.w-9 {
width: calc(var(--spacing) * 9);
}
.w-12 {
width: calc(var(--spacing) * 12);
}
.scale-0 {
--tw-scale-x: 0%;
--tw-scale-y: 0%;
--tw-scale-z: 0%;
scale: var(--tw-scale-x) var(--tw-scale-y);
}
.scale-70 {
--tw-scale-x: 70%;
--tw-scale-y: 70%;
--tw-scale-z: 70%;
scale: var(--tw-scale-x) var(--tw-scale-y);
}
.scale-100 {
--tw-scale-x: 100%;
--tw-scale-y: 100%;
--tw-scale-z: 100%;
scale: var(--tw-scale-x) var(--tw-scale-y);
}
.cursor-pointer {
cursor: pointer;
}
.items-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
.rounded-md {
border-radius: calc(var(--radius) - 2px);
}
.text-neutral-900 {
color: var(--color-neutral-900);
}
.opacity-0 {
opacity: 0%;
}
.opacity-100 {
opacity: 100%;
}
.blur-\[2px\] {
--tw-blur: blur(2px);
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
}
.blur-none {
--tw-blur: ;
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
}
.transition-all {
transition-property: all;
transition-timing-function: var(--tw-ease, var(--default-transition-timing-function));
transition-duration: var(--tw-duration, var(--default-transition-duration));
}
.duration-200 {
--tw-duration: 200ms;
transition-duration: 200ms;
}
.ease-out {
--tw-ease: var(--ease-out);
transition-timing-function: var(--ease-out);
}
.active\:scale-\[0\.97\]:active {
scale: 0.97;
}
.disabled\:pointer-events-none:disabled {
pointer-events: none;
}
.disabled\:opacity-100:disabled {
opacity: 100%;
}
.dark\:text-neutral-50:where(.dark, .dark *) {
color: var(--color-neutral-50);
}
}
:root {
--background: #fff;
--foreground: #111;
--primary: #171717;
--primary-foreground: #fff;
--secondary: #eee;
--secondary-foreground: #171717;
--muted: #eee;
--muted-foreground: #666;
--accent: #eee;
--accent-foreground: #171717;
--border: #ddd;
--input: #ddd;
--ring: #777;
--destructive: #dc2626;
--destructive-foreground: #fff;
--popover: #fff;
--popover-foreground: #111;
--success: #15803d;
--success-foreground: #fff;
--radius: .625rem;
--hairline: 1px;
}
.dark {
--background: #090909;
--foreground: #fafafa;
--primary: #fafafa;
--primary-foreground: #171717;
--secondary: #262626;
--secondary-foreground: #fafafa;
--muted: #262626;
--muted-foreground: #a3a3a3;
--accent: #262626;
--accent-foreground: #fafafa;
--border: #333;
--input: #333;
--popover: #171717;
--popover-foreground: #fafafa;
}
:root {
--radius: 0.625rem;
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
--card: oklch(1 0 0);
--card-foreground: oklch(0.145 0 0);
--popover: oklch(1 0 0);
--popover-foreground: oklch(0.145 0 0);
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);
--secondary: oklch(0.97 0 0);
--secondary-foreground: oklch(0.205 0 0);
--muted: oklch(0.97 0 0);
--muted-foreground: oklch(0.556 0 0);
--accent: oklch(0.97 0 0);
--accent-foreground: oklch(0.205 0 0);
--destructive: oklch(0.577 0.245 27.325);
--border: oklch(0.922 0 0);
--input: oklch(0.922 0 0);
--ring: oklch(0.708 0 0);
--chart-1: oklch(0.646 0.222 41.116);
--chart-2: oklch(0.6 0.118 184.704);
--chart-3: oklch(0.398 0.07 227.392);
--chart-4: oklch(0.828 0.189 84.429);
--chart-5: oklch(0.769 0.188 70.08);
--sidebar: oklch(1 0 0);
--sidebar-foreground: oklch(0.145 0 0);
--sidebar-primary: oklch(0.205 0 0);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.97 0 0);
--sidebar-accent-foreground: oklch(0.205 0 0);
--sidebar-border: oklch(0.922 0 0);
--sidebar-ring: oklch(0.708 0 0);
--rotating-border-color: var(--color-neutral-200);
}
.dark {
--background: oklch(0.1149 0 0);
--foreground: oklch(0.985 0 0);
--card: oklch(0.205 0 0);
--card-foreground: oklch(0.985 0 0);
--popover: oklch(0.205 0 0);
--popover-foreground: oklch(0.985 0 0);
--primary: oklch(0.922 0 0);
--primary-foreground: oklch(0.205 0 0);
--secondary: oklch(22.213% 0.00003 271.152);
--secondary-foreground: oklch(0.985 0 0);
--muted: oklch(0.269 0 0);
--muted-foreground: oklch(0.708 0 0);
--accent: oklch(0.269 0 0);
--accent-foreground: oklch(0.985 0 0);
--destructive: oklch(0.704 0.191 22.216);
--border: oklch(1 0 0 / 10%);
--input: oklch(1 0 0 / 15%);
--ring: oklch(0.556 0 0);
--chart-1: oklch(0.488 0.243 264.376);
--chart-2: oklch(0.696 0.17 162.48);
--chart-3: oklch(0.769 0.188 70.08);
--chart-4: oklch(0.627 0.265 303.9);
--chart-5: oklch(0.645 0.246 16.439);
--sidebar: oklch(0.1149 0 0);
--sidebar-foreground: oklch(0.985 0 0);
--sidebar-primary: oklch(0.488 0.243 264.376);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.269 0 0);
--sidebar-accent-foreground: oklch(0.985 0 0);
--sidebar-border: oklch(1 0 0 / 10%);
--sidebar-ring: oklch(0.556 0 0);
--rotating-border-color: var(--muted);
}
.dark .shiki, .dark .shiki span {
color: var(--shiki-dark) !important;
background-color: #0f0f0f !important;
}
@property --tw-scale-x {
syntax: "*";
inherits: false;
initial-value: 1;
}
@property --tw-scale-y {
syntax: "*";
inherits: false;
initial-value: 1;
}
@property --tw-scale-z {
syntax: "*";
inherits: false;
initial-value: 1;
}
@property --tw-blur {
syntax: "*";
inherits: false;
}
@property --tw-brightness {
syntax: "*";
inherits: false;
}
@property --tw-contrast {
syntax: "*";
inherits: false;
}
@property --tw-grayscale {
syntax: "*";
inherits: false;
}
@property --tw-hue-rotate {
syntax: "*";
inherits: false;
}
@property --tw-invert {
syntax: "*";
inherits: false;
}
@property --tw-opacity {
syntax: "*";
inherits: false;
}
@property --tw-saturate {
syntax: "*";
inherits: false;
}
@property --tw-sepia {
syntax: "*";
inherits: false;
}
@property --tw-drop-shadow {
syntax: "*";
inherits: false;
}
@property --tw-drop-shadow-color {
syntax: "*";
inherits: false;
}
@property --tw-drop-shadow-alpha {
syntax: "<percentage>";
inherits: false;
initial-value: 100%;
}
@property --tw-drop-shadow-size {
syntax: "*";
inherits: false;
}
@property --tw-duration {
syntax: "*";
inherits: false;
}
@property --tw-ease {
syntax: "*";
inherits: false;
}
@layer properties {
@supports ((-webkit-hyphens: none) and (not (margin-trim: inline))) or ((-moz-orient: inline) and (not (color:rgb(from red r g b)))) {
*, ::before, ::after, ::backdrop {
--tw-scale-x: 1;
--tw-scale-y: 1;
--tw-scale-z: 1;
--tw-blur: initial;
--tw-brightness: initial;
--tw-contrast: initial;
--tw-grayscale: initial;
--tw-hue-rotate: initial;
--tw-invert: initial;
--tw-opacity: initial;
--tw-saturate: initial;
--tw-sepia: initial;
--tw-drop-shadow: initial;
--tw-drop-shadow-color: initial;
--tw-drop-shadow-alpha: 100%;
--tw-drop-shadow-size: initial;
--tw-duration: initial;
--tw-ease: initial;
}
}
}