Skip to content

Commit

Permalink
refactor: CopyToClipboard.tsx with best practices with enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
0xabdulkhaliq authored Aug 4, 2023
1 parent 2739db4 commit 7f5555d
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions components/CopyToClipboard/CopyToClipboard.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
import useCopyToClipboard from 'hooks/useCopyToClipboard'
import React, { useState } from 'react'
import { FaRegCopy } from 'react-icons/fa'
import useCopyToClipboard from 'hooks/useCopyToClipboard';
import React, { useState } from 'react';
import { FaRegCopy, FaCheckSquare } from 'react-icons/fa';

type CopyToClipboardProps = {
url: string
}
url: string;
};

export const CopyToClipboard = ({ url }: CopyToClipboardProps): JSX.Element => {
const [copyToClipboard, { success }] = useCopyToClipboard()
const [isHovering, setIsHovering] = useState(false)
const [copyToClipboard, { success }] = useCopyToClipboard();
const [isCopied, setIsCopied] = useState(false);

function handleCopy(e: React.MouseEvent<SVGElement, MouseEvent>) {
e.stopPropagation()
copyToClipboard(url)
function handleCopy() {
copyToClipboard(url);
setIsCopied(true);
setTimeout(() => setIsCopied(false), 2000); // Reset the icon after 2 seconds
}

return (
<div
className="dropdown dropdown-left dropdown-hover"
onMouseEnter={() => setIsHovering(true)}
onMouseLeave={() => setIsHovering(false)}
<button
className="text-theme-primary cursor-pointer bg-transparent border-none p-0"
onClick={handleCopy}
title="Copy link"
aria-label={success && isCopied ? 'Link copied' : 'Copy link'}
aria-live="polite"
>
<div style={{ position: 'relative' }}>
<FaRegCopy
size={'1.3rem'}
className="text-theme-primary cursor-pointer"
title="Copy link"
onClick={(e) => handleCopy(e)}
/>
{isHovering && (
<p
className="bg-theme-secondary text-white text-sm rounded-lg px-3 py-1 cursor-default"
style={{
position: 'absolute',
top: '-2rem', // Adjust this value to position the text above the icon
left: '50%',
transform: 'translateX(-50%)',
zIndex: 1,
}}
>
{success ? 'Copied!' : 'Copy'}
</p>
)}
</div>
</div>
)
}
{success && isCopied ? (
<FaCheckSquare size={'1.3rem'} />
) : (
<FaRegCopy size={'1.3rem'} />
)}
</button>
);
};

export default CopyToClipboard
export default CopyToClipboard;

0 comments on commit 7f5555d

Please sign in to comment.