Todae

Todae

usePopover

usePopover is the open machine behind Popover. Spread getTriggerProps on the trigger and getPopoverProps on Popover.Root, which is surface chrome only. Menu and the Combobox recipe are built on it.

On this page

An outside press and Escape call onOpenChange(false, reason) with outside or escape. The trigger toggles, so it reports trigger on both open and close. An owner can veto one reason and keep the others; in onOpenChange(open, reason), check open === false before vetoing a trigger close. Popover has a veto demo. haspopup sets the trigger's aria-haspopup. anchorRef is the CSS anchor when the trigger is not the element getTriggerProps marks; do not pass it and also spread getTriggerProps.

import { Button, OptionList, Popover, Stack, usePopover } from '@tounsoo/todae';
import { useState } from 'react';

export default function PopoverMenu() {
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState<string | null>(null);
  const [under, setUnder] = useState(0);
  const popover = usePopover({ open, onOpenChange: setOpen, haspopup: 'listbox' });
  return (
    <Stack.Root>
      <Button.Root tooltip={false} {...popover.getTriggerProps()}>
        {value ? `Fruit: ${value}` : 'Choose fruit'}
      </Button.Root>
      <Popover.Root {...popover.getPopoverProps()}>
        <OptionList.Root
          aria-label="Fruit"
          value={value}
          onValueChange={(next) => {
            setValue(next);
            setOpen(false);
          }}
        >
          <OptionList.Option value="apple">Apple</OptionList.Option>
          <OptionList.Option value="pear">Pear</OptionList.Option>
          <OptionList.Option value="plum">Plum</OptionList.Option>
        </OptionList.Root>
      </Popover.Root>
      <Button.Root variant="secondary" onClick={() => setUnder((count) => count + 1)}>
        Page under {under}
      </Button.Root>
    </Stack.Root>
  );
}

Accessible composition

Todae handles: getTriggerProps adds aria-expanded, aria-controls with the surface's id, aria-haspopup (dialog unless you set haspopup) and an onClick that toggles. On open, focus moves into the surface: initialFocusRef, else its first tabbable element, else the surface. Escape and an outside press ask to close. On close, focus goes to restoreFocusRef, else back to what had focus before it opened, unless the person already moved it elsewhere.

You supply: The trigger element. getTriggerProps sets no role, tabIndex or key handling, so spread it on a <button> or Button.Root. The surface's role and name: for haspopup: 'dialog', Popover.Root with role="dialog" and aria-label or aria-labelledby; for listbox, a role-less surface with a named OptionList.Root inside, as above. Your own onClick, ref and style go through getTriggerProps(props), which merges them. Your onClick runs first, and calling event.preventDefault() in it skips the toggle. So on an <a href> trigger, a click handler that prevents navigation also stops the popover; use a <button> instead.

Watch for: Props put after the getTriggerProps() spread replace what it set: a later onClick drops the toggle. A trigger keeps aria-expanded and aria-haspopup while enabled is false, so disable the trigger too. A role-less popover left on the default haspopup announces a dialog that is not there.

When to use

When: A non-modal surface anchored to a trigger, with open and dismiss on the hook and chrome on Popover.Root.

When not: An action menu → Menu. Modal, focus-trapped work → Dialog. A mobile full-bleed picker → BottomSheet, or useSheetPresentation to choose at runtime. A text-only hover hint → Tooltip.

Props

usePopover props
NameTypeDescription
openboolean

Controlled open state.

defaultOpenboolean

Initial open state when uncontrolled.

onOpenChange(open: boolean, reason: PopoverCloseReason) => void

Fires on toggle or dismiss. Leaving open unchanged is a veto.

anchorRefRefObject<HTMLElement | null>

Sets CSS anchor-name on this element and excludes it from outside press.

initialFocusRefRefObject<HTMLElement | null>

Focus this element when the popover opens.

restoreFocusRefRefObject<HTMLElement | null>

Return focus here when the popover closes.

enabledboolean

False skips toggle, dismiss, and focus moves.

Default: true

haspopup"dialog"| "menu"| "listbox"| "tree"| "grid"

aria-haspopup on the trigger. Use "listbox" for an OptionList in a role-less popover.

Default: "dialog"

anchorNamestring | null

CSS anchor name the popover sits at, set on an element other than the trigger, such as useMentions caretPeekAnchorName. Default is the trigger.

idstring

Popover id. Default is a generated id.