Skip to content

Commit 06b3ea6

Browse files
authored
docs: Fix copying markdown in Safari (#9198)
* docs: Fix copying markdown in Safari * Add pending state for slow networks
1 parent 0101e6e commit 06b3ea6

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

packages/dev/s2-docs/src/MarkdownMenu.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface MarkdownMenuProps {
1515
export function MarkdownMenu({url}: MarkdownMenuProps) {
1616
let mdUrl = (url ?? '').replace(/\.html?$/i, '') + '.md';
1717
let [isCopied, setIsCopied] = useState(false);
18+
let [isPending, setPending] = useState(false);
1819
let timeout = useRef<ReturnType<typeof setTimeout> | null>(null);
1920

2021
let pageUrl = typeof window !== 'undefined' && url ? new URL(url, window.location.origin).href : url ?? '';
@@ -38,20 +39,25 @@ export function MarkdownMenu({url}: MarkdownMenuProps) {
3839
}
3940
if (typeof navigator !== 'undefined' && navigator.clipboard) {
4041
try {
41-
let response = await fetch(mdUrl);
42-
let markdown = await response.text();
43-
await navigator.clipboard.writeText(markdown);
42+
setPending(true);
43+
await navigator.clipboard.write([
44+
new ClipboardItem({
45+
['text/plain']: fetch(mdUrl).then(res => res.text())
46+
})
47+
]);
4448
setIsCopied(true);
4549
timeout.current = setTimeout(() => setIsCopied(false), 2000);
4650
} catch {
4751
ToastQueue.negative('Failed to copy markdown.');
52+
} finally {
53+
setPending(false);
4854
}
4955
}
5056
}, [mdUrl]);
5157

5258
return (
5359
<div className={style({display: 'flex', justifyContent: 'space-between', paddingX: 4, paddingBottom: 16})}>
54-
<ActionButton isQuiet size="M" onPress={handleCopy}>
60+
<ActionButton isQuiet size="M" onPress={handleCopy} isPending={isPending}>
5561
{isCopied ? <CheckmarkCircle /> : <Copy />}
5662
<Text>Copy for LLM</Text>
5763
</ActionButton>

0 commit comments

Comments
 (0)