diff --git a/server/shell.ts b/server/shell.ts index efc741b..4b7c7bc 100644 --- a/server/shell.ts +++ b/server/shell.ts @@ -333,12 +333,18 @@ export function renderShell(opts: ShellOptions): string { } catch(e) { tabCache = null; } // ── Tab events ── + // Set active on tab bar ONLY after switchTo() confirms the pane is ready. + // This prevents the visual desync where the tab highlights before content loads. tabBar.addEventListener('layer-switch', (e) => { - const { moduleId } = e.detail; + const { layerId, moduleId } = e.detail; saveTabs(); if (tabCache) { tabCache.switchTo(moduleId).then(ok => { - if (!ok) window.location.href = window.__rspaceNavUrl(spaceSlug, moduleId); + if (ok) { + tabBar.setAttribute('active', layerId); + } else { + window.location.href = window.__rspaceNavUrl(spaceSlug, moduleId); + } }); } else { window.location.href = window.__rspaceNavUrl(spaceSlug, moduleId); @@ -353,7 +359,11 @@ export function renderShell(opts: ShellOptions): string { saveTabs(); if (tabCache) { tabCache.switchTo(moduleId).then(ok => { - if (!ok) window.location.href = window.__rspaceNavUrl(spaceSlug, moduleId); + if (ok) { + tabBar.setAttribute('active', 'layer-' + moduleId); + } else { + window.location.href = window.__rspaceNavUrl(spaceSlug, moduleId); + } }); } else { window.location.href = window.__rspaceNavUrl(spaceSlug, moduleId); diff --git a/shared/components/rstack-tab-bar.ts b/shared/components/rstack-tab-bar.ts index 81a1933..43b7ff8 100644 --- a/shared/components/rstack-tab-bar.ts +++ b/shared/components/rstack-tab-bar.ts @@ -794,14 +794,14 @@ export class RStackTabBar extends HTMLElement { // Clean up previous document-level listeners to prevent leak if (this.#docCleanup) { this.#docCleanup(); this.#docCleanup = null; } - // Tab clicks + // Tab clicks — dispatch event but do NOT set active yet. + // The shell's event handler calls switchTo() and sets active only after success. this.#shadow.querySelectorAll(".tab").forEach(tab => { tab.addEventListener("click", (e) => { const target = e.target as HTMLElement; if (target.classList.contains("tab-close")) return; const layerId = tab.dataset.layerId!; const moduleId = tab.dataset.moduleId!; - this.active = layerId; this.trackRecent(moduleId); this.dispatchEvent(new CustomEvent("layer-switch", { detail: { layerId, moduleId }, @@ -929,12 +929,11 @@ export class RStackTabBar extends HTMLElement { this.#shadow.querySelectorAll(".layer-plane").forEach(plane => { const layerId = plane.dataset.layerId!; - // Click to switch layer + // Click to switch layer — do NOT set active here, let shell handler confirm plane.addEventListener("click", (e) => { if (this.#flowDragSource || this.#orbitDragging) return; const layer = this.#layers.find(l => l.id === layerId); if (layer) { - this.active = layerId; this.dispatchEvent(new CustomEvent("layer-switch", { detail: { layerId, moduleId: layer.moduleId }, bubbles: true,