-
Notifications
You must be signed in to change notification settings - Fork 53
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
Update scroll lock logic to lock everything but disabled targets #858
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,12 @@ | |
|
||
let isLocked = false; | ||
let initialClientY = -1; | ||
let initialClientX = -1; | ||
let scrolledClientY = 0; | ||
// Adds this attribute to an inner scrollable element to allow it to scroll | ||
// Adds this attribute to an vertical scrollable element to allow it to scroll | ||
export const SCROLL_LOCK_DISABLE_ATTR = 'data-scroll-lock-disable'; | ||
// Adds this attribute to an horizontal scrollable element to allow it to scroll | ||
export const SCROLL_LOCK_DISABLE_HORIZONTAL_ATTR = 'data-scroll-lock-horizontal-disable'; | ||
|
||
const isIosDevice = () => window.navigator | ||
&& window.navigator.platform | ||
|
@@ -79,17 +82,22 @@ function advancedUnlock(targetElement) { | |
* @param {HTMLElement} targetElement | ||
* @return {boolean} | ||
*/ | ||
function handleScroll(event, targetElement) { | ||
function handleScroll(event, target, isHorizontal) { | ||
const clientY = event.targetTouches[0].clientY - initialClientY; | ||
// check if any parent has a scroll-lock disable, if not use the targetElement | ||
const target = event.target.closest(`[${SCROLL_LOCK_DISABLE_ATTR}]`) || targetElement; | ||
if (target.scrollTop === 0 && clientY > 0) { | ||
// element is at the top of its scroll. | ||
return preventDefault(event); | ||
} | ||
const clientX = event.targetTouches[0].clientX - initialClientX; | ||
|
||
if (isTargetElementTotallyScrolled(target) && clientY < 0) { | ||
// element is at the bottom of its scroll. | ||
if (!isHorizontal) { | ||
if (target.scrollTop === 0 && clientY > 0) { | ||
// element is at the top of its scroll. | ||
return preventDefault(event); | ||
} | ||
|
||
if (isTargetElementTotallyScrolled(target) && clientY < 0) { | ||
// element is at the bottom of its scroll. | ||
return preventDefault(event); | ||
} | ||
} else if (Math.abs(clientY) > Math.abs(clientX)) { | ||
// prevent event if user tries to perform vertical scroll in an horizontal scrolling element | ||
Comment on lines
+99
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So smart :PP There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have idea about how WebKit really works but here I'm just comparing the absolute value of So if the user has moved more in the Y axis than to the X axis, it's a vertical scroll. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand what you are doing here, but there are examples when a gesture that moved more in the X axis than the Y axis be considered a vertical scroll on an iOS device. |
||
return preventDefault(event); | ||
} | ||
|
||
|
@@ -102,7 +110,7 @@ function handleScroll(event, targetElement) { | |
* Advanced scroll locking for iOS devices. | ||
* @param targetElement | ||
*/ | ||
function advancedLock(targetElement) { | ||
function advancedLock(targetElement, isHorizontal = false) { | ||
// add a scroll listener to the body | ||
document.addEventListener('touchmove', preventDefault, { passive: false }); | ||
if (!targetElement) return; | ||
|
@@ -112,12 +120,13 @@ function advancedLock(targetElement) { | |
if (event.targetTouches.length === 1) { | ||
// detect single touch. | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
} | ||
}; | ||
targetElement.ontouchmove = (event) => { | ||
if (event.targetTouches.length === 1) { | ||
// detect single touch. | ||
handleScroll(event, targetElement); | ||
handleScroll(event, targetElement, isHorizontal); | ||
} | ||
}; | ||
} | ||
|
@@ -138,7 +147,14 @@ export default { | |
if (!isIosDevice()) { | ||
simpleLock(); | ||
} else { | ||
// lock everything but target element | ||
advancedLock(targetElement); | ||
// lock everything but disabled targets with vertical scrolling | ||
const disabledTargets = document.querySelectorAll(`[${SCROLL_LOCK_DISABLE_ATTR}]`); | ||
disabledTargets.forEach(target => advancedLock(target)); | ||
// lock everything but disabled targets with horizontal scrolling | ||
const disabledHorizontalTargets = document.querySelectorAll(`[${SCROLL_LOCK_DISABLE_HORIZONTAL_ATTR}]`); | ||
disabledHorizontalTargets.forEach(target => advancedLock(target, true)); | ||
} | ||
isLocked = true; | ||
}, | ||
|
@@ -152,6 +168,10 @@ export default { | |
if (isIosDevice()) { | ||
// revert the old scroll position | ||
advancedUnlock(targetElement); | ||
// revert the old scroll position for disabled targets | ||
const disabledTargets = document.querySelectorAll(`[${SCROLL_LOCK_DISABLE_ATTR}]`); | ||
const disabledHorizontalTargets = document.querySelectorAll(`[${SCROLL_LOCK_DISABLE_HORIZONTAL_ATTR}]`); | ||
[...disabledTargets, ...disabledHorizontalTargets].forEach(target => advancedUnlock(target)); | ||
} else { | ||
// remove all inline styles added by the `simpleLock` function | ||
document.body.style.removeProperty('overflow'); | ||
|
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.
Do you think it could solve our problem if we applied the
SCROLL_LOCK_DISABLE_ATTR
here, on the entire taglist component, instead of on individual tags within it?And also conditionally apply the disable attribute onto the
input
element by checking ifmodelValue
is overflowing? This way we don't disable the lock whenFilterInput
is not overflowing.