overlays

Popovers, menus, dialogs, drawers, and tooltips

All five components float above the page without changing its layout. Native popovers close on Escape or a click outside them (light dismiss). While a modal dialog is open, the browser marks the rest of the page inert. AnswerUI keeps those browser behaviours, applies a shared floating surface, and places popovers beside their opener. Dropdown menus use popovers. Tooltips use CSS anchor positioning instead of the Popover API.

from fasthtml.common import *
from answerui.demo import demo
from fastcore.test import *

Popover

Popover renders a floating panel on the raised neutral surface (--color-surface). Set popovertarget on a button to open it. The browser closes the panel on Escape or a click outside it. While the panel is open, mountPopover sets aria-expanded="true" on the opener, and Btn displays its pressed style.

CSS anchor rules place the panel beside the opener and flip it when it would overflow the viewport. In browsers without anchor positioning, mountPopover computes coordinates in JavaScript and closes the panel if the opener leaves the viewport. Pass side ('bottom'), align ('start'), and side_offset (6 pixels) to configure initial placement. Popover emits popover, popup, data-side, data-align, and data-side-offset for mountPopover with no per-panel JavaScript.


Popover

def Popover(
    *c, popover:str='auto', side:str='bottom', align:str='start', side_offset:int=6, **attrs
):

A native popover positioned beside its opener by the shared controller

The shared stylesheet sets the floating surface, viewport size limits, and entry animation. The browser's Popover API handles hiding, light dismiss, and focus restoration.

Set popovertarget on any button to the popover's id to open it. Put ordinary HTML content inside the panel:

Popover.demo(height='9rem')

Dropdown returns a (trigger, panel) pair containing a button trigger and a native popover of links, buttons, separators, and supplementary content. Pass a label string to create a secondary Btn trigger, or pass a configured Btn to control its variant and attributes. Give an icon-only trigger an accessible name such as aria_label. Dropdown always sets type='button' on the trigger so it never submits an enclosing form.

Dropdown items are ordinary HTML links and buttons rather than an ARIA role="menu" widget. Users move between them with Tab and Shift+Tab, skipping disabled buttons. When the panel opens, mountPopover focuses the first enabled visible control inside it. Dropdown adds menu-item to each direct a and button child and sets popovertarget and popovertargetaction='hide' on buttons that do not already have a popovertarget. Clicking a link with href closes the panel in mountPopover. The browser handles Escape and light dismiss. To mark an unavailable item, set disabled=True on a button or omit href on a link.


def Dropdown(
    trigger, *items, id=None, **attrs
):

A button and its native popover of ordinary links and actions

The Account example uses a text trigger and starts with the user's name and email aligned with its items. The Options example passes a configured Btn with aria_label. Tabbing through the items skips both the static header text and the disabled Duplicate button:

Dropdown.demo(height='14rem')

Invoker commands

Set commandfor on a button to a dialog's id and command to 'show-modal' or 'close'. In browsers without native Invoker Commands support, the shared click handler in browser.js calls showModal() or close() on the target dialog. Use popovertarget for popovers.

Dialog

Dialog renders a native <dialog> element with dialog and dialog-side attributes. Open it as a modal with command='show-modal' and commandfor. The browser moves focus into the dialog, closes it on Escape, and marks the rest of the page inert. Because the dialog stays in its DOM position while rendered in the top layer, it inherits its font and custom properties from its parent element. Close it with command='close' or a <form method="dialog">.

The shared stylesheet sets the floating surface (--color-surface), rounded corners (--radius1), entry animation (ui-dialog-enter), and backdrop tint. Add closedby='any' so a click on the backdrop closes the dialog. Omit closedby='any' on confirmations where the user must click a button.


Dialog

def Dialog(
    *c, side:str='center', **attrs
):

A native modal panel, or a full-height drawer at the start or end edge

The htmx listener in browser.js calls showModal() on a closed Dialog after htmx swaps content into a descendant inside it. A button that loads the dialog body with hx-get does not need command='show-modal'. Because the dialog opens after the swap finishes, users never see stale content. Swapping the <dialog> element itself does not open it. A server can send a closed <dialog> as a partial to close an open one.

This confirmation dialog uses a Stack for its body and a Cluster for its action buttons. Set aria_labelledby to the heading's id to give the dialog an accessible name. The browser does not derive a dialog's accessible name from a nested heading:

Dialog.demo(height='16rem')

Drawer

Pass side='end' or side='start' to Dialog to place it against the inline edge of the viewport (right and left in left-to-right pages). The drawer uses --drawer-size (24rem by default) for its width and expands to 100vw on viewports narrower than 40rem. Tall content scrolls inside the drawer. A drawer opens and closes like a centred Dialog, with the same Escape and page-inertness behaviour. Add closedby='any' so a click outside closes the drawer in supporting browsers. A button with command='close' closes it in all browsers.

demo(Cluster(Btn('Filters', variant='secondary', command='show-modal', commandfor='filters'),
    Btn('Navigation', variant='secondary', command='show-modal', commandfor='nav-drawer'),
    Dialog(Stack(H2('Filters', id='filters-title'), P('Narrow the list.'),
        Btn('Done', command='close', commandfor='filters')),
        id='filters', aria_labelledby='filters-title', side='end', closedby='any'),
    Dialog(Stack(H2('Navigation', id='nav-title'), P('Sections of the app.'),
        Btn('Close', variant='secondary', command='close', commandfor='nav-drawer')),
        id='nav-drawer', aria_labelledby='nav-title', side='start', closedby='any')), height='12rem')

Tooltip

Tooltip pairs a control with a short supplementary description inside an inline span[tooltip]. It appends the description's id to the control's aria-describedby without overwriting existing IDs. Give the control its own accessible name because a tooltip never replaces a label. Use Popover for floating panels that contain interactive controls.

The description is real HTML shown by CSS alone. It appears immediately when the pointer is over the control or description, or when the control has keyboard focus (:focus-visible). Clicking the control does not show the tooltip. Once hover and keyboard focus end, the tooltip stays visible for 150ms and then fades out over --motion. You can move the pointer onto the description during that 150ms delay. Moving to another tooltip hides the first one immediately with no fade, matching native tooltips.

Tooltip links the control and description with inline anchor-name and position-anchor styles. With CSS anchor positioning, the browser centres the description above the control with a --tooltip-gap (6px default) gap and flips it below when there is no room above. In browsers without anchor positioning, the description is placed above the control and aligned to its left edge. Tooltips render outside the top layer rather than using the Popover API. An ancestor with transform or filter becomes the containing block and can clip the tooltip.


Tooltip

def Tooltip(
    trigger, text, id=None, **attrs
):

A control and supplementary description shown by CSS on hover or keyboard focus

Tooltip.demo(height='7rem')