UNPKG

@wordpress/compose

Version:
67 lines (65 loc) 2.46 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/compose/src/hooks/use-merge-refs/index.js var use_merge_refs_exports = {}; __export(use_merge_refs_exports, { default: () => useMergeRefs }); module.exports = __toCommonJS(use_merge_refs_exports); var import_element = require("@wordpress/element"); function assignRef(ref, value) { if (typeof ref === "function") { ref(value); } else if (ref && ref.hasOwnProperty("current")) { ref.current = value; } } function useMergeRefs(refs) { const element = (0, import_element.useRef)(); const isAttachedRef = (0, import_element.useRef)(false); const didElementChangeRef = (0, import_element.useRef)(false); const previousRefsRef = (0, import_element.useRef)([]); const currentRefsRef = (0, import_element.useRef)(refs); currentRefsRef.current = refs; (0, import_element.useLayoutEffect)(() => { if (didElementChangeRef.current === false && isAttachedRef.current === true) { refs.forEach((ref, index) => { const previousRef = previousRefsRef.current[index]; if (ref !== previousRef) { assignRef(previousRef, null); assignRef(ref, element.current); } }); } previousRefsRef.current = refs; }, refs); (0, import_element.useLayoutEffect)(() => { didElementChangeRef.current = false; }); return (0, import_element.useCallback)((value) => { assignRef(element, value); didElementChangeRef.current = true; isAttachedRef.current = value !== null; const refsToAssign = value ? currentRefsRef.current : previousRefsRef.current; for (const ref of refsToAssign) { assignRef(ref, value); } }, []); } //# sourceMappingURL=index.js.map