Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

sheet-aware-history.ts

37 lines1.4 KB

1import { createBrowserHistory } from "@tanstack/react-router";
2import type { RouterHistory } from "@tanstack/react-router";
3​
4import { withSheetState } from "./app-sheet-history";
5​
6let recordSheetState = false;
7​
8/**
9 * Turned on while the small-screen layout (the one with the sheet) is
10 * showing. The split layout has no sheet, so its entries record nothing and
11 * a later switch to small screens treats them like direct loads.
12 */
13export function setRecordSheetState(enabled: boolean) {
14 recordSheetState = enabled;
16​
17export function isRecordingSheetState(): boolean {
18 return recordSheetState;
20​
21/**
22 * Browser history where every new entry records whether the app sheet is
23 * open, inheriting it from the current entry unless the navigation says
24 * otherwise. Links inside the app don't need to know the sheet exists.
25 * See `app-sheet-history.ts` for the whole contract. Tests pass a memory
26 * history as `history`.
27 */
28export function createSheetAwareHistory(
29 history: RouterHistory = createBrowserHistory(),
30): RouterHistory {
31 const { push, replace } = history;
32 const withState = (state: object | undefined) =>
33 recordSheetState ? withSheetState(history.location, state) : state;
34 history.push = (path, state, navigateOpts) => push(path, withState(state), navigateOpts);
35 history.replace = (path, state, navigateOpts) => replace(path, withState(state), navigateOpts);
36 return history;