mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
fix: handle undefined navigator.clipboard with fallback (#697)
- Add optional chaining check for navigator.clipboard - Fallback to textarea + execCommand for SSR/non-HTTPS contexts - Fixes TypeError when clipboard API is unavailable Co-authored-by: Дмитрий Золотарь <d.zolotar@solarl.ru>
This commit is contained in:
co-authored by
Дмитрий Золотарь
parent
afc4c308bb
commit
850766d54f
@@ -12,7 +12,21 @@ export function useCopyToClipboard(resetDelay = 2000) {
|
||||
const timeoutRef = useRef(null);
|
||||
|
||||
const copy = useCallback((text, id = "default") => {
|
||||
navigator.clipboard.writeText(text);
|
||||
const write = async () => {
|
||||
if (navigator?.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} else {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.opacity = "0";
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
document.execCommand("copy");
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
};
|
||||
write();
|
||||
setCopied(id);
|
||||
|
||||
if (timeoutRef.current) {
|
||||
|
||||
Reference in New Issue
Block a user