Zaidan

Command Palette

Search for a command to run...

GitHub43

Hover Card

For sighted users to preview content available behind a link.

Installation

CLI

Manual

Copy and paste the following code into your project.

import * as HoverCardPrimitive from "@kobalte/core/hover-card";
import type { PolymorphicProps } from "@kobalte/core/polymorphic";
import type { ComponentProps, ValidComponent } from "solid-js";
import { mergeProps, splitProps } from "solid-js";
import { cn } from "~/lib/utils";
const HoverCard = (props: HoverCardPrimitive.HoverCardRootProps) => {
const mergedProps = mergeProps({ gutter: 4 }, props);
return <HoverCardPrimitive.Root data-slot="hover-card" {...mergedProps} />;
};
type HoverCardTriggerProps<T extends ValidComponent = "a"> = PolymorphicProps<
T,
HoverCardPrimitive.HoverCardTriggerProps<T>
>;
const HoverCardTrigger = <T extends ValidComponent = "a">(props: HoverCardTriggerProps<T>) => (
<HoverCardPrimitive.Trigger data-slot="hover-card-trigger" {...props} />
);
type HoverCardContentProps<T extends ValidComponent = "div"> = PolymorphicProps<
T,
HoverCardPrimitive.HoverCardContentProps<T>
> &
Pick<ComponentProps<T>, "class" | "children">;
const HoverCardContent = <T extends ValidComponent = "div">(props: HoverCardContentProps<T>) => {
const [local, others] = splitProps(props as HoverCardContentProps, ["class", "children"]);
return (
<HoverCardPrimitive.Portal>
<HoverCardPrimitive.Content
data-slot="hover-card-content"
class={cn(
"z-50 z-hover-card-content origin-(--kb-hovercard-content-transform-origin) outline-hidden",
local.class,
)}
{...others}
>
{local.children}
</HoverCardPrimitive.Content>
</HoverCardPrimitive.Portal>
);
};
export { HoverCard, HoverCardTrigger, HoverCardContent };

Examples

Here are the source code of all the examples from the preview page:

Sides

import { For } from "solid-js";
import { Button } from "~/components/ui/button";
import {
HoverCard,
HoverCardContent,
HoverCardTrigger,
} from "~/components/ui/hover-card";
const HOVER_CARD_SIDES = ["top", "right", "bottom", "left"] as const;
function HoverCardSides() {
return (
<Example title="Sides">
<div class="flex flex-wrap items-center justify-center gap-4">
<For each={HOVER_CARD_SIDES}>
{(side) => (
<HoverCard openDelay={100} closeDelay={100} placement={side}>
<HoverCardTrigger as={Button} variant="outline" class="capitalize">
{side}
</HoverCardTrigger>
<HoverCardContent>
<div class="flex flex-col gap-1.5">
<h4 class="font-medium">Hover Card</h4>
<p>This hover card appears on the {side} side of the trigger.</p>
</div>
</HoverCardContent>
</HoverCard>
)}
</For>
</div>
</Example>
);
}

In Dialog

import { Button } from "~/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "~/components/ui/dialog";
import {
HoverCard,
HoverCardContent,
HoverCardTrigger,
} from "~/components/ui/hover-card";
function HoverCardInDialog() {
return (
<Example title="In Dialog">
<Dialog>
<DialogTrigger as={Button} variant="outline">
Open Dialog
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Hover Card Example</DialogTitle>
<DialogDescription>
Hover over the button below to see the hover card.
</DialogDescription>
</DialogHeader>
<HoverCard openDelay={100} closeDelay={100}>
<HoverCardTrigger as={Button} variant="outline" class="w-fit">
Hover me
</HoverCardTrigger>
<HoverCardContent>
<div class="flex flex-col gap-1.5">
<h4 class="font-medium">Hover Card</h4>
<p>This hover card appears inside a dialog. Hover over the button to see it.</p>
</div>
</HoverCardContent>
</HoverCard>
</DialogContent>
</Dialog>
</Example>
);
}