vael-ui v0.5.7

Used with a component · Popover

usePopover

The headless popover behind <Popover>, <Menu>, <Select>, <Combobox>, <DatePicker> and <TreeSelect>. It positions the panel, closes on Escape and on outside pointer down or focus, joins the shared layer stack so only the topmost layer closes, and returns focus when the panel closes. It doesn't open anything or toggle on a trigger click: you own open. <Popover> adds the trigger slot, teleport, panel, z-index and animation.

Install

import { usePopover } from 'vael-ui'

Examples

<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue'
import { usePopover } from 'vael-ui'

const open = shallowRef(false)
const triggerEl = useTemplateRef<HTMLElement>('trigger')
const positionerEl = useTemplateRef<HTMLElement>('positioner')
const { positionerStyle, close } = usePopover(open, {
  triggerEl,
  positionerEl,
  side: 'bottom',
  align: 'start',
  tabIntoPanel: true,
})
</script>

<template>
  <button ref="trigger" :aria-expanded="open" @click="open = !open">Filters</button>
  <Teleport to="body">
    <div v-if="open" ref="positioner" :style="positionerStyle">
      <label><input type="checkbox" /> In stock</label>
      <button @click="close">Done</button>
    </div>
  </Teleport>
</template>

Parameters

NameTypeDescription
openRef<boolean>Whether the popover is open (first argument). You set it to true; the popover sets it to false when it closes.
triggerElRef<HTMLElement | null>The anchor. Pointer down on it never counts as outside, so your own click toggle works. If focus was in the panel when it closes, focus goes back to the element that had it before opening, or to the trigger.
positionerElRef<HTMLElement | null>The positioned panel wrapper. Teleport it to the body.
sideMaybeRefOrGetter<'top' | 'right' | 'bottom' | 'left'>The side of the trigger to open on. It flips when there is no room. Default bottom.
alignMaybeRefOrGetter<'start' | 'center' | 'end'>How it lines up along that side. Default center.
sideOffset / alignOffsetMaybeRefOrGetter<number>The gap from the trigger and the shift along it, in px. Default 8 and 0.
matchReferenceWidthMaybeRefOrGetter<boolean>Makes the panel at least as wide as the trigger, as <Select> does. Default false.
maxHeightCapMaybeRefOrGetter<number | undefined>Caps the returned maxHeight, in px, even when the viewport has more room.
closeOnEscMaybeRefOrGetter<boolean>Closes on Escape. Only the topmost layer closes. Default true.
closeOnOutsideMaybeRefOrGetter<boolean>Closes on pointer down or focus outside the trigger and panel, and when Tab leaves the panel. Default true.
onOpenChange(value: boolean, details: PopoverOpenChangeDetails) => voidFires before every close, with value: false. details.reason is 'trigger', 'escape', 'outside' or 'programmatic'. Call details.cancel() to keep it open. It doesn't fire on open.
beforeClose() => BeforeCloseA getter for the close guard. Return true to close, false or nothing to stay open, or a promise of either. isClosing is true while a promise is pending.
scopeRef<HTMLElement | null>The region this popover belongs to, for Escape ownership in the layer stack. Omit it for a page-level popover.
tabIntoPanelbooleanTab from the trigger moves into the panel, and Tab past either end closes it and continues from the trigger. Turn it on for panels that take focus. Leave it off when focus stays on the trigger, as in a combobox. Not reactive.

Returns

NameTypeDescription
positionerStyleRef<Record<string, string>>Bind it on the positioner. It stays hidden until the first measurement.
placementRef<Placement>The resolved placement after flipping, such as 'bottom-start'.
transformOriginRef<string>The trigger-facing origin for a scale-in animation.
maxHeightRef<number | null>The room left on the chosen side, in px. Put it on a scrolling body inside the panel.
isClosingRef<boolean>true while the beforeClose guard decides.
close() => voidCloses with the 'programmatic' reason, through onOpenChange and beforeClose.
requestClose(reason: PopoverCloseReason, event?: Event) => voidCloses with your own reason. <Popover> uses 'trigger' for its trigger click.
cancelClose() => voidKeeps it open and ignores the pending beforeClose answer.
layerIndex() => numberThis popover's position in the shared layer stack, or -1 while closed. <Popover> adds it to --ui-z-dialog, so later layers stack on top.