UNPKG

@wordpress/element

Version:
69 lines (68 loc) 1.71 kB
// packages/element/src/find-dom-node.ts import deprecated from "@wordpress/deprecated"; var internalsKey = "_reactInternals"; var HostComponent = 5; var HostText = 6; function findCurrentFiber(fiber) { if (!fiber.alternate) { return fiber; } let node = fiber; while (node.return) { node = node.return; } if (node.stateNode.current === node) { return fiber; } return fiber.alternate; } function findHostFiber(fiber) { const current = findCurrentFiber(fiber); if (!current) { return null; } return findHostFiberImpl(current); } function findHostFiberImpl(fiber) { if (fiber.tag === HostComponent || fiber.tag === HostText) { return fiber; } let child = fiber.child; while (child) { const hostFiber = findHostFiberImpl(child); if (hostFiber) { return hostFiber; } child = child.sibling; } return null; } function findDOMNode(instance) { deprecated("wp.element.findDOMNode", { since: "7.1", alternative: "DOM refs", link: "https://react.dev/reference/react-dom/findDOMNode" }); if (instance === null || instance === void 0) { return null; } if (instance.nodeType !== void 0) { return instance; } const fiber = instance[internalsKey]; if (fiber === void 0) { if (typeof instance.render === "function") { throw new Error("Unable to find node on an unmounted component."); } const keys = Object.keys(instance).join(","); throw new Error( `Argument appears to not be a ReactComponent. Keys: ${keys}` ); } const hostFiber = findHostFiber(fiber); return hostFiber?.stateNode ?? null; } export { findDOMNode as default }; //# sourceMappingURL=find-dom-node.mjs.map