Popover
Features
- Can be controlled or uncontrolled.
- Customize side, alignment, offsets, collision handling.
- Optionally render a pointing arrow.
- Focus is fully managed and customizable.
- Supports modal and non-modal modes.
- Dismissing and layering behavior is highly customizable.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import { PopoverAnchor, PopoverArrow, PopoverClose, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
</script>
<template>
<PopoverRoot>
<PopoverTrigger />
<PopoverAnchor />
<PopoverPortal>
<PopoverContent>
<PopoverClose />
<PopoverArrow />
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>API Reference
Root
Contains all the parts of a popover.
Trigger
The button that toggles the popover. By default, the PopoverContent will position itself against the trigger.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
Anchor
An optional element to position the PopoverContent against. If this part is not used, the content will position alongside the PopoverTrigger.
Portal
When used, portals the content part into the body.
Content
The component that pops out when the popover is open.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-side] | "left" | "right" | "bottom" | "top" |
[data-align] | "start" | "end" | "center" |
| CSS Variable | Description |
|---|---|
--reka-popover-content-transform-origin | The transform-origin computed from the content and arrow positions/offsets |
--reka-popover-content-available-width | The remaining width between the trigger and the boundary edge |
--reka-popover-content-available-height | The remaining height between the trigger and the boundary edge |
--reka-popover-trigger-width | The width of the trigger |
--reka-popover-trigger-height | The height of the trigger |
Arrow
An optional arrow element to render alongside the popover. This can be used to help visually link the anchor with the PopoverContent. Must be rendered inside PopoverContent.
Close
The button that closes an open popover.
Examples
Constrain the content size
You may want to constrain the width of the content so that it matches the trigger width. You may also want to constrain its height to not exceed the viewport.
We expose several CSS custom properties such as --reka-popover-trigger-width and --reka-popover-content-available-height to support this. Use them to constrain the content dimensions.
<script setup>
import { PopoverArrow, PopoverClose, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
</script>
<template>
<PopoverRoot>
<PopoverTrigger>…</PopoverTrigger>
<PopoverPortal>
<PopoverContent
class="PopoverContent"
:side-offset="5"
>
…
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>/* styles.css */
.PopoverContent {
width: var(--reka-popover-trigger-width);
max-height: var(--reka-popover-content-available-height);
}Origin-aware animations
We expose a CSS custom property --reka-popover-content-transform-origin. Use it to animate the content from its computed origin based on side, sideOffset, align, alignOffset and any collisions.
<script setup>
import { PopoverArrow, PopoverClose, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
</script>
<template>
<PopoverRoot>
<PopoverTrigger>…</PopoverTrigger>
<PopoverPortal>
<PopoverContent class="PopoverContent">
…
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>/* styles.css */
.PopoverContent {
transform-origin: var(--reka-popover-content-transform-origin);
animation: scaleIn 0.5s ease-out;
}
@keyframes scaleIn {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}Collision-aware animations
We expose data-side and data-align attributes. Their values will change at runtime to reflect collisions. Use them to create collision and direction-aware animations.
<script setup>
import { PopoverArrow, PopoverClose, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
</script>
<template>
<PopoverRoot>
<PopoverTrigger>…</PopoverTrigger>
<PopoverPortal>
<PopoverContent class="PopoverContent">
…
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>/* styles.css */
.PopoverContent {
animation-duration: 0.6s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.PopoverContent[data-side="top"] {
animation-name: slideUp;
}
.PopoverContent[data-side="bottom"] {
animation-name: slideDown;
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}With custom anchor
You can anchor the content to another element if you do not want to use the trigger as the anchor.
<script setup>
import { PopoverAnchor, PopoverArrow, PopoverClose, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
</script>
<template>
<PopoverRoot>
<PopoverAnchor as-child>
<div class="Row">
Row as anchor <PopoverTrigger>Trigger</PopoverTrigger>
</div>
</PopoverAnchor>
<PopoverPortal>
<PopoverContent>…</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>/* styles.css */
.Row {
background-color: gainsboro;
padding: 20px;
}Close using slot props
Alternatively, you can use the close method provided by the PopoverRoot slot props to programmatically close the popover.
<script setup>
import { PopoverAnchor, PopoverArrow, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
</script>
<template>
<PopoverRoot v-slot="{ close }">
<PopoverTrigger>Open</PopoverTrigger>
<PopoverAnchor />
<PopoverPortal>
<PopoverContent>
<button type="submit" @click="close">
Submit
</button>
<PopoverArrow />
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>Editing in place
A popover often holds a small form. Bind v-model:open so the form can close it after saving, and read the reason in the details of update:open to tell how it closed: 'close-press' (a PopoverClose), 'escape-key', 'outside-press', 'focus-outside' or 'trigger-press'. Here only Save keeps the draft; every other way out discards it. Closing by setting open yourself emits nothing, as with any controlled prop.
Detached triggers
A PopoverTrigger does not have to be inside its PopoverRoot. Create a handle with createPopoverHandle() and pass it to the handle prop of the root and of any number of triggers. The triggers can live anywhere in the app, including other components and portals. Here each user in a list opens the same popover with their own data, and a separate button opens it from code for one of the triggers.
The popover anchors to the trigger that opened it and returns focus to it on close. Only that trigger gets aria-expanded="true" and aria-controls. Its payload reaches the root's default slot as payload, next to activeTriggerId. While a non-modal popover is open, pressing another of its triggers moves it to that trigger without closing it. A modal popover covers the other triggers, so the same press closes it instead.
<script setup lang="ts">
import { createPopoverHandle, PopoverArrow, PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
interface User { id: string, name: string, email: string }
const users: User[] = [
{ id: 'ada', name: 'Ada Lovelace', email: 'ada@example.com' },
{ id: 'alan', name: 'Alan Turing', email: 'alan@example.com' },
]
const userPopover = createPopoverHandle<User>()
</script>
<template>
<ul>
<li
v-for="user in users"
:key="user.id"
>
{{ user.name }}
<PopoverTrigger
:id="`user-${user.id}`"
:handle="userPopover"
:payload="user"
>
Details
</PopoverTrigger>
</li>
</ul>
<button @click="userPopover.open('user-alan')">
Show Alan
</button>
<PopoverRoot
v-slot="{ payload }"
:handle="userPopover"
>
<PopoverPortal>
<PopoverContent :aria-label="payload?.name">
<p>{{ payload?.email }}</p>
<PopoverArrow />
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>handle.open(triggerId) opens the popover for the trigger with that id. It throws if no such trigger uses the handle, because a popover needs a trigger to anchor to. handle.close() closes it. handle.isOpen is reactive. These calls report the reason 'imperative-action' through beforeUpdate:open and update:open, so details.cancel() can veto them. They do nothing while no root with the handle is mounted. details.trigger is the trigger's element when a trigger press opens, switches or closes the popup, or handle.open(triggerId) opens it. It is undefined for other changes, such as handle.close(), Escape or an outside press.
The handle itself holds no state. The mounted root owns it, and the root and the triggers only connect to the handle after they mount. A handle created at module scope is therefore safe under SSR: nothing from one request can reach another, and every trigger renders closed on the server. Prefer creating the handle in the <script setup> of the component that renders the root and passing it down. Use a module-scope handle when the triggers live in unrelated parts of the app. Use one mounted root per handle: a second root that mounts with the same handle takes it over and closes the first, with a dev warning. A root or trigger kept alive by <KeepAlive> disconnects from the handle while it is inactive.
Real-world examples
- Selection Popover: A formatting toolbar anchored to the current text selection using a virtual Popper reference.
- Color Picker: A comprehensive color picker component that combines ColorArea, ColorSlider, ColorField, and ColorSwatch inside a Popover. This example demonstrates how to build a complete color selection interface with 2D color area, hue and alpha sliders, and input fields for precise color value entry.
- Date Range Presets: Pair a RangeCalendar with the analytics presets people actually reach for — last 7 days, month to date, year to date.
Accessibility
Adheres to the Dialog WAI-ARIA design pattern.
Keyboard Interactions
| Key | Description |
|---|---|
Space | Opens/closes the popover. |
Enter | Opens/closes the popover. |
Tab | Moves focus to the next focusable element |
Shift + Tab | Moves focus to the previous focusable element |
Esc | Closes the popover and moves focus to PopoverTrigger. |
Custom APIs
Create your own API by abstracting the primitive parts into your own component.
Abstract the arrow and set default configuration
This example abstracts the PopoverArrow part and sets a default sideOffset configuration.
Usage
<script setup lang="ts">
import { Popover, PopoverContent, PopoverTrigger } from './your-popover'
</script>
<template>
<Popover>
<PopoverTrigger>Popover trigger</PopoverTrigger>
<PopoverContent>Popover content</PopoverContent>
</Popover>
</template>Implementation
// your-popover.ts
export { default as PopoverContent } from 'PopoverContent.vue'
export { PopoverRoot as Popover, PopoverTrigger } from 'reka-ui'<!-- PopoverContent.vue -->
<script setup lang="ts">
import type { PopoverContentEmits, PopoverContentProps } from 'reka-ui'
import { PopoverContent, PopoverPortal, useForwardPropsEmits } from 'reka-ui'
const props = defineProps<PopoverContentProps>()
const emits = defineEmits<PopoverContentEmits>()
const forwarded = useForwardPropsEmits(props, emits)
</script>
<template>
<PopoverPortal>
<PopoverContent v-bind="{ ...forwarded, ...$attrs }">
<slot />
</PopoverContent>
</PopoverPortal>
</template>Headless composable
usePopover holds the open state (open, onOpenChange(value, reason?), onOpenToggle()), the ids, the detached-trigger state of a handle, and the trigger, close and content part surfaces. Mounting and exit animations (Presence), focus trapping (FocusScope), dismissal on Escape and outside interaction, positioning against the trigger and the portal are not part of it: compose them yourself, or use the components. It is pure; pass a stable baseId when rendering on the server. The composable is experimental; see Headless composables.
