fix: unable to opt out of nested client nav (#1940)

This commit is contained in:
Marvin Hagemeister
2023-10-15 08:41:13 +02:00
committed by GitHub
parent fe1e622cf1
commit 0192316785
9 changed files with 347 additions and 151 deletions
+11 -7
View File
@@ -872,8 +872,14 @@ export interface FreshHistoryState {
scrollY: number;
}
function checkClientNavEnabled() {
return document.querySelector(`[${CLIENT_NAV_ATTR}]`) !== null;
function checkClientNavEnabled(el: HTMLElement | null) {
if (el === null) {
return document.querySelector(`[${CLIENT_NAV_ATTR}="true"]`) !== null;
}
const setting = el.closest(`[${CLIENT_NAV_ATTR}]`);
if (setting === null) return false;
return setting.getAttribute(CLIENT_NAV_ATTR) === "true";
}
// Keep track of history state to apply forward or backward animations
@@ -917,8 +923,7 @@ document.addEventListener("click", async (e) => {
// we're doing a fragment navigation.
if (
el.getAttribute("href")?.startsWith("#") ||
!checkClientNavEnabled() ||
el.closest(`[${CLIENT_NAV_ATTR}="true"]`) === null
!checkClientNavEnabled(el)
) {
return;
}
@@ -978,8 +983,7 @@ document.addEventListener("click", async (e) => {
// Check if the user opted out of client side navigation.
if (
partial === null ||
!checkClientNavEnabled() ||
button.closest(`[${CLIENT_NAV_ATTR}="true"]`) === null
!checkClientNavEnabled(button)
) {
return;
}
@@ -1009,7 +1013,7 @@ addEventListener("popstate", async (e) => {
const nextIdx = state.index ?? index + 1;
index = nextIdx;
if (!checkClientNavEnabled()) {
if (!checkClientNavEnabled(null)) {
location.reload();
return;
}
+1 -1
View File
@@ -205,7 +205,7 @@ options.vnode = (vnode) => {
}
if (typeof props[CLIENT_NAV_ATTR] === "boolean") {
props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : null;
props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : "false";
}
} else if (
current && typeof vnode.type === "function" && vnode.type !== Fragment &&