mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-09-16 18:35:33 -04:00
next-mdx-remote v4 (#737)
This commit is contained in:
19
components/CopyButton/CopyButton.module.css
Normal file
19
components/CopyButton/CopyButton.module.css
Normal file
@@ -0,0 +1,19 @@
|
||||
.copy {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
padding: 0.65em;
|
||||
line-height: 1;
|
||||
color: var(--medium-dark);
|
||||
background-color: var(--background-inner);
|
||||
border: 1px solid var(--kinda-light);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.copy:hover {
|
||||
color: var(--link);
|
||||
}
|
||||
|
||||
.success {
|
||||
color: var(--success) !important;
|
||||
}
|
61
components/CopyButton/CopyButton.tsx
Normal file
61
components/CopyButton/CopyButton.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import classNames from "classnames/bind";
|
||||
import copy from "copy-to-clipboard";
|
||||
import innerText from "react-innertext";
|
||||
import { ClipboardOcticon, CheckOcticon } from "../Icons";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
import styles from "./CopyButton.module.css";
|
||||
const cx = classNames.bind(styles);
|
||||
|
||||
type Props = {
|
||||
source: ReactNode;
|
||||
timeout?: number;
|
||||
};
|
||||
|
||||
const CopyButton = ({ source, timeout = 2000 }: Props) => {
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
const handleCopy = (e) => {
|
||||
// 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 to original icon after given ms (defaults to 2 seconds)
|
||||
if (copied) {
|
||||
const reset = setTimeout(() => {
|
||||
setCopied(false);
|
||||
}, timeout);
|
||||
|
||||
return () => clearTimeout(reset);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
return () => {};
|
||||
}, [timeout, copied]);
|
||||
|
||||
return (
|
||||
<button
|
||||
className={cx({ copy: true, success: !!copied })}
|
||||
title="Copy to clipboard"
|
||||
aria-label="Copy to clipboard"
|
||||
onClick={handleCopy}
|
||||
disabled={!!copied}
|
||||
>
|
||||
{copied ? (
|
||||
<CheckOcticon className="icon" fill="currentColor" />
|
||||
) : (
|
||||
<ClipboardOcticon className="icon" fill="currentColor" />
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
export default CopyButton;
|
Reference in New Issue
Block a user