-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from Mezzanine-UI/fix/react-utils
Fix the bug where scroll lock will cause layout shifting
- Loading branch information
Showing
3 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export default function getScrollbarWidth(): number { | ||
// Create a temporary div container and append it into the body | ||
const container = document.createElement('div'); | ||
|
||
// Append the element into the body | ||
document.body.appendChild(container); | ||
|
||
// Force scrollbar on the temporary div element | ||
container.style.overflow = 'scroll'; | ||
container.style.width = '100px'; | ||
container.style.height = '100px'; | ||
|
||
// Add a fake inner element to get the scrollbar width | ||
const inner = document.createElement('div'); | ||
|
||
inner.style.width = '100%'; | ||
inner.style.height = '100%'; | ||
container.appendChild(inner); | ||
|
||
// Calculate the width based on the container width minus its child width | ||
const scrollbarWidth = container.offsetWidth - inner.offsetWidth; | ||
|
||
// Remove the temporary elements from the DOM | ||
document.body.removeChild(container); | ||
|
||
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