Popover

PohonGitHub
A non-modal dialog that floats around a trigger element.
<script setup lang="ts">
import { APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot>
    <APopoverTrigger
      class="text-sm color-text p-1.5 rounded-md inline-flex ring ring-ring-accented ring-inset shadow-md transition-colors-280 items-center focus-visible:bg-background-elevated hover:bg-background-elevated"
    >
      <i class="i-lucide:info" />
    </APopoverTrigger>
    <APopoverPortal>
      <APopoverContent
        side="bottom"
        :side-offset="5"
        class="rounded-md bg-background pointer-events-auto ring ring-ring shadow-lg origin-$akar-popover-content-transform-origin focus:outline-none data-[state=closed]:(animate-out fade-out-0 zoom-out-95) data-[state=open]:(animate-in fade-in-0 zoom-in-95) data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2"
      >
        <CorePlaceholder class="m-4 inline-flex size-48" />
      </APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>

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.

Anatomy

Import all parts and piece them together.

<script setup>
import { APopoverAnchor, APopoverArrow, APopoverClose, APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot>
    <APopoverTrigger />
    <APopoverAnchor />
    <APopoverPortal>
      <APopoverContent>
        <APopoverClose />
        <APopoverArrow />
      </APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>

Pohon

One benefit of using Akar is its flexibility and low-level control over the components. However, this also means that you may need to manually construct more complex UI elements by combining multiple Akar components together.

If you feel there's a lot of elements that needs to be constructed manually using Akar, consider using Pohon UI instead. It provides a higher-level abstraction over Akar components with pre-defined styles and behaviors that can help you build UIs faster.

API Reference

Root

Contains all the parts of a popover.

Props

Prop Default Type
defaultOpenfalseboolean

The open state of the popover when it is initially rendered. Use when you do not need to control its open state.

modalfalseboolean

The modality of the popover. When set to true, interaction with outside elements will be disabled and only popover content will be visible to screen readers.

openboolean

Emits

Event Type
update:open[value: boolean]

Event handler called when the open state of the popover changes.

Slots

Slot Type
openboolean

The controlled open state of the popover.

close(): void

Close the popover

Trigger

The button that toggles the popover. By default, the APopoverContent will position itself against the trigger.

Props

Prop Default Type
as'button'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

Data Attributes

Attribute Value
[data-state]'open' | 'closed'

Anchor

An optional element to position the APopoverContent against. If this part is not used, the content will position alongside the APopoverTrigger.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

referenceReferenceElement

The reference (or anchor) element that is being referred to for positioning.

If not provided will use the current component as anchor.

Portal

When used, portals the content part into the body.

Props

Prop Default Type
deferboolean

Defer the resolving of a Teleport target until other parts of the application have mounted (requires Vue 3.5.0+)

{@link https://vuejs.org/guide/built-ins/teleport.html#deferred-teleport}

disabledboolean

Disable teleport and render the component inline

{@link https://vuejs.org/guide/built-ins/teleport.html#disabling-teleport}

forceMountboolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

tostring | HTMLElement

Vue native teleport component prop :to

{@link https://vuejs.org/guide/built-ins/teleport.html#basic-usage}

Content

The component that pops out when the popover is open.

Built with Presence component - supports any animation techniques while maintaining access to presence emitted events.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

align'start' | 'center' | 'end'

The preferred alignment against the trigger. May change when collisions occur.

alignFlipboolean

Flip alignment when colliding with boundary. May only occur when prioritizePosition is true.

alignOffsetnumber

An offset in pixels from the start or end alignment options.

arrowPaddingnumber

The padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

avoidCollisionsboolean

When true, overrides the side and align preferences to prevent collisions with boundary edges.

collisionBoundaryElement | (Element | null)[] | null

The element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check.

collisionPaddingnumber | Partial<Record<Side, number>>

The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }.

disableOutsidePointerEventsboolean

When true, hover/focus/click interactions will be disabled on elements outside the DismissableLayer. Users will need to click twice on outside elements to interact with them: once to close the DismissableLayer, and again to trigger the element.

disableUpdateOnLayoutShiftboolean

Whether to disable the update position for the content when the layout shifted.

forceMountboolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

hideWhenDetachedboolean

Whether to hide the content when the trigger becomes fully occluded.

positionStrategy'fixed' | 'absolute'

The type of CSS position property to use.

prioritizePositionboolean

Force content to be position within the viewport.

Might overlap the reference element, which may not be desired.

referenceReferenceElement

The reference (or anchor) element that is being referred to for positioning.

If not provided will use the current component as anchor.

side'top' | 'right' | 'bottom' | 'left'

The preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled.

sideFlipboolean

Flip to the opposite side when colliding with boundary.

sideOffsetnumber

The distance in pixels from the trigger.

sticky'always' | 'partial'

The sticky behavior on the align axis. partial will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst 'always' will keep the content in the boundary regardless.

trapFocusboolean

Whether focus should be trapped within the AMenuContent

updatePositionStrategy'always' | 'optimized'

Strategy to update the position of the floating element on every animation frame.

Emits

Event Type
closeAutoFocus[event: Event]

Event handler called when auto-focusing on close. Can be prevented.

escapeKeyDown[event: KeyboardEvent]
focusOutside[event: FocusOutsideEvent]
interactOutside[event: PointerDownOutsideEvent | FocusOutsideEvent]
openAutoFocus[event: Event]

Event handler called when auto-focusing on open. Can be prevented.

pointerDownOutside[event: PointerDownOutsideEvent]

Data Attributes

Attribute Value
[data-state]'open' | 'closed'
[data-side]'left' | 'right' | 'bottom' | 'top'
[data-align]'start' | 'end' | 'center'

CSS Variables

Variable Description
--akar-popover-content-transform-origin

The transform-origin computed from the content and arrow positions/offsets

--akar-popover-content-available-width

The remaining width between the trigger and the boundary edge

--akar-popover-content-available-height

The remaining height between the trigger and the boundary edge

--akar-popover-trigger-width

The width of the trigger

--akar-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 APopoverContent. Must be rendered inside APopoverContent.

Props

Prop Default Type
as'svg'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

height5number

The height of the arrow in pixels.

roundedboolean

When true, render the rounded version of arrow. Do not work with as/asChild

width10number

The width of the arrow in pixels.

Close

The button that closes an open popover.

Props

Prop Default Type
as'button'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

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 --akar-popover-trigger-width and --akar-popover-content-available-height to support this. Use them to constrain the content dimensions.

<script setup>
import { APopoverArrow, APopoverClose, APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot>
    <APopoverTrigger>…</APopoverTrigger>
    <APopoverPortal>
      <APopoverContent
        class="APopoverContent"
        :side-offset="5"
      >
        …
      </APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>
/* styles.css */
.APopoverContent {
  width: var(--akar-popover-trigger-width);
  max-height: var(--akar-popover-content-available-height);
}

Origin-aware animations

We expose a CSS custom property --akar-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 { APopoverArrow, APopoverClose, APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot>
    <APopoverTrigger>…</APopoverTrigger>
    <APopoverPortal>
      <APopoverContent class="APopoverContent">
        …
      </APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>
/* styles.css */
.APopoverContent {
  transform-origin: var(--akar-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 { APopoverArrow, APopoverClose, APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot>
    <APopoverTrigger>…</APopoverTrigger>
    <APopoverPortal>
      <APopoverContent class="APopoverContent">
        …
      </APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>
/* styles.css */
.APopoverContent {
  animation-duration: 0.6s;
  animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.APopoverContent[data-side='top'] {
  animation-name: slideUp;
}
.APopoverContent[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 { APopoverAnchor, APopoverArrow, APopoverClose, APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot>
    <APopoverAnchor as-child>
      <div class="Row">
        Row as anchor <APopoverTrigger>Trigger</APopoverTrigger>
      </div>
    </APopoverAnchor>

    <APopoverPortal>
      <APopoverContent>…</APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>
/* styles.css */
.Row {
  background-color: gainsboro;
  padding: 20px;
}

Close using slot props

Alternatively, you can use the close method provided by the APopoverRoot slot props to programmatically close the popover.

<script setup>
import { APopoverAnchor, APopoverArrow, APopoverContent, APopoverPortal, APopoverRoot, APopoverTrigger } from 'akar';
</script>

<template>
  <APopoverRoot v-slot="{ close }">
    <APopoverTrigger>Open</APopoverTrigger>
    <APopoverAnchor />
    <APopoverPortal>
      <APopoverContent>
        <button
          type="submit"
          @click="close"
        >
          Submit
        </button>
        <APopoverArrow />
      </APopoverContent>
    </APopoverPortal>
  </APopoverRoot>
</template>

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 APopoverTrigger.

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 APopoverArrow part and sets a default sideOffset configuration.

Usage

<script setup lang="ts">
import { APopover, APopoverContent, APopoverTrigger } from './your-popover';
</script>

<template>
  <APopover>
    <APopoverTrigger>APopover trigger</APopoverTrigger>
    <APopoverContent>APopover content</APopoverContent>
  </APopover>
</template>

Implementation

export { APopoverRoot as APopover, APopoverTrigger } from 'akar';

// your-popover.ts
export { default as APopoverContent } from 'APopoverContent.vue';
<!-- APopoverContent.vue -->
<script setup lang="ts">
import type { APopoverContentEmits, APopoverContentProps } from 'akar';
import { APopoverContent, APopoverPortal, useForwardPropsEmits } from 'akar';

const props = defineProps<APopoverContentProps>();
const emits = defineEmits<APopoverContentEmits>();

const forwarded = useForwardPropsEmits(props, emits);
</script>

<template>
  <APopoverPortal>
    <APopoverContent v-bind="{ ...forwarded, ...$attrs }">
      <slot />
    </APopoverContent>
  </APopoverPortal>
</template>