piral-core
Version:
The core library for creating a Piral instance.
36 lines • 1.21 kB
JavaScript
import * as React from 'react';
function removeAll(nodes) {
nodes.forEach((node) => node.remove());
}
const SlotCarrier = ({ nodes }) => {
const host = React.useRef();
React.useEffect(() => {
host.current?.append(...nodes);
return () => removeAll(nodes);
}, [nodes]);
if (nodes.length) {
return React.createElement("piral-slot", { ref: host });
}
return null;
};
/**
* Transforms the given component to an extension component.
* @param Component The component to transform.
* @returns The extension component (receiving its props via params).
*/
export function toExtension(Component) {
return (props) => React.createElement(Component, { ...props.params });
}
/**
* Reactifies the list of child nodes to a React Node by removing the
* nodes from the DOM and carrying it in a React Node, where it would be
* attached at a slot.
* @param childNodes The child nodes to reactify.
* @returns The React Node.
*/
export function reactifyContent(childNodes) {
const nodes = Array.prototype.filter.call(childNodes, Boolean);
removeAll(nodes);
return React.createElement(SlotCarrier, { nodes: nodes });
}
//# sourceMappingURL=extension.js.map