@moxy/next-layout
Version:
Add persistent and nested layouts to your Next.js projects in a declarative way
31 lines (24 loc) • 786 B
JavaScript
import { isValidElement, cloneElement } from 'react';
const validateLayoutTree = node => {
/* istanbul ignore if */
if (process.env.NODE_ENV === 'production') {
return;
}
if (!isValidElement(node)) {
throw new TypeError('Only unary trees composed by react elements are supported as layouts');
}
if (node.props.children) {
validateLayoutTree(node.props.children);
}
};
const addPageToLayoutTree = (layoutNode, page) => cloneElement(layoutNode, {
children: layoutNode.props.children ? addPageToLayoutTree(layoutNode.props.children, page) : page
});
const createFullTree = (layoutTree, page) => {
if (!layoutTree) {
return page;
}
validateLayoutTree(layoutTree);
return addPageToLayoutTree(layoutTree, page);
};
export default createFullTree;