refactor using Set
This commit is contained in:
parent
d3a6c030f8
commit
eb695fd61d
@ -2,18 +2,16 @@ import * as bodyScrollLock from 'body-scroll-lock'
|
|||||||
|
|
||||||
let previousNavPaddingRight
|
let previousNavPaddingRight
|
||||||
let previousAppBgWrapperRight
|
let previousAppBgWrapperRight
|
||||||
let lockerEls = []
|
const lockerEls = new Set([])
|
||||||
|
|
||||||
const disableBodyScroll = (el) => {
|
const disableBodyScroll = (el) => {
|
||||||
const scrollBarGap = window.innerWidth - document.documentElement.clientWidth
|
const scrollBarGap = window.innerWidth - document.documentElement.clientWidth
|
||||||
bodyScrollLock.disableBodyScroll(el, {
|
bodyScrollLock.disableBodyScroll(el, {
|
||||||
reserveScrollBarGap: true
|
reserveScrollBarGap: true
|
||||||
})
|
})
|
||||||
if (!lockerEls.includes(el)) {
|
lockerEls.add(el)
|
||||||
lockerEls.push(el)
|
|
||||||
}
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (lockerEls.length <= 1) {
|
if (lockerEls.size <= 1) {
|
||||||
// If previousNavPaddingRight is already set, don't set it again.
|
// If previousNavPaddingRight is already set, don't set it again.
|
||||||
if (previousNavPaddingRight === undefined) {
|
if (previousNavPaddingRight === undefined) {
|
||||||
const navEl = document.getElementById('nav')
|
const navEl = document.getElementById('nav')
|
||||||
@ -32,11 +30,9 @@ const disableBodyScroll = (el) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const enableBodyScroll = (el) => {
|
const enableBodyScroll = (el) => {
|
||||||
if (lockerEls.includes(el)) {
|
lockerEls.delete(el)
|
||||||
lockerEls = lockerEls.filter(e => e !== el)
|
|
||||||
}
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (lockerEls.length === 0) {
|
if (lockerEls.size === 0) {
|
||||||
if (previousNavPaddingRight !== undefined) {
|
if (previousNavPaddingRight !== undefined) {
|
||||||
document.getElementById('nav').style.paddingRight = previousNavPaddingRight
|
document.getElementById('nav').style.paddingRight = previousNavPaddingRight
|
||||||
// Restore previousNavPaddingRight to undefined so disableBodyScroll knows it can be set again.
|
// Restore previousNavPaddingRight to undefined so disableBodyScroll knows it can be set again.
|
||||||
|
Loading…
Reference in New Issue
Block a user