-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix body padding * Fix race condition with browser disposing context * Revert "Fix race condition with browser disposing context" This reverts commit 584cc6a. --------- Co-authored-by: Chris Sainty <[email protected]>
- Loading branch information
1 parent
f27ab5f
commit 671cd33
Showing
1 changed file
with
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
export function setBodyStyle() { | ||
const el = document.body; | ||
const originalProps = {overflow: el.style.overflow, paddingRight: el.style.paddingRight}; | ||
const getScrollBarWidth = () => { | ||
let el = document.createElement("div"); | ||
el.style.cssText = "overflow:scroll; visibility:hidden; position:absolute;"; | ||
document.body.appendChild(el); | ||
let width = el.offsetWidth - el.clientWidth; | ||
el.remove(); | ||
return width; | ||
} | ||
const isScrollbarPresent = () => { | ||
const beforeScrollbarHidden = document.body.clientWidth; | ||
const overflowState = document.body?.style.overflow; | ||
document.body.style.overflow = 'hidden'; | ||
const afterScrollbarHidden = document.body.clientWidth; | ||
document.body.style.overflow = overflowState; | ||
return beforeScrollbarHidden !== afterScrollbarHidden; | ||
}; | ||
|
||
export function setBodyStyle() { | ||
if (isScrollbarPresent()) { | ||
el.style.paddingRight = `${getScrollBarWidth()}px`; | ||
} | ||
|
||
el.style.overflow = 'hidden'; | ||
} | ||
|
||
export function removeBodyStyle() { | ||
document.body.style.overflow = 'auto'; | ||
el.style.overflow = originalProps.overflow || 'auto'; | ||
el.style.paddingRight = originalProps.paddingRight; | ||
} |