-
-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathcopy.tsx
106 lines (92 loc) · 2.29 KB
/
copy.tsx
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
import React, { FC, useState } from "react";
import styled from "styled-components";
import { IconContainer } from "@/components/misc";
import { Icon } from "@/components/sprites";
import { FONT_FAMILY, THEME_COLORS } from "@/style";
// Icons
import CopyIconSvg from "@/images/icons/copy.svg";
function copyToClipboard(content: string): void {
const el = document.createElement(`textarea`);
el.value = content;
el.setAttribute(`readonly`, ``);
el.style.position = `absolute`;
el.style.left = `-9999px`;
document.body.appendChild(el);
el.select();
document.execCommand(`copy`);
document.body.removeChild(el);
}
export interface CopyProps {
readonly content: string;
}
export const Copy: FC<CopyProps> = ({ content }) => {
const [showToast, setShowToast] = useState(false);
return (
<>
<CopyIconButton
onClick={() => {
copyToClipboard(content);
setShowToast(true);
setTimeout(() => {
setShowToast(false);
}, 3000);
}}
>
<IconContainer $size={20}>
<Icon {...CopyIconSvg} />
</IconContainer>
</CopyIconButton>
{showToast && <CopySuccessToast />}
</>
);
};
const CopySuccessToast: FC = () => {
return (
<ToastContainer>
<ToastText>Copied code example</ToastText>
</ToastContainer>
);
};
const ToastText = styled.div`
font-size: 1.25rem;
font-family: ${FONT_FAMILY};
font-weight: 600;
color: ${THEME_COLORS.textContrast};
`;
const ToastContainer = styled.div`
position: fixed;
left: 50%;
bottom: 30px;
transform: translateX(-50%);
z-index: 9999;
border: 1px solid ${THEME_COLORS.boxBorder};
border-radius: var(--box-border-radius);
padding: 20px;
backdrop-filter: blur(4px);
background-color: ${THEME_COLORS.backdrop};
opacity: 0;
animation: animation 3s cubic-bezier(0.98, 0.01, 0.53, 0.47);
@keyframes animation {
0%,
50% {
opacity: 1;
}
50%,
100% {
opacity: 0;
}
}
`;
const CopyIconButton = styled.button`
display: flex;
align-items: center;
justify-content: center;
border-radius: 0 var(--box-border-radius) 0 var(--button-border-radius);
border: 1px solid ${THEME_COLORS.boxBorder};
border-top: 0 none;
border-right: 0 none;
padding: 6px;
svg {
fill: #eb64b9;
}
`;