-
Notifications
You must be signed in to change notification settings - Fork 52
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 2 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,6 +10,7 @@ | |
|
||
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 | ||
export const SCROLL_LOCK_DISABLE_ATTR = 'data-scroll-lock-disable'; | ||
|
@@ -73,23 +74,30 @@ function advancedUnlock(targetElement) { | |
document.removeEventListener('touchmove', preventDefault); | ||
} | ||
|
||
const isVerticalScroll = ({ scrollHeight, scrollWidth }) => scrollHeight > scrollWidth; | ||
|
||
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. If I understood this correctly, I'm not sure if we can determine if an element allows vertical or horizontal scroll just by comparing its scroll height to its width. I think we should compare 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. Thinking out loud here: what you implemented here determines if a container is more "portrait" or "landscape", but that may not translate exactly to the direction of the scroll. For example, a longer code block in a narrow viewport would be considered "portrait" but we most likely need it to scroll horizontally 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. In the example you give, I don't think that would happen because in the case of a code block, the A narrow viewport wouldn't affect the 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. Ops... I was reading from here 😅 |
||
/** | ||
* Handles the scrolling of the targetElement | ||
* @param {TouchEvent} event | ||
* @param {HTMLElement} targetElement | ||
* @return {boolean} | ||
*/ | ||
function handleScroll(event, targetElement) { | ||
function handleScroll(event, target) { | ||
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 (isVerticalScroll(target)) { | ||
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); | ||
} | ||
|
||
|
@@ -112,6 +120,7 @@ function advancedLock(targetElement) { | |
if (event.targetTouches.length === 1) { | ||
// detect single touch. | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
} | ||
}; | ||
targetElement.ontouchmove = (event) => { | ||
|
@@ -138,7 +147,11 @@ export default { | |
if (!isIosDevice()) { | ||
simpleLock(); | ||
} else { | ||
// lock everything but target element | ||
advancedLock(targetElement); | ||
// lock everything but disabled targets | ||
const disabledTargets = document.querySelectorAll(`[${SCROLL_LOCK_DISABLE_ATTR}]`); | ||
disabledTargets.forEach(target => advancedLock(target)); | ||
} | ||
isLocked = true; | ||
}, | ||
|
@@ -152,6 +165,9 @@ 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}]`); | ||
disabledTargets.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.