Skip to content

Commit

Permalink
hide clipboard interface if the clipboard doesn't exist (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr authored Oct 4, 2023
1 parent 4955d55 commit 1fe3394
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
6 changes: 5 additions & 1 deletion app/src/tasks/TaskDetail/ClientConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ await task.sendMeasurement(...); // your measurement here`;

<Copy clipboardContents={contents}>
{(copy) => (
<div onClick={copy} style={{ cursor: "pointer" }} className="my-3">
<div
onClick={copy}
style={copy && { cursor: "pointer" }}
className="my-3"
>
<SyntaxHighlighter
language="javascript"
style={syntaxStyle}
Expand Down
52 changes: 30 additions & 22 deletions app/src/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,44 @@ export function Copy({
children,
clipboardContents,
}: {
children(copy: () => void, copied: boolean): React.ReactElement;
children(copy: undefined | (() => void), copied: boolean): React.ReactElement;
clipboardContents: string;
}) {
const [copied, setCopied] = React.useState(false);
const copy = React.useCallback(() => {
navigator.clipboard.writeText(clipboardContents).then(() => {
setCopied(true);
});
}, [setCopied, clipboardContents]);
if ("clipboard" in navigator) {
const [copied, setCopied] = React.useState(false);
const copy = React.useCallback(() => {
navigator.clipboard.writeText(clipboardContents).then(() => {
setCopied(true);
});
}, [setCopied, clipboardContents]);

return (
<OverlayTrigger
overlay={<Tooltip>{copied ? "Copied!" : "Click to copy"}</Tooltip>}
>
{children(copy, copied)}
</OverlayTrigger>
);
return (
<OverlayTrigger
overlay={<Tooltip>{copied ? "Copied!" : "Click to copy"}</Tooltip>}
>
{children(copy, copied)}
</OverlayTrigger>
);
} else {
return children(undefined, false);
}
}

export function CopyCode({ code }: { code: string }) {
return (
<Copy clipboardContents={code}>
{(copy, copied) => (
<span onClick={copy} style={{ cursor: "pointer" }}>
<code className="user-select-all">{code}</code>{" "}
<Button size="sm" variant="outline-secondary" className="ml-auto">
{copied ? <ClipboardCheckFill /> : <Clipboard />}
</Button>
</span>
)}
{(copy, copied) =>
copy ? (
<span onClick={copy} style={{ cursor: "pointer" }}>
<code className="user-select-all">{code}</code>{" "}
<Button size="sm" variant="outline-secondary" className="ml-auto">
{copied ? <ClipboardCheckFill /> : <Clipboard />}
</Button>
</span>
) : (
<code className="user-select-all">{code}</code>
)
}
</Copy>
);
}
Expand Down

0 comments on commit 1fe3394

Please sign in to comment.