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

allowTouchMove receive touch event #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,19 @@ To disable scrolling on iOS, `disableBodyScroll` prevents `touchmove` events.
However, there are cases where you have called `disableBodyScroll` on an
element, but its children still require `touchmove` events to function.

The `allowTouchMove` option should be a function which returns a boolean. It is
passed two arguments: the element receiving the touch event, and the touch event
itself. If a truthy value is returned, the touch event will not be prevented.

See below for 2 use cases:

##### Simple

```javascript
disableBodyScroll(container, {
allowTouchMove: el => el.tagName === 'TEXTAREA',
allowTouchMove(el) {
return el.tagName === 'TEXTAREA';
},
});
```

Expand All @@ -304,12 +310,11 @@ Javascript:

```javascript
disableBodyScroll(container, {
allowTouchMove: el => {
allowTouchMove(el, event) {
while (el && el !== document.body) {
if (el.getAttribute('body-scroll-lock-ignore') !== null) {
return true;
}

el = el.parentElement;
}
},
Expand Down
13 changes: 7 additions & 6 deletions src/bodyScrollLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Adopted and modified solution from Bohdan Didukh (2017)
// https://stackoverflow.com/questions/41594997/ios-10-safari-prevent-scrolling-behind-a-fixed-overlay-and-maintain-scroll-posi

type HandleScrollEvent = TouchEvent;

export interface BodyScrollOptions {
reserveScrollBarGap?: boolean;
allowTouchMove?: (el: any) => boolean;
allowTouchMove?: (el: any, event: HandleScrollEvent) => boolean;
}

interface Lock {
Expand All @@ -31,7 +33,6 @@ const isIosDevice =
window.navigator.platform &&
(/iP(ad|hone|od)/.test(window.navigator.platform) ||
(window.navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1));
type HandleScrollEvent = TouchEvent;

let locks: Array<Lock> = [];
let documentListenerAdded: boolean = false;
Expand All @@ -41,9 +42,9 @@ let previousBodyPosition;
let previousBodyPaddingRight;

// returns true if `el` should be allowed to receive touchmove events.
const allowTouchMove = (el: EventTarget): boolean =>
const allowTouchMove = (el: EventTarget, event: HandleScrollEvent): boolean =>
locks.some(lock => {
if (lock.options.allowTouchMove && lock.options.allowTouchMove(el)) {
if (lock.options.allowTouchMove && lock.options.allowTouchMove(el, event)) {
return true;
}

Expand All @@ -57,7 +58,7 @@ const preventDefault = (rawEvent: HandleScrollEvent): boolean => {
// Recall that we do document.addEventListener('touchmove', preventDefault, { passive: false })
// in disableBodyScroll - so if we provide this opportunity to allowTouchMove, then
// the touchmove event on document will break.
if (allowTouchMove(e.target)) {
if (allowTouchMove(e.target, e)) {
return true;
}

Expand Down Expand Up @@ -158,7 +159,7 @@ const isTargetElementTotallyScrolled = (targetElement: any): boolean =>
const handleScroll = (event: HandleScrollEvent, targetElement: any): boolean => {
const clientY = event.targetTouches[0].clientY - initialClientY;

if (allowTouchMove(event.target)) {
if (allowTouchMove(event.target, event)) {
return false;
}

Expand Down