nsarrazin HF Staff commited on
Commit
2ac7abd
·
unverified ·
1 Parent(s): afc9294

fix(front): fix clipboard copy on chrome (#1497)

Browse files
Files changed (1) hide show
  1. src/lib/utils/share.ts +7 -5
src/lib/utils/share.ts CHANGED
@@ -17,10 +17,12 @@ export async function share(url: string, title: string, appendLeafId: boolean =
17
  if (navigator.share && !isDesktop(window)) {
18
  navigator.share({ url, title });
19
  } else {
20
- if (document.hasFocus()) {
21
- await navigator.clipboard.writeText(url);
22
- } else {
23
- alert("Document is not focused. Please try again.");
24
- }
 
 
25
  }
26
  }
 
17
  if (navigator.share && !isDesktop(window)) {
18
  navigator.share({ url, title });
19
  } else {
20
+ // this is really ugly
21
+ // but on chrome the clipboard write doesn't work if the window isn't focused
22
+ // and after we use confirm() to ask the user if they want to share, the window is no longer focused
23
+ // for a few ms until the confirm dialog closes. tried await tick(), tried window.focus(), didnt work
24
+ // bug doesnt occur in firefox, if you can find a better fix for it please do
25
+ await new Promise((resolve) => setTimeout(resolve, 250));
26
+ await navigator.clipboard.writeText(url);
27
  }
28
  }