Apply the auto-fixable lint rules and the formatter, and drop the Vite build artifacts.
This commit is contained in:
@@ -67,12 +67,9 @@ const DEFAULT_OPTIONS: Required<UseInertialHorizontalScrollOptions> = {
|
||||
* Shared hook for drag-scrolling horizontal overflow areas with inertial momentum.
|
||||
*/
|
||||
export function useInertialHorizontalScroll(
|
||||
options: UseInertialHorizontalScrollOptions = {}
|
||||
options: UseInertialHorizontalScrollOptions = {},
|
||||
): UseInertialHorizontalScrollResult {
|
||||
const resolvedOptions = useMemo(
|
||||
() => ({ ...DEFAULT_OPTIONS, ...options }),
|
||||
[options]
|
||||
);
|
||||
const resolvedOptions = useMemo(() => ({ ...DEFAULT_OPTIONS, ...options }), [options]);
|
||||
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const stateRef = useRef<ScrollState>({
|
||||
@@ -134,23 +131,26 @@ export function useInertialHorizontalScroll(
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
}
|
||||
},
|
||||
[]
|
||||
[],
|
||||
);
|
||||
|
||||
const scrollByStep = useCallback((direction: 1 | -1) => {
|
||||
const scroll = scrollRef.current;
|
||||
if (!scroll) return;
|
||||
const scrollByStep = useCallback(
|
||||
(direction: 1 | -1) => {
|
||||
const scroll = scrollRef.current;
|
||||
if (!scroll) return;
|
||||
|
||||
const isHorizontal = resolvedOptions.axis === 'x';
|
||||
const currentPrimary = isHorizontal ? scroll.scrollLeft : scroll.scrollTop;
|
||||
const offsetSize = isHorizontal ? scroll.offsetWidth : scroll.offsetHeight;
|
||||
scroll.scrollTo({
|
||||
...(isHorizontal
|
||||
? { left: currentPrimary + offsetSize / resolvedOptions.scrollStepRatio * direction }
|
||||
: { top: currentPrimary + offsetSize / resolvedOptions.scrollStepRatio * direction }),
|
||||
behavior: 'smooth',
|
||||
});
|
||||
}, [resolvedOptions.axis, resolvedOptions.scrollStepRatio]);
|
||||
const isHorizontal = resolvedOptions.axis === 'x';
|
||||
const currentPrimary = isHorizontal ? scroll.scrollLeft : scroll.scrollTop;
|
||||
const offsetSize = isHorizontal ? scroll.offsetWidth : scroll.offsetHeight;
|
||||
scroll.scrollTo({
|
||||
...(isHorizontal
|
||||
? { left: currentPrimary + (offsetSize / resolvedOptions.scrollStepRatio) * direction }
|
||||
: { top: currentPrimary + (offsetSize / resolvedOptions.scrollStepRatio) * direction }),
|
||||
behavior: 'smooth',
|
||||
});
|
||||
},
|
||||
[resolvedOptions.axis, resolvedOptions.scrollStepRatio],
|
||||
);
|
||||
|
||||
const stopInertia = useCallback(() => {
|
||||
const state = stateRef.current;
|
||||
@@ -193,133 +193,162 @@ export function useInertialHorizontalScroll(
|
||||
return;
|
||||
}
|
||||
|
||||
currentState.velocity *= Math.pow(resolvedOptions.friction, deltaMs / 16.6667);
|
||||
currentState.animationFrameId = requestAnimationFrame((nextTimestamp) => step(nextTimestamp, timestamp));
|
||||
currentState.velocity *= resolvedOptions.friction ** (deltaMs / 16.6667);
|
||||
currentState.animationFrameId = requestAnimationFrame((nextTimestamp) =>
|
||||
step(nextTimestamp, timestamp),
|
||||
);
|
||||
};
|
||||
|
||||
stopInertia();
|
||||
state.animationFrameId = requestAnimationFrame((timestamp) => step(timestamp, timestamp));
|
||||
}, [resolvedOptions.axis, resolvedOptions.friction, resolvedOptions.minVelocity, stopInertia, updateScrollState]);
|
||||
}, [
|
||||
resolvedOptions.axis,
|
||||
resolvedOptions.friction,
|
||||
resolvedOptions.minVelocity,
|
||||
stopInertia,
|
||||
updateScrollState,
|
||||
]);
|
||||
|
||||
const endDrag = useCallback((pointerId: number | null) => {
|
||||
const state = stateRef.current;
|
||||
if (!state.dragging) return;
|
||||
const endDrag = useCallback(
|
||||
(pointerId: number | null) => {
|
||||
const state = stateRef.current;
|
||||
if (!state.dragging) return;
|
||||
|
||||
const scroll = scrollRef.current;
|
||||
if (scroll && pointerId !== null && scroll.hasPointerCapture(pointerId)) {
|
||||
scroll.releasePointerCapture(pointerId);
|
||||
}
|
||||
const scroll = scrollRef.current;
|
||||
if (scroll && pointerId !== null && scroll.hasPointerCapture(pointerId)) {
|
||||
scroll.releasePointerCapture(pointerId);
|
||||
}
|
||||
|
||||
if (state.moved) {
|
||||
state.suppressClick = true;
|
||||
startInertia();
|
||||
}
|
||||
if (state.moved) {
|
||||
state.suppressClick = true;
|
||||
startInertia();
|
||||
}
|
||||
|
||||
state.dragging = false;
|
||||
state.activePointerId = null;
|
||||
state.moved = false;
|
||||
state.velocity = 0;
|
||||
setIsDragging(false);
|
||||
}, [startInertia]);
|
||||
state.dragging = false;
|
||||
state.activePointerId = null;
|
||||
state.moved = false;
|
||||
state.velocity = 0;
|
||||
setIsDragging(false);
|
||||
},
|
||||
[startInertia],
|
||||
);
|
||||
|
||||
const processPointerMove = useCallback((evt: PointerLikeEvent) => {
|
||||
const state = stateRef.current;
|
||||
if (!state.dragging || state.activePointerId !== evt.pointerId) {
|
||||
return;
|
||||
}
|
||||
const processPointerMove = useCallback(
|
||||
(evt: PointerLikeEvent) => {
|
||||
const state = stateRef.current;
|
||||
if (!state.dragging || state.activePointerId !== evt.pointerId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scroll = scrollRef.current;
|
||||
if (!scroll) return;
|
||||
const isHorizontal = resolvedOptions.axis === 'x';
|
||||
const scroll = scrollRef.current;
|
||||
if (!scroll) return;
|
||||
const isHorizontal = resolvedOptions.axis === 'x';
|
||||
|
||||
const currentPrimary = isHorizontal ? evt.clientX : evt.clientY;
|
||||
const deltaPrimary = currentPrimary - state.startPrimary;
|
||||
const now = evt.timeStamp;
|
||||
const deltaTime = Math.max(now - state.lastTime, 1);
|
||||
const hasExceededThreshold = Math.abs(deltaPrimary) >= resolvedOptions.dragThreshold;
|
||||
const currentPrimary = isHorizontal ? evt.clientX : evt.clientY;
|
||||
const deltaPrimary = currentPrimary - state.startPrimary;
|
||||
const now = evt.timeStamp;
|
||||
const deltaTime = Math.max(now - state.lastTime, 1);
|
||||
const hasExceededThreshold = Math.abs(deltaPrimary) >= resolvedOptions.dragThreshold;
|
||||
|
||||
if (hasExceededThreshold) {
|
||||
state.moved = true;
|
||||
}
|
||||
if (hasExceededThreshold) {
|
||||
state.moved = true;
|
||||
}
|
||||
|
||||
if (!state.moved) {
|
||||
if (!state.moved) {
|
||||
state.lastPrimary = currentPrimary;
|
||||
state.lastTime = now;
|
||||
return;
|
||||
}
|
||||
|
||||
evt.preventDefault?.();
|
||||
|
||||
if (isHorizontal) {
|
||||
scroll.scrollLeft = state.startScrollPrimary - deltaPrimary;
|
||||
} else {
|
||||
scroll.scrollTop = state.startScrollPrimary - deltaPrimary;
|
||||
}
|
||||
|
||||
const rawVelocity = (currentPrimary - state.lastPrimary) / deltaTime;
|
||||
state.velocity = Math.max(Math.min(rawVelocity, 2), -2);
|
||||
state.lastPrimary = currentPrimary;
|
||||
state.lastTime = now;
|
||||
return;
|
||||
}
|
||||
},
|
||||
[resolvedOptions.axis, resolvedOptions.dragThreshold],
|
||||
);
|
||||
|
||||
evt.preventDefault?.();
|
||||
const handlePointerDown = useCallback(
|
||||
(evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (evt.button !== 0) return;
|
||||
if (!evt.isPrimary) return;
|
||||
|
||||
if (isHorizontal) {
|
||||
scroll.scrollLeft = state.startScrollPrimary - deltaPrimary;
|
||||
} else {
|
||||
scroll.scrollTop = state.startScrollPrimary - deltaPrimary;
|
||||
}
|
||||
const state = stateRef.current;
|
||||
if (state.dragging && state.activePointerId === evt.pointerId) return;
|
||||
|
||||
const rawVelocity = (currentPrimary - state.lastPrimary) / deltaTime;
|
||||
state.velocity = Math.max(Math.min(rawVelocity, 2), -2);
|
||||
state.lastPrimary = currentPrimary;
|
||||
state.lastTime = now;
|
||||
}, [resolvedOptions.axis, resolvedOptions.dragThreshold]);
|
||||
const { target } = evt;
|
||||
if (target instanceof Element && target.closest(TEXT_SELECTION_TARGET_SELECTOR)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const handlePointerDown = useCallback((evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (evt.button !== 0) return;
|
||||
if (!evt.isPrimary) return;
|
||||
const scroll = scrollRef.current;
|
||||
if (!scroll) return;
|
||||
const isHorizontal = resolvedOptions.axis === 'x';
|
||||
|
||||
const state = stateRef.current;
|
||||
if (state.dragging && state.activePointerId === evt.pointerId) return;
|
||||
stopInertia();
|
||||
|
||||
const target = evt.target;
|
||||
if (target instanceof Element && target.closest(TEXT_SELECTION_TARGET_SELECTOR)) {
|
||||
return;
|
||||
}
|
||||
state.activePointerId = evt.pointerId;
|
||||
state.startPrimary = isHorizontal ? evt.clientX : evt.clientY;
|
||||
state.startScrollPrimary = isHorizontal ? scroll.scrollLeft : scroll.scrollTop;
|
||||
state.lastPrimary = isHorizontal ? evt.clientX : evt.clientY;
|
||||
state.lastTime = evt.timeStamp;
|
||||
state.velocity = 0;
|
||||
state.dragging = true;
|
||||
state.moved = false;
|
||||
state.suppressClick = false;
|
||||
|
||||
const scroll = scrollRef.current;
|
||||
if (!scroll) return;
|
||||
const isHorizontal = resolvedOptions.axis === 'x';
|
||||
try {
|
||||
scroll.setPointerCapture(evt.pointerId);
|
||||
} catch {
|
||||
// Ignore capture failures when browser has already ended pointer interaction.
|
||||
}
|
||||
|
||||
stopInertia();
|
||||
setIsDragging(true);
|
||||
},
|
||||
[resolvedOptions.axis, stopInertia],
|
||||
);
|
||||
|
||||
state.activePointerId = evt.pointerId;
|
||||
state.startPrimary = isHorizontal ? evt.clientX : evt.clientY;
|
||||
state.startScrollPrimary = isHorizontal ? scroll.scrollLeft : scroll.scrollTop;
|
||||
state.lastPrimary = isHorizontal ? evt.clientX : evt.clientY;
|
||||
state.lastTime = evt.timeStamp;
|
||||
state.velocity = 0;
|
||||
state.dragging = true;
|
||||
state.moved = false;
|
||||
state.suppressClick = false;
|
||||
const handlePointerMove = useCallback(
|
||||
(evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
processPointerMove(evt);
|
||||
},
|
||||
[processPointerMove],
|
||||
);
|
||||
|
||||
try {
|
||||
scroll.setPointerCapture(evt.pointerId);
|
||||
} catch {
|
||||
// Ignore capture failures when browser has already ended pointer interaction.
|
||||
}
|
||||
const handlePointerUp = useCallback(
|
||||
(evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
const state = stateRef.current;
|
||||
if (state.activePointerId !== evt.pointerId) return;
|
||||
endDrag(evt.pointerId);
|
||||
},
|
||||
[endDrag],
|
||||
);
|
||||
|
||||
setIsDragging(true);
|
||||
}, [resolvedOptions.axis, stopInertia]);
|
||||
const handlePointerCancel = useCallback(
|
||||
(evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
const state = stateRef.current;
|
||||
if (state.activePointerId !== evt.pointerId) return;
|
||||
endDrag(evt.pointerId);
|
||||
},
|
||||
[endDrag],
|
||||
);
|
||||
|
||||
const handlePointerMove = useCallback((evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
processPointerMove(evt);
|
||||
}, [processPointerMove]);
|
||||
|
||||
const handlePointerUp = useCallback((evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
const state = stateRef.current;
|
||||
if (state.activePointerId !== evt.pointerId) return;
|
||||
endDrag(evt.pointerId);
|
||||
}, [endDrag]);
|
||||
|
||||
const handlePointerCancel = useCallback((evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
const state = stateRef.current;
|
||||
if (state.activePointerId !== evt.pointerId) return;
|
||||
endDrag(evt.pointerId);
|
||||
}, [endDrag]);
|
||||
|
||||
const handleLostPointerCapture = useCallback((evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
const state = stateRef.current;
|
||||
if (state.activePointerId !== evt.pointerId) return;
|
||||
endDrag(evt.pointerId);
|
||||
}, [endDrag]);
|
||||
const handleLostPointerCapture = useCallback(
|
||||
(evt: React.PointerEvent<HTMLDivElement>) => {
|
||||
const state = stateRef.current;
|
||||
if (state.activePointerId !== evt.pointerId) return;
|
||||
endDrag(evt.pointerId);
|
||||
},
|
||||
[endDrag],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const handleWindowPointerMove = (evt: PointerEvent) => {
|
||||
@@ -399,4 +428,4 @@ export function useInertialHorizontalScroll(
|
||||
onClickCapture: handleClickCapture,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user