Menhir

Popover

View as Markdown

A popup placed against its trigger that leaves the page usable, such as a few settings, a short form or details about an item: the WAI-ARIA dialog pattern, read for a non-modal dialog. The Popup is a popover="auto" element, so the browser puts it in the top layer, above every z-index, and closes it on Escape or a press outside; there is no portal. Menhir places it against the Trigger with Floating UI, flipping it to the other side or shifting it along when it would leave the viewport, and keeps it shown until its exit animation has ended. Render the Popup right after the Trigger: Tab then moves from the Trigger into the Popup and on to what follows, as WCAG's focus order asks. The Popup gets data-side and data-align, with the side and alignment it was placed on, and the CSS variables --anchor-width, --anchor-height and --transform-origin (the point nearest the trigger, for an animation that grows from it). For a popup that keeps focus inside until it is closed, use Dialog. A popover open by default is rendered closed on the server and opens once the page has hydrated.

Size
9.6 kB

Example

Styling
PopoverExample.tsx
import { Popover } from "menhir/popover";

export default function PopoverExample() {
  return (
    <Popover.Root>
      <Popover.Trigger class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
        Notifications
      </Popover.Trigger>
      <Popover.Popup
        sideOffset={8}
        class="w-64 origin-(--transform-origin) overflow-visible rounded-lg border border-stone-200 bg-white p-4 text-sm text-inherit shadow-lg transition-[opacity,scale] duration-150 data-closed:scale-95 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 starting:data-open:scale-95 starting:data-open:opacity-0"
      >
        <Popover.Arrow class="absolute size-2.5 rotate-45 border-stone-200 bg-white data-[side=bottom]:-top-1.5 data-[side=bottom]:border-t data-[side=bottom]:border-l data-[side=left]:-right-1.5 data-[side=left]:border-t data-[side=left]:border-r data-[side=right]:-left-1.5 data-[side=right]:border-b data-[side=right]:border-l data-[side=top]:-bottom-1.5 data-[side=top]:border-r data-[side=top]:border-b dark:border-stone-700 dark:bg-stone-900" />
        <Popover.Title class="m-0 text-sm font-semibold">
          Notifications
        </Popover.Title>
        <Popover.Description class="mt-1 mb-3 text-stone-600 dark:text-stone-400">
          You are all caught up.
        </Popover.Description>
        <Popover.Close class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
          Close
        </Popover.Close>
      </Popover.Popup>
    </Popover.Root>
  );
}

Anatomy

<Popover.Root>
  <Popover.Trigger />
  <Popover.Popup>
    <Popover.Arrow />
    <Popover.Title />
    <Popover.Description />
    <Popover.Close />
  </Popover.Popup>
</Popover.Root>

API reference

Root

Holds the open state and renders no element. Every part has data-open or data-closed.

PropTypeDefaultDescription
openboolean—Whether the popover is open, when controlled. Pair it with onOpenChange. A controlled popover with no Trigger needs an anchor on the Popup.
defaultOpenbooleanfalseWhether the popover is open at first, when uncontrolled.
onOpenChange(open: boolean, details: { reason, event }) => void—Called with the new value when the user opens or closes the popover, and why: "trigger-press", "close-press", "escape-key", "outside-press" or "close-request" (such as another popover opening). A controlled popover changes only if open does, so keeping open true refuses the close.

Trigger

<button type="button" aria-haspopup="dialog"> that opens and closes the popover, with aria-expanded and with popovertarget and aria-controls naming the Popup. Name it with its content. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the trigger with your own button component. Read once.

Popup

<div popover="auto" role="dialog">, placed against the Trigger. Named by the Title in aria-labelledby and described by the Description in aria-describedby, while they are rendered; without a Title, give it an aria-label. Its id is generated, for the Trigger to name. The browser's own popover styles scroll its overflow: set overflow: visible to show an Arrow outside its edge.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left""bottom"The side of the Trigger it is placed against. It moves to the other side when it does not fit, and data-side says where it went.
align"start" | "center" | "end""center"Where along that side it sits. start and end follow the writing direction.
sideOffsetnumber0The gap between the Trigger and the Popup, in pixels.
alignOffsetnumber0The shift along the Trigger's side, in pixels.
collisionPaddingnumber5The space it keeps from the viewport's edges, in pixels.
anchorElement | VirtualElement | (() => Element | VirtualElement | undefined)—What it is placed against instead of the Trigger: an element, a virtual element with a getBoundingClientRect(), such as a point, or a function returning one, for an element you hold in a ref.
initialFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has opened, instead of staying on the Trigger, or going to the element inside with autofocus. A function gets the browser's choice; use one for an element you hold in a ref.
returnFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has closed with focus inside, instead of the Trigger. A function gets the element the browser focused, or null.
render(props) => JSX.Element—Renders the popup with your own component. Read once.

Arrow

aria-hidden <div> that points at the center of the Trigger: Menhir sets its left or top. Position it absolutely on the Popup's edge by its data-side, which it shares with the Popup. An app that renders no Arrow does not bundle the code that places it.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the arrow with your own component. Read once.

Title

<h2> that names the Popup. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the title with your own component. Read once.

Description

<p> that describes the Popup. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the description with your own component. Read once.

Close

<button type="button"> that closes the popover, such as Done, or an icon with an aria-label. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the close button with your own button component. Read once.

Keyboard

KeysAction
SpaceOn the Trigger, opens or closes the popover.
EnterOn the Trigger, opens or closes the popover.
TabFrom the Trigger, moves focus into the popover; from its last element, on to what follows the Trigger. The popover stays open.
Shift + TabMoves focus back, through the popover to the Trigger.
EscapeCloses the popover, the topmost one of nested popovers, and returns focus to its Trigger when it was inside.