Skip to content

Commit

Permalink
Fix #5077:Improve block/unblockScroll architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
habubey committed Oct 11, 2023
1 parent e9264f8 commit aa5b9df
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
9 changes: 3 additions & 6 deletions components/lib/blockui/BlockUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export const BlockUI = React.forwardRef((inProps, ref) => {
const callback = () => {
setVisibleState(false);

props.fullScreen && DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
props.fullScreen && DomHandler.unblockBodyScroll();
props.onUnblocked && props.onUnblocked();
};

Expand All @@ -46,8 +45,7 @@ export const BlockUI = React.forwardRef((inProps, ref) => {

const onPortalMounted = () => {
if (props.fullScreen) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px');
DomHandler.blockBodyScroll();
document.activeElement.blur();
}

Expand All @@ -70,8 +68,7 @@ export const BlockUI = React.forwardRef((inProps, ref) => {

useUnmountEffect(() => {
if (props.fullScreen) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
DomHandler.unblockBodyScroll();
}

ZIndexUtils.clear(maskRef.current);
Expand Down
6 changes: 2 additions & 4 deletions components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,7 @@ export const Calendar = React.memo(
touchUIMask.current.addEventListener('click', touchUIMaskClickListener.current);

document.body.appendChild(touchUIMask.current);
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px');
DomHandler.blockBodyScroll();
}
};

Expand Down Expand Up @@ -1591,8 +1590,7 @@ export const Calendar = React.memo(
}

if (!hasBlockerMasks) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
DomHandler.unblockBodyScroll();
}
};

Expand Down
6 changes: 2 additions & 4 deletions components/lib/dialog/Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,9 @@ export const Dialog = React.forwardRef((inProps, ref) => {
const isThereAnyDialogThatBlocksScrolling = document.primeDialogParams && document.primeDialogParams.some((i) => i.hasBlockScroll);

if (isThereAnyDialogThatBlocksScrolling) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px');
DomHandler.blockBodyScroll();
} else {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
DomHandler.unblockBodyScroll();
}
};

Expand Down
6 changes: 2 additions & 4 deletions components/lib/galleria/Galleria.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export const Galleria = React.memo(
};

const onEnter = () => {
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px');
DomHandler.blockBodyScroll();
};

const onEntering = () => {
Expand All @@ -84,8 +83,7 @@ export const Galleria = React.memo(
};

const onExit = () => {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
DomHandler.unblockBodyScroll();
!isUnstyled() && DomHandler.addClass(maskRef.current, 'p-component-overlay-leave');
};

Expand Down
8 changes: 3 additions & 5 deletions components/lib/image/Image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useOnEscapeKey } from '../../lib/hooks/Hooks';
import PrimeReact, { PrimeReactContext, localeOption } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
Expand All @@ -13,7 +14,6 @@ import { UndoIcon } from '../icons/undo';
import { Portal } from '../portal/Portal';
import { DomHandler, IconUtils, ObjectUtils, ZIndexUtils, classNames, mergeProps } from '../utils/Utils';
import { ImageBase } from './ImageBase';
import { useOnEscapeKey } from '../../lib/hooks/Hooks';

export const Image = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -48,8 +48,7 @@ export const Image = React.memo(
const show = () => {
if (props.preview) {
setMaskVisibleState(true);
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px');
DomHandler.blockBodyScroll();
setTimeout(() => {
setPreviewVisibleState(true);
}, 25);
Expand All @@ -59,8 +58,7 @@ export const Image = React.memo(
const hide = () => {
if (!previewClick.current) {
setPreviewVisibleState(false);
DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
DomHandler.unblockBodyScroll();
setRotateState(0);
setScaleState(1);
}
Expand Down
8 changes: 3 additions & 5 deletions components/lib/sidebar/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useOnEscapeKey } from '../../lib/hooks/Hooks';
import PrimeReact, { PrimeReactContext, localeOption } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
Expand All @@ -8,7 +9,6 @@ import { Portal } from '../portal/Portal';
import { Ripple } from '../ripple/Ripple';
import { DomHandler, IconUtils, ObjectUtils, ZIndexUtils, mergeProps } from '../utils/Utils';
import { SidebarBase } from './SidebarBase';
import { useOnEscapeKey } from '../../lib/hooks/Hooks';

export const Sidebar = React.forwardRef((inProps, ref) => {
const context = React.useContext(PrimeReactContext);
Expand Down Expand Up @@ -104,17 +104,15 @@ export const Sidebar = React.forwardRef((inProps, ref) => {
}

if (props.blockScroll) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.body.style.setProperty('--scrollbar-width', DomHandler.calculateScrollbarWidth() + 'px');
DomHandler.blockBodyScroll();
}
};

const disableDocumentSettings = () => {
unbindDocumentClickListener();

if (props.blockScroll) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
document.body.style.removeProperty('--scrollbar-width');
DomHandler.unblockBodyScroll();
}
};

Expand Down
14 changes: 14 additions & 0 deletions components/lib/utils/DomHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,10 @@ export default class DomHandler {
}
}

static calculateBodyScrollbarWidth() {
return window.innerWidth - document.documentElement.offsetWidth;
}

static getBrowser() {
if (!this.browser) {
let matched = this.resolveUserAgent();
Expand Down Expand Up @@ -860,6 +864,16 @@ export default class DomHandler {
};
}

static blockBodyScroll(className = 'p-overflow-hidden') {
document.body.style.setProperty('--scrollbar-width', this.calculateBodyScrollbarWidth() + 'px');
this.addClass(document.body, className);
}

static unblockBodyScroll(className = 'p-overflow-hidden') {
document.body.style.removeProperty('--scrollbar-width');
this.removeClass(document.body, className);
}

static isVisible(element) {
// https://stackoverflow.com/a/59096915/502366 (in future use IntersectionObserver)
return element && (element.clientHeight !== 0 || element.getClientRects().length !== 0 || getComputedStyle(element).display !== 'none');
Expand Down
3 changes: 3 additions & 0 deletions components/lib/utils/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export declare class DomHandler {
static scrollInView(container: HTMLElement, item: HTMLElement): void;
static clearSelection(): void;
static calculateScrollbarWidth(el: HTMLElement): number;
static calculateBodyScrollbarWidth(): number;
static getBrowser(): object;
static blockBodyScroll(className?: string): void;
static unblockBodyScroll(className?: string): void;
static resolveUserAgent(): { browser: string; version: string };
static isVisible(el: HTMLElement): boolean;
static isExist(el: HTMLElement): boolean;
Expand Down

0 comments on commit aa5b9df

Please sign in to comment.