From 8d2d9c94405f42a50c8b60d252fa80e3b57090af Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 20 Jun 2025 16:23:42 +0200 Subject: [PATCH] Core: Fall back to regular clipboard when parent frame's clipboard is not accessible --- .../syntaxhighlighter/syntaxhighlighter.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/code/core/src/components/components/syntaxhighlighter/syntaxhighlighter.tsx b/code/core/src/components/components/syntaxhighlighter/syntaxhighlighter.tsx index b073d9b2bbdf..2247aae42247 100644 --- a/code/core/src/components/components/syntaxhighlighter/syntaxhighlighter.tsx +++ b/code/core/src/components/components/syntaxhighlighter/syntaxhighlighter.tsx @@ -32,7 +32,7 @@ import type { SyntaxHighlighterRendererProps, } from './syntaxhighlighter-types'; -const { navigator, document, window: globalWindow } = global; +const { document, window: globalWindow } = global; export const supportedLanguages = { jsextra: jsExtras, @@ -59,9 +59,14 @@ const themedSyntax = memoize(2)((theme) => const copyToClipboard: (text: string) => Promise = createCopyToClipboardFunction(); export function createCopyToClipboardFunction() { - if (globalWindow.top?.navigator?.clipboard) { - const clipboard = globalWindow.top.navigator.clipboard; - return async (text: string) => clipboard.writeText(text); + if (globalWindow.navigator?.clipboard) { + return async (text: string) => { + try { + await globalWindow.top?.navigator.clipboard.writeText(text); + } catch { + await globalWindow.navigator.clipboard.writeText(text); + } + }; } return async (text: string) => { const tmp = document.createElement('TEXTAREA') as HTMLTextAreaElement;