Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Popover

A small panel that a button opens: a hint, a short form or a few controls. It sits next to the button, stays inside the screen, and closes with Escape or a press outside.

Package
@kvirn-ui/react
Pattern
None exactly: APG Disclosure for the button, Dialog (Modal) roles for the popup, used non-modal
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a popover for a little extra that the user asks for with a press: a short explanation, a short form, a few related controls, small enough to sit beside the button.
  • Give a popover with a form or several controls a visible Close button. It is recommended, not required: it gives touch, voice and magnifier users a target.
  • Not for instructions people need to fill in a field correctly: show those under the field as help text, where everyone sees them, or in a Disclosure for longer optional help.
  • Not for text that should appear on hover or focus: use a Tooltip for a name or a shortcut.
  • Not for content that must take over the page or stop the user from going on: use a Dialog. A popover never blocks the page behind it.

Example#

Use cases#

Change one value without leaving the page

Changing a phone number does not need a page of its own. The form sits in the popup, so Tab goes from the button into the form and on to Cancel. After saving, close the popup, put focus back on the button and confirm the change in text that is already on the page.

Phone number: 070 123 45 67

Props in this example:

Warning: Don't forget

Focus goes back to the button by itself when a person closes the popup with Escape, Close, a press outside or a light dismiss, and focus was in the popup. When your own code closes it, as after Save, move focus there yourself (2.4.3 Focus Order). See Focus management.

Open with the cursor in the field

Opening a popover leaves focus on its button, so a screen reader user hears only “expanded” and has to move on to find the popup. When it opens with one field, move focus to that field yourself, in an effect that runs after it has opened. Escape or Close then returns focus to the button.

Props in this example:

Information: Did you know?

Moving focus is your choice, and the contract names it as the one case where the pattern asks for a move. For a hint or a few links, leave focus on the button. See Focus management.

A details button on every row

Each row gets its own Popover. In the browser, opening one popover closes any other that is open. Give every button a name that says which row it is for, starting with the visible word, and name each popup by its own heading.

Your applications

  • 2024-118
  • 2024-131

Warning: Don't forget

Give each repeated button a name that begins with its visible word (“Details for application 2024-118”), so a screen reader’s list of buttons can tell them apart (2.5.3 Label in Name). Name each popup too (4.1.2 Name, Role, Value).

Know why it closed

Keep the open state yourself when the page needs to react to the popover opening or closing. onOpenChange says why it changed. This example shows whether the popover is open and the last reason. With open set, closing is a request: you decide whether to close.

Closed. Last change: –

Props in this example:

Choose where it opens

The popup opens below the start of the button. Pick another side or alignment when the layout needs it, such as the end of a button at the edge of the page. It still flips when there is no room.

Props in this example:

Information: Did you know?

start and end follow the reading direction, so the same placement works in right-to-left languages.

Accessibility#

What it does for you#

  • APG pattern: none exactly. APG has no non-modal dialog pattern. The button follows Disclosure (aria-expanded, aria-controls) and adds aria-haspopup="dialog". The popup uses role="dialog" from Dialog (Modal), used non-modal: no aria-modal, no trap, focus not moved, page not inert.
  • Deviations: none from the APG keyboard practice. Design decisions: the popup uses the browser's own popover attribute, a placement function that keeps it on screen, and one shared stack of dismissable layers, so Escape closes one layer at a time. Opening does not move focus, which is the disclosure pattern's rule, not the modal dialog's (see Focus management).
  • Native elements used: <button> (Trigger, Close), <div popover="auto" role="dialog"> (Popup). Nothing for Root: it renders no element.

A Popover is a small floating panel that a button opens: a hint, a short form, a few controls. The browser puts it in the top layer, so no ancestor's overflow clips it and no z-index is needed. KvirnUI places it next to the button, flips it when there is no room and keeps it inside the viewport. Escape and a press outside close it, and focus goes back to the button. Opening never moves focus: the panel follows the button in the Tab order.

PartElement / roleARIA / stateNotes
Popover.Rootnone: it renders no elementnoneOwns the open state: open / defaultOpen / onOpenChange(open, { reason, event }), and placement, offset (4), padding (8), matchAnchorWidth. Reasons: trigger-press, close-press, escape, outside-press, light-dismiss
Popover.Trigger<button type="button">aria-expanded="true" | "false", aria-controls (the popup's id), aria-haspopup="dialog". data-open while openClass kv-popover-trigger. The anchor the popup is placed against. Its name is the consumer's content. Enter and Space are the button's own
Popover.Popup<div popover="auto" role="dialog">, always renderedname from aria-label or aria-labelledby (the consumer's). No aria-modal. data-open while open, data-placement (bottom-start, top, end-start, …)Class kv-popover-popup. Hidden by the browser while closed (display none, out of the tree). Inline position: fixed, left, top, box-sizing, max-width and max-height, and the CSS variables --kv-popup-width, --kv-popup-max-height and --kv-anchor-width. Never animates its position
Popover.Close<button type="button">none of its own. The name is the consumer's content or aria-labelClass kv-popover-close. Closes the popup and returns focus to the trigger
usePopoverthe same attributes, for your own elementstriggerProps, popupProps, closeProps, isOpen, placementOptions as the Root. The refs are in the props: mergeProps merges yours
usePopupa shared hook for any floating element (Select, Combobox, Menu)popupProps (popover, data-open, data-placement), placement, reposition()Options: open, anchorRef, popupRef, placement, offset, padding, matchAnchorWidth, popover: 'auto' | 'manual', onNativeDismiss. Shows and hides the popup, measures, calls computePlacement and applies the result. Where the Popover API is missing it toggles hidden
useDismissableLayera shared hook for any layer that dismisses on Escape and outside pressesnone{ open, onDismiss(reason, event), ref, ignore, dismissOnEscape, dismissOnOutsidePress }. One page-wide stack: only the top layer reacts. ignore predicates make targets outside the layer (the anchor, a Combobox's input and button) count as inside

Rules, tested in popover.test.tsx, use-popup.test.tsx and use-dismissable-layer.test.tsx:

  • Wiring. aria-controls is the popup's id on every render (the popup is always in the DOM), aria-expanded follows the state, aria-haspopup="dialog" says what opens. The trigger is a real <button type="button">.
  • Top layer. The popup has the popover attribute: auto for Popover (the platform also closes it on Escape and a press outside), manual where the owner closes it (a Combobox keeps focus in its input). showPopover() and hidePopover() are called when open changes, and never throw: a missing API toggles hidden.
  • Placement. computePlacement (@kvirn-ui/core) gets the measured anchor, the popup at its natural size and the viewport. The popup flips to the other side when it doesn't fit, shifts to stay inside the viewport (keeping padding), is never taller than the room that is left (--kv-popup-max-height, and it scrolls inside) and never covers the anchor (2.4.11 Focus Not Obscured (Minimum), AA). start and end follow the anchor's reading direction. It is placed again on scroll (any scroller except the popup itself), on resize, and when the anchor or the popup changes size. While the anchor is entirely outside the viewport (scrolled away) the popup is visibility: hidden and has data-detached, so it never floats over unrelated content, and it returns with the anchor. maxHeight is never more than the viewport minus padding.
  • Dismissal. Escape closes the innermost open layer only. A press outside the top layer closes it, a press on the trigger or inside the popup does not (the trigger's press is a toggle). A touch press counts when the finger lifts, and not at all when the gesture becomes a scroll. A key whose handler called preventDefault() and a key during IME composition don't dismiss. When it closes a layer, the hook calls preventDefault() on that Escape, so the browser's own close request doesn't also close the next layer down: one Escape closes one layer.
  • Same state as the page. When the platform hides an open popup (a light dismiss, or another auto popover opening), onOpenChange(false, { reason: 'light-dismiss' }) runs, so the state matches. A click on the trigger never opens a popup the platform just closed.
  • Focus. See Focus management.
  • Server rendering. Nothing reads window while rendering: the server writes popover, data-placement, aria-* and no inline position. Everything else happens in effects.
  • as. Popover.Trigger and Popover.Close take a component (as={Button}) that must be focusable and spread its props on a DOM node; its props are plain props, merged with the part's (handlers chain, className joins, refs merge). Allowed elements: Popover.Popup div (default) or section; another tag warns once (as-not-allowed) and falls back to div. Without as the trigger and close are <button type="button">.
  • Dev warnings: a Popup with no accessible name, and a Trigger, Popup or Close outside a Root.

It announces nothing.

What you need to do#

  • Name the popup. Its role is dialog: give it aria-label, or aria-labelledby pointing at a heading inside it (4.1.2 Name, Role, Value, A). A development warning appears without one. Use the trigger's wording ("About the service") or a heading, never "popup".
  • Name the trigger with visible text, or aria-label from your translations for an icon-only button (4.1.2 Name, Role, Value, A; 2.5.3 Label in Name, A: the name contains the visible label).
  • Render the Popup right after the Trigger, in the same parent, so Tab goes from the trigger into the popup and back (2.4.3 Focus Order, A). The popup's place in the DOM is its place in the Tab order, wherever it is drawn.
  • Don't hide essential information in a popover that only opens on hover or focus. This one opens on press. A hover or focus card is a Tooltip, with its own contract (1.4.13 Content on Hover or Focus, AA).
  • Provide a visible Close button in a popup with a form or several controls. Recommended, not required by WCAG: the trigger closes the popup and so does Escape, but a visible Close button gives touch, voice and magnifier users a target when the trigger is out of view.
  • Keep the content light. The popup is always rendered and the browser hides it while closed, so its content is in the DOM, with its ids, from the first render.
  • Controlled mode: when open is yours, onOpenChange(false) is a request. If you keep it open, focus has still been returned to the trigger.
  • Everything in button.a11y.md applies to the trigger and the Close button.

Warning: Don't forget

Name the popup with aria-label or aria-labelledby, and render it right after its button. Without a name a screen reader says only “dialog” (4.1.2 Name, Role, Value). Out of order, Tab skips it (2.4.3 Focus Order).

Visual and modes#

  • Focus indicator: the trigger and the Close button are Buttons, so they use the Button focus ring: at least 2px thick, with at least 3:1 contrast against its background (2.4.7 Focus Visible, AA). The default theme draws the popup like a menu (raised surface, edge, rounded corners, padding); if you restyle it, keep the edge and the surface.
  • Target size: the trigger and Close are at least 24 by 24 CSS pixels, and taller in comfortable density (2.5.8 Target Size (Minimum), AA). Nothing in the popup is smaller.
  • Colour: the popup's edge keeps at least 3:1 contrast against the page (1.4.11 Non-text Contrast, AA) and its text at least 4.5:1 on its surface (1.4.3 Contrast (Minimum), AA). The default theme draws the popup on the raised surface in the body text colour, the same pair as a menu; if you restyle it you meet both pairs.
  • forced-colors behaviour (a mode where the system chooses the colours): the default theme gives the popup a CanvasText border and no shadow; if you restyle it, keep a visible edge in the system's text colour. The open state is shown by aria-expanded, never by colour alone (1.4.1 Use of Color, A).
  • reduced-motion behaviour: the hook animates nothing. If you fade the popup, fade opacity only and switch it off under prefers-reduced-motion.
  • Reflow: at 320px the popup is never wider than the viewport minus the padding, scrolls inside when it is too tall, and the page has no horizontal scroll. No horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA): the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow.
  • RTL (right-to-left text): start and end placements follow the anchor's direction: bottom-start lines the popup's right edge with the trigger's in right-to-left text.

Warning: Don't forget

If you style the popup yourself, keep its edge at 3:1 contrast against the page (1.4.11 Non-text Contrast) and its text at 4.5:1 (1.4.3 Contrast (Minimum)).

Focus management#

  • Initial focus: not moved. Opening a popover never moves focus, so a keyboard user who opened it with Enter stays on the trigger, and Tab goes into the popup, which is next in the DOM (render Popover.Popup right after Popover.Trigger). A popover whose first thing is a form can move focus itself, in an effect that runs after open has become true (the popup is hidden until then, so an earlier focus() does nothing). That is the consumer's choice, and it is the one case where the pattern asks for a move.
  • Trap: no. The popup is non-modal: the page stays reachable, nothing is inert, Tab leaves the popup and leaves it open. A modal needs a Dialog.
  • Restore to: the trigger, when the popup closes by Escape, Popover.Close, an outside press or a light dismiss and focus was inside the popup, or on the trigger. Focus on body (lost) counts only after focus entered the popup or the trigger since it opened, or when the user closed it with Escape or Popover.Close (Safari doesn't focus a pressed button). A defaultOpen popup the platform closes before anyone interacted leaves focus alone, and the page doesn't scroll. Focus is never taken from another control the user moved to, so pressing another button closes the popup and leaves that button focused. A press on the trigger leaves focus on the trigger. Focus returned after a pointer press outside or a light dismiss does not scroll the page (preventScroll); after Escape or Close it does, so the trigger comes into view (2.4.11 Focus Not Obscured (Minimum), AA).
  • Closing through open from your own code (open={false}) restores no focus. Only a close the user causes (Escape, Popover.Close, an outside press or a light dismiss) returns it to the trigger, so move focus yourself after you close it.
  • Never obscured by: the popup never covers its anchor (2.4.11 Focus Not Obscured (Minimum), AA), and it is placed inside the viewport with padding. It is in the top layer, so no sticky header covers it. A consumer with a sticky header keeps scroll-padding for the trigger.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): the trigger's aria-expanded, aria-controls and aria-haspopup, and the popup's role and name.
  • 1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A): the popup follows the trigger in the DOM and the Tab order.
  • 1.4.10 Reflow (AA), 1.4.4 Resize Text (AA): the popup is limited to the viewport and scrolls inside.
  • 1.4.11 Non-text Contrast (AA), 1.4.1 Use of Color (A): the edge, and state in aria-expanded.
  • 2.1.1 Keyboard (A), 2.1.2 No Keyboard Trap (A), 2.1.4 Character Key Shortcuts (A): native button keys and Escape. Tab leaves. No shortcuts.
  • 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA): never covers the anchor, and sits in the top layer.
  • 2.5.8 Target Size (Minimum) (AA): the buttons are at least 24 by 24 CSS pixels.
  • 3.2.1 On Focus (A), 3.2.2 On Input (A): opening needs a press, and nothing moves focus on its own.

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none
KeyWhereWhat happens
Tabbefore the triggerMoves focus to the trigger. The trigger is one stop
Test: popover.test.tsx › Tab focuses the trigger, and is one stop
Shift+Tabon the triggerMoves focus to the previous focusable element
Test: popover.test.tsx › Shift+Tab leaves the trigger backwards
Enter / Spaceon the triggerOpens the popup, or closes it when it is open. Focus stays on the trigger
Test: popover.test.tsx › Enter and Space on the trigger toggle the popup
Tabon the trigger, popup openMoves focus into the popup, to its first focusable element: the popup follows the trigger in the Tab order
Test: popover.test.tsx › Tab goes from the trigger into the popup, and Shift+Tab goes back
Tabon the last focusable element in the popupMoves focus to the next focusable element after the popup. The popup stays open: it is not modal, and Tab never traps or dismisses (2.1.2 No Keyboard Trap, A)
Test: popover.test.tsx › Tab goes from the trigger into the popup, and Shift+Tab goes back
Shift+Tabon the first focusable element in the popupMoves focus back to the trigger. The popup stays open
Test: popover.test.tsx › Tab goes from the trigger into the popup, and Shift+Tab goes back
Enter / Spaceon a Close button in the popupCloses the popup and returns focus to the trigger
Test: popover.test.tsx › Enter and Space on Close close the popup and return focus to the trigger
Escapein the popupCloses the popup and returns focus to the trigger
Test: popover.test.tsx › Escape closes the popup and returns focus to the trigger from inside the popup
Escapeon the trigger, popup openCloses the popup. Focus stays on the trigger
Test: popover.test.tsx › Escape closes the popup with focus on the trigger, which keeps focus
Escapea popover open inside another popoverCloses the innermost popup only. A second Escape closes the next
Test: popover.test.tsx › Escape closes the innermost popup only, then the next
Escapepopup closedNothing: the key is the page's own
Test: popover.test.tsx › Escape does nothing while the popup is closed
Pointer presson the triggerOpens the popup, or closes it when it is open. A press on the trigger is never an outside press: a closing click doesn't reopen the popup
Test: popover.test.tsx › a second press on the trigger closes it, and does not open it again
Pointer pressoutside the popup and the triggerCloses the popup. A touch press counts when the finger lifts, so scrolling the page doesn't close it. A control that is pressed keeps the focus
Test: popover.test.tsx › a press outside closes the popup

The popover handles no other key. Arrow keys, Home, End, Page Up and Page Down are the content's own (a list, a form, native scrolling), and the component prevents none of them. Escape is read on the document, after the page's handlers: a handler that calls preventDefault() on it keeps the popup open.

API reference#

import { Popover, usePopover } from '@kvirn-ui/react'

Popover.Root

Renders no element. It owns the open state and passes it to the parts inside it.

PropTypeDefaultDescription
openboolean–Controlled: whether the popover is open. Pair it with onOpenChange.
defaultOpenbooleanfalseUncontrolled: whether the popover starts open.
onOpenChange(open: boolean, details: PopoverChangeDetails) => void–Called when the user opens or closes it. It only reports: with open set, you change open yourself. details.reason is trigger-press, close-press, escape, outside-press or light-dismiss, and details.event is the native event.
placementPlacement'bottom-start'Where the popup goes when there is room: top, bottom, start or end, with -start, -center or -end. It flips when it does not fit, and start and end follow the reading direction.
offsetnumber4The gap between the trigger and the popup, in pixels.
paddingnumber8The space kept to the edge of the viewport, in pixels.
matchAnchorWidthbooleanfalseMakes the popup as wide as the trigger.
childrenReactNode–The Trigger and the Popup. Root renders no element.

Popover.Trigger

Renders <button type="button">. It takes every attribute of a <button> and passes ref to it. The popup is placed against it.

PropTypeDefaultDescription
asElementType'button'A component to render instead of the button, with its props set on the part: as={Button} className="kv-button--icon-only". It must be focusable, forward its ref and spread its props on a DOM node.
Class or attributeValuesMeaning
kv-popover-triggeralwaysThe part class.
aria-expanded"true" or "false"Whether the popup is open.
aria-controlsthe popup’s idPoints at the popup, which stays in the page while closed.
aria-haspopup"dialog"The trigger opens a dialog.
data-openpresent or absentThe popup is open.

Popover.Popup

Renders <div popover="auto"> with the role dialog. It takes every attribute of a <div> and passes ref to it. Render it right after the trigger.

PropTypeDefaultDescription
as'div' | 'section''div'The element the popup is. Any other tag warns once in development and falls back to a div. The role is dialog either way.
Class or attributeValuesMeaning
kv-popover-popupalwaysThe part class.
popover"auto"Puts the popup in the top layer. The browser hides it while closed.
role"dialog"A non-modal dialog. Name it with aria-label or aria-labelledby.
data-openpresent or absentThe popup is open.
data-placementa Placement, such as bottom-startThe side the popup is on now, after it flipped.
data-detachedpresent or absentThe trigger is scrolled out of view. The popup is hidden until it comes back.
--kv-popup-width, --kv-popup-max-height, --kv-anchor-widthlengths in pxSet on the popup: the room it has and the trigger’s width. A long popup scrolls inside instead of leaving the screen.
--kv-popup-width-limit, --kv-popup-height-limitlengths you setCap the width and height of the popup below the room that is left.

Popover.Close

Renders <button type="button"> that closes the popover and returns focus to the trigger. It takes every attribute of a <button> and passes ref to it.

PropTypeDefaultDescription
asElementType'button'A component to render instead of the button, with its props set on the part: as={Button} className="kv-button--icon-only". It must be focusable, forward its ref and spread its props on a DOM node.
Class or attributeValuesMeaning
kv-popover-closealwaysThe part class.

usePopover

Options

PropTypeDefaultDescription
openboolean–Controlled: whether the popover is open. Pair it with onOpenChange.
defaultOpenbooleanfalseUncontrolled: whether the popover starts open.
onOpenChange(open: boolean, details: PopoverChangeDetails) => void–Called when the user opens or closes it. It only reports: with open set, you change open yourself. details.reason is trigger-press, close-press, escape, outside-press or light-dismiss, and details.event is the native event.
placementPlacement'bottom-start'Where the popup goes when there is room: top, bottom, start or end, with -start, -center or -end. It flips when it does not fit, and start and end follow the reading direction.
offsetnumber4The gap between the trigger and the popup, in pixels.
paddingnumber8The space kept to the edge of the viewport, in pixels.
matchAnchorWidthbooleanfalseMakes the popup as wide as the trigger.

Result

PropTypeDefaultDescription
isOpenboolean–Whether the popover is open.
placementPlacement–The placement in use. It differs from the one asked for when the popup flipped.
triggerPropsPopoverTriggerPartProps–Spread on a <button>: type, aria-expanded, aria-controls, aria-haspopup, ref and handlers.
popupPropsPopoverPopupPartProps–Spread on a <div>: popover="auto", role="dialog", id, ref and the data attributes. Name it yourself.
closePropsPopoverClosePartProps–Spread on a <button> inside the popup that closes it.

Known issues#

  • Opening does not move focus, so a screen reader user hears only "expanded" on the trigger and has to move on with Tab (or a virtual cursor key) to reach the popup. This is the disclosure pattern and is deliberate. A Dialog is the pattern for content that must take focus.
  • A closed popup's content is in the DOM, hidden by the browser. A popup with a heavy subtree, or with ids that must be unique, is the consumer's to render conditionally.
  • Safari 17 and iOS are not covered by the automated tests. popover light dismiss and the toggle event differ a little between browsers. Check them in Safari yourself.
  • The default popup is a raised surface with an edge, rounded corners and padding. If you restyle it, keep the edge and the surface (Visual / modes).

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apasspopover.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasspopover.test.tsx › an open popover: expanded, shown, data-open, no axe violations
Keyboard contract rows (Vitest browser)passn/apass13 rows, each names a test (popover.test.tsx › Tab focuses the trigger, and is one stop)
Focus order and managementpassn/apasspopover.test.tsx asserts focus (toHaveFocus); first rule: Tab focuses the trigger, and is one stop
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

Also pending, by hand: whether screen readers announce the popup's name when focus first enters it with Tab, and whether popover="auto" light dismiss and our own Escape and outside-press handling agree on Safari 17 and iOS.

What to check#

With a screen reader and the keyboard, try:

  • Press Enter on the button: "expanded" is read, and focus stays on the button.
  • Tab goes into the popup, and its name is read.
  • Tab past the last control leaves the popup, which stays open.
  • Escape inside the popup closes it and focus returns to the button.
  • Close closes the popup and focus returns to the button.
  • Click elsewhere: the popup closes and focus does not land in an odd place.