@wordpress/compose
Version:
WordPress higher-order components (HOCs).
113 lines (111 loc) • 3.99 kB
JavaScript
;
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-to-clipboard/index.ts
var use_copy_to_clipboard_exports = {};
__export(use_copy_to_clipboard_exports, {
clearSelection: () => clearSelection,
copyToClipboard: () => copyToClipboard,
default: () => useCopyToClipboard
});
module.exports = __toCommonJS(use_copy_to_clipboard_exports);
var import_element = require("@wordpress/element");
var import_use_ref_effect = __toESM(require("../use-ref-effect/index.cjs"));
async function copyToClipboard(text, trigger) {
if (!trigger) {
return false;
}
const { ownerDocument } = trigger;
if (!ownerDocument) {
return false;
}
const { defaultView } = ownerDocument;
try {
if (defaultView?.navigator?.clipboard?.writeText) {
await defaultView.navigator.clipboard.writeText(text);
return true;
}
const textarea = ownerDocument.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.left = "-9999px";
textarea.style.top = "-9999px";
ownerDocument.body.appendChild(textarea);
textarea.select();
const success = ownerDocument.execCommand("copy");
textarea.remove();
return success;
} catch {
return false;
}
}
function clearSelection(trigger) {
if ("focus" in trigger && typeof trigger.focus === "function") {
trigger.focus();
}
trigger.ownerDocument?.defaultView?.getSelection()?.removeAllRanges();
}
function useUpdatedRef(value) {
const ref = (0, import_element.useRef)(value);
(0, import_element.useLayoutEffect)(() => {
ref.current = value;
}, [value]);
return ref;
}
function useCopyToClipboard(text, onSuccess) {
const textRef = useUpdatedRef(text);
const onSuccessRef = useUpdatedRef(onSuccess);
return (0, import_use_ref_effect.default)((node) => {
let isActive = true;
const handleClick = async () => {
const textToCopy = typeof textRef.current === "function" ? textRef.current() : textRef.current || "";
const success = await copyToClipboard(textToCopy, node);
if (!isActive) {
return;
}
if (success) {
clearSelection(node);
if (onSuccessRef.current) {
onSuccessRef.current();
}
}
};
node.addEventListener("click", handleClick);
return () => {
isActive = false;
node.removeEventListener("click", handleClick);
};
}, []);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
clearSelection,
copyToClipboard
});
//# sourceMappingURL=index.cjs.map