mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 09:11:38 +00:00
* Add strip-markdown * Add CopyButton component * Fix copy code * Add about page * Use async function with get all markdown content * Add shortcut icon * Remove useless code
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { Button, useClipboard } from '@chakra-ui/react';
|
|
import { FC, MouseEventHandler, useEffect, useState } from 'react';
|
|
|
|
const CopyButton: FC = ({ children }) => {
|
|
// Copy code
|
|
const [codeContent, setCodeContent] = useState('');
|
|
const { hasCopied, onCopy } = useClipboard(codeContent);
|
|
|
|
const copyCode: MouseEventHandler<HTMLButtonElement> = (e) => {
|
|
const target = e.target as HTMLButtonElement;
|
|
// Button is sibling with Code tag
|
|
const codeToCopy = target.nextElementSibling?.textContent;
|
|
codeToCopy && setCodeContent(codeToCopy);
|
|
};
|
|
useEffect(() => {
|
|
codeContent && onCopy();
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [codeContent]);
|
|
|
|
return (
|
|
<>
|
|
<pre>
|
|
<Button
|
|
size="xs"
|
|
colorScheme="teal"
|
|
position="absolute"
|
|
top="5px"
|
|
right="5px"
|
|
onClick={copyCode}
|
|
>
|
|
{hasCopied ? 'COPYIED' : 'COPY'}
|
|
</Button>
|
|
{children}
|
|
</pre>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default CopyButton;
|