<script setup lang="ts">
import { AHoverCardContent, AHoverCardPortal, AHoverCardRoot, AHoverCardTrigger } from 'akar';
</script>
<template>
<AHoverCardRoot>
<AHoverCardTrigger
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" />
</AHoverCardTrigger>
<AHoverCardPortal>
<AHoverCardContent
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" />
</AHoverCardContent>
</AHoverCardPortal>
</AHoverCardRoot>
</template>
Import all parts and piece them together.
<script setup>
import { AHoverCardArrow, AHoverCardContent, AHoverCardPortal, AHoverCardRoot, AHoverCardTrigger } from 'akar';
</script>
<template>
<AHoverCardRoot>
<AHoverCardTrigger />
<AHoverCardPortal>
<AHoverCardContent>
<AHoverCardArrow />
</AHoverCardContent>
</AHoverCardPortal>
</AHoverCardRoot>
</template>
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.
Contains all the parts of a hover card.
| Prop | Default | Type |
|---|---|---|
closeDelay | 300 | numberThe duration from when the mouse leaves the trigger or content until the hover card closes. |
defaultOpen | false | booleanThe open state of the hover card when it is initially rendered. Use when you do not need to control its open state. |
open | boolean | |
openDelay | 700 | numberThe duration from when the mouse enters the trigger until the hover card opens. |
| Event | Type |
|---|---|
update:open | [value: boolean]Event handler called when the open state of the hover card changes. |
| Slot | Type |
|---|---|
open | booleanThe controlled open state of the hover card. Can be binded as |
The link that opens the hover card when hovered.
| Prop | Default | Type |
|---|---|---|
as | 'a' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
reference | ReferenceElementThe reference (or anchor) element that is being referred to for positioning. If not provided will use the current component as anchor. |
| Attribute | Value |
|---|---|
[data-state] | 'open' | 'closed' |
When used, portals the content part into the body.
| Prop | Default | Type |
|---|---|---|
defer | booleanDefer 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} | |
disabled | booleanDisable teleport and render the component inline {@link https://vuejs.org/guide/built-ins/teleport.html#disabling-teleport} | |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | |
to | string | HTMLElementVue native teleport component prop {@link https://vuejs.org/guide/built-ins/teleport.html#basic-usage} |
The component that pops out when the hover card is open.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
align | 'start' | 'center' | 'end'The preferred alignment against the trigger. May change when collisions occur. | |
alignFlip | booleanFlip alignment when colliding with boundary.
May only occur when | |
alignOffset | numberAn offset in pixels from the | |
arrowPadding | numberThe padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. | |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
avoidCollisions | booleanWhen | |
collisionBoundary | Element | (Element | null)[] | nullThe 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. | |
collisionPadding | number | 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 }. | |
disableUpdateOnLayoutShift | booleanWhether to disable the update position for the content when the layout shifted. | |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | |
hideWhenDetached | booleanWhether to hide the content when the trigger becomes fully occluded. | |
positionStrategy | 'fixed' | 'absolute'The type of CSS position property to use. | |
prioritizePosition | booleanForce content to be position within the viewport. Might overlap the reference element, which may not be desired. | |
reference | ReferenceElementThe 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. | |
sideFlip | booleanFlip to the opposite side when colliding with boundary. | |
sideOffset | numberThe distance in pixels from the trigger. | |
sticky | 'always' | 'partial'The sticky behavior on the align axis. | |
updatePositionStrategy | 'always' | 'optimized'Strategy to update the position of the floating element on every animation frame. |
| Event | Type |
|---|---|
escapeKeyDown | [event: KeyboardEvent] |
focusOutside | [event: FocusOutsideEvent] |
interactOutside | [event: PointerDownOutsideEvent | FocusOutsideEvent] |
pointerDownOutside | [event: PointerDownOutsideEvent] |
| Attribute | Value |
|---|---|
[data-state] | 'open' | 'closed' |
[data-side] | 'left' | 'right' | 'bottom' | 'top' |
[data-align] | 'start' | 'end' | 'center' |
| Variable | Description |
|---|---|
--akar-hover-card-content-transform-origin | The |
--akar-hover-card-content-available-width | The remaining width between the trigger and the boundary edge |
--akar-hover-card-content-available-height | The remaining height between the trigger and the boundary edge |
--akar-hover-card-trigger-width | The width of the trigger |
--akar-hover-card-trigger-height | The height of the trigger |
An optional arrow element to render alongside the hover card. This can be used to help visually link the trigger with the AHoverCardContent. Must be rendered inside AHoverCardContent.
| Prop | Default | Type |
|---|---|---|
as | 'svg' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
height | 5 | numberThe height of the arrow in pixels. |
rounded | booleanWhen | |
width | 10 | numberThe width of the arrow in pixels. |
Use the openDelay prop to control the time it takes for the hover card to open.
<script setup>
import {
AHoverCardContent,
AHoverCardRoot,
AHoverCardTrigger,
} from 'akar';
</script>
<template>
<AHoverCardRoot :open-delay="0">
<AHoverCardTrigger>…</AHoverCardTrigger>
<AHoverCardContent>…</AHoverCardContent>
</AHoverCardRoot>
</template>
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-hover-card-trigger-width and --akar-hover-card-content-available-height to support this. Use them to constrain the content dimensions.
// index.vue
<script setup>
import { AHoverCardContent, AHoverCardPortal, AHoverCardRoot, AHoverCardTrigger } from 'akar';
</script>
<template>
<AHoverCardRoot>
<AHoverCardTrigger>…</AHoverCardTrigger>
<AHoverCardPortal>
<AHoverCardContent
class="AHoverCardContent"
:side-offset="5"
>
…
</AHoverCardContent>
</AHoverCardPortal>
</AHoverCardRoot>
</template>
/* styles.css */
.AHoverCardContent {
width: var(--akar-hover-card-trigger-width);
max-height: var(--akar-hover-card-content-available-height);
}
We expose a CSS custom property --akar-hover-card-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 { AHoverCardContent, AHoverCardRoot, AHoverCardTrigger } from 'akar';
</script>
<template>
<AHoverCardRoot>
<AHoverCardTrigger>…</AHoverCardTrigger>
<AHoverCardContent class="AHoverCardContent">
…
</AHoverCardContent>
</AHoverCardRoot>
</template>
/* styles.css */
.AHoverCardContent {
transform-origin: var(--akar-hover-card-content-transform-origin);
animation: scaleIn 0.5s ease-out;
}
@keyframes scaleIn {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}
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 { AHoverCardContent, AHoverCardRoot, AHoverCardTrigger } from 'akar';
</script>
<template>
<AHoverCardRoot>
<AHoverCardTrigger>…</AHoverCardTrigger>
<AHoverCardContent class="AHoverCardContent">
…
</AHoverCardContent>
</AHoverCardRoot>
</template>
/* styles.css */
.AHoverCardContent {
animation-duration: 0.6s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.AHoverCardContent[data-side="top"] {
animation-name: slideUp;
}
.AHoverCardContent[data-side="bottom"] {
animation-name: slideDown;
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
The hover card is intended for sighted users only, the content will be inaccessible to keyboard users.
| Key | Description |
|---|---|
Tab | Opens/closes the hover card. |
Enter | Opens the hover card link |