Skip to content

Instantly share code, notes, and snippets.

@cecigarcia
Last active July 4, 2021 21:39
Show Gist options
  • Save cecigarcia/9a04d4614710367fc6db9738a559c187 to your computer and use it in GitHub Desktop.
Save cecigarcia/9a04d4614710367fc6db9738a559c187 to your computer and use it in GitHub Desktop.
const containerStyle = {
display: "inline-block",
position: "absolute",
visibility: "hidden",
zIndex: -1,
};
export const measureDomNode = (node, enhanceMeasurableNode = e => e) => {
const container = document.createElement("div");
container.style = containerStyle;
const clonedNode = node.cloneNode(true);
const content = enhanceMeasurableNode(clonedNode);
container.appendChild(content);
document.body.appendChild(container);
const height = container.clientHeight;
const width = container.clientWidth;
container.parentNode.removeChild(container);
return { height, width };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment