Popover
popover() is a behavior for an element with the HTML popover attribute. The browser shows it in the top layer, above everything else, and a button with popovertarget opens and closes it with no code at all. The browser also closes it when the user clicks outside or presses Escape (light dismiss), sets aria-expanded on the button and returns the focus to it. The behavior keeps open in state and lets the model open and close the popover.
import { popover } from 'sygnal/ui'
function Filters({ state, uid }) { return ( <div> <button className="filters-button" popovertarget={uid('filters')}>Filters</button> <div className="filters" id={uid('filters')} popover="auto" aria-label="Filters"> <label><input type="checkbox" className="only-open" checked={state.onlyOpen} /> Only open tasks</label> <button className="filters-done">Done</button> </div> <p>{state.filters.open ? 'Choosing filters…' : ''}</p> </div> )}
Filters.initialState = { onlyOpen: false }Filters.uses = { filters: popover({ popover: '.filters', close: '.filters-done' }) }Filters.intent = ({ DOM }) => ({ ONLY_OPEN: DOM.change('.only-open').checked() })Filters.model = { ONLY_OPEN: (state, onlyOpen) => ({ ...state, onlyOpen }) }popovertarget takes the popover’s id; uid() keeps it unique when the component is rendered more than once.
Options
Section titled “Options”| Option | ||
|---|---|---|
popover | (required) | The popover element: a selector or a control |
trigger | A button whose clicks open and close the popover through the model (TOGGLE), as Radix’s Popover.Trigger; see A trigger button | |
close | A button inside: its clicks close the popover | |
id | the key in uses | The prefix of the ids popoverAttrs makes |
open | Open at the start. Given at all, even open: false, it makes the popover controlled (see Controlled dismissal) |
A trigger button
Section titled “A trigger button”A popovertarget button needs no option. To open and close the popover through the model instead, as Radix’s Popover.Trigger does, name the button in trigger and spread popoverAttrs on both elements:
import { popover, popoverAttrs } from 'sygnal/ui'
function Share({ state, uid }) { const a = popoverAttrs(state.share, uid) return ( <div> <button className="share-button" {...a.trigger}>Share</button> <div className="share" {...a.popover} popover="auto" aria-label="Share"> <button>Copy link</button> <button>Email</button> </div> <p>{state.share.open ? 'Sharing…' : ''}</p> </div> )}
Share.uses = { share: popover({ popover: '.share', trigger: '.share-button' }) }- A click on the trigger dispatches
share.TOGGLEwith the value to go to,trueorfalse. The behavior reads it from the state at the press, because a press on the trigger light-dismisses anautopopover before the click comes: a plain flip would open it again. popoverAttrs(state.share, uid)gives the triggeraria-haspopup="dialog",aria-expanded,aria-controlsanddata-state, and the popover itsidanddata-state.- A press on the trigger isn’t outside a controlled popover.
State and actions
Section titled “State and actions”state.filters is { id, open } (id: the prefix of popoverAttrs’ ids), and open is the source of truth: a reducer that writes it shows or hides the popover, and the popover’s toggle event writes it back, whatever opened or closed it.
| Action | Data | |
|---|---|---|
filters.OPEN | Sets open: true and shows the popover (showPopover) | |
filters.CLOSE | Sets open: false and hides it (hidePopover) | |
filters.TOGGLE | none, or true / false | Flips open (with data: sets it) and shows or hides it: the trigger |
filters.TOGGLED | true / false | The toggle event (a popovertarget button, a light dismiss, Escape): sets open |
filters.DISMISS | the event | Controlled only: Escape, a press outside, the focus moving outside (the keydown, pointerdown or focusin event): sets open: false and hides it |
filters.SYNC | false | The popover left the page while open (a page change, a conditional render), or its component was disposed while it was open (a wizard step left: the step’s state is kept): sets open: false, as no toggle event comes |
| filters.APPLY | true / false | open changed outside these actions (your reducer wrote it): the popover is shown or hidden to match |
The commands check the popover itself (:popover-open): OPEN then CLOSE in one go leaves it closed, and showPopover() is never called on an open popover.
The popover works inside a <Portal> too.
Controlled dismissal
Section titled “Controlled dismissal”Without the open option the browser light-dismisses an auto popover, and the toggle event tells the state afterwards. A host entry for filters.TOGGLED that keeps open: true shows it again, but the browser has already hidden it, so it can disappear for a frame.
To decide before anything is hidden, pass the open option, even open: false. The popover is then controlled, as Radix’s open / onOpenChange is:
- It is a
manualpopover (the behavior setspopover="manual"before it shows it), so the browser never dismisses it. - The behavior dismisses it itself, as Radix’s
DismissableLayerdoes: Escape, a press outside and the focus moving outside arrive asfilters.DISMISS, with the event as the data. One interaction is oneDISMISS: a press outside that also moves the focus outside reports thepointerdown. A press on itstriggeror its ownpopovertargetbutton isn’t outside. - Its
popovertargetbuttons andcommandforcommands go through the model too: the behavior prevents the browser’s own toggle and dispatchesTOGGLE,OPENorCLOSE(frompopovertargetaction).
A host entry that keeps open: true keeps it shown, with no frame where it is hidden:
Filters.uses = { filters: popover({ popover: '.filters', close: '.filters-done', open: false }) }Filters.model = { // pinned: Escape and a click elsewhere leave it open 'filters.DISMISS': (state, event, next, { previousState }) => (state.pinned ? previousState : state),}Controlled popovers and dialogs stack as layers. Escape dismisses the top layer only (not while an auto popover inside it is open, which the browser closes first), and an Escape that an element inside handled (preventDefault()) dismisses nothing. A press on a parent layer outside a child dismisses the child only, and a press outside both dismisses both. A layer opened in another one counts as inside it, also when it renders through a <Portal>. Under a modal dialog, presses can’t reach the page, so the layers under it aren’t dismissed by them.
The Menu, Select and Combobox parts are layers too. One open inside a controlled popover or dialog is the top layer: Escape and a press outside it close it first, and a press in it (in its list as well) isn’t outside the popover. The next Escape closes the popover, and a press outside both closes both.
A modal dialog that isn’t rendered inside the popover moves the focus out of it when it opens, so it dismisses the popover, as in Radix. Keep the popover open by rendering the dialog inside it, or with a DISMISS entry that refuses while the dialog is open.
Open it from somewhere else, for example a keyboard shortcut, with an intent action of the same name: 'filters.OPEN': DOM.keydown('document').filter((e) => e.key === 'f' && e.altKey).
auto or manual
Section titled “auto or manual”popover="auto"(or justpopover): light dismiss, and opening one closes the otherautopopovers that aren’t its ancestors. Menus, pickers and filter panels.popover="manual": only your code and itspopovertargetbutton close it. Use it for something that stays open, such as a tooltip or toasts. A controlled popover is a manual one with the dismissal done by the behavior.
A popover inside a modal dialog works: it opens above the dialog, the first Escape closes the popover and the second the dialog (controlled or not).
Accessibility
Section titled “Accessibility”- Name the popover (
aria-labeloraria-labelledby) when it is a region of controls, as above. popovertargetgives the buttonaria-expandedand links it to the popover for assistive technology. The browser returns the focus to the button when the popover closes with the focus inside it.- For a menu with arrow-key navigation, use a menu part, not a bare popover.
Styling
Section titled “Styling”Browsers center a popover in the viewport by default. To place it next to its button, use CSS anchor positioning:
.filters-button { anchor-name: --filters; }.filters { position-anchor: --filters; position-area: bottom span-right; margin: 4px 0 0; inset: auto; }.filters:popover-open { animation: fade-in 120ms ease-out; }For a component rendered more than once, give each instance its own anchor name, from uid() in a style prop, as the tooltip does.
Testing
Section titled “Testing”The mock DOM doesn’t open popovers, so simulate the toggle event as the browser sends it:
import { it, expect } from 'vitest'import { renderComponent } from 'sygnal'import Filters from './Filters.jsx'
it('follows the popover and closes it from the Done button', async () => { const t = renderComponent(Filters) await t.ready() t.simulateEvent('.filters', 'toggle', { newState: 'open', oldState: 'closed' }) await t.next((s) => s.filters.open)
t.simulateEvent('.filters-done', 'click') await t.settle() expect(t.commands('ELEMENT')).toEqual([{ hidePopover: '.filters' }]) expect(t.actions.map((a) => a.type)).toEqual(['INITIALIZE', 'filters.TOGGLED', 'filters.CLOSE']) t.dispose()})