mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
feat(client): calm page changes on the desktop and sections loaded ahead
The desktop now runs the View Transition the phone already had: one short cross-fade under a navbar that stands still. The route fallback holds the previous page until the new one is ready (at most 350 ms), so the full-screen spinner no longer flashes. Once signed in, the main sections' code loads in the background, so the first switch to one does not wait for it. The shared transition helpers move to utils/viewTransition.ts. The navbar tabs lose their title tooltips; their aria-label stays.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { flushSync } from 'react-dom';
|
||||
|
||||
/**
|
||||
* The View Transitions groundwork the app's page changes share: whether to
|
||||
* animate at all, how long to hold the old picture while new code arrives, and
|
||||
* the plain cross-fade a change inside a page (a trip's tab) runs. The browser
|
||||
* animates snapshots, so the views themselves render as before;
|
||||
* styles/pageTransition.css holds the motion.
|
||||
*/
|
||||
|
||||
// Short on purpose: while it waits the browser shows a frozen snapshot of the old
|
||||
// page, so on a slow connection a longer wait reads as the app hanging.
|
||||
const SETTLE_MS = 350;
|
||||
let current: ViewTransition | null = null;
|
||||
|
||||
/** A newer change skipped this transition: its promises reject, and that is no error. */
|
||||
function skippedByNewer(): void {}
|
||||
|
||||
export function canAnimate(): boolean {
|
||||
return (
|
||||
typeof document !== 'undefined' &&
|
||||
typeof document.startViewTransition === 'function' &&
|
||||
!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves once no placeholder for code still loading (`data-route-fallback`)
|
||||
* is on screen, or after SETTLE_MS. Polled with a timer, never
|
||||
* requestAnimationFrame: while a view transition waits for its update, the
|
||||
* browser renders no frames, so a frame callback never came and the
|
||||
* transition hung until the browser gave up after four seconds.
|
||||
*/
|
||||
export function settled(): Promise<void> {
|
||||
const began = performance.now();
|
||||
return new Promise((resolve) => {
|
||||
const check = () => {
|
||||
if (!document.querySelector('[data-route-fallback]') || performance.now() - began > SETTLE_MS) resolve();
|
||||
else window.setTimeout(check, 16);
|
||||
};
|
||||
check();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs `update` inside a view transition marked `kind` on the root, which the
|
||||
* stylesheets read for the motion. Only the newest transition clears the mark,
|
||||
* or a skipped one would take it away from the one that replaced it.
|
||||
*/
|
||||
export function transitionTo(kind: string, update: () => void): void {
|
||||
if (!canAnimate()) {
|
||||
update();
|
||||
return;
|
||||
}
|
||||
const root = document.documentElement;
|
||||
root.dataset.routeTransition = kind;
|
||||
const transition = document.startViewTransition(async () => {
|
||||
flushSync(update);
|
||||
await settled();
|
||||
});
|
||||
current = transition;
|
||||
transition.ready.catch(skippedByNewer);
|
||||
void transition.finished.catch(skippedByNewer).finally(() => {
|
||||
if (current === transition) delete root.dataset.routeTransition;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user