HoverCard
<script setup lang="ts">
import { ref } from 'vue'
import { HoverCardArrow, HoverCardContent, HoverCardPortal, HoverCardRoot, HoverCardTrigger } from 'radix-vue'
const hoverState = ref(false)
</script>
<template>
<HoverCardRoot v-model:open="hoverState">
<HoverCardTrigger
class="inline-block cursor-pointer rounded-full shadow-[hsl(206_22%_7%_/_35%)_0px_10px_38px_-10px,hsl(206_22%_7%_/_20%)_0px_10px_20px_-15px] outline-none focus:shadow-[0_0_0_2px_white]"
href="https://twitter.com/radix_ui"
target="_blank"
rel="noreferrer noopener"
>
<img
class="block h-[45px] w-[45px] rounded-full"
src="https://pbs.twimg.com/profile_images/1337055608613253126/r_eiMp2H_400x400.png"
alt="Radix UI"
>
</HoverCardTrigger>
<HoverCardPortal>
<HoverCardContent
class="data-[side=bottom]:animate-slideUpAndFade data-[side=right]:animate-slideLeftAndFade data-[side=left]:animate-slideRightAndFade data-[side=top]:animate-slideDownAndFade w-[300px] rounded-md bg-white p-5 shadow-[hsl(206_22%_7%_/_35%)_0px_10px_38px_-10px,hsl(206_22%_7%_/_20%)_0px_10px_20px_-15px] data-[state=open]:transition-all"
:side-offset="5"
>
<div class="flex flex-col gap-[7px]">
<img
class="block h-[60px] w-[60px] rounded-full"
src="https://pbs.twimg.com/profile_images/1337055608613253126/r_eiMp2H_400x400.png"
alt="Radix UI"
>
<div class="flex flex-col gap-[15px]">
<div>
<div class="text-mauve12 m-0 text-[15px] font-medium leading-[1.5]">
Radix
</div>
<div class="text-mauve10 m-0 text-[15px] leading-[1.5]">
@radix_ui
</div>
</div>
<div class="text-mauve12 m-0 text-[15px] leading-[1.5]">
Components, icons, colors, and templates for building high-quality, accessible UI. Free and open-source.
</div>
<div class="flex gap-[15px]">
<div class="flex gap-[5px]">
<div class="text-mauve12 m-0 text-[15px] font-medium leading-[1.5]">
0
</div>
<div class="text-mauve10 m-0 text-[15px] leading-[1.5]">
Following
</div>
</div>
<div class="flex gap-[5px]">
<div class="text-mauve12 m-0 text-[15px] font-medium leading-[1.5]">
2,900
</div>
<div class="text-mauve10 m-0 text-[15px] leading-[1.5]">
Followers
</div>
</div>
</div>
</div>
</div>
<HoverCardArrow class="fill-white" size="8" />
</HoverCardContent>
</HoverCardPortal>
</HoverCardRoot>
</template>Features
- Can be controlled or uncontrolled.
- Customize side, alignment, offsets, collision handling.
- Optionally render a pointing arrow.
- Supports custom open and close delays.
- Opens on hover only.
- Ignored by screen readers.
Installation
Install the component from your command line.
npm install radix-vueAnatomy
Import all parts and piece them together.
<script setup>
import { HoverCardArrow, HoverCardContent, HoverCardPortal, HoverCardRoot, HoverCardTrigger } from 'radix-vue'
</script>
<template>
<HoverCardRoot>
<HoverCardTrigger />
<HoverCardPortal>
<HoverCardContent>
<HoverCardArrow />
</HoverCardContent>
</HoverCardPortal>
</HoverCardRoot>
</template>API Reference
Root
Contains all the parts of a hover card.
| Prop | Default | Type |
|---|---|---|
defaultOpen | 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 The controlled open state of the hover card. Must be binded with | |
openDelay | 700 | numberThe duration from when the mouse enters the trigger until the hover card opens. |
closeDelay | 300 | numberThe duration from when the mouse leaves the trigger or content until the hover card closes. |
| Emit | Type |
|---|---|
@update:open | (value: boolean) => void |
Trigger
The link that opens the hover card when hovered.
| Prop | Default | Type |
|---|---|---|
as | a | string | ComponentThe element or component this component should render as. Can be overwrite by |
asChild | false | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. |
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
Portal
When used, portals the content part into the body.
| Prop | Default | Type |
|---|---|---|
to | body | string | HTMLElementVue native teleport component props. (to) |
Content
The component that pops out when the hover card is open.
| Prop | Default | Type |
|---|---|---|
as | div | string | ComponentThe element or component this component should render as. Can be overwrite by |
asChild | false | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue.js animation libraries. | |
side | "bottom" | enum The preferred side of the trigger to render against when open. Will be reversed when collisions occur and |
sideOffset | 0 | numberThe distance in pixels from the trigger. |
align | "center" | enumThe preferred alignment against the trigger. May change when collisions occur. |
alignOffset | 0 | number An offset in pixels from the |
avoidCollisions | true | boolean When |
collisionBoundary | [] | BoundaryThe 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 | 0 | number | Padding 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: |
arrowPadding | 0 | number The padding between the arrow and the edges of the content. If your content has |
sticky | "partial" | enum The sticky behavior on the align axis. |
hideWhenDetached | false | booleanWhether to hide the content when the trigger becomes fully occluded. |
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-side] | "left" | "right" | "bottom" | "top" |
[data-align] | "start" | "end" | "center" |
| CSS Variable | Description |
|---|---|
--radix-hover-card-content-transform-origin | The transform-origin computed from the content and arrow positions/offsets |
--radix-hover-card-content-available-width | The remaining width between the trigger and the boundary edge |
--radix-hover-card-content-available-height | The remaining height between the trigger and the boundary edge |
--radix-hover-card-trigger-width | The width of the trigger |
--radix-hover-card-trigger-height | The height of the trigger |
Arrow
An optional arrow element to render alongside the hover card. This can be used to help visually link the trigger with the HoverCardContent. Must be rendered inside HoverCardContent.
| Prop | Default | Type |
|---|---|---|
as | svg | string | ComponentThe element or component this component should render as. Can be overwrite by |
asChild | false | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. |
width | 10 | numberThe width of the arrow in pixels. |
height | 5 | numberThe height of the arrow in pixels. |
Examples
Show instantly
Use the openDelay prop to control the time it takes for the hover card to open.
<script setup>
import {
HoverCardArrow,
HoverCardContent,
HoverCardPortal,
HoverCardRoot,
HoverCardTrigger,
} from 'radix-vue'
</script>
<template>
<HoverCardRoot :open-delay="0">
<HoverCardTrigger>…</HoverCardTrigger>
<HoverCardContent>…</HoverCardContent>
</HoverCardRoot>
</template>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 --radix-hover-card-trigger-width and --radix-hover-card-content-available-height to support this. Use them to constrain the content dimensions.
// index.vue
<script setup>
import { HoverCardArrow, HoverCardContent, HoverCardPortal, HoverCardRoot, HoverCardTrigger } from 'radix-vue'
</script>
<template>
<HoverCardRoot>
<HoverCardTrigger>…</HoverCardTrigger>
<HoverCardPortal>
<HoverCardContent class="HoverCardContent" :side-offset="5">
…
</HoverCardContent>
</HoverCardPortal>
</HoverCardRoot>
</template>/* styles.css */
.HoverCardContent {
width: var(--radix-hover-card-trigger-width);
max-height: var(--radix-hover-card-content-available-height);
}Origin-aware animations
We expose a CSS custom property --radix-hover-card-content-transform-origin. Use it to animate the content from its computed origin based on side, sideOffset, align, alignOffset and any collisions.
// index.vue
<script setup>
import { HoverCardArrow, HoverCardContent, HoverCardPortal, HoverCardRoot, HoverCardTrigger } from 'radix-vue'
</script>
<template>
<HoverCardRoot>
<HoverCardTrigger>…</HoverCardTrigger>
<HoverCardContent class="HoverCardContent">
…
</HoverCardContent>
</HoverCardRoot>
</template>/* styles.css */
.HoverCardContent {
transform-origin: var(--radix-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);
}
}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.
// index.vue
<script setup>
import { HoverCardArrow, HoverCardContent, HoverCardPortal, HoverCardRoot, HoverCardTrigger } from 'radix-vue'
</script>
<template>
<HoverCardRoot>
<HoverCardTrigger>…</HoverCardTrigger>
<HoverCardContent class="HoverCardContent">
…
</HoverCardContent>
</HoverCardRoot>
</template>/* styles.css */
.HoverCardContent {
animation-duration: 0.6s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.HoverCardContent[data-side="top"] {
animation-name: slideUp;
}
.HoverCardContent[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);
}
}Accessibility
Keyboard Interactions
The hover card is intended for mouse users only so will not respond to keyboard navigation.
