Last active
November 20, 2018 08:10
-
-
Save tsmd/d1f08823eb5ee8435b064221c1c5caf7 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
let scrollbarWidth; | |
export default function measureScrollBarWidth(opts = {}) { | |
const { force = false } = opts; | |
if (force || typeof scrollbarWidth === "undefined") { | |
const div = document.createElement("div"); | |
div.style.position = "absolute"; | |
div.style.overflowY = "scroll"; | |
div.style.width = div.style.height = "100px"; | |
document.body.appendChild(div); | |
scrollbarWidth = div.offsetWidth - div.scrollWidth; | |
document.body.removeChild(div); | |
} | |
return scrollbarWidth; | |
} |
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
import measureScrollBarWidth from "./measure-scroll-bar-width"; | |
export default function setScrollbarCssVar() { | |
const scrollBarWidth = measureScrollBarWidth(); | |
document.documentElement.style.setProperty("--scrollbar-width", `${scrollBarWidth}px`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment