mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
80c04df4fc
Packing items, tasks and expenses swipe sideways to their actions, as in Mail or Reminders: done or open one way, delete and edit the other, the first one run by a full swipe. They are second ways to what each row already offers; an expense still asks before it goes. A long press on a trip card lifts it and opens its menu with the card's own actions. Plain confirmations in MConfirmSheet now also ask through the system where the app can. Lists, the atlas and a journey show grey placeholders in their shape while loading instead of a spinner in an empty screen (MSkeleton); buttons keep theirs. A picture still loading fades in once it is there; one from the cache shows at once. In the iOS app the page bounces at its ends again, except on screens with pull-to-refresh, whose pull it would race; Android keeps its stretch off. On iOS a long press on the app icon offers "New trip" and the trip coming up by its name, set by the UI in its language (TrekShell.setQuickActions, feature quickActions), and a tap opens it, also from a cold start. The app's own CSS moves out of index.css into native/nativeApp.css, which had outgrown its size budget.
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
import { type MouseEvent, type TouchEvent, useEffect, useRef } from 'react';
|
|
import { pressHaptic } from '../../native/haptics';
|
|
|
|
/**
|
|
* A long press on a card, as the phone knows it: hold still for a moment and
|
|
* the card lifts, the phone bumps, and `onLongPress` opens its menu. A finger
|
|
* that moves is scrolling and cancels it; the tap that ends a long press opens
|
|
* nothing else. Android also reports the press as a contextmenu event, which
|
|
* counts as the same press.
|
|
*
|
|
* Spread the returned props on the card. For the first moments it presses like
|
|
* any tap; once the hold is clearly a hold the card gets data-pressing and lifts
|
|
* (index.css), and the press dimming lets go of it.
|
|
*/
|
|
|
|
const LIFT_MS = 180;
|
|
const HOLD_MS = 450;
|
|
const SLOP_PX = 8;
|
|
|
|
export function useLongPress(onLongPress: (() => void) | undefined) {
|
|
const timer = useRef<number | null>(null);
|
|
const lift = useRef<number | null>(null);
|
|
const start = useRef<{ x: number; y: number } | null>(null);
|
|
// The click that ends a long press, swallowed.
|
|
const fired = useRef(false);
|
|
const target = useRef<HTMLElement | null>(null);
|
|
const handler = useRef(onLongPress);
|
|
useEffect(() => {
|
|
handler.current = onLongPress;
|
|
});
|
|
useEffect(
|
|
() => () => {
|
|
if (timer.current) window.clearTimeout(timer.current);
|
|
if (lift.current) window.clearTimeout(lift.current);
|
|
},
|
|
[]
|
|
);
|
|
|
|
const settle = () => {
|
|
if (timer.current) window.clearTimeout(timer.current);
|
|
if (lift.current) window.clearTimeout(lift.current);
|
|
timer.current = null;
|
|
lift.current = null;
|
|
start.current = null;
|
|
target.current?.removeAttribute('data-pressing');
|
|
};
|
|
const fire = () => {
|
|
settle();
|
|
fired.current = true;
|
|
pressHaptic();
|
|
handler.current?.();
|
|
};
|
|
|
|
if (!onLongPress) return {};
|
|
return {
|
|
'data-long-press': '',
|
|
onTouchStart: (e: TouchEvent<HTMLElement>) => {
|
|
fired.current = false;
|
|
if (e.touches.length !== 1) return;
|
|
start.current = { x: e.touches[0].clientX, y: e.touches[0].clientY };
|
|
const card = e.currentTarget;
|
|
target.current = card;
|
|
lift.current = window.setTimeout(() => card.setAttribute('data-pressing', ''), LIFT_MS);
|
|
timer.current = window.setTimeout(fire, HOLD_MS);
|
|
},
|
|
onTouchMove: (e: TouchEvent<HTMLElement>) => {
|
|
if (!start.current) return;
|
|
const t = e.touches[0];
|
|
if (Math.abs(t.clientX - start.current.x) > SLOP_PX || Math.abs(t.clientY - start.current.y) > SLOP_PX) settle();
|
|
},
|
|
onTouchEnd: settle,
|
|
onTouchCancel: settle,
|
|
onContextMenu: (e: MouseEvent<HTMLElement>) => {
|
|
e.preventDefault();
|
|
if (!fired.current) fire();
|
|
},
|
|
onClickCapture: (e: MouseEvent<HTMLElement>) => {
|
|
if (!fired.current) return;
|
|
fired.current = false;
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
},
|
|
};
|
|
}
|