| @@ -70,30 +70,42 @@ | ||
| 70 | 70 | * the Zustand store so we don't have to share a chunk with the |
| 71 | 71 | * agent bundle. |
| 72 | 72 | */ |
| 73 | 73 | function watchAgentSidebar(toolbar, aiBtn) { |
| 74 | - let attached = false; | |
| 74 | + const setOpen = (open) => { | |
| 75 | + toolbar.classList.toggle('ext-tb-agent-open', open); | |
| 76 | + if (!aiBtn) return; | |
| 77 | + aiBtn.inert = open; | |
| 78 | + if (open) { | |
| 79 | + aiBtn.setAttribute('tabindex', '-1'); | |
| 80 | + aiBtn.setAttribute('aria-hidden', 'true'); | |
| 81 | + } else { | |
| 82 | + aiBtn.removeAttribute('tabindex'); | |
| 83 | + aiBtn.removeAttribute('aria-hidden'); | |
| 84 | + } | |
| 85 | + }; | |
| 86 | + // A resize past the mobile breakpoint replaces this node, so never latch on one. | |
| 87 | + let watched = null; | |
| 88 | + let inertObserver = null; | |
| 75 | 89 | const observer = watch( |
| 76 | 90 | document.body, |
| 77 | 91 | { childList: true, subtree: true }, |
| 78 | 92 | () => { |
| 79 | 93 | const sidebar = document.getElementById(AGENT_SIDEBAR_ID); |
| 80 | - if (!sidebar || attached) return; | |
| 81 | - attached = true; | |
| 82 | - const update = () => { | |
| 83 | - const open = !sidebar.hasAttribute('inert'); | |
| 84 | - toolbar.classList.toggle('ext-tb-agent-open', open); | |
| 85 | - if (!aiBtn) return; | |
| 86 | - aiBtn.inert = open; | |
| 87 | - if (open) { | |
| 88 | - aiBtn.setAttribute('tabindex', '-1'); | |
| 89 | - aiBtn.setAttribute('aria-hidden', 'true'); | |
| 90 | - } else { | |
| 91 | - aiBtn.removeAttribute('tabindex'); | |
| 92 | - aiBtn.removeAttribute('aria-hidden'); | |
| 93 | - } | |
| 94 | - }; | |
| 95 | - watch(sidebar, { attributes: true, attributeFilter: ['inert'] }, update); | |
| 94 | + if (sidebar === watched) return; | |
| 95 | + watched = sidebar; | |
| 96 | + inertObserver?.disconnect(); | |
| 97 | + inertObserver = null; | |
| 98 | + if (!sidebar) { | |
| 99 | + setOpen(false); | |
| 100 | + return; | |
| 101 | + } | |
| 102 | + const update = () => setOpen(!sidebar.hasAttribute('inert')); | |
| 103 | + inertObserver = watch( | |
| 104 | + sidebar, | |
| 105 | + { attributes: true, attributeFilter: ['inert'] }, | |
| 106 | + update, | |
| 107 | + ); | |
| 96 | 108 | update(); |
| 97 | 109 | }, |
| 98 | 110 | ); |
| 99 | 111 | if (document.getElementById(AGENT_SIDEBAR_ID)) observer.takeRecords(); |