Skip to content

Commit

Permalink
feat(home): prevent vertical scroll while sliding shelf
Browse files Browse the repository at this point in the history
  • Loading branch information
royschut committed Jun 24, 2021
1 parent 5ff99e1 commit 10b2c45
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/components/TileDock/TileDock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,29 @@ const TileDock = <T extends unknown>({
if (!animated) setDoAnimationReset(true);
};

const handleTouchStart = (event: React.TouchEvent): void =>
const handleTouchStart = (event: React.TouchEvent): void => {
setTouchPosition({
x: event.touches[0].clientX,
y: event.touches[0].clientY,
});
};

const [verticalScrollStopped, setVerticalScrollStopped] = useState<boolean>(false);

const handleTouchMove = (event: React.TouchEvent): void => {
if (verticalScrollStopped) return;

const newPosition = {
x: event.changedTouches[0].clientX,
y: event.changedTouches[0].clientY,
};
const movementX: number = Math.abs(newPosition.x - touchPosition.x);
const movementY: number = Math.abs(newPosition.y - touchPosition.y);
if (movementX > movementY && movementX > 10) {
setVerticalScrollStopped(true);
document.body.style.overflowY = 'hidden';
}
};
const handleTouchEnd = (event: React.TouchEvent): void => {
const newPosition = {
x: event.changedTouches[0].clientX,
Expand All @@ -127,6 +145,8 @@ const TileDock = <T extends unknown>({
const movementY: number = Math.abs(newPosition.y - touchPosition.y);
const direction: Direction = newPosition.x < touchPosition.x ? 'right' : 'left';

setVerticalScrollStopped(false);
document.body.style.overflowY = '';
if (movementX > minimalTouchMovement && movementX > movementY) {
slide(direction);
}
Expand Down Expand Up @@ -175,7 +195,14 @@ const TileDock = <T extends unknown>({
return (
<div className={styles.tileDock}>
{showLeftControl && !!renderLeftControl && <div className={styles.leftControl}>{renderLeftControl(() => slide('left'))}</div>}
<ul ref={frameRef} style={ulStyle} onTouchStart={handleTouchStart} onTouchEnd={handleTouchEnd} onTransitionEnd={handleTransitionEnd}>
<ul
ref={frameRef}
style={ulStyle}
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
onTouchMove={handleTouchMove}
onTransitionEnd={handleTransitionEnd}
>
{wrapWithEmptyTiles ? (
<li
className={styles.emptyTile}
Expand Down

0 comments on commit 10b2c45

Please sign in to comment.