Skip to content

Commit

Permalink
fix(a11y): prevented page scroll when nav menu is opened
Browse files Browse the repository at this point in the history
Co-authored-by: Massimo Marsan <[email protected]>
Co-authored-by: Andrea Stagi <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2022
1 parent 54af974 commit 127b416
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/js/plugins/navbar-collapsible.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SelectorEngine from 'bootstrap/js/src/dom/selector-engine'

import { isScreenMobile } from './util/device'
import { getElementIndex } from './util/dom'
import { disablePageScroll, enablePageScroll } from './util/pageScroll'

const NAME = 'navbarcollapsible'
const DATA_KEY = 'bs.navbarcollapsible'
Expand Down Expand Up @@ -92,6 +93,7 @@ class NavBarCollapsible extends BaseComponent {

this._isShown = true

disablePageScroll()
this._showElement()
}

Expand Down Expand Up @@ -123,6 +125,7 @@ class NavBarCollapsible extends BaseComponent {

this._element.classList.remove(CLASS_NAME_EXPANDED)

enablePageScroll()
this._queueCallback(() => this._hideElement(), this._menuWrapper, isAnimated)
}

Expand Down
26 changes: 26 additions & 0 deletions src/js/plugins/util/pageScroll.js
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
}
})
1 change: 1 addition & 0 deletions src/scss/bootstrap-italia-comuni.scss
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ $card-comuni-bg-dark: #2c2c2c;
@import 'custom/images';
@import 'custom/just-validate';
@import 'custom/accessible-autocomplete';
@import 'custom/page-scroll';

// datepicker
@import 'custom/form-datepicker';
Expand Down
1 change: 1 addition & 0 deletions src/scss/bootstrap-italia.scss
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
@import 'custom/images';
@import 'custom/just-validate';
@import 'custom/accessible-autocomplete';
@import 'custom/page-scroll';

// datepicker
@import 'custom/form-datepicker';
Expand Down
3 changes: 3 additions & 0 deletions src/scss/custom/_page-scroll.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pagescroll-scroll-disabled {
scroll-behavior: auto !important;
}

0 comments on commit 127b416

Please sign in to comment.