Placement hover cards
Five hover cards that open in different directions relative to the trigger: top, bottom, left, right and a following offset. Each is self-contained (no radix), sized, token-driven, and opens on hover or keyboard focus.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/hover-card-placementDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/hover-card-placement.tsx"use client"
import * as React from "react"
import { ArrowDown, ArrowLeft, ArrowRight, ArrowUp, Move } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Placement hover card family: 5 cards that open in different DIRECTIONS relative
to the trigger (top, bottom, left, right, side-offset). Self-contained (no radix,
no portal) - hover/focus on the trigger opens the JS state and AnimatePresence
brings the card in with a slight slide from that direction. The positioning is
per-direction through className: a relative wrapper + an absolute position wrapper
(the centering translate lives here) + the inner motion card, which carries only
an opacity + small x/y offset animation (so no transform collision). Color comes
ONLY from tokens; alpha via color-mix(in oklab, ...). Under useReducedMotion all
transforms drop away and only an opacity fade remains. size = card width. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type Placement = "top" | "bottom" | "left" | "right" | "follow"
const cardWidth: Record<StyledSize, string> = {
sm: "w-56",
md: "w-64",
lg: "w-72",
xl: "w-80",
}
/* Position wrapper: direction relative to the trigger plus a centring translate. The inner card carries motion x/y, which is why the centring translate lives here (on a separate element). */
const posClass: Record<Placement, string> = {
top: "bottom-full left-1/2 mb-2 -translate-x-1/2",
bottom: "top-full left-1/2 mt-2 -translate-x-1/2",
left: "right-full top-1/2 mr-2 -translate-y-1/2",
right: "left-full top-1/2 ml-2 -translate-y-1/2",
follow: "left-full top-0 ml-2",
}
/* Entry offset per direction (px). It enters sliding slightly from that side. */
const offset: Record<Placement, { x: number; y: number }> = {
top: { x: 0, y: -8 },
bottom: { x: 0, y: 8 },
left: { x: -8, y: 0 },
right: { x: 8, y: 0 },
follow: { x: 6, y: 6 },
}
const cardBase =
"pointer-events-auto block rounded-xl border border-border bg-popover p-4 text-left text-popover-foreground shadow-lg"
const triggerBase =
"inline-flex items-center gap-2 rounded-md font-medium text-primary underline-offset-4 outline-none hover:underline focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
interface Props {
size?: StyledSize
trigger?: React.ReactNode
children?: React.ReactNode
className?: string
}
interface ShellProps extends Props {
placement: Placement
icon: React.ReactNode
title: React.ReactNode
fallback: React.ReactNode
}
/* Shared shell: trigger plus an AnimatePresence card positioned in that direction. It opens on hover and focus (JS state). data-slot="styled-hover-card" is preserved. */
function HoverCardShell({
placement,
size = "md",
trigger,
icon,
title,
fallback,
children,
className,
}: ShellProps) {
const [open, setOpen] = React.useState(false)
const reduce = useReducedMotion()
const off = offset[placement]
const initial = reduce ? { opacity: 0 } : { opacity: 0, x: off.x, y: off.y }
const animate = reduce ? { opacity: 1 } : { opacity: 1, x: 0, y: 0 }
return (
<span
data-slot="styled-hover-card"
className="group relative inline-flex"
onPointerEnter={() => setOpen(true)}
onPointerLeave={() => setOpen(false)}
onFocus={() => setOpen(true)}
onBlur={() => setOpen(false)}
>
{trigger ?? (
<button type="button" className={triggerBase}>
{icon}
{fallback}
</button>
)}
<span className={cn("pointer-events-none absolute z-20", posClass[placement])}>
<AnimatePresence>
{open ? (
<motion.span
role="dialog"
className={cn(cardBase, cardWidth[size], className)}
initial={initial}
animate={animate}
exit={initial}
transition={reduce ? { duration: 0.12 } : { duration: 0.18, ease: "easeOut" }}
>
<span className="flex items-center gap-2 text-sm font-semibold [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none">
{icon}
{title}
</span>
<span className="mt-2 block text-sm text-muted-foreground">{children}</span>
</motion.span>
) : null}
</AnimatePresence>
</span>
</span>
)
}
/* Top: the card opens ABOVE the trigger, entering with a slight slide downwards. */
export function TopHoverCard({ size, trigger, children, className }: Props) {
return (
<HoverCardShell
placement="top"
size={size}
trigger={trigger}
className={className}
icon={<ArrowUp />}
title="Top placement"
fallback="Above"
>
{children ?? "Opens above the trigger and eases down into its resting spot."}
</HoverCardShell>
)
}
/* Bottom: the card opens BELOW the trigger, entering with a slight slide upwards. */
export function BottomHoverCard({ size, trigger, children, className }: Props) {
return (
<HoverCardShell
placement="bottom"
size={size}
trigger={trigger}
className={className}
icon={<ArrowDown />}
title="Bottom placement"
fallback="Below"
>
{children ?? "Opens below the trigger and eases up into its resting spot."}
</HoverCardShell>
)
}
/* Left: the card opens to the LEFT of the trigger, entering with a slight slide from the left. */
export function LeftHoverCard({ size, trigger, children, className }: Props) {
return (
<HoverCardShell
placement="left"
size={size}
trigger={trigger}
className={className}
icon={<ArrowLeft />}
title="Left placement"
fallback="Left"
>
{children ?? "Opens to the left of the trigger and slides in from that side."}
</HoverCardShell>
)
}
/* Right: the card opens to the RIGHT of the trigger, entering with a slight slide from the right. */
export function RightHoverCard({ size, trigger, children, className }: Props) {
return (
<HoverCardShell
placement="right"
size={size}
trigger={trigger}
className={className}
icon={<ArrowRight />}
title="Right placement"
fallback="Right"
>
{children ?? "Opens to the right of the trigger and slides in from that side."}
</HoverCardShell>
)
}
/* Follow: the card opens BESIDE the trigger with a slight diagonal offset. */
export function FollowHoverCard({ size, trigger, children, className }: Props) {
return (
<HoverCardShell
placement="follow"
size={size}
trigger={trigger}
className={className}
icon={<Move />}
title="Follow placement"
fallback="Beside"
>
{children ?? "Sits beside the trigger with a small offset and drifts into place."}
</HoverCardShell>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Top
A card that opens above the trigger.
import { TopHoverCard } from "@/components/ui/hover-card-placement"
<TopHoverCard />Bottom
A card that opens below the trigger.
import { BottomHoverCard } from "@/components/ui/hover-card-placement"
<BottomHoverCard />Left
A card that opens to the left of the trigger.
import { LeftHoverCard } from "@/components/ui/hover-card-placement"
<LeftHoverCard />Right
A card that opens to the right of the trigger.
import { RightHoverCard } from "@/components/ui/hover-card-placement"
<RightHoverCard />Follow
A card that sits beside the trigger with a small offset.
import { FollowHoverCard } from "@/components/ui/hover-card-placement"
<FollowHoverCard />ai2 Placement hover cards: 5 styled variations on the token system
The ai2 Placement hover cards are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around cards that open in different directions relative to the trigger. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: each card fades in and slides a few pixels from its placement direction on framer-motion when the trigger is hovered or focused. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the directional slide is removed and the card only fades in.
What is in the ai2 Placement hover cards?
5 exports in one file: Top, Bottom, Left, Right and Follow. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: each card fades in and slides a few pixels from its placement direction on framer-motion when the trigger is hovered or focused.
- Reduced-motion aware: Under prefers-reduced-motion, the directional slide is removed and the card only fades in.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Placement hover cards sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.