-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a copy button in playground editor (#176)
* Made the copy to clipboard component and added it to playground editor and code snippets * Fixed the copy button click event handler type * Fixed copyToClipboard function argument type * Made changes as per review suggestions * Added logic to hide the copy to clipboard button only on medium viewports and above Co-authored-by: Shubham Singh <shubham.singh1@spinny.com>
- Loading branch information
1 parent
d1b487f
commit dd076b3
Showing
6 changed files
with
137 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SVGProps } from "react"; | ||
|
||
export default function CopyIcon(props: SVGProps<SVGSVGElement>) { | ||
return ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
className="w-6 h-6" | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
stroke="currentColor" | ||
strokeWidth={1.5} | ||
{...props} | ||
> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" | ||
/> | ||
</svg> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { SVGProps } from "react"; | ||
|
||
export default function TickIcon(props: SVGProps<SVGSVGElement>) { | ||
return ( | ||
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} {...props}> | ||
<path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" /> | ||
</svg> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import CopyIcon from "./CopyIcon"; | ||
import TickIcon from "./TickIcon"; | ||
|
||
interface Props { | ||
text: string; | ||
} | ||
|
||
export default function CopyToClipboard(props: Props) { | ||
const { text } = props; | ||
const [copySuccess, setCopySuccess] = useState(false); | ||
const buttonRef = useRef<HTMLButtonElement>(null); | ||
|
||
useEffect(() => { | ||
if (copySuccess) { | ||
setTimeout(() => { | ||
setCopySuccess(false); | ||
}, 3000); | ||
} | ||
}, [copySuccess]); | ||
|
||
function copyToClipboard(e: React.MouseEvent) { | ||
e.stopPropagation(); | ||
if (text) { | ||
navigator.clipboard?.writeText(text).then(() => { | ||
setCopySuccess(true); | ||
}).catch(error => { | ||
console.log(error); | ||
}); | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<button | ||
ref={buttonRef} | ||
className={"md:hidden absolute top-2 right-2 sm:top-4 sm:right-4 group-hover:flex items-center justify-center p-1 sm:p-2 border border-[#f0f6fc1a] text-base font-medium rounded-md text-gray-400 bg-[#333] hover:bg-[#4d4d4d] cursor-pointer transition-all duration-100 " + (copySuccess ? 'text-bhagwa-300 border-bhagwa-300' : '')} | ||
onClick={copyToClipboard} | ||
> | ||
{copySuccess ? | ||
<> | ||
<TickIcon /> | ||
<span className="copied-text absolute top-1/2 -translate-y-1/2 right-[calc(100%+8px)] p-1 rounded-md text-xs font-normal text-bhagwa-300 bg-[#666] border border-transparent"> | ||
Copied! | ||
</span> | ||
</> | ||
: <CopyIcon />} | ||
</button> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters