mirror of
https://github.com/outline/outline.git
synced 2026-12-01 17:32:49 +03:00
* React 19 upgrade Bumps react and react-dom to 19.2.8, with the dependency changes React 19 requires: react-is 19, @types/react 19, @types/react-dom 19, react-helmet-async 3 and react-avatar-editor 15 (which now ships its own types). framer-motion moves to 13 — its v6 types reference React.ReactHTML, removed from @types/react 19. The type changes fall into a few classes: - The global JSX namespace is gone, so JSX.Element becomes React.JSX.Element and the custom element declarations move into a module augmentation. - useRef() now requires an initial value. - A ref created with a null initial value is typed nullable, so consumers that accept one are widened to RefObject<T | null>. - React.ReactChild was removed; tooltips take React.ReactNode. - Ref callbacks may only return a cleanup function. react-dnd connectors return the connected node, so useDragRef and useDropRef wrap useDrag and useDrop and hand back a ref callback. react-i18next stays on v12: i18next 23 drops compatibilityJSON "v3", which would rewrite all 64 _plural keys across every locale. outline-icons, oy-vey and react-waypoint have no release that lists React 19 as a peer. All three render correctly under it; the install reports them as unmet peers. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Remove react-helmet and forwardRef --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
import { isNumber } from "es-toolkit/compat";
|
|
import { useRef } from "react";
|
|
|
|
/** Minimum distance in pixels that a touch must travel to count as a swipe. */
|
|
const MinSwipeDistance = 40;
|
|
|
|
type Props = {
|
|
onSwipeRight: () => void;
|
|
onSwipeLeft: () => void;
|
|
onSwipeUp: () => void;
|
|
onSwipeDown: () => void;
|
|
};
|
|
|
|
export default function useSwipe({
|
|
onSwipeRight,
|
|
onSwipeLeft,
|
|
onSwipeUp,
|
|
onSwipeDown,
|
|
}: Props) {
|
|
const touchXStart = useRef<number | undefined>(undefined);
|
|
const touchXEnd = useRef<number | undefined>(undefined);
|
|
const touchYStart = useRef<number | undefined>(undefined);
|
|
const touchYEnd = useRef<number | undefined>(undefined);
|
|
|
|
const resetTouchPoints = () => {
|
|
touchXStart.current = undefined;
|
|
touchXEnd.current = undefined;
|
|
touchYStart.current = undefined;
|
|
touchYEnd.current = undefined;
|
|
};
|
|
|
|
const onTouchStartCapture = (e: React.TouchEvent<HTMLImageElement>) => {
|
|
if (e.touches.length === 1) {
|
|
// Stop propagation only for single touch gestures, otherwise it prevents
|
|
// multi-touch gestures like pinch to zoom to take effect
|
|
e.stopPropagation();
|
|
touchXStart.current = e.changedTouches[0].screenX;
|
|
touchYStart.current = e.changedTouches[0].screenY;
|
|
}
|
|
};
|
|
|
|
const onTouchMoveCapture = (e: React.TouchEvent<HTMLImageElement>) => {
|
|
if (
|
|
isNumber(touchXStart.current) &&
|
|
isNumber(touchYStart.current) &&
|
|
e.touches.length === 1
|
|
) {
|
|
touchXEnd.current = e.changedTouches[0].screenX;
|
|
touchYEnd.current = e.changedTouches[0].screenY;
|
|
const dx = touchXEnd.current - touchXStart.current;
|
|
const dy = touchYEnd.current - touchYStart.current;
|
|
|
|
// Ignore small movements so that a tap, or the start of a gesture, is not
|
|
// mistaken for a swipe.
|
|
if (Math.max(Math.abs(dx), Math.abs(dy)) < MinSwipeDistance) {
|
|
return;
|
|
}
|
|
|
|
const swipeRight = dx > 0 && Math.abs(dy) < Math.abs(dx);
|
|
if (swipeRight) {
|
|
resetTouchPoints();
|
|
return onSwipeRight();
|
|
}
|
|
|
|
const swipeLeft = dx < 0 && Math.abs(dy) < Math.abs(dx);
|
|
if (swipeLeft) {
|
|
resetTouchPoints();
|
|
return onSwipeLeft();
|
|
}
|
|
|
|
const swipeDown = dy > 0 && Math.abs(dy) > Math.abs(dx);
|
|
if (swipeDown) {
|
|
resetTouchPoints();
|
|
return onSwipeDown();
|
|
}
|
|
|
|
const swipeUp = dy < 0 && Math.abs(dy) > Math.abs(dx);
|
|
if (swipeUp) {
|
|
resetTouchPoints();
|
|
return onSwipeUp();
|
|
}
|
|
}
|
|
};
|
|
|
|
const onTouchCancelCapture = () => {
|
|
resetTouchPoints();
|
|
};
|
|
|
|
return {
|
|
onTouchStartCapture,
|
|
onTouchMoveCapture,
|
|
onTouchCancelCapture,
|
|
};
|
|
}
|