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