Skip to content

Commit f2e602e

Browse files
committed
fix(web): allow clipboard API writes without a window global
1 parent 795f33c commit f2e602e

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎apps/web/src/hooks/useCopyToClipboard.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ export async function writeTextToClipboard(
100100
target = "text",
101101
extraFlavors?: Readonly<Record<string, string>>,
102102
) {
103-
if (typeof window === "undefined") {
103+
// The DOM fallback below needs `window`; the async clipboard API itself does not, and some
104+
// hosts (tests, embedded bridges) expose `navigator.clipboard` without a `window` global.
105+
const canUseClipboardApi =
106+
typeof navigator !== "undefined" && typeof navigator.clipboard?.writeText === "function";
107+
if (typeof window === "undefined" && !canUseClipboardApi) {
104108
throw new ClipboardApiUnavailableError({
105109
target,
106110
});
@@ -124,7 +128,7 @@ export async function writeTextToClipboard(
124128
),
125129
};
126130

127-
if (typeof navigator === "undefined" || !navigator.clipboard?.writeText) {
131+
if (!canUseClipboardApi) {
128132
if (writeTextWithExecCommand(value, extraFlavors)) return true;
129133
throw new ClipboardApiUnavailableError({
130134
target,

0 commit comments

Comments
 (0)