Skip to content

Commit

Permalink
fix(plasma-new-hope): fix no scroll in Modal
Browse files Browse the repository at this point in the history
  • Loading branch information
kayman233 committed Nov 10, 2023
1 parent 2fada60 commit 68bf296
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { css } from '@linaria/core';

export const noScroll = css`
:global(body) {
overflow-y: hidden;
:global() {
body.disable-scroll {
overflow-y: hidden;
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const modalRoot = (Root: RootProps<HTMLInputElement, ModalProps>) =>
popupInfo,
children,
view,
isOpen,
...rest
},
outerRootRef,
Expand All @@ -50,11 +51,12 @@ export const modalRoot = (Root: RootProps<HTMLInputElement, ModalProps>) =>
const uniqId = useUniqId();
const innerId = id || uniqId;

const { modalInfo } = useModal({ id: innerId, closeOnEsc, onEscKeyDown, onClose, popupInfo });
const { modalInfo } = useModal({ id: innerId, isOpen, closeOnEsc, onEscKeyDown, onClose, popupInfo });

return (
<Popup
id={id}
isOpen={isOpen}
ref={innerRef}
popupInfo={modalInfo}
withAnimation={withAnimation}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export type ModalBaseRootProps = PopupRootProps & Pick<ModalProps, 'initialFocus
export type ModalOverlayProps = Pick<PopupRootProps, 'id' | 'zIndex'> &
Pick<ModalProps, 'withBlur' | 'closeOnOverlayClick' | 'onOverlayClick' | 'onClose'>;

export type ModalHookArgs = Pick<PopupHookArgs, 'id' | 'popupInfo'> &
export type ModalHookArgs = Pick<PopupHookArgs, 'id' | 'popupInfo' | 'isOpen'> &
Pick<ModalProps, 'closeOnEsc' | 'onEscKeyDown' | 'onClose'>;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const getModals = (items: ModalInfo[]) => {
return items.filter((item: ModalInfo) => item?.info?.isModal);
};

export const hasModals = (items: ModalInfo[]) => {
return getModals(items).length > 0;
};

export const getIdLastModal = (items: ModalInfo[]) => {
const modals = getModals(items);
if (!modals.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, useEffect } from 'react';

import { ModalInfo, getIdLastModal } from '../ModalContext';
import { ModalInfo, getIdLastModal, hasModals } from '../ModalContext';
import { ModalHookArgs } from '../Modal.types';
import { usePopupContext } from '../../../Popup';

const ESCAPE_KEYCODE = 27;

export const useModal = ({ id, popupInfo, onEscKeyDown, onClose, closeOnEsc = true }: ModalHookArgs) => {
export const useModal = ({ id, isOpen, popupInfo, onEscKeyDown, onClose, closeOnEsc = true }: ModalHookArgs) => {
const popupController = usePopupContext();

// При ESC закрывает текущее окно, если это возможно
Expand Down Expand Up @@ -36,6 +36,15 @@ export const useModal = ({ id, popupInfo, onEscKeyDown, onClose, closeOnEsc = tr
};
}, [onClose, onEscKeyDown, popupController.items, closeOnEsc]);

// linaria не поддерживает динамический global
useEffect(() => {
if (isOpen) {
document.body.classList.add('disable-scroll');
} else if (!hasModals(popupController.items)) {
document.body.classList.remove('disable-scroll');
}
}, [isOpen, popupController.items]);

const modalInfo: ModalInfo = {
id,
info: {
Expand Down

0 comments on commit 68bf296

Please sign in to comment.