mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
151 lines
5.4 KiB
TypeScript
151 lines
5.4 KiB
TypeScript
import type { RefObject } from 'react';
|
|
import { useEffect } from 'react';
|
|
import { closeTopOverlay, hasOpenOverlay } from '../../hooks/overlayStack';
|
|
import { tickHaptic } from '../../native/haptics';
|
|
import {
|
|
currentTranslation,
|
|
isMotionReduced,
|
|
releaseVelocity,
|
|
sampleMotion,
|
|
type MotionSample,
|
|
} from '../gestureMotion';
|
|
import { skipNextRouteTransition } from '../routeTransition';
|
|
|
|
/**
|
|
* The swipe from the left edge that goes back, the way every iOS app has it.
|
|
*
|
|
* WebKit's own version slides away a screenshot of the whole page, closes no
|
|
* sheet and fights every gesture near the edge (the day pager, swipe rows), so
|
|
* the app turns it off (TrekBridgeViewController) and this takes over: the
|
|
* page follows the finger, and a release past a third of the screen or a flick
|
|
* slides it off and goes back. An open sheet closes first instead, the same
|
|
* order Android's back gesture follows. Mounted only where the binary reports
|
|
* the `edgeBack` feature, so an older binary keeps WebKit's.
|
|
*/
|
|
|
|
/** A touch has to start this close to the left edge. */
|
|
const EDGE_PX = 22;
|
|
/** Sideways must clearly beat vertical before the gesture is claimed. */
|
|
const CLAIM_PX = 10;
|
|
const AXIS_BIAS = 1.5;
|
|
const COMMIT_SHARE = 0.33;
|
|
const FLICK_V = 0.5;
|
|
|
|
/** react-router keeps its position in the session history on history.state. */
|
|
function canGoBack(): boolean {
|
|
const idx = (window.history.state as { idx?: number } | null)?.idx;
|
|
return typeof idx === 'number' && idx > 0;
|
|
}
|
|
|
|
export default function MEdgeBack({ target, enabled }: { target: RefObject<HTMLElement | null>; enabled: boolean }) {
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
let start: { x: number; y: number } | null = null;
|
|
let claimed = false;
|
|
let overlay = false;
|
|
let samples: MotionSample[] = [];
|
|
let grabOffset = 0;
|
|
let navigationTimer: number | undefined;
|
|
let frame = 0;
|
|
|
|
const move = (x: number, transition: string) => {
|
|
const el = target.current;
|
|
if (!el) return;
|
|
el.style.transition = transition;
|
|
el.style.transform = x ? `translateX(${x}px)` : '';
|
|
el.style.boxShadow = x ? '-16px 0 40px -12px rgba(0,0,0,.35)' : '';
|
|
};
|
|
const settle = () => move(0, 'transform 260ms var(--ease-drawer), box-shadow 260ms');
|
|
|
|
const onCancel = () => {
|
|
if (claimed && !overlay) settle();
|
|
start = null;
|
|
claimed = false;
|
|
grabOffset = 0;
|
|
};
|
|
|
|
const onStart = (e: TouchEvent) => {
|
|
onCancel();
|
|
if (e.touches.length !== 1) return;
|
|
const t = e.touches[0];
|
|
if (t.clientX > EDGE_PX) return;
|
|
if (navigationTimer !== undefined) return;
|
|
overlay = hasOpenOverlay();
|
|
if (!overlay && !canGoBack()) return;
|
|
start = { x: t.clientX, y: t.clientY };
|
|
samples = [{ position: t.clientX, time: e.timeStamp }];
|
|
};
|
|
|
|
const onMove = (e: TouchEvent) => {
|
|
if (!start || e.touches.length !== 1) return;
|
|
const t = e.touches[0];
|
|
const dx = t.clientX - start.x;
|
|
const dy = t.clientY - start.y;
|
|
if (!claimed) {
|
|
if (Math.abs(dy) > CLAIM_PX && Math.abs(dy) > Math.abs(dx)) {
|
|
start = null;
|
|
return;
|
|
}
|
|
if (dx < CLAIM_PX || dx < Math.abs(dy) * AXIS_BIAS) return;
|
|
claimed = true;
|
|
if (!overlay) grabOffset = currentTranslation(target.current, 'x');
|
|
}
|
|
if (e.cancelable) e.preventDefault();
|
|
sampleMotion(samples, t.clientX, e.timeStamp);
|
|
// An open sheet closes on release; the page itself stays put under it.
|
|
if (!overlay && !isMotionReduced()) move(Math.max(0, grabOffset + dx), 'none');
|
|
};
|
|
|
|
const onEnd = (e: TouchEvent) => {
|
|
const s = start;
|
|
start = null;
|
|
if (!s || !claimed) return;
|
|
claimed = false;
|
|
const x = e.changedTouches[0]?.clientX ?? samples[samples.length - 1].position;
|
|
const dx = Math.max(0, grabOffset + x - s.x);
|
|
const v = releaseVelocity(samples, x, e.timeStamp);
|
|
const width = window.innerWidth;
|
|
const commit = dx > 0 && (dx > width * COMMIT_SHARE || v > FLICK_V);
|
|
if (!commit) {
|
|
if (!overlay) settle();
|
|
return;
|
|
}
|
|
tickHaptic();
|
|
if (overlay) {
|
|
closeTopOverlay();
|
|
return;
|
|
}
|
|
if (isMotionReduced()) {
|
|
skipNextRouteTransition();
|
|
window.history.back();
|
|
move(0, 'none');
|
|
return;
|
|
}
|
|
const duration = Math.round(Math.min(240, Math.max(120, (width - dx) / Math.max(v, 1.2))));
|
|
move(width, `transform ${duration}ms cubic-bezier(0.2, 0.8, 0.2, 1)`);
|
|
navigationTimer = window.setTimeout(() => {
|
|
navigationTimer = undefined;
|
|
skipNextRouteTransition();
|
|
window.history.back();
|
|
// The page that comes back takes the place without a slide of its own.
|
|
frame = requestAnimationFrame(() => move(0, 'none'));
|
|
}, duration);
|
|
};
|
|
|
|
document.addEventListener('touchstart', onStart, { passive: true });
|
|
document.addEventListener('touchmove', onMove, { passive: false });
|
|
document.addEventListener('touchend', onEnd, { passive: true });
|
|
document.addEventListener('touchcancel', onCancel, { passive: true });
|
|
return () => {
|
|
document.removeEventListener('touchstart', onStart);
|
|
document.removeEventListener('touchmove', onMove);
|
|
document.removeEventListener('touchend', onEnd);
|
|
document.removeEventListener('touchcancel', onCancel);
|
|
window.clearTimeout(navigationTimer);
|
|
cancelAnimationFrame(frame);
|
|
move(0, 'none');
|
|
};
|
|
}, [enabled, target]);
|
|
return null;
|
|
}
|