Skip to content

fix(block-scroll-strategy): disable smooth scrolling before restoring scroll position #8132

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

Merged
merged 1 commit into from
Nov 2, 2017
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
21 changes: 21 additions & 0 deletions src/cdk/overlay/scroll/block-scroll-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ describe('BlockScrollStrategy', () => {
expect(document.documentElement.getBoundingClientRect().width).toBe(previousContentWidth);
});

it('should not clobber user-defined scroll-behavior', skipIOS(() => {
const root = document.documentElement;
const body = document.body;

root.style['scrollBehavior'] = body.style['scrollBehavior'] = 'smooth';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to reset this after the test?


// Get the value via the style declaration in order to
// handle browsers that don't support the property yet.
const initialRootValue = root.style['scrollBehavior'];
const initialBodyValue = root.style['scrollBehavior'];

overlayRef.attach(componentPortal);
overlayRef.detach();

expect(root.style['scrollBehavior']).toBe(initialRootValue);
expect(body.style['scrollBehavior']).toBe(initialBodyValue);

// Avoid bleeding styles into other tests.
root.style['scrollBehavior'] = body.style['scrollBehavior'] = '';
}));

/**
* Skips the specified test, if it is being executed on iOS. This is necessary, because
* programmatic scrolling inside the Karma iframe doesn't work on iOS, which renders these
Expand Down
20 changes: 17 additions & 3 deletions src/cdk/overlay/scroll/block-scroll-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,25 @@ export class BlockScrollStrategy implements ScrollStrategy {
/** Unblocks page-level scroll while the attached overlay is open. */
disable() {
if (this._isEnabled) {
const html = document.documentElement;
const body = document.body;
const previousHtmlScrollBehavior = html.style['scrollBehavior'] || '';
const previousBodyScrollBehavior = body.style['scrollBehavior'] || '';

this._isEnabled = false;
document.documentElement.style.left = this._previousHTMLStyles.left;
document.documentElement.style.top = this._previousHTMLStyles.top;
document.documentElement.classList.remove('cdk-global-scrollblock');

html.style.left = this._previousHTMLStyles.left;
html.style.top = this._previousHTMLStyles.top;
html.classList.remove('cdk-global-scrollblock');

// Disable user-defined smooth scrolling temporarily while we restore the scroll position.
// See https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior
html.style['scrollBehavior'] = body.style['scrollBehavior'] = 'auto';

window.scroll(this._previousScrollPosition.left, this._previousScrollPosition.top);

html.style['scrollBehavior'] = previousHtmlScrollBehavior;
body.style['scrollBehavior'] = previousBodyScrollBehavior;
}
}

Expand Down