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

sheet-aware-history.ts

35 lines1.3 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.
26 */
27export function createSheetAwareHistory(): RouterHistory {
28 const history = createBrowserHistory();
29 const { push, replace } = history;
30 const withState = (state: object | undefined) =>
31 recordSheetState ? withSheetState(history.location, state) : state;
32 history.push = (path, state, navigateOpts) => push(path, withState(state), navigateOpts);
33 history.replace = (path, state, navigateOpts) => replace(path, withState(state), navigateOpts);
34 return history;