@stokr/components-library
Version:
STOKR - Components Library
47 lines (42 loc) • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.RenderToBody = void 0;
var _react = _interopRequireWildcard(require("react"));
var _client = require("react-dom/client");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const RenderToBody = _ref => {
let {
children
} = _ref;
const containerRef = (0, _react.useRef)(null);
const rootRef = (0, _react.useRef)(null);
(0, _react.useEffect)(() => {
// Create container div
containerRef.current = document.createElement('div');
document.body.appendChild(containerRef.current);
// Create React root
rootRef.current = (0, _client.createRoot)(containerRef.current);
// Render children
rootRef.current.render(children);
// Cleanup function
return () => {
if (rootRef.current) {
rootRef.current.unmount();
}
if (containerRef.current && document.body.contains(containerRef.current)) {
document.body.removeChild(containerRef.current);
}
};
}, [children]);
// Re-render when children change
(0, _react.useEffect)(() => {
if (rootRef.current) {
rootRef.current.render(children);
}
}, [children]);
return null;
};
exports.RenderToBody = RenderToBody;
var _default = exports.default = RenderToBody;