Open History to see the last commit
1import type { MouseEvent as ReactMouseEvent } from "react";
2
3const isThumbButton = (event: ReactMouseEvent) => event.button === 3 || event.button === 4;
4
5// A thumb press would otherwise focus the URL bar, which then stops tracking the page.
6export function preventThumbButtonFocus(event: ReactMouseEvent) {
7 if (isThumbButton(event)) event.preventDefault();
8}
9
10// Chromium runs app-router history on a thumb-button mouseup unless it is cancelled.
11// Page content that already stepped its own history cancels it first.
12export function takeThumbButtonNavigation(
13 event: ReactMouseEvent,
14 navigate: ((direction: "back" | "forward") => void) | undefined,
15) {
16 if (!isThumbButton(event) || event.defaultPrevented) return;
17 event.preventDefault();
18 navigate?.(event.button === 3 ? "back" : "forward");
19}