Skip to content

Commit

Permalink
Merge pull request #47 from maheshj01/issue-16
Browse files Browse the repository at this point in the history
Add a Copy Button to code Block
  • Loading branch information
maheshj01 authored Sep 5, 2024
2 parents db59665 + 8db2694 commit 39ff48b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
63 changes: 46 additions & 17 deletions src/app/(main)/_components/CodeHighlight.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
import DoneIcon from '@mui/icons-material/Done';
import { useTheme } from 'next-themes';
import { useState } from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
import { a11yDark } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import IconButton from './IconButton';
import { Theme } from './ThemeSwitcher';

interface CodeBlockProps {
language: string; // The programming language of the code block (e.g., 'javascript', 'python')
children: string; // The code to be highlighted
Expand All @@ -16,24 +19,50 @@ interface CodeBlockProps {
*/
const CodeBlock: React.FC<CodeBlockProps> = ({ language, children, ...rest }) => {
const { theme } = useTheme();
const [copied, setCopied] = useState<boolean>(false);
const handleCopy = () => {
navigator.clipboard.writeText(children);
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 2000);
console.log('Copied to Clipboard!');
}
return (
<SyntaxHighlighter
customStyle={{
padding: '0.5em',
backgroundColor: 'var(--color-code-surface)',
}}
lineProps={{
style: {
wordBreak: 'break-all', whiteSpace: 'pre-wrap', paddingBottom: 1
<div className='relative'>
<IconButton
className='absolute top-[-12px] right-[-12px]'
onClick={handleCopy}
ariaLabel="Copy to clipboard"
>{!copied ?
(<ContentCopyIcon
className='w-5 h-5'
/>)
:
(<DoneIcon
className='w-5 h-5'
color='success'
/>)
}
}}
// wrapLines={true}
{...rest}
PreTag={'div'}
showLineNumbers={true}
language={language} style={theme == Theme.DARK ? a11yDark : docco}>
{children}
</SyntaxHighlighter>
</IconButton>
<SyntaxHighlighter
customStyle={{
padding: '0.5em',
backgroundColor: 'var(--color-code-surface)',
}}
lineProps={{
style: {
wordBreak: 'break-all', whiteSpace: 'pre-wrap', paddingBottom: 1
}
}}
// wrapLines={true}
{...rest}
PreTag={'div'}
showLineNumbers={true}
language={language} style={theme == Theme.DARK ? a11yDark : docco}>
{children}
</SyntaxHighlighter>
</div>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/_components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const Sidebar: React.FC = () => {
<IconButton
ariaLabel='Report a Bug'
onClick={() => {
window.open(`${process.env.NEXT_PUBLIC_GITHUB_REPO}/issues/new` ?? '', '_blank');
window.open(`${process.env.NEXT_PUBLIC_GITHUB_REPO}/issues/new`, '_blank');
}}
>
<FaBug className='size-6 text-black dark:text-white' />
Expand Down

0 comments on commit 39ff48b

Please sign in to comment.