Repository navigation
feat(desktop): add Back and Forward buttons to the titlebar #15071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saphid
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
saphid:feat/titlebar-nav-buttons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { createMemoryHistory } from "@tanstack/react-router"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { canGoForward, trackNavigationHistory } from "./navigationHistory"; | ||
|
|
||
| function memoryStorage(): Storage { | ||
| const values = new Map<string, string>(); | ||
| return { | ||
| get length() { | ||
| return values.size; | ||
| }, | ||
| clear: () => values.clear(), | ||
| getItem: (key) => values.get(key) ?? null, | ||
| key: (index) => [...values.keys()][index] ?? null, | ||
| removeItem: (key) => void values.delete(key), | ||
| setItem: (key, value) => void values.set(key, value), | ||
| }; | ||
| } | ||
|
|
||
| function trackedHistory(storage = memoryStorage()) { | ||
| const history = createMemoryHistory({ initialEntries: ["/"] }); | ||
| trackNavigationHistory(history, () => storage); | ||
| return history; | ||
| } | ||
|
|
||
| describe("canGoForward", () => { | ||
| it("allows Forward only after going back", () => { | ||
| const history = trackedHistory(); | ||
| history.push("/a"); | ||
| history.push("/b"); | ||
| expect(canGoForward(history)).toBe(false); | ||
|
|
||
| history.back(); | ||
| history.back(); | ||
| expect(canGoForward(history)).toBe(true); | ||
|
|
||
| history.forward(); | ||
| expect(canGoForward(history)).toBe(true); | ||
| history.forward(); | ||
| expect(canGoForward(history)).toBe(false); | ||
| }); | ||
|
|
||
| it("drops Forward entries when a new page is pushed", () => { | ||
| const history = trackedHistory(); | ||
| history.push("/a"); | ||
| history.push("/b"); | ||
| history.back(); | ||
| history.push("/c"); | ||
| expect(canGoForward(history)).toBe(false); | ||
| }); | ||
|
|
||
| it("keeps Forward entries across a replace", () => { | ||
| const history = trackedHistory(); | ||
| history.push("/a"); | ||
| history.back(); | ||
| history.replace("/home"); | ||
| expect(canGoForward(history)).toBe(true); | ||
| }); | ||
|
|
||
| it("does not offer Forward for an untracked history", () => { | ||
| const history = createMemoryHistory({ initialEntries: ["/"] }); | ||
| history.push("/a"); | ||
| history.back(); | ||
| expect(canGoForward(history)).toBe(false); | ||
| }); | ||
|
|
||
| it("restores Forward after a reload of the same entry", () => { | ||
| const storage = memoryStorage(); | ||
| const history = createMemoryHistory({ initialEntries: ["/"] }); | ||
| storage.setItem( | ||
| "t3code:navigation-furthest-index", | ||
| JSON.stringify({ key: history.location.state.__TSR_key, furthest: 2 }), | ||
| ); | ||
| trackNavigationHistory(history, () => storage); | ||
| expect(canGoForward(history)).toBe(true); | ||
| }); | ||
|
|
||
| it("ignores a stored position from a different entry", () => { | ||
| const storage = memoryStorage(); | ||
| storage.setItem( | ||
| "t3code:navigation-furthest-index", | ||
| JSON.stringify({ key: "another-entry", furthest: 2 }), | ||
| ); | ||
| expect(canGoForward(trackedHistory(storage))).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { useRouter, type RouterHistory } from "@tanstack/react-router"; | ||
| import { useSyncExternalStore } from "react"; | ||
|
|
||
| // Browsers expose how far back history goes (TanStack's `__TSR_index`) but not | ||
| // how far forward it goes. Each history remembers the furthest entry reached | ||
| // since its latest push, so the Forward control knows whether it has anywhere | ||
| // to go. Start tracking before the first navigation with `trackNavigationHistory`. | ||
| // The position lives in sessionStorage because a reload keeps the window's | ||
| // forward entries. | ||
| const FURTHEST_INDEX_KEY = "t3code:navigation-furthest-index"; | ||
|
|
||
| const furthestIndexByHistory = new WeakMap<RouterHistory, { furthest: number }>(); | ||
|
|
||
| function currentIndex(history: RouterHistory): number | null { | ||
| const index = history.location.state.__TSR_index; | ||
| return Number.isInteger(index) ? index : null; | ||
| } | ||
|
|
||
| // Stored with the current entry's key, so only a reload of that same entry reuses it. | ||
| function readStoredIndex(history: RouterHistory, getStorage: () => Storage): number | null { | ||
| try { | ||
| const stored = JSON.parse(getStorage().getItem(FURTHEST_INDEX_KEY) ?? "{}") as { | ||
| key?: unknown; | ||
| furthest?: unknown; | ||
| }; | ||
| const { key, furthest } = stored; | ||
| return key === history.location.state.__TSR_key && Number.isInteger(furthest) | ||
| ? Number(furthest) | ||
| : null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function storeIndex(history: RouterHistory, getStorage: () => Storage, furthest: number) { | ||
| try { | ||
| const key = history.location.state.__TSR_key; | ||
| getStorage().setItem(FURTHEST_INDEX_KEY, JSON.stringify({ key, furthest })); | ||
| } catch { | ||
| // Without storage, Forward still works until the next reload. | ||
| } | ||
| } | ||
|
|
||
| export function trackNavigationHistory( | ||
| history: RouterHistory, | ||
| getStorage: () => Storage = () => window.sessionStorage, | ||
| ) { | ||
| if (furthestIndexByHistory.has(history)) return; | ||
| const index = currentIndex(history) ?? 0; | ||
| const tracked = { furthest: Math.max(index, readStoredIndex(history, getStorage) ?? index) }; | ||
| furthestIndexByHistory.set(history, tracked); | ||
| history.subscribe(({ action }) => { | ||
| const index = currentIndex(history); | ||
| if (index === null) return; | ||
| tracked.furthest = action.type === "PUSH" ? index : Math.max(tracked.furthest, index); | ||
| storeIndex(history, getStorage, tracked.furthest); | ||
| }); | ||
| } | ||
|
|
||
| export function canGoForward(history: RouterHistory): boolean { | ||
| const tracked = furthestIndexByHistory.get(history); | ||
| const index = currentIndex(history); | ||
| return tracked !== undefined && index !== null && index < tracked.furthest; | ||
| } | ||
|
|
||
| export function useCanGoForward(): boolean { | ||
| const { history } = useRouter(); | ||
| return useSyncExternalStore(history.subscribe, () => canGoForward(history)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tailwind in the owning component: This static titlebar inset belongs in a conditional Tailwind arbitrary-property class on the sidebar provider, rather than in
sidebarProviderStyle.Posted via Macroscope — UI Consistency