Open History to see the last commit
1import { SymbolView } from "../components/AppSymbol";
2import { memo, useEffect, useRef, useState } from "react";
3import { Pressable, type ColorValue } from "react-native";
4
5import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
6
7const COPY_FEEDBACK_DURATION_MS = 1200;
8
9export const CopyTextButton = memo(function CopyTextButton(props: {
10 readonly accessibilityLabel: string;
11 readonly text: string;
12 readonly tintColor: ColorValue;
13 readonly copiedTintColor?: ColorValue;
14 readonly backgroundColor?: ColorValue;
15 readonly borderColor?: ColorValue;
16 readonly iconSize?: number;
17 readonly buttonSize?: number;
18}) {
19 const [copied, setCopied] = useState(false);
20 const resetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
21
22 useEffect(
23 () => () => {
24 if (resetTimeoutRef.current) {
25 clearTimeout(resetTimeoutRef.current);
26 }
27 },
28 [],
29 );
30
31 return (
32 <Pressable
33 accessibilityRole="button"
34 accessibilityLabel={copied ? "Copied" : props.accessibilityLabel}
35 disabled={props.text.length === 0}
36 hitSlop={8}
37 onPress={() => {
38 copyTextWithHaptic(props.text);
39 setCopied(true);
40 if (resetTimeoutRef.current) {
41 clearTimeout(resetTimeoutRef.current);
42 }
43 resetTimeoutRef.current = setTimeout(() => {
44 setCopied(false);
45 resetTimeoutRef.current = null;
46 }, COPY_FEEDBACK_DURATION_MS);
47 }}
48 style={({ pressed }) => ({
49 width: props.buttonSize ?? 30,
50 height: props.buttonSize ?? 30,
51 alignItems: "center",
52 justifyContent: "center",
53 borderRadius: 9,
54 borderWidth: props.borderColor ? 1 : 0,
55 borderColor: props.borderColor,
56 backgroundColor: props.backgroundColor,
57 opacity: pressed ? 0.52 : 1,
58 })}
59 >
60 <SymbolView
61 name={
62 copied
63 ? { ios: "checkmark", android: "check" }
64 : { ios: "doc.on.doc", android: "content_copy" }
65 }
66 size={props.iconSize ?? 13}
67 tintColor={copied ? (props.copiedTintColor ?? props.tintColor) : props.tintColor}
68 type="monochrome"
69 />
70 </Pressable>
71 );
72});