Skip to content

Commit a9a933e

Browse files
skshetryclaude
andauthored
Fix copy page button not working in Safari (#5560)
* Fix copy page button not working in Safari Safari requires clipboard writes to begin in the synchronous call stack of a user gesture. The previous code awaited fetch() before calling navigator.clipboard.writeText(), breaking the gesture chain. Use ClipboardItem with a Promise<Blob> so the clipboard write starts synchronously while content resolves asynchronously. https://claude.ai/code/session_018e1sgeEGsu8UsSYkq6tkfb * Simplify clipboard write to use ClipboardItem unconditionally Remove the ClipboardItem.supports check and writeText fallback. All modern browsers support ClipboardItem with Promise<Blob>, and the supports() method may not exist in older Safari versions that still need the synchronous gesture fix. https://claude.ai/code/session_018e1sgeEGsu8UsSYkq6tkfb --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent e6e63b0 commit a9a933e

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

  • src/components/Documentation/CopyPageButton

src/components/Documentation/CopyPageButton/index.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@ const CopyPageButton: React.FC<ICopyPageButtonProps> = ({ pagePath }) => {
2626
setCopyState('copying')
2727
setOpen(false)
2828
try {
29-
const res = await fetch(markdownUrl)
30-
if (!res.ok) throw new Error(res.statusText)
31-
const text = await res.text()
32-
await navigator.clipboard.writeText(text)
29+
const textPromise = fetch(markdownUrl).then(res => {
30+
if (!res.ok) throw new Error(res.statusText)
31+
return res.text()
32+
})
33+
34+
// Safari requires clipboard writes to start in the synchronous call
35+
// stack of a user gesture. Using ClipboardItem with a Promise<Blob>
36+
// lets us begin the write synchronously while content resolves async.
37+
await navigator.clipboard.write([
38+
new ClipboardItem({
39+
'text/plain': textPromise.then(
40+
t => new Blob([t], { type: 'text/plain' })
41+
)
42+
})
43+
])
3344
} catch {
3445
setCopyState('idle')
3546
return

0 commit comments

Comments
 (0)