-
Notifications
You must be signed in to change notification settings - Fork 949
Expand file tree
/
Copy pathcode-container.tsx
More file actions
168 lines (156 loc) Β· 4.98 KB
/
code-container.tsx
File metadata and controls
168 lines (156 loc) Β· 4.98 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { LayoutGroup, motion } from 'framer-motion';
import type { Language } from 'prism-react-renderer';
import * as React from 'react';
import { copyTextToClipboard } from '../utils';
import { tabTransition } from '../utils/constants';
import languageMap from '../utils/language-map';
import { Code } from './code';
import { IconButton } from './icons/icon-button';
import { IconCheck } from './icons/icon-check';
import { IconClipboard } from './icons/icon-clipboard';
import { IconDownload } from './icons/icon-download';
import { Tooltip } from './tooltip';
interface CodeContainerProps {
markups: MarkupProps[];
basename: string;
activeLang: string;
setActiveLang: (lang: string) => void;
}
interface MarkupProps {
language: Language;
extension?: string;
content: string;
}
export const CodeContainer: React.FC<Readonly<CodeContainerProps>> = ({
markups,
basename: filename,
activeLang,
setActiveLang,
}) => {
const activeMarkup = markups.find(({ language }) => activeLang === language);
if (!activeMarkup) {
throw new Error('No markup found for the active language!', {
cause: {
activeLang,
markups,
},
});
}
const codeId = React.useId();
return (
<div
className="relative max-h-[650px] w-full h-full whitespace-pre rounded-md border border-slate-6 text-sm"
style={{
lineHeight: '130%',
background:
'linear-gradient(145.37deg, rgba(255, 255, 255, 0.09) -8.75%, rgba(255, 255, 255, 0.027) 83.95%)',
boxShadow: 'rgb(0 0 0 / 10%) 0px 5px 30px -5px',
}}
>
<div className="h-9 border-b border-slate-6">
<div className="flex">
<LayoutGroup id={codeId}>
{markups.map(({ language }) => {
const isCurrentLang = activeLang === language;
return (
<motion.button
className={`relative px-4 py-[8px] font-sans text-sm font-medium transition duration-200 ease-in-out hover:text-slate-12 ${activeLang !== language ? 'text-slate-11' : 'text-slate-12'
}`}
key={language}
onClick={() => {
setActiveLang(language);
}}
>
{isCurrentLang ? (
<motion.span
animate={{ opacity: 1 }}
className="absolute bottom-0 left-0 right-0 top-0 bg-slate-4"
exit={{ opacity: 0 }}
initial={{ opacity: 0 }}
layoutId="code"
transition={tabTransition}
/>
) : null}
{languageMap[language]}
</motion.button>
);
})}
</LayoutGroup>
</div>
<CopyToClipboardButton content={activeMarkup.content} />
<DownloadButton
content={activeMarkup.content}
filename={`${filename}.${activeMarkup.extension || activeMarkup.language}`}
/>
</div>
<div className="h-[calc(100%-2.25rem)]">
<Code language={activeLang}>{activeMarkup.content}</Code>
</div>
</div>
);
};
interface CopyToClipboardButtonProps {
content: string;
}
const CopyToClipboardButton = ({ content }: CopyToClipboardButtonProps) => {
const [isCopied, setIsCopied] = React.useState(false);
const unsetIsCopiedTimeout = React.useRef<NodeJS.Timeout>(undefined);
React.useEffect(() => {
setIsCopied(false);
clearTimeout(unsetIsCopiedTimeout.current);
unsetIsCopiedTimeout.current = undefined;
}, [content]);
return (
<Tooltip>
<Tooltip.Trigger
asChild
className="absolute right-2 top-2 hidden md:block"
>
<IconButton
onClick={async () => {
setIsCopied(true);
await copyTextToClipboard(content);
unsetIsCopiedTimeout.current = setTimeout(() => {
setIsCopied(false);
}, 3000);
}}
>
{isCopied ? <IconCheck /> : <IconClipboard />}
</IconButton>
</Tooltip.Trigger>
<Tooltip.Content>Copy to Clipboard</Tooltip.Content>
</Tooltip>
);
};
interface DownloadButtonProps {
content: string;
filename: string;
}
const DownloadButton = ({ content, filename }: DownloadButtonProps) => {
const generatedUrl = React.useMemo(() => {
const file = new File([content], filename);
return URL.createObjectURL(file);
}, [content, filename]);
const url = React.useSyncExternalStore(
() => () => { },
() => generatedUrl,
() => undefined,
);
return (
<Tooltip>
<Tooltip.Trigger
asChild
className="text-gray-11 absolute right-8 top-2 hidden md:block"
>
<a
className="text-slate-11 transition duration-200 ease-in-out hover:text-slate-12"
download={filename}
href={url}
>
<IconDownload />
</a>
</Tooltip.Trigger>
<Tooltip.Content>Download</Tooltip.Content>
</Tooltip>
);
};