UNPKG

@onesy/utils

Version:
33 lines (32 loc) 1.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const is_1 = __importDefault(require("./is")); const stringify_1 = __importDefault(require("./stringify")); const copyToClipboardFallback = (value_) => { const value = (0, is_1.default)('string', value_) ? value_ : (0, stringify_1.default)(value_); const textArea = document.createElement('textarea'); textArea.value = value; // Avoid scrolling to bottom textArea.style.top = '0'; textArea.style.left = '0'; textArea.style.position = 'fixed'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { document.execCommand('copy'); } catch (error) { } // Clean up textArea.remove(); }; const copyToClipboard = async (value_) => { const value = (0, is_1.default)('string', value_) ? value_ : (0, stringify_1.default)(value_); if (!navigator.clipboard) return copyToClipboardFallback(value); await navigator.clipboard.writeText(value); }; exports.default = copyToClipboard;