fix(ui): derive tab selection from the url instead of syncing to it
My diagnosis in the previous commit was wrong. The component root was not being replaced by a reactive re-render: the client router owns popstate and swaps the whole page shell on back and forward, which discards component state entirely. Every mechanism that tried to push state into the component from outside was therefore doomed -- clicking a tab, announcing an event, tracking the last applied value. In url mode the query parameter is now simply the source of truth, read where the selection is computed. Whatever render happens next produces the right tab, with no listener to lose and nothing to keep in step. This deletes the runtime tab sync entirely -- 1590 bytes -- and fixes the back/forward cases that were previously broken. Verified in the showcase: click writes the url, two backs and two forwards each land on the right tab, and a ?tab= deep link opens on it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3193,40 +3193,6 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
syncRovingGroups();
|
||||
}
|
||||
|
||||
/*
|
||||
* Back/forward for tabs that mirror their selection into the URL. The
|
||||
* runtime never touches component state -- a popstate listener assigning to
|
||||
* it would write after the client function returned, and that write is
|
||||
* dropped. It announces the value instead and the component applies it.
|
||||
*/
|
||||
function syncTabsFromUrl() {
|
||||
var groups = document.querySelectorAll("[data-wrn-tabs-param]");
|
||||
for (var index = 0; index < groups.length; index += 1) {
|
||||
var group = groups[index];
|
||||
var param = group.getAttribute("data-wrn-tabs-param");
|
||||
if (!param) continue;
|
||||
var value = new URLSearchParams(window.location.search).get(param);
|
||||
// Back past the first click lands on a URL with no parameter at all;
|
||||
// the selection the component started with is the answer there.
|
||||
if (value === null) value = group.getAttribute("data-wrn-tabs-default");
|
||||
if (value === null || value === "") continue;
|
||||
if (!group.querySelector('[role="tab"][data-value="' + value + '"]')) continue;
|
||||
// Announce the value; synthesising a click hits nodes a re-render may
|
||||
// have replaced and left unbound.
|
||||
group.dispatchEvent(
|
||||
new CustomEvent("wrnexus:tabs:restore", { detail: { value: value } }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function setupTabUrlSync() {
|
||||
if (window.__wrnexusTabUrlBound) return;
|
||||
window.__wrnexusTabUrlBound = true;
|
||||
window.addEventListener("popstate", syncTabsFromUrl);
|
||||
// Once after hydration, so a shared link opens on the right tab.
|
||||
window.setTimeout(syncTabsFromUrl, 0);
|
||||
}
|
||||
|
||||
function hydrateScopes(root) {
|
||||
var host = root || document;
|
||||
|
||||
@@ -5445,7 +5411,6 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
setupAnchoredOverlays();
|
||||
setupModalDialogs();
|
||||
setupRovingFocus();
|
||||
setupTabUrlSync();
|
||||
window.__wrnexusRepositionAnchored = repositionAnchored;
|
||||
window.__wrnexusHydrateScopes = hydrateScopes;
|
||||
window.__wrnexusInvalidateClientModule = function (url) { clientModuleCache.delete(url); };
|
||||
|
||||
@@ -1052,72 +1052,3 @@ test("an empty or false roving attribute opts the group out entirely", () => {
|
||||
a.dispatchEvent(keydown(win, "ArrowRight"));
|
||||
expect(doc.activeElement!.id).toBe("a");
|
||||
});
|
||||
|
||||
test("popstate announces the value the url names instead of clicking a tab", () => {
|
||||
const win = mount(
|
||||
`<section data-wrn-tabs-param="tab" data-wrn-tabs-default="one">
|
||||
<div role="tablist">
|
||||
<button role="tab" data-value="one" id="t1">One</button>
|
||||
<button role="tab" data-value="two" id="t2">Two</button>
|
||||
</div>
|
||||
</section>`,
|
||||
);
|
||||
const doc = win.document;
|
||||
const group = doc.querySelector("[data-wrn-tabs-param]") as unknown as HTMLElement;
|
||||
const seen: string[] = [];
|
||||
group.addEventListener("wrnexus:tabs:restore", (event) => {
|
||||
seen.push((event as CustomEvent).detail.value);
|
||||
});
|
||||
|
||||
win.location.search = "?tab=two";
|
||||
win.dispatchEvent(
|
||||
new (win as unknown as { Event: new (t: string) => unknown }).Event(
|
||||
"popstate",
|
||||
) as unknown as Parameters<Window["dispatchEvent"]>[0],
|
||||
);
|
||||
expect(seen).toEqual(["two"]);
|
||||
|
||||
// Back past the first click lands on a url with no parameter at all; the
|
||||
// component's starting selection is the right answer there.
|
||||
win.location.search = "";
|
||||
win.dispatchEvent(
|
||||
new (win as unknown as { Event: new (t: string) => unknown }).Event(
|
||||
"popstate",
|
||||
) as unknown as Parameters<Window["dispatchEvent"]>[0],
|
||||
);
|
||||
expect(seen).toEqual(["two", "one"]);
|
||||
});
|
||||
|
||||
test("the url sync announces on every popstate and leaves idempotence to the component", () => {
|
||||
const win = mount(
|
||||
`<section data-wrn-tabs-param="tab" data-wrn-tabs-default="one">
|
||||
<div role="tablist">
|
||||
<button role="tab" data-value="one" id="t1">One</button>
|
||||
<button role="tab" data-value="two" id="t2">Two</button>
|
||||
</div>
|
||||
</section>`,
|
||||
);
|
||||
const doc = win.document;
|
||||
const group = doc.querySelector("[data-wrn-tabs-param]") as unknown as HTMLElement;
|
||||
const seen: string[] = [];
|
||||
group.addEventListener("wrnexus:tabs:restore", (event) => {
|
||||
seen.push((event as CustomEvent).detail.value);
|
||||
});
|
||||
|
||||
win.location.search = "?tab=two";
|
||||
for (let i = 0; i < 3; i += 1) {
|
||||
win.dispatchEvent(
|
||||
new (win as unknown as { Event: new (t: string) => unknown }).Event(
|
||||
"popstate",
|
||||
) as unknown as Parameters<Window["dispatchEvent"]>[0],
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Deliberately not deduplicated here. Tracking what was last applied meant
|
||||
* the runtime held state that drifted out of step with the component, which
|
||||
* silently swallowed real changes. The component compares against its own
|
||||
* selection instead, which cannot drift.
|
||||
*/
|
||||
expect(seen).toEqual(["two", "two", "two"]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user