| @@ -34,9 +34,13 @@ | ||
| 34 | 34 | |
| 35 | 35 | function findExtendifyAgentButton() { |
| 36 | 36 | const host = document.getElementById(AGENT_BTN_HOST_ID); |
| 37 | 37 | if (!host) return null; |
| 38 | - return host.querySelector('button') || host; | |
| 38 | + const mounted = host.querySelector('button:not([disabled])'); | |
| 39 | + if (mounted) return mounted; | |
| 40 | + // The stand-in never leaves the slot, so a disabled button means not-yet-mounted. | |
| 41 | + if (host.querySelector('button')) return null; | |
| 42 | + return host; | |
| 39 | 43 | } |
| 40 | 44 | |
| 41 | 45 | function clickExtendifyAgent() { |
| 42 | 46 | const btn = findExtendifyAgentButton(); |
| @@ -66,30 +70,42 @@ | ||
| 66 | 70 | * the Zustand store so we don't have to share a chunk with the |
| 67 | 71 | * agent bundle. |
| 68 | 72 | */ |
| 69 | 73 | function watchAgentSidebar(toolbar, aiBtn) { |
| 70 | - 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; | |
| 71 | 89 | const observer = watch( |
| 72 | 90 | document.body, |
| 73 | 91 | { childList: true, subtree: true }, |
| 74 | 92 | () => { |
| 75 | 93 | const sidebar = document.getElementById(AGENT_SIDEBAR_ID); |
| 76 | - if (!sidebar || attached) return; | |
| 77 | - attached = true; | |
| 78 | - const update = () => { | |
| 79 | - const open = !sidebar.hasAttribute('inert'); | |
| 80 | - toolbar.classList.toggle('ext-tb-agent-open', open); | |
| 81 | - if (!aiBtn) return; | |
| 82 | - aiBtn.inert = open; | |
| 83 | - if (open) { | |
| 84 | - aiBtn.setAttribute('tabindex', '-1'); | |
| 85 | - aiBtn.setAttribute('aria-hidden', 'true'); | |
| 86 | - } else { | |
| 87 | - aiBtn.removeAttribute('tabindex'); | |
| 88 | - aiBtn.removeAttribute('aria-hidden'); | |
| 89 | - } | |
| 90 | - }; | |
| 91 | - 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 | + ); | |
| 92 | 108 | update(); |
| 93 | 109 | }, |
| 94 | 110 | ); |
| 95 | 111 | if (document.getElementById(AGENT_SIDEBAR_ID)) observer.takeRecords(); |