Skip to content

Commit

Permalink
feat: add copy button for code
Browse files Browse the repository at this point in the history
  • Loading branch information
ashgw committed Feb 7, 2024
1 parent fb2029c commit 8096ed3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/blogs/tester.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async function VideoComponent({ fileName }) {
}`}
language="typescript"
showLineNumbers={true}

copy={false}
> </Code>
<C>
All data is encrypted via SSL/TLS when transmitted from our servers to your browser.
Expand Down
5 changes: 1 addition & 4 deletions src/app/components/nav/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const Nav = () => {
)}
>
<div className="flex flex-col gap-4 md:flex-row md:gap-8">
<Link
className="nav-link-shadow dimmed-4 hover:text-white"
href="/"
>
<Link className="nav-link-shadow dimmed-4 hover:text-white" href="/">
Home
</Link>
<Link
Expand Down
9 changes: 9 additions & 0 deletions src/app/components/reusables/code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import typescript from 'react-syntax-highlighter/dist/cjs/languages/prism/typesc
import go from 'react-syntax-highlighter/dist/cjs/languages/prism/go';
import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash';
import oneDark from 'react-syntax-highlighter/dist/cjs/styles/prism/one-dark';
import CopyButton from './copy-code';

SyntaxHighlighter.registerLanguage('rust', rust);
SyntaxHighlighter.registerLanguage('python', python);
Expand All @@ -20,13 +21,15 @@ type Props = {
code: string;
showLineNumbers?: boolean;
className?: string;
copy?: boolean;
};

export default function CodeBlock({
code,
language,
showLineNumbers,
className,
copy = true,
}: Props) {
return (
<div
Expand All @@ -35,6 +38,12 @@ export default function CodeBlock({
className
)}
>
{copy && (
<CopyButton
code={code}
className="absolute right-2 top-2 inline-flex items-center"
/>
)}
<SyntaxHighlighter
className="!m-0 overflow-auto !p-0 text-sm dark:!bg-black dark:[&>*]:!bg-black"
language={language}
Expand Down
29 changes: 29 additions & 0 deletions src/app/components/reusables/copy-code.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';
import { FC } from 'react';
import { Copy } from 'lucide-react';
import clsx from 'clsx';
import { useCopyToClipboard } from 'react-use';

interface CopyButtonProps {
code: string;
className?: string;
}

const CopyButton: FC<CopyButtonProps> = ({ code, className }) => {
const [_, copyToClipboard] = useCopyToClipboard();
return (
<button
className={clsx(
'rounded-xl border-2 border-[#191919] p-2 px-3 py-2 transition hover:border-[#340929] active:bg-[#340929]',
className
)}
onClick={() => {
copyToClipboard(code);
}}
>
<Copy size={'24px'} strokeWidth={'2px'} />
</button>
);
};

export default CopyButton;

0 comments on commit 8096ed3

Please sign in to comment.