mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
feat(native): the app's own swipe back, and a way back closes the sheet on top first
WebKit's swipe from the left edge slid away a screenshot of the whole page, closed no sheet and took every drag near the edge from the UI. Binaries from this one turn it off and report an edgeBack feature, and the UI draws its own: the page follows the finger, a release past a third or a flick slides it off and goes back, anything else springs it home, with a light detent. Open sheets now register in one overlay stack, so a way back closes the one on top first, through its own close path (unsaved changes still ask): the edge swipe on iOS and Android's back gesture alike.
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import { render } from '@testing-library/react';
|
||||
import { useRef } from 'react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { tickHaptic } from '../../native/haptics';
|
||||
import { closeTopOverlay, hasOpenOverlay } from '../overlayStack';
|
||||
import MEdgeBack from './MEdgeBack';
|
||||
|
||||
vi.mock('../overlayStack', () => ({ closeTopOverlay: vi.fn(), hasOpenOverlay: vi.fn(() => false) }));
|
||||
vi.mock('../../native/haptics', () => ({ tickHaptic: vi.fn() }));
|
||||
|
||||
function Host({ enabled = true }: { enabled?: boolean }) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
return (
|
||||
<>
|
||||
<div ref={ref} data-testid="page" />
|
||||
<MEdgeBack target={ref} enabled={enabled} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function touch(type: string, x: number, y = 300, t = 0) {
|
||||
const e = new Event(type, { bubbles: true, cancelable: true }) as Event & Record<string, unknown>;
|
||||
Object.defineProperty(e, 'touches', { value: type === 'touchend' ? [] : [{ clientX: x, clientY: y }] });
|
||||
Object.defineProperty(e, 'changedTouches', { value: [{ clientX: x, clientY: y }] });
|
||||
Object.defineProperty(e, 'timeStamp', { value: t });
|
||||
document.dispatchEvent(e);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.mocked(hasOpenOverlay).mockReturnValue(false);
|
||||
window.history.replaceState({ idx: 2 }, '');
|
||||
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 400 });
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('MEdgeBack', () => {
|
||||
it('drags the page with a finger from the left edge, and goes back past a third', () => {
|
||||
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
|
||||
const { getByTestId } = render(<Host />);
|
||||
touch('touchstart', 5, 300, 0);
|
||||
touch('touchmove', 40, 302, 50);
|
||||
touch('touchmove', 200, 304, 400);
|
||||
expect(getByTestId('page').style.transform).toBe('translateX(195px)');
|
||||
touch('touchend', 200, 304, 900);
|
||||
vi.advanceTimersByTime(250);
|
||||
expect(back).toHaveBeenCalledTimes(1);
|
||||
expect(tickHaptic).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('springs back on a short, slow drag', () => {
|
||||
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
|
||||
const { getByTestId } = render(<Host />);
|
||||
touch('touchstart', 5, 300, 0);
|
||||
touch('touchmove', 30, 300, 200);
|
||||
touch('touchmove', 60, 300, 800);
|
||||
touch('touchend', 60, 300, 1600);
|
||||
vi.advanceTimersByTime(300);
|
||||
expect(back).not.toHaveBeenCalled();
|
||||
expect(getByTestId('page').style.transform).toBe('');
|
||||
});
|
||||
|
||||
it('closes an open sheet first and leaves the page where it is', () => {
|
||||
vi.mocked(hasOpenOverlay).mockReturnValue(true);
|
||||
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
|
||||
const { getByTestId } = render(<Host />);
|
||||
touch('touchstart', 5, 300, 0);
|
||||
touch('touchmove', 200, 300, 100);
|
||||
expect(getByTestId('page').style.transform).toBe('');
|
||||
touch('touchend', 200, 300, 200);
|
||||
expect(closeTopOverlay).toHaveBeenCalledTimes(1);
|
||||
expect(back).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignores a touch away from the edge, a vertical scroll, the first page and a binary without the feature', () => {
|
||||
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
|
||||
const { unmount } = render(<Host />);
|
||||
touch('touchstart', 120);
|
||||
touch('touchmove', 300);
|
||||
touch('touchend', 300, 300, 100);
|
||||
touch('touchstart', 5, 300);
|
||||
touch('touchmove', 8, 400);
|
||||
touch('touchend', 8, 400, 100);
|
||||
window.history.replaceState({ idx: 0 }, '');
|
||||
touch('touchstart', 5);
|
||||
touch('touchmove', 300);
|
||||
touch('touchend', 300, 300, 100);
|
||||
unmount();
|
||||
window.history.replaceState({ idx: 2 }, '');
|
||||
render(<Host enabled={false} />);
|
||||
touch('touchstart', 5);
|
||||
touch('touchmove', 300);
|
||||
touch('touchend', 300, 300, 100);
|
||||
vi.advanceTimersByTime(300);
|
||||
expect(back).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
import type { RefObject } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { tickHaptic } from '../../native/haptics';
|
||||
import { closeTopOverlay, hasOpenOverlay } from '../overlayStack';
|
||||
|
||||
/**
|
||||
* 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; t: number } | null = null;
|
||||
let claimed = false;
|
||||
let overlay = false;
|
||||
let last = { x: 0, t: 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 onStart = (e: TouchEvent) => {
|
||||
claimed = false;
|
||||
start = null;
|
||||
if (e.touches.length !== 1) return;
|
||||
const t = e.touches[0];
|
||||
if (t.clientX > EDGE_PX) return;
|
||||
overlay = hasOpenOverlay();
|
||||
if (!overlay && !canGoBack()) return;
|
||||
start = { x: t.clientX, y: t.clientY, t: e.timeStamp };
|
||||
last = { x: t.clientX, t: 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 (e.cancelable) e.preventDefault();
|
||||
last = { x: t.clientX, t: e.timeStamp };
|
||||
// An open sheet closes on release; the page itself stays put under it.
|
||||
if (!overlay) move(Math.max(0, dx), 'none');
|
||||
};
|
||||
|
||||
const onEnd = (e: TouchEvent) => {
|
||||
const s = start;
|
||||
start = null;
|
||||
if (!s || !claimed) return;
|
||||
const x = e.changedTouches[0]?.clientX ?? last.x;
|
||||
const dx = Math.max(0, x - s.x);
|
||||
const v = (x - last.x) / Math.max(1, e.timeStamp - last.t) || dx / Math.max(1, e.timeStamp - s.t);
|
||||
const width = window.innerWidth;
|
||||
const commit = dx > width * COMMIT_SHARE || v > FLICK_V;
|
||||
if (!commit) {
|
||||
move(0, 'transform 260ms var(--ease-drawer), box-shadow 260ms');
|
||||
return;
|
||||
}
|
||||
tickHaptic();
|
||||
if (overlay) {
|
||||
closeTopOverlay();
|
||||
return;
|
||||
}
|
||||
move(width, 'transform 200ms cubic-bezier(0.2, 0.8, 0.2, 1)');
|
||||
window.setTimeout(() => {
|
||||
window.history.back();
|
||||
// The page that comes back takes the place without a slide of its own.
|
||||
requestAnimationFrame(() => move(0, 'none'));
|
||||
}, 190);
|
||||
};
|
||||
|
||||
document.addEventListener('touchstart', onStart, { passive: true });
|
||||
document.addEventListener('touchmove', onMove, { passive: false });
|
||||
document.addEventListener('touchend', onEnd, { passive: true });
|
||||
document.addEventListener('touchcancel', onEnd, { passive: true });
|
||||
return () => {
|
||||
document.removeEventListener('touchstart', onStart);
|
||||
document.removeEventListener('touchmove', onMove);
|
||||
document.removeEventListener('touchend', onEnd);
|
||||
document.removeEventListener('touchcancel', onEnd);
|
||||
move(0, 'none');
|
||||
};
|
||||
}, [enabled, target]);
|
||||
return null;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { useTranslation } from '../../i18n'
|
||||
import { useDiscardGuard } from '../../components/shared/useDiscardGuard'
|
||||
import { keepTabInside, useModalFocus } from './modalFocus'
|
||||
import { useSheetDrag } from './useSheetDrag'
|
||||
import { useOverlay } from '../overlayStack'
|
||||
|
||||
export type MSheetVariant = 'card' | 'bottom' | 'drawer'
|
||||
export type MSheetMaterial = 'glass' | 'bar-glass' | 'opaque'
|
||||
@@ -151,6 +152,10 @@ export default function MSheet({
|
||||
return lockBodyScroll()
|
||||
}, [open])
|
||||
|
||||
// A way back (the edge swipe, Android's back gesture) closes the sheet on top
|
||||
// first, through the same path as the scrim, so unsaved changes still ask.
|
||||
useOverlay(open && rendered, requestClose)
|
||||
|
||||
// Move focus into the dialog on open, hand it back on close.
|
||||
useModalFocus(panelRef, open && rendered)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user