|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + useMemo, |
| 5 | + useId, |
| 6 | + useEffect, |
| 7 | + useState, |
| 8 | + type ComponentProps, |
| 9 | +} from "react"; |
| 10 | + |
| 11 | +import { |
| 12 | + motion, |
| 13 | + AnimatePresence, |
| 14 | + type Transition, |
| 15 | + type Variants, |
| 16 | +} from "motion/react"; |
| 17 | + |
| 18 | +import { cn } from "@/utils/cn"; |
| 19 | +import { copyToClipboard } from "@/utils/copy"; |
| 20 | + |
| 21 | +interface CopyTextAnimatedProps extends ComponentProps<"button"> { |
| 22 | + content: string; |
| 23 | + iconSize?: number; |
| 24 | +} |
| 25 | + |
| 26 | +export type TextMorphProps = { |
| 27 | + children: string; |
| 28 | + as?: React.ElementType; |
| 29 | + className?: string; |
| 30 | + style?: React.CSSProperties; |
| 31 | + variants?: Variants; |
| 32 | + transition?: Transition; |
| 33 | +}; |
| 34 | + |
| 35 | +export function TextMorph({ |
| 36 | + children, |
| 37 | + as: Component = "p", |
| 38 | + className, |
| 39 | + style, |
| 40 | + variants, |
| 41 | + transition, |
| 42 | +}: TextMorphProps) { |
| 43 | + const uniqueId = useId(); |
| 44 | + |
| 45 | + const characters = useMemo(() => { |
| 46 | + const charCounts: Record<string, number> = {}; |
| 47 | + |
| 48 | + return children.split("").map((char) => { |
| 49 | + const lowerChar = char.toLowerCase(); |
| 50 | + charCounts[lowerChar] = (charCounts[lowerChar] || 0) + 1; |
| 51 | + |
| 52 | + return { |
| 53 | + id: `${uniqueId}-${lowerChar}${charCounts[lowerChar]}`, |
| 54 | + label: char === " " ? "\u00A0" : char, |
| 55 | + }; |
| 56 | + }); |
| 57 | + }, [children, uniqueId]); |
| 58 | + |
| 59 | + const defaultVariants: Variants = { |
| 60 | + initial: { opacity: 0 }, |
| 61 | + animate: { opacity: 1 }, |
| 62 | + exit: { opacity: 0 }, |
| 63 | + }; |
| 64 | + |
| 65 | + const defaultTransition: Transition = { |
| 66 | + type: "spring", |
| 67 | + stiffness: 280, |
| 68 | + damping: 18, |
| 69 | + mass: 0.3, |
| 70 | + }; |
| 71 | + |
| 72 | + return ( |
| 73 | + <Component className={cn(className)} aria-label={children} style={style}> |
| 74 | + <AnimatePresence mode="popLayout" initial={false}> |
| 75 | + {characters.map((character) => ( |
| 76 | + <motion.span |
| 77 | + key={character.id} |
| 78 | + layoutId={character.id} |
| 79 | + className="inline-block" |
| 80 | + aria-hidden="true" |
| 81 | + initial="initial" |
| 82 | + animate="animate" |
| 83 | + exit="exit" |
| 84 | + variants={variants || defaultVariants} |
| 85 | + transition={transition || defaultTransition} |
| 86 | + > |
| 87 | + {character.label} |
| 88 | + </motion.span> |
| 89 | + ))} |
| 90 | + </AnimatePresence> |
| 91 | + </Component> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +const CopyTextMorph = ({ |
| 96 | + content, |
| 97 | + className, |
| 98 | + ...props |
| 99 | +}: CopyTextAnimatedProps) => { |
| 100 | + const [isCopied, setIsCopied] = useState<boolean>(false); |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + if (!isCopied) return; |
| 104 | + |
| 105 | + const timeout = setTimeout(() => { |
| 106 | + setIsCopied(false); |
| 107 | + }, 2000); |
| 108 | + return () => clearTimeout(timeout); |
| 109 | + }, [isCopied]); |
| 110 | + |
| 111 | + const handleCopy = async () => { |
| 112 | + await copyToClipboard(content); |
| 113 | + setIsCopied(true); |
| 114 | + }; |
| 115 | + |
| 116 | + return ( |
| 117 | + <button |
| 118 | + title="Copy to clipboard" |
| 119 | + className={cn( |
| 120 | + "cursor-pointer", |
| 121 | + "transition-colors duration-200 ease-in-out", |
| 122 | + "text-xs text-neutral-700 dark:text-neutral-300 hover:text-neutral-950 dark:hover:text-neutral-50", |
| 123 | + "rounded-md bg-neutral-300/60 px-1.5 py-1 dark:bg-neutral-700/60", |
| 124 | + "border border-transparent hover:border-neutral-400/60 dark:hover:border-neutral-600/60", |
| 125 | + isCopied && "text-neutral-950 dark:text-neutral-50", |
| 126 | + className, |
| 127 | + )} |
| 128 | + onClick={handleCopy} |
| 129 | + {...props} |
| 130 | + > |
| 131 | + <TextMorph>{isCopied ? `Copied` : `Copy`}</TextMorph> |
| 132 | + </button> |
| 133 | + ); |
| 134 | +}; |
| 135 | + |
| 136 | +export { CopyTextMorph }; |
0 commit comments