mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 09:11:38 +00:00
Fix checklist in markdown
* 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
This commit is contained in:
39
components/post/CopyButton.tsx
Normal file
39
components/post/CopyButton.tsx
Normal file
@ -0,0 +1,39 @@
|
||||
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;
|
Reference in New Issue
Block a user