-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(a11y): prevented page scroll when nav menu is opened
Co-authored-by: Massimo Marsan <[email protected]> Co-authored-by: Andrea Stagi <[email protected]>
- Loading branch information
1 parent
54af974
commit 127b416
Showing
5 changed files
with
34 additions
and
0 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,26 @@ | ||
/** | ||
* Prevents page scroll | ||
*/ | ||
|
||
const CLASS_SCROLL_DISABLED = 'pagescroll-scroll-disabled' | ||
|
||
let disabled = false | ||
let currentScrollPos = document.scrollingElement.scrollTop | ||
const htmlContainer = document.querySelector('html') | ||
|
||
export function disablePageScroll() { | ||
disabled = true | ||
currentScrollPos = document.scrollingElement.scrollTop | ||
htmlContainer.classList.add(CLASS_SCROLL_DISABLED) | ||
} | ||
|
||
export function enablePageScroll() { | ||
disabled = false | ||
htmlContainer.classList.remove(CLASS_SCROLL_DISABLED) | ||
} | ||
|
||
document.addEventListener('scroll', () => { | ||
if (disabled) { | ||
document.scrollingElement.scrollTop = currentScrollPos | ||
} | ||
}) |
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
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,3 @@ | ||
.pagescroll-scroll-disabled { | ||
scroll-behavior: auto !important; | ||
} |