diff --git a/components/lib/blockui/BlockUI.vue b/components/lib/blockui/BlockUI.vue index 1dfb4d8c7..0464f271d 100755 --- a/components/lib/blockui/BlockUI.vue +++ b/components/lib/blockui/BlockUI.vue @@ -50,8 +50,7 @@ export default { }); document.body.appendChild(this.mask); - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll(); document.activeElement.blur(); } else { this.mask = DomHandler.createElement('div', { @@ -92,8 +91,7 @@ export default { if (this.fullScreen) { document.body.removeChild(this.mask); - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); } else { this.$refs.container.removeChild(this.mask); } diff --git a/components/lib/calendar/Calendar.vue b/components/lib/calendar/Calendar.vue index ef0bdb962..05c176303 100755 --- a/components/lib/calendar/Calendar.vue +++ b/components/lib/calendar/Calendar.vue @@ -1656,8 +1656,7 @@ export default { this.mask.addEventListener('click', this.maskClickListener); document.body.appendChild(this.mask); - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll(); } }, disableModality() { @@ -1691,8 +1690,7 @@ export default { } if (!hasBlockerMasks) { - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); } }, updateCurrentMetaData() { diff --git a/components/lib/dialog/Dialog.vue b/components/lib/dialog/Dialog.vue index 9587f5f08..253cab11c 100755 --- a/components/lib/dialog/Dialog.vue +++ b/components/lib/dialog/Dialog.vue @@ -203,25 +203,17 @@ export default { } if (!this.modal) { - if (this.maximized) { - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); - } else { - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); - } + this.maximized ? DomHandler.blockBodyScroll() : DomHandler.unblockBodyScroll(); } }, enableDocumentSettings() { if (this.modal || (!this.modal && this.blockScroll) || (this.maximizable && this.maximized)) { - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll(); } }, unbindDocumentState() { if (this.modal || (!this.modal && this.blockScroll) || (this.maximizable && this.maximized)) { - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); } }, onKeyDown(event) { diff --git a/components/lib/galleria/Galleria.vue b/components/lib/galleria/Galleria.vue index d8aaa4675..39c39c5d4 100755 --- a/components/lib/galleria/Galleria.vue +++ b/components/lib/galleria/Galleria.vue @@ -35,8 +35,7 @@ export default { }, beforeUnmount() { if (this.fullScreen) { - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); } this.mask = null; @@ -52,8 +51,7 @@ export default { }, onEnter(el) { this.mask.style.zIndex = String(parseInt(el.style.zIndex, 10) - 1); - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll(); this.focus(); }, onBeforeLeave() { @@ -62,8 +60,7 @@ export default { onAfterLeave(el) { ZIndexUtils.clear(el); this.containerVisible = false; - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); }, onActiveItemChange(index) { if (this.activeIndex !== index) { diff --git a/components/lib/image/Image.vue b/components/lib/image/Image.vue index 0eca4a757..5abdfa650 100644 --- a/components/lib/image/Image.vue +++ b/components/lib/image/Image.vue @@ -93,8 +93,7 @@ export default { }, onImageClick() { if (this.preview) { - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll(); this.maskVisible = true; setTimeout(() => { this.previewVisible = true; @@ -164,8 +163,7 @@ export default { !this.isUnstyled && DomHandler.addClass(this.mask, 'p-component-overlay-leave'); }, onLeave() { - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); this.$emit('hide'); }, onAfterLeave(el) { @@ -183,8 +181,7 @@ export default { this.previewVisible = false; this.rotate = 0; this.scale = 1; - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); } }, computed: { diff --git a/components/lib/sidebar/Sidebar.vue b/components/lib/sidebar/Sidebar.vue index 9817b9fe4..a0ab8caed 100755 --- a/components/lib/sidebar/Sidebar.vue +++ b/components/lib/sidebar/Sidebar.vue @@ -124,16 +124,14 @@ export default { } if (this.blockScroll) { - DomHandler.addClass(document.body, 'p-overflow-hidden'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll(); } }, disableDocumentSettings() { this.unbindOutsideClickListener(); if (this.blockScroll) { - DomHandler.removeClass(document.body, 'p-overflow-hidden'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll(); } }, onKeydown(event) { diff --git a/components/lib/utils/DomHandler.js b/components/lib/utils/DomHandler.js index 8b6df3f7a..d1f5a9b6b 100755 --- a/components/lib/utils/DomHandler.js +++ b/components/lib/utils/DomHandler.js @@ -564,6 +564,10 @@ export default { return scrollbarWidth; }, + calculateBodyScrollbarWidth() { + return window.innerWidth - document.documentElement.offsetWidth; + }, + getBrowser() { if (!this.browser) { let matched = this.resolveUserAgent(); @@ -755,5 +759,15 @@ export default { window.open(encodeURI(csv)); } } + }, + + blockBodyScroll(className = 'p-overflow-hidden') { + document.body.style.setProperty('--scrollbar-width', this.calculateBodyScrollbarWidth() + 'px'); + this.addClass(document.body, className); + }, + + unblockBodyScroll(className = 'p-overflow-hidden') { + document.body.style.removeProperty('--scrollbar-width'); + this.removeClass(document.body, className); } }; diff --git a/components/lib/utils/Utils.d.ts b/components/lib/utils/Utils.d.ts index 96daafc7d..e9b83bffd 100644 --- a/components/lib/utils/Utils.d.ts +++ b/components/lib/utils/Utils.d.ts @@ -46,6 +46,7 @@ export declare class DomHandler { static clearSelection(): void; static getSelection(): string | null; static calculateScrollbarWidth(): number; + static calculateBodyScrollbarWidth(): number; static getBrowser(): object; static resolveUserAgent(): { browser: string; version: string }; static isVisible(el: HTMLElement): boolean; @@ -64,6 +65,8 @@ export declare class DomHandler { static hasCSSAnimation(el: HTMLElement): boolean; static hasCSSTransition(el: HTMLElement): boolean; static exportCSV(csv: any, filename: string): void; + static blockBodyScroll(className?: string): void; + static unblockBodyScroll(className?: string): void; } export declare class ObjectUtils { diff --git a/layouts/default.vue b/layouts/default.vue index fb52a02f2..ea3bd8289 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -42,8 +42,7 @@ export default { } this.sidebarActive = false; - DomHandler.removeClass(document.body, 'blocked-scroll'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll('blocked-scroll'); this.$toast.removeAllGroups(); } } @@ -61,18 +60,15 @@ export default { onMenuButtonClick() { if (this.sidebarActive) { this.sidebarActive = false; - DomHandler.removeClass(document.body, 'blocked-scroll'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll('blocked-scroll'); } else { this.sidebarActive = true; - DomHandler.addClass(document.body, 'blocked-scroll'); - document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px'); + DomHandler.blockBodyScroll('blocked-scroll'); } }, onMaskClick() { this.sidebarActive = false; - DomHandler.removeClass(document.body, 'blocked-scroll'); - document.body.style.removeProperty('--scrollbar-width'); + DomHandler.unblockBodyScroll('blocked-scroll'); }, hideNews(event) { this.$appState.newsActive = false;