Skip to content

Commit 2294a89

Browse files
Merge pull request #568 from PolymathNetwork/fix/modal-lock-scroll
fix(new-ui): fix body scroll lock jump when body doesnt scroll
2 parents 015c02d + b73ff48 commit 2294a89

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

packages/new-polymath-ui/src/components/Modal/Modal.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React, { FC } from 'react';
1+
import React, { FC, useEffect, useState } from 'react';
22
import ReactModal from 'react-modal';
3+
import scrollbarWidth from 'scrollbarwidth';
34
import { withTheme, ThemeInterface, styled } from '~/styles';
45
import { Header } from './Header';
56
import { Body } from './Body';
@@ -19,6 +20,10 @@ interface Props {
1920
theme: ThemeInterface;
2021
}
2122

23+
function hasScrollbar() {
24+
return document.documentElement.scrollHeight > window.innerHeight;
25+
}
26+
2227
export const ModalBase: FC<Props> = ({
2328
children,
2429
className,
@@ -30,6 +35,7 @@ export const ModalBase: FC<Props> = ({
3035
status,
3136
}) => {
3237
let overlayRef: HTMLDivElement | null = null;
38+
const [prevOpen, setPrevOpen] = useState(false);
3339
const handleCloseRequest = () => {
3440
if (!isCloseable) {
3541
return;
@@ -47,6 +53,30 @@ export const ModalBase: FC<Props> = ({
4753
}
4854
};
4955

56+
useEffect(
57+
() => {
58+
if (prevOpen !== isOpen) {
59+
if (isOpen) {
60+
if (hasScrollbar()) {
61+
document.body.style.overflow = 'hidden';
62+
document.body.style.paddingRight = `${scrollbarWidth()}px`;
63+
}
64+
} else {
65+
document.body.style.overflow = null;
66+
document.body.style.paddingRight = null;
67+
}
68+
69+
setPrevOpen(isOpen);
70+
}
71+
72+
return () => {
73+
document.body.style.overflow = null;
74+
document.body.style.paddingRight = null;
75+
};
76+
},
77+
[isOpen]
78+
);
79+
5080
return (
5181
<ReactModal
5282
isOpen={isOpen}

packages/new-polymath-ui/src/styles/GlobalStyles.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { normalize } from 'polished';
22
import fontFaceDefinition from 'typeface-overpass';
33
import * as styledComponents from 'styled-components';
44
import { ThemedStyledComponentsModule } from 'styled-components';
5-
import scrollbarWidth from 'scrollbarwidth';
65
import { ThemeInterface } from './types';
76

87
const { createGlobalStyle } = styledComponents as ThemedStyledComponentsModule<
@@ -27,11 +26,6 @@ export const GlobalStyles = createGlobalStyle`
2726
color: ${({ theme }) => theme.colors.baseText};
2827
font-family: ${({ theme }) => theme.fontFamilies.baseText};
2928
font-weight: ${({ theme }) => theme.fontWeights.normal};
30-
31-
&.ReactModal__Body--open {
32-
overflow: hidden;
33-
padding-right: ${scrollbarWidth()}px;
34-
}
3529
}
3630
3731
strong {

0 commit comments

Comments
 (0)