Navigation and overlays

Styles and interactive controllers for responsive navbars, toolbars, breadcrumbs, modal dialogs and drawers, popovers, dropdowns, tooltips, tabs, disclosures, and toasts.

from answerui import assets
from answerui.preview import preview

Responsive navbars, toolbars, and breadcrumbs

Translated labels and dynamic items can overflow a navbar at widths where a fixed breakpoint does not trigger. watchInlineFit(host, row, onCompact) toggles data-fitting to measure the row's natural width against the container. Wide and compact states share one labelled <nav> element and its links (a[aria-current="page"] marks the current page). Put [navbar-toggle] on a disclosure button with aria-controls and aria-expanded rather than using an ARIA menu. Without JavaScript, the links remain visible and wrap.

Use [navbar] with [navbar-row], [navbar-brand], <nav>, [navbar-toggle], and [navbar-end]. Optional modifiers are [navbar-align="center"], [navbar-align="end"], [navbar-vivid], and [navbar-sticky]. mountNavbar(host, {open, onOpenChange}) sets data-compact when the row overflows and toggles data-open on the <nav> row. [navbar-brand], [navbar-toggle], and [navbar-end] stay on the first row while <nav> expands below them. mountNavbar returns {setOpen, dispose} and closes the compact <nav> on Escape, link clicks, or outside clicks. Call mountNavbar on a [navbar-sticky] navbar even without [navbar-toggle]. It writes the navbar's height to --navbar-size on :root for scroll-padding-block-start and removes --navbar-size on dispose().

[responsive-toolbar] and [breadcrumb] use the same data-compact and data-fitting attributes with their own compact markup and focus handling. A [responsive-toolbar] contains [toolbar-actions], [toolbar-group], [toolbar-divider], [toolbar-shortcut], and [toolbar-menu], switching from [toolbar-actions] to [toolbar-menu] when compact. A [breadcrumb] contains [breadcrumb-row], [breadcrumb-parents], [breadcrumb-collapse], [breadcrumb-ancestor], and an ellipsized [breadcrumb-current], with spacing controlled by --breadcrumb-gap (default --space-3). When compact, [breadcrumb-collapse] replaces [breadcrumb-parents].

preview('''
<header navbar>
  <div navbar-row>
    <a navbar-brand href="#workspace">Workspace</a>
    <button type="button" btn btn-variant="quiet" navbar-toggle aria-controls="nav" aria-expanded="false">Navigation</button>
    <nav id="nav" aria-label="Main">
      <a href="#dialogs" aria-current="page">Dialogs</a>
      <a href="#shared">Shared with me</a>
      <a href="#account">Account settings</a>
    </nav>
    <div navbar-end><a href="#help" btn btn-variant="secondary">Help</a></div>
  </div>
</header>
''', width='28rem', height='12rem',
    js=(assets/'fit.js').read_text() + "mountNavbar(document.querySelector('[navbar]'));")

Dialogs, popovers, dropdowns, and tooltips

Use a native <dialog dialog> opened with showModal() for modal tasks that block the page and trap focus until completed or cancelled. Structured slots are header (h2, p), [dialog-close] in the top-end corner, body div elements, and footer. Add [dialog-side="start"] or [dialog-side="end"] to render a full-height drawer whose width defaults to --drawer-size (24rem) and expands to 100vw below 40rem.

Use native popover="auto" with [popup] or [dropdown] for non-modal panels. Native popovers appear in the browser's top layer without moving the element out of its [ui] theme. A [dropdown] holds ordinary links, buttons, and form inputs rather than an ARIA menu. Users move through it with Tab and dismiss it with Escape or an outside click. Inside [dropdown], style entries with [menu-item] (supporting aria-pressed="true", [ui-tone="danger"], and disabled states), [menu-label], hr, and <details option-group> for nested choices instead of cascading flyouts.

On browsers with CSS position-area, [popup] and [dropdown] flip to fit the viewport using position-try-fallbacks. mountPopover(trigger, panel, {onOpenChange, side, align, sideOffset}) keeps aria-expanded in sync and calls positionFloating. On supporting browsers, positionFloating sets anchor-name, position-anchor, and position-area. On other engines, it computes left, top, and --floating-available-height in JavaScript. mountPopover hides the popover if the trigger scrolls out of the viewport and returns a cleanup function. When the panel has [dropdown], it also focuses the first enabled item on open and closes on link clicks.

Wrap a trigger and its [role="tooltip"] element in [tooltip], linking them with aria-describedby. CSS reveals the tooltip on :hover or :has(:focus-visible) and delays hiding by 150ms so the pointer can cross into the tooltip. Set --tooltip-gap (default 6px) to adjust the offset. A tooltip never traps focus, does not close on Escape, and requires no JavaScript.

preview(r'''
<div cluster>
  <button type="button" btn onclick="document.querySelector('#details').showModal()">Open dialog</button>
  <span tooltip>
    <button type="button" btn btn-variant="secondary" id="about" aria-describedby="tip" style="anchor-name: --about">About this workspace</button>
    <span role="tooltip" id="tip" style="position-anchor: --about">Shared with two coworkers</span>
  </span>
  <button type="button" btn btn-variant="secondary" id="actions" popovertarget="options">Options</button>
  <div dropdown popover="auto" id="options">
    <button menu-item type="button" popovertarget="options" popovertargetaction="hide">Rename workspace</button>
    <a menu-item href="#files">View files</a>
    <details option-group><summary>Preview type</summary>
      <button menu-item type="button" aria-pressed="true">Code</button><button menu-item type="button" aria-pressed="false">Note</button>
    </details>
  </div>
</div>
<dialog dialog id="details" aria-labelledby="title">
  <header><h2 id="title">Workspace details</h2></header>
  <p>Files and settings stay in the workspace.</p>
  <form method="dialog"><button btn>Close</button></form>
</dialog>
''', height='18rem', js=(assets/'floating.js').read_text() +
    r'''mountPopover(document.querySelector('#actions'), document.querySelector('#options'));''')

Disclosures, tabs, toolbars, and toasts

Use <details disclosure> with <summary> for disclosures. Group multiple <details disclosure> elements inside [accordion] with the same name attribute when only one item should stay open at a time.

Mark tab structures with [tabs], [tablist] (role="tablist"), [tab] (role="tab", data-value, aria-selected, aria-controls), and [tabpanel] (role="tabpanel", hidden). mountTabs(root, {activationMode, onValueChange}) handles arrow keys, Home, End, and disabled tabs, returning {setValue, dispose}. In 'automatic' mode (the default), moving focus to a tab with arrow keys, Home, or End selects it immediately. In 'manual' mode, selection waits for Enter, Space, or a click. Call setValue(value) to update selection programmatically and dispose() to remove event handlers. For vertical tabs, add both [tabs-orientation="vertical"] on [tabs] and aria-orientation="vertical" on [tablist] to switch layout and arrow keys to Up and Down.

mountToolbar(root) sets a single roving Tab stop on buttons and links in a command row, with Left, Right, Home, and End traversal (respecting RTL). It leaves elements inside [popover] dropdowns in normal Tab order and returns a cleanup function. Application commands and responsive collapsing stay outside mountToolbar.

mountHoverPopover(trigger, panel, {openDelay, closeDelay}) opens a popover on mouse hover after openDelay (default 0 ms). It closes the popover after closeDelay (default 300 ms) once neither element has hover or focus. Pressing ArrowDown on the trigger opens the panel and focuses its first enabled item. Tapping on touch also opens the panel, while the trigger's click handler stays independent (for example, a toggle button whose popover holds extra settings). mountHoverPopover returns a cleanup function.

Use [toaster] and [toast] for transient feedback, and place urgent messages during a modal task inside that modal. Keep the [toaster] live region mounted in the DOM so new messages are announced. [toaster] is fixed at the top-end corner by default, supports [popover] top-layer rendering, and accepts [toaster-position="top-start"], "bottom-start", or "bottom-end". CSS runs the ui-toast-lifetime animation on each [toast] (with optional non-neutral [ui-tone]) for --toast-duration and pauses it on :hover or :focus-within. Remove the toast from state or the DOM when that animation ends.

preview(r'''
<div tabs tabs-orientation="vertical" id="preferences">
  <div tablist role="tablist" aria-label="Preferences" aria-orientation="vertical">
    <button tab type="button" role="tab" id="general-tab" data-value="general" aria-selected="true" aria-controls="general">General</button>
    <button tab type="button" role="tab" id="locked-tab" data-value="locked" aria-selected="false" aria-controls="locked" disabled>Team</button>
    <button tab type="button" role="tab" id="files-tab" data-value="files" aria-selected="false" aria-controls="files">Files</button>
  </div>
  <div tabpanel role="tabpanel" id="general" aria-labelledby="general-tab" tabindex="0">Use Up/Down to focus a tab, then Enter to select it.</div>
  <div tabpanel role="tabpanel" id="locked" aria-labelledby="locked-tab" hidden>Team settings</div>
  <div tabpanel role="tabpanel" id="files" aria-labelledby="files-tab" tabindex="0" hidden>Generated files stay in the workspace.</div>
</div>
''', height='12rem', js=(assets/'interactions.js').read_text() +
    r'''mountTabs(document.querySelector('#preferences'), {activationMode: 'manual'});''')
preview('''
<section accordion>
  <details disclosure name="faq" open>
    <summary>Where are files saved?</summary><p>Inside your workspace.</p>
  </details>
  <details disclosure name="faq">
    <summary>Can I invite someone?</summary><p>Use the account settings page.</p>
  </details>
</section>
''', height='12rem')