1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 08:58:30 -04:00
jarv.is/components/clipboard/CopyButton.tsx
Jake Jarvis 41d2b8c64b
scss ➡️ vanilla css (#727)
* scss isn't worth the technical/mental overhead anymore

* try to translate my old Hugo pygments themes

* update lockfile

* consolidate .hljs classes
2022-01-07 16:17:14 -05:00

62 lines
1.5 KiB
TypeScript

import { useState, useEffect } from "react";
import copy from "copy-to-clipboard";
import innerText from "react-innertext";
import { CopyOcticon, CheckOcticon } from "../icons/octicons";
import styles from "./CopyButton.module.css";
import type { ReactNode } from "react";
type Props = {
source: ReactNode;
timeout?: number;
};
const CopyButton = ({ source, timeout = 2000 }: Props) => {
const [copied, setCopied] = useState(false);
const handleCopy = (e) => {
// stop browser from navigating away from page (this shouldn't happen anyways)
e.preventDefault();
// prevent unintentional double-clicks by unfocusing button
e.target.blur();
// send plaintext to the clipboard
const didCopy = copy(innerText(source));
// indicate success
setCopied(didCopy);
};
useEffect(() => {
// reset everything after given ms (defaults to 2 seconds)
if (copied) {
const id = setTimeout(() => {
setCopied(false);
}, timeout);
return () => clearTimeout(id);
}
return () => {};
}, [timeout, copied]);
return (
<button
className={styles.copy_button}
title="Copy to clipboard"
aria-label="Copy to clipboard"
onClick={handleCopy}
disabled={copied}
>
{copied ? (
<CheckOcticon fill="currentColor" className={`${styles.octicon} ${styles["octicon-check"]}`} />
) : (
<CopyOcticon fill="currentColor" className={styles.octicon} />
)}
</button>
);
};
export default CopyButton;