Copy Button
lyminhnghia/copy-buttonFreeComponent
Copy text to clipboard with visual feedback and animation.
Install it
npx shadcn@latest add https://lyminhnghia.github.io/r/copy-button.jsonSource
1"use client"23import { CheckIcon, CircleXIcon, CopyIcon } from "lucide-react"4import type { HTMLMotionProps, Variants } from "motion/react"5import { AnimatePresence, motion } from "motion/react"6import type { ComponentProps } from "react"78import { Button } from "@/components/ui/button"9import type { CopyState } from "@/hooks/use-copy-to-clipboard"10import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"1112export const motionIconVariants: Variants = {13 initial: { opacity: 0, scale: 0.8, filter: "blur(2px)" },14 animate: { opacity: 1, scale: 1, filter: "blur(0px)" },15 exit: { opacity: 0, scale: 0.8 },16}1718export const motionIconProps: HTMLMotionProps<"span"> = {19 variants: motionIconVariants,20 initial: "initial",21 animate: "animate",22 exit: "exit",23 transition: { duration: 0.15, ease: "easeOut" },24}2526export type CopyStateIconProps = {27 state: CopyState28 /**29 * Custom icon for idle state.30 * @defaultValue CopyIcon31 * */32 idleIcon?: React.ReactNode33 /**34 * Custom icon for done state.35 * @defaultValue CheckIcon36 * */37 doneIcon?: React.ReactNode38 /**39 * Custom icon for error state.40 * @defaultValue CircleXIcon41 * */42 errorIcon?: React.ReactNode43}4445export function CopyStateIcon({46 state,47 idleIcon,48 doneIcon,49 errorIcon,50}: CopyStateIconProps) {51 return (52 <AnimatePresence mode="popLayout" initial={false}>53 {state === "idle" ? (54 <motion.span key="idle" {...motionIconProps}>55 {idleIcon ?? <CopyIcon />}56 </motion.span>57 ) : state === "done" ? (58 <motion.span key="done" {...motionIconProps}>59 {doneIcon ?? <CheckIcon strokeWidth={3} />}60 </motion.span>61 ) : state === "error" ? (62 <motion.span key="error" {...motionIconProps}>63 {errorIcon ?? <CircleXIcon />}64 </motion.span>65 ) : null}66 </AnimatePresence>67 )68}6970export type CopyButtonProps = ComponentProps<typeof Button> & {71 /** The text to copy, or a function that returns the text. */72 text: string | (() => string)73 /** Called with the copied text on successful copy. */74 onCopySuccess?: (text: string) => void75 /** Called with the error if the copy operation fails. */76 onCopyError?: (error: Error) => void77} & Pick<CopyStateIconProps, "idleIcon" | "doneIcon" | "errorIcon">7879export function CopyButton({80 size = "icon",81 children,82 text,83 idleIcon,84 doneIcon,85 errorIcon,86 onClick,87 onCopySuccess,88 onCopyError,89 ...props90}: CopyButtonProps) {91// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from lyminhnghia
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Apple Hello Effectapple-hello-effect | lyminhnghia | Components | Free | 1 dep | |
| chevrons-up-down-iconchevrons-up-down-icon | lyminhnghia | Components | Free | no deps | |
| Code Block Commandcode-block-command | lyminhnghia | Components | Free | 4 deps · 3 files | |
| Consent Managerconsent-manager | lyminhnghia | Components | Free | 1 dep | |
| GitHub Starsgithub-stars | lyminhnghia | Components | Free | no deps | |
| Scroll Fade Effectscroll-fade-effect | lyminhnghia | Components | Free | no deps | |
| Shimmering Textshimmering-text | lyminhnghia | Components | Free | 1 dep | |
| Slide to Unlockslide-to-unlock | lyminhnghia | Components | Free | 1 dep |
