Last active
July 4, 2021 21:39
-
-
Save cecigarcia/9a04d4614710367fc6db9738a559c187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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