diff --git a/packages/starlight/src/components/PageFrame.astro b/packages/starlight/src/components/PageFrame.astro --- a/packages/starlight/src/components/PageFrame.astro +++ b/packages/starlight/src/components/PageFrame.astro @@ -17,8 +17,26 @@ constructor() { super(); - // Close the menu when the viewport is resized between mobile and desktop sizes. - matchMedia('(min-width: 50em)').addEventListener('change', () => this.hidePopover()); + const desktop = matchMedia('(min-width: 50em)'); + + // The sidebar is always visible on desktop but relies on the popover API on mobile. + // Style resets may hide `[popover]:not(:popover-open)` elements with an `!important` + // rule in an early cascade layer, which no later layer can override. Dropping the + // `popover` attribute on desktop prevents the sidebar from matching such resets. + const syncPopover = () => { + if (desktop.matches) { + if (this.hasAttribute('popover')) { + this.hidePopover(); + this.removeAttribute('popover'); + } + } else if (!this.hasAttribute('popover')) { + this.setAttribute('popover', ''); + } + }; + syncPopover(); + + // Update the popover state when the viewport is resized between mobile and desktop sizes. + desktop.addEventListener('change', syncPopover); // Update the menu state when the sidebar popover is toggled. this.addEventListener('toggle', ({ newState }) => this.trapFocus(newState === 'open'));