-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e53cb6
commit a880bd2
Showing
16 changed files
with
2,874 additions
and
246 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
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,73 @@ | ||
import { useState, useRef } from "react"; | ||
|
||
const Pre = (props: any) => { | ||
const textInput = useRef<HTMLDivElement>(null); | ||
const [hovered, setHovered] = useState(false); | ||
const [copied, setCopied] = useState(false); | ||
|
||
const onEnter = () => { | ||
setHovered(true); | ||
}; | ||
|
||
const onExit = () => { | ||
setHovered(false); | ||
setCopied(false); | ||
}; | ||
|
||
const onCopy = () => { | ||
setCopied(true); | ||
if (textInput.current !== null && textInput.current.textContent !== null) { | ||
navigator.clipboard.writeText(textInput.current.textContent); | ||
setTimeout(() => { | ||
setCopied(false); | ||
}, 2000); | ||
} | ||
}; | ||
|
||
return ( | ||
<div ref={textInput} onMouseEnter={onEnter} onMouseLeave={onExit} className="relative"> | ||
{hovered && ( | ||
<button | ||
aria-label="Copy code" | ||
type="button" | ||
className={`absolute right-2 top-2 h-8 w-8 rounded border-2 bg-gray-700 p-1 dark:bg-gray-800 ${ | ||
copied ? "border-green-400 focus:border-green-400 focus:outline-none" : "border-gray-300" | ||
}`} | ||
onClick={onCopy} | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
stroke="currentColor" | ||
fill="none" | ||
className={copied ? "text-green-400" : "text-gray-300"} | ||
> | ||
{copied ? ( | ||
<> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth={2} | ||
d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4" | ||
/> | ||
</> | ||
) : ( | ||
<> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth={2} | ||
d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" | ||
/> | ||
</> | ||
)} | ||
</svg> | ||
</button> | ||
)} | ||
|
||
<pre>{props.children}</pre> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Pre; |
Oops, something went wrong.