Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@gera2ld/plaid": "~2.5.5",
"@gera2ld/plaid-common-ts": "~2.5.1",
"@gera2ld/plaid-rollup": "~2.5.0",
"@types/dom-navigation": "^1.0.6",
"del-cli": "^4.0.1",
"husky": "^8.0.1",
"typedoc": "^0.23.10"
Expand Down
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,33 @@ function hookHistory() {
});
}

function hookNavigation() {
if (historyHijacked) return;

// The Navigation API is experimental and support for it is still limited.
// See the browsers that support it below:
// https://developer.mozilla.org/en-US/docs/Web/API/Navigation
if (
typeof window !== "undefined" &&
window.navigation &&
window.navigation.addEventListener
) {
historyHijacked = true;
window.navigation.addEventListener("navigate", handleNavigate);
return true;
}
return false;
}

function handleNavigate() {
navigateCallbacks.forEach((callback) => callback());
}

export function onNavigate(callback: () => void) {
hookHistory();
// Prefer the modern Navigation API when it is available
if (!historyHijacked && !hookNavigation()) {
hookHistory();
}
navigateCallbacks.add(callback);
return () => {
navigateCallbacks.delete(callback);
Expand Down