-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Avoid scroll bleed when displaying modal UI on mobile #4398
Merged
brandonpayton
merged 1 commit into
WordPress:master
from
brandonpayton:add/mobile-scroll-lock-for-modals
Mar 21, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
ScrollLock | ||
========== | ||
|
||
ScrollLock is a content-free React component for declaratively preventing scroll bleed from modal UI to the page body. This component applies a `lockscroll` class to the `document.documentElement` and `document.scrollingElement` elements to stop the body from scrolling. When it is present, the lock is applied. | ||
|
||
## Usage | ||
|
||
Declare scroll locking as part of modal UI. | ||
|
||
```jsx | ||
import { ScrollLock } from '@wordpress/components'; | ||
|
||
function Sidebar( { isMobile } ) { | ||
return ( | ||
<div> | ||
Sidebar Content! | ||
{ isMobile && <ScrollLock /> } | ||
</div> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './index.scss'; | ||
|
||
/** | ||
* Creates a ScrollLock component bound to the specified document. | ||
* | ||
* This function creates a ScrollLock component for the specified document | ||
* and is exposed so we can create an isolated component for unit testing. | ||
* | ||
* @param {Object} args Keyword args. | ||
* @param {HTMLDocument} args.htmlDocument The document to lock the scroll for. | ||
* @param {string} args.className The name of the class used to lock scrolling. | ||
* @return {Component} The bound ScrollLock component. | ||
*/ | ||
export function createScrollLockComponent( { | ||
htmlDocument = document, | ||
className = 'lockscroll', | ||
} = {} ) { | ||
let lockCounter = 0; | ||
|
||
/* | ||
* Setting `overflow: hidden` on html and body elements resets body scroll in iOS. | ||
* Save scroll top so we can restore it after locking scroll. | ||
* | ||
* NOTE: It would be cleaner and possibly safer to find a localized solution such | ||
* as preventing default on certain touchmove events. | ||
*/ | ||
let previousScrollTop = 0; | ||
|
||
/** | ||
* Locks and unlocks scroll depending on the boolean argument. | ||
* | ||
* @param {boolean} locked Whether or not scroll should be locked. | ||
*/ | ||
function setLocked( locked ) { | ||
const scrollingElement = htmlDocument.scrollingElement || htmlDocument.body; | ||
|
||
if ( locked ) { | ||
previousScrollTop = scrollingElement.scrollTop; | ||
} | ||
|
||
const methodName = locked ? 'add' : 'remove'; | ||
scrollingElement.classList[ methodName ]( className ); | ||
|
||
// Adding the class to the document element seems to be necessary in iOS. | ||
htmlDocument.documentElement.classList[ methodName ]( className ); | ||
|
||
if ( ! locked ) { | ||
scrollingElement.scrollTop = previousScrollTop; | ||
} | ||
} | ||
|
||
/** | ||
* Requests scroll lock. | ||
* | ||
* This function tracks requests for scroll lock. It locks scroll on the first | ||
* request and counts each request so `releaseLock` can unlock scroll when | ||
* all requests have been released. | ||
*/ | ||
function requestLock() { | ||
if ( lockCounter === 0 ) { | ||
setLocked( true ); | ||
} | ||
|
||
++lockCounter; | ||
} | ||
|
||
/** | ||
* Releases a request for scroll lock. | ||
* | ||
* This function tracks released requests for scroll lock. When all requests | ||
* have been released, it unlocks scroll. | ||
*/ | ||
function releaseLock() { | ||
if ( lockCounter === 1 ) { | ||
setLocked( false ); | ||
} | ||
|
||
--lockCounter; | ||
} | ||
|
||
return class ScrollLock extends Component { | ||
/** | ||
* Requests scroll lock on mount. | ||
*/ | ||
componentDidMount() { | ||
requestLock(); | ||
} | ||
/** | ||
* Releases scroll lock before unmount. | ||
*/ | ||
componentWillUnmount() { | ||
releaseLock(); | ||
} | ||
|
||
/** | ||
* Render nothing as this component is merely a way to declare scroll lock. | ||
* | ||
* @return {null} Render nothing by returning `null`. | ||
*/ | ||
render() { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
export default createScrollLockComponent(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
html.lockscroll, | ||
body.lockscroll { | ||
overflow: hidden; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mount } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createScrollLockComponent } from '..'; | ||
|
||
describe( 'scroll-lock', () => { | ||
const lockingClassName = 'test-lock-scroll'; | ||
|
||
// Use a separate document to reduce the risk of test side-effects. | ||
let testDocument = null; | ||
let ScrollLock = null; | ||
let wrapper = null; | ||
|
||
function expectLocked( locked ) { | ||
expect( testDocument.documentElement.classList.contains( lockingClassName ) ).toBe( locked ); | ||
// Assert against `body` because `scrollingElement` does not exist on our test DOM implementation. | ||
expect( testDocument.body.classList.contains( lockingClassName ) ).toBe( locked ); | ||
} | ||
|
||
beforeEach( () => { | ||
testDocument = document.implementation.createHTMLDocument( 'Test scroll-lock' ); | ||
ScrollLock = createScrollLockComponent( { | ||
htmlDocument: testDocument, | ||
className: lockingClassName, | ||
} ); | ||
} ); | ||
|
||
afterEach( () => { | ||
testDocument = null; | ||
|
||
if ( wrapper ) { | ||
wrapper.unmount(); | ||
wrapper = null; | ||
} | ||
} ); | ||
|
||
it( 'locks when mounted', () => { | ||
expectLocked( false ); | ||
wrapper = mount( <ScrollLock /> ); | ||
expectLocked( true ); | ||
} ); | ||
it( 'unlocks when unmounted', () => { | ||
wrapper = mount( <ScrollLock /> ); | ||
expectLocked( true ); | ||
wrapper.unmount(); | ||
expectLocked( false ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no risk, all test suites are isolated when you use Jest :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was very excited by this, but in my testing, it seems like the global
document
persists between tests and suites.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I will check it once in
master
, out of curiosity :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a class to the body in one test, checked for its presence in
beforeEach
, and found the class on the body in the second test. Are you up for letting me know if you find different behavior? I'd love to take advantage of isolated environment.