Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update the CopyButton Component #40

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions components/elements/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { FC } from 'react'
import { Copy } from 'components/svgs'
import clsx from 'clsx'
import { useCopyToClipboard } from 'react-use'

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

const CopyButton: FC<CopyButtonProps> = ({ code, className }) => {
const [_, copyToClipboard] = useCopyToClipboard()
return (
<button
className={clsx(
'rounded-xl border border-[#191919] border-transparent p-2 px-3 py-2 text-sm font-medium leading-4 shadow-sm transition hover:border-[#484848] dark:text-white dark:text-[#C2C2C2] dark:hover:bg-[#343434]',
className
)}
onClick={() => {
copyToClipboard(code)
}}
>
<Copy width={32} height={32} />
</button>
)
}
const handleClick = async () => {
try {
await navigator.clipboard.writeText(code);
} catch (err) {
console.error('Failed to copy text: ', err);
}
};

return (
<button
className={clsx(
'rounded-xl border border-[#191919] border-transparent p-2 px-3 py-2 text-sm font-medium leading-4 shadow-sm transition hover:border-[#484848] dark:text-white dark:text-[#C2C2C2] dark:hover:bg-[#343434]',
className
)}
onClick={handleClick}
>
<Copy width={32} height={32} />
</button>
);
};

export default CopyButton
export default CopyButton;