Skip to content

Instantly share code, notes, and snippets.

@adaniliuk
Created September 23, 2016 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaniliuk/52b2ed8900538ade4f474c43e76fe0a6 to your computer and use it in GitHub Desktop.
Save adaniliuk/52b2ed8900538ade4f474c43e76fe0a6 to your computer and use it in GitHub Desktop.
import { PropTypes, Children } from 'react';
import classNames from 'classnames';
/**
* Defines whether it's server or client side rendering environment (can use DOM client side)
*
* @type {Boolean}
*/
const canUseDOM = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
/**
* Page component
* On server side renders the children components above the fold only
*
* @param {Object} props Component properties
* @return {ReactElement} Page component React element
*/
function Page(props) {
const pageClassName = classNames('page', props.className);
let newChildren;
if (canUseDOM) {
newChildren = props.children;
} else {
let metTheFold = false;
newChildren = [];
Children.forEach(props.children, (child) => {
if (metTheFold) return;
if (child.isTheFold) {
metTheFold = true;
return;
}
newChildren.push(child);
});
}
return (
<div className={pageClassName}>
{newChildren}
</div>
);
}
}
/**
* Page component propTypes definition
*
* @type {Object}
*/
Page.propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string
};
export default Page;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment