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

[Modal] Fix scroll jump issue #18808

Merged
merged 3 commits into from
Dec 12, 2019
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
40 changes: 19 additions & 21 deletions packages/material-ui/src/Modal/ModalManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,8 @@ function handleContainer(containerInfo, props) {
let fixedNodes;

if (!props.disableScrollLock) {
const overflowing = isOverflowing(container);

// Improve Gatsby support
// https://css-tricks.com/snippets/css/force-vertical-scrollbar/
const parent = container.parentElement;
const scrollContainer =
parent.nodeName === 'HTML' && window.getComputedStyle(parent)['overflow-y'] === 'scroll'
? parent
: container;

restoreStyle.push({
value: scrollContainer.style.overflow,
key: 'overflow',
el: scrollContainer,
});

// Block the scroll even if no scrollbar is visible to account for mobile keyboard
// screensize shrink.
scrollContainer.style.overflow = 'hidden';

if (overflowing) {
if (isOverflowing(container)) {
// Compute the size before applying overflow hidden to avoid any scroll jumps.
const scrollbarSize = getScrollbarSize();

restoreStyle.push({
Expand All @@ -97,6 +78,23 @@ function handleContainer(containerInfo, props) {
node.style.paddingRight = `${getPaddingRight(node) + scrollbarSize}px`;
});
}

// Improve Gatsby support
// https://css-tricks.com/snippets/css/force-vertical-scrollbar/
const parent = container.parentElement;
const scrollContainer =
parent.nodeName === 'HTML' && window.getComputedStyle(parent)['overflow-y'] === 'scroll'
? parent
: container;

// Block the scroll even if no scrollbar is visible to account for mobile keyboard
// screensize shrink.
restoreStyle.push({
value: scrollContainer.style.overflow,
key: 'overflow',
el: scrollContainer,
});
scrollContainer.style.overflow = 'hidden';
}

const restore = () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/material-ui/src/Modal/ModalManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ describe('ModalManager', () => {
assert.strictEqual(fixedNode.style.paddingRight, '14px');
});

it('should disable the scroll even when not overflowing', () => {
// simulate non-overflowing container
const container2 = document.createElement('div');
Object.defineProperty(container2, 'scrollHeight', {
value: 100,
writable: false,
});
Object.defineProperty(container2, 'clientHeight', {
value: 100,
writable: false,
});
document.body.appendChild(container2);

const modal = {};
modalManager.add(modal, container2);
modalManager.mount(modal, {});
assert.strictEqual(container2.style.overflow, 'hidden');
modalManager.remove(modal);
assert.strictEqual(container2.style.overflow, '');

document.body.removeChild(container2);
});

it('should restore styles correctly if none existed before', () => {
const modal = {};
modalManager.add(modal, container1);
Expand Down