Spaces:
Runtime error
Runtime error
File size: 1,279 Bytes
8e20687 |
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 |
import { StreamLanguage } from '@codemirror/language';
import { go } from '@codemirror/legacy-modes/mode/go';
import { tokyoNight } from '@uiw/codemirror-theme-tokyo-night';
import CodeMirror from '@uiw/react-codemirror';
import { FC, useEffect, useState } from 'react';
interface Props {
code: string;
editable?: boolean;
onChange?: (value: string) => void;
}
export const CodeBlock: FC<Props> = ({
code,
editable = false,
onChange = () => {},
}) => {
const [copyText, setCopyText] = useState<string>('Copy');
useEffect(() => {
const timeout = setTimeout(() => {
setCopyText('Copy');
}, 2000);
return () => clearTimeout(timeout);
}, [copyText]);
return (
<div className="relative">
<button
className="absolute right-0 top-0 z-10 rounded bg-[#1A1B26] p-1 text-xs text-white hover:bg-[#2D2E3A] active:bg-[#2D2E3A]"
onClick={() => {
navigator.clipboard.writeText(code);
setCopyText('Copied!');
}}
>
{copyText}
</button>
<CodeMirror
editable={editable}
value={code}
minHeight="500px"
extensions={[StreamLanguage.define(go)]}
theme={tokyoNight}
onChange={(value) => onChange(value)}
/>
</div>
);
};
|