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:
@@ -38,15 +38,23 @@ component Tabs {
|
||||
return String(item.value || item.id || index)
|
||||
}
|
||||
|
||||
/*
|
||||
* In url mode the query parameter is the source of truth, not local state.
|
||||
*
|
||||
* The client router owns popstate and swaps the whole page on back and
|
||||
* forward, which discards component state anyway. Reading the URL means
|
||||
* the right tab simply falls out of whatever render happens next, with no
|
||||
* listener to lose and nothing to keep in step.
|
||||
*/
|
||||
shared function currentValue() {
|
||||
if (mode === "url" && typeof window !== "undefined" && window.location) {
|
||||
var fromUrl = new URLSearchParams(window.location.search).get(param || "tab")
|
||||
return fromUrl ? fromUrl : defaultValue()
|
||||
}
|
||||
if (activeValue) {
|
||||
return activeValue
|
||||
}
|
||||
if (active) {
|
||||
return active
|
||||
}
|
||||
var list = itemList()
|
||||
return list.length ? valueOf(list[0], 0) : ""
|
||||
return defaultValue()
|
||||
}
|
||||
|
||||
// The selection this instance started with. The runtime falls back to it
|
||||
@@ -67,34 +75,6 @@ component Tabs {
|
||||
return orientation === "vertical" ? "vertical" : "horizontal"
|
||||
}
|
||||
|
||||
// Back and forward arrive here. The runtime announces the value the URL
|
||||
// now names; applying it must not write history, or stepping back would
|
||||
// push a new entry and trap the user.
|
||||
client function applyUrlValue(sourceEvent) {
|
||||
var detail = sourceEvent ? sourceEvent.detail : null
|
||||
var value = detail ? detail.value : ""
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
var list = itemList()
|
||||
var found = -1
|
||||
for (var index = 0; index < list.length; index += 1) {
|
||||
if (valueOf(list[index], index) === value) {
|
||||
found = index
|
||||
}
|
||||
}
|
||||
if (found === -1) {
|
||||
return
|
||||
}
|
||||
// Idempotent against our own state rather than a DOM attribute the
|
||||
// re-render owns: popstate can fire for a value already selected.
|
||||
if (value === currentValue()) {
|
||||
return
|
||||
}
|
||||
activeValue = value
|
||||
output.change({ value: value, item: list[found], index: found })
|
||||
}
|
||||
|
||||
client function selectTab(item, index, sourceEvent) {
|
||||
if (item.disabled) {
|
||||
return
|
||||
@@ -123,9 +103,6 @@ component Tabs {
|
||||
data-size='{size}'
|
||||
data-mode='{mode}'
|
||||
data-param='{param}'
|
||||
data-wrn-tabs-param='{mode === "url" ? param : ""}'
|
||||
data-wrn-tabs-default='{defaultValue()}'
|
||||
@wrnexus:tabs:restore='applyUrlValue(event)'
|
||||
>
|
||||
<div
|
||||
class="wire-tabs__list"
|
||||
|
||||
Reference in New Issue
Block a user