UNPKG

@wordpress/compose

Version:
100 lines (98 loc) 3.62 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/compose/src/hooks/use-copy-on-click/index.ts var use_copy_on_click_exports = {}; __export(use_copy_on_click_exports, { default: () => useCopyOnClick }); module.exports = __toCommonJS(use_copy_on_click_exports); var import_element = require("@wordpress/element"); var import_deprecated = __toESM(require("@wordpress/deprecated")); var import_use_copy_to_clipboard = require("../use-copy-to-clipboard/index.cjs"); function useCopyOnClick(ref, text, timeout = 4e3) { (0, import_deprecated.default)("wp.compose.useCopyOnClick", { since: "5.8", alternative: "wp.compose.useCopyToClipboard" }); const [hasCopied, setHasCopied] = (0, import_element.useState)(false); (0, import_element.useEffect)(() => { let isActive = true; let timeoutId; if (!ref.current) { return; } let targets; if (typeof ref.current === "string") { targets = typeof document !== "undefined" ? Array.from(document.querySelectorAll(ref.current)) : []; } else if ("length" in ref.current && typeof ref.current.length === "number") { targets = Array.from(ref.current); } else { targets = [ref.current]; } if (targets.length === 0) { return; } const handleClick = async (event) => { const trigger = event.currentTarget; if (!trigger) { return; } const success = await (0, import_use_copy_to_clipboard.copyToClipboard)( typeof text === "function" ? text() : text || "", trigger ); if (!isActive) { return; } if (success) { (0, import_use_copy_to_clipboard.clearSelection)(trigger); if (timeout) { setHasCopied(true); clearTimeout(timeoutId); timeoutId = setTimeout( () => setHasCopied(false), timeout ); } } }; for (const target of targets) { target.addEventListener("click", handleClick); } return () => { isActive = false; for (const target of targets) { target.removeEventListener("click", handleClick); } clearTimeout(timeoutId); }; }, [ref, text, timeout]); return hasCopied; } //# sourceMappingURL=index.cjs.map