Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(modals): prevent html overflow #894

Merged
merged 3 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/modals/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"index.cjs.js": {
"bundled": 52497,
"minified": 37987,
"gzipped": 7696
"bundled": 52699,
"minified": 38025,
"gzipped": 7718
},
"index.esm.js": {
"bundled": 48983,
"minified": 34897,
"gzipped": 7518,
"bundled": 49185,
"minified": 34935,
"gzipped": 7543,
"treeshaked": {
"rollup": {
"code": 27649,
"code": 27687,
"import_statements": 792
},
"webpack": {
"code": 30826
"code": 30864
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,20 @@ describe('DrawerModal', () => {

it('only locks page scrolling when drawer modal is opened', async () => {
const { getByText, getByRole, queryByRole } = render(<Example />);
const htmlElement = document.querySelector('html');

expect(queryByRole('dialog')).not.toBeInTheDocument();
expect(document.body).not.toHaveAttribute('style', 'overflow: hidden;');
expect(htmlElement).not.toHaveAttribute('style', 'overflow: hidden;');

userEvent.click(getByText('Open Drawer'));

expect(getByRole('dialog')).toBeInTheDocument();
expect(document.body).toHaveAttribute('style', 'overflow: hidden;');
expect(htmlElement).toHaveAttribute('style', 'overflow: hidden;');

userEvent.type(getByRole('dialog'), '{esc}');

await waitFor(() => expect(queryByRole('dialog')).not.toBeInTheDocument());
expect(document.body).not.toHaveAttribute('style', 'overflow: hidden;');
expect(htmlElement).not.toHaveAttribute('style', 'overflow: hidden;');
});

it('applies backdropProps to Backdrop element', () => {
Expand Down
19 changes: 10 additions & 9 deletions packages/modals/src/elements/DrawerModal/DrawerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,20 @@ export const DrawerModal = forwardRef<
return undefined;
}

const bodyElement = environment.querySelector('body');
const htmlElement = environment.querySelector('html');
let previousHtmlOverflow: string;

if (bodyElement && isOpen) {
const previousBodyOverflow = bodyElement.style.overflow;
if (htmlElement && isOpen) {
previousHtmlOverflow = htmlElement.style.overflow;

bodyElement.style.overflow = 'hidden';

return () => {
bodyElement.style.overflow = previousBodyOverflow;
};
htmlElement.style.overflow = 'hidden';
}

return undefined;
return () => {
if (htmlElement && isOpen) {
htmlElement.style.overflow = previousHtmlOverflow;
austingreendev marked this conversation as resolved.
Show resolved Hide resolved
}
};
}, [environment, isOpen]);

const rootNode = useMemo(() => {
Expand Down
22 changes: 13 additions & 9 deletions packages/modals/src/elements/Modal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,27 @@ describe('Modal', () => {
});

describe('componentDidMount()', () => {
it('should disable overflow scrolling for body element', () => {
const { baseElement } = render(<BasicExample />);
it('should disable overflow scrolling for the html element', () => {
render(<BasicExample />);

expect(baseElement.style.overflow).toBe('hidden');
expect(document.querySelector('html')?.style.overflow).toBe('hidden');
});
});

describe('componentWillUnmount()', () => {
it('should apply previous body overflow style', () => {
document.body.style.overflow = 'auto';
const { baseElement, unmount } = render(<BasicExample />);
it('should apply previous html overflow style', () => {
const htmlElement = document.querySelector('html');

expect(baseElement.style.overflow).toBe('hidden');
if (htmlElement) {
htmlElement.style.overflow = 'scroll';

unmount();
const { unmount } = render(<BasicExample />);

expect(baseElement.style.overflow).toBe('auto');
expect(htmlElement.style.overflow).toBe('hidden');
unmount();

expect(htmlElement.style.overflow).toBe('scroll');
}
});
});

Expand Down
13 changes: 10 additions & 3 deletions packages/modals/src/elements/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export const Modal = React.forwardRef<HTMLDivElement, IModalProps>(
return undefined;
}

const htmlElement = environment.querySelector('html');
const bodyElement = environment.querySelector('body');
let previousHtmlOverflow: string;
let previousBodyPaddingRight: string;

if (bodyElement) {
Expand All @@ -138,12 +140,17 @@ export const Modal = React.forwardRef<HTMLDivElement, IModalProps>(
bodyElement.style.paddingRight = `${bodyPaddingRight + scrollbarSize}px`;
}

const previousBodyOverflow = bodyElement.style.overflow;
if (htmlElement) {
previousHtmlOverflow = htmlElement.style.overflow;

bodyElement.style.overflow = 'hidden';
htmlElement.style.overflow = 'hidden';
}

return () => {
bodyElement.style.overflow = previousBodyOverflow;
if (htmlElement) {
htmlElement.style.overflow = previousHtmlOverflow;
}

bodyElement.style.paddingRight = previousBodyPaddingRight;
};
}
Expand Down