1import { createSignal, type JSX } from "solid-js";
2import CopyIcon from "lucide-solid/icons/copy";
3import "./Copy.css";
4
5export function CopyField(props: { value: string; label: string }) {
6 return <div class="copy-field"><Copy value={props.value} label={props.label}><span class="value">{props.value}</span><CopyIcon size={14} class="icon" aria-hidden="true" /></Copy></div>;
7}
8
9/**
10 * Text that copies `value` on click; the tooltip turns into the result. Shows `value` unless given children, and
11 * `label` names what's copied when that isn't what's shown.
12 */
13export function Copy(props: { value: string; label?: string; children?: JSX.Element }) {
14 const [result, setResult] = createSignal("");
15 const copy = () =>
16 Promise.resolve()
17 .then(() => navigator.clipboard.writeText(props.value))
18 .then(() => setResult("copied"), () => setResult("Couldn't copy. Select the text instead"));
19 return (
20 <>
21 <button type="button" class="copy" aria-label={props.label ? `Copy ${props.label}` : undefined} aria-description={props.label ? props.value : undefined} data-tip={result() || (props.label ? `copy ${props.label}` : "copy")} onClick={copy}
22 onPointerLeave={() => setResult("")} onBlur={() => setResult("")}>
23 {props.children ?? props.value}
24 </button>
25 <span class="sr-only" role="status">{result()}</span>
26 </>
27 );
28}