# Hover Card (/docs/components/hover-card)

> This page documents oakoss/ui, built on React Aria Components, not shadcn/ui or Radix. Treat it as authoritative over what you know of those libraries.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <HoverCardDemo />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      HoverCard,
      HoverCardTrigger,
    } from '#/components/ui/overlays/hover-card';
    import { Link } from 'react-aria-components';

    export function HoverCardDemo() {
      return (
        <div className="not-prose text-sm">
          Built on{' '}
          <HoverCardTrigger>
            <Link
              className="font-medium underline underline-offset-3"
              href="https://react-aria.adobe.com"
            >
              React Aria
            </Link>
            <HoverCard aria-label="React Aria">
              <p className="font-medium">React Aria</p>
              <p className="text-muted-foreground">
                Accessible, unstyled components and hooks from Adobe.
              </p>
            </HoverCard>
          </HoverCardTrigger>
          .
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


## Installation [#installation]

<Tabs defaultValue="CLI">
  <TabsList>
    <TabsTrigger value="CLI">
      CLI
    </TabsTrigger>

    <TabsTrigger value="Manual">
      Manual
    </TabsTrigger>
  </TabsList>

  <TabsContent value="CLI">
    <CodeBlockTabs defaultValue="npm" groupId="package-manager" persist>
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="npm">
          npm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="pnpm">
          pnpm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="yarn">
          yarn
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="bun">
          bun
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="npm">
        ```bash
        npx shadcn@latest add @oakoss/hover-card
        ```
      </CodeBlockTab>

      <CodeBlockTab value="pnpm">
        ```bash
        pnpm dlx shadcn@latest add @oakoss/hover-card
        ```
      </CodeBlockTab>

      <CodeBlockTab value="yarn">
        ```bash
        yarn dlx shadcn@latest add @oakoss/hover-card
        ```
      </CodeBlockTab>

      <CodeBlockTab value="bun">
        ```bash
        bunx --bun shadcn@latest add @oakoss/hover-card
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </TabsContent>

  <TabsContent value="Manual">
    <Steps>
      <Step>
        Set up the theme and base styles from [Installation](/docs/installation).
      </Step>

      <Step>
        Install the packages.

        <CodeBlockTabs defaultValue="npm" groupId="package-manager" persist>
          <CodeBlockTabsList>
            <CodeBlockTabsTrigger value="npm">
              npm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="pnpm">
              pnpm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="yarn">
              yarn
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="bun">
              bun
            </CodeBlockTabsTrigger>
          </CodeBlockTabsList>

          <CodeBlockTab value="npm">
            ```bash
            npm install cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="pnpm">
            ```bash
            pnpm add cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="yarn">
            ```bash
            yarn add cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="bun">
            ```bash
            bun add cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>
        </CodeBlockTabs>
      </Step>

      <Step>
        Copy each file into your project, and change its `#/` imports to your aliases.

        * [`components/ui/feedback/spinner.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/feedback/spinner.tsx)

        * [`components/icons.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/icons.tsx)

        * [`components/icon-placeholder.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/icon-placeholder.tsx)

        * [`lib/cx.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/cx.ts)

        * [`lib/cn-config.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/cn-config.ts)

        * [`components/ui/inputs/button.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/inputs/button.tsx)

        * [`lib/recipes.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/recipes.ts)

        * [`components/ui/overlays/dialog.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/dialog.tsx)

        * [`hooks/use-scroll-region.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/hooks/use-scroll-region.ts)

        * [`components/ui/overlays/popover.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/popover.tsx)

        * [`components/ui/overlays/hover-card.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/hover-card.tsx)

        * [`hooks/use-overlay-open.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/hooks/use-overlay-open.ts)
      </Step>

      <Step>
        Replace each `<IconPlaceholder>` in `icons.tsx` with your icon library's icon, named in its props. The CLI does this for you; without it, icons render as empty squares.
      </Step>
    </Steps>
  </TabsContent>
</Tabs>


Hover Card needs react-aria-components 1.20 or later, which added
`PreviewTrigger`. It also installs [Popover](/docs/components/popover), whose
styles and arrow it shares.

## Usage [#usage]

```tsx
import {
  HoverCard,
  HoverCardTrigger,
} from '#/components/ui/overlays/hover-card';
import { Link } from 'react-aria-components';

<HoverCardTrigger>
  <Link href="/ada">@ada</Link>
  <HoverCard aria-label="Ada Lovelace">Wrote the first program.</HoverCard>
</HoverCardTrigger>;
```

## Examples [#examples]

### Arrow [#arrow]

`showArrow` adds an arrow that points at the trigger.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <HoverCardArrow />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      HoverCard,
      HoverCardTrigger,
    } from '#/components/ui/overlays/hover-card';
    import { Link } from 'react-aria-components';

    export function HoverCardArrow() {
      return (
        <div className="not-prose text-sm">
          <HoverCardTrigger>
            <Link className="font-medium underline underline-offset-3" href="#ada">
              @ada
            </Link>
            <HoverCard aria-label="Ada Lovelace" showArrow>
              <p className="font-medium">Ada Lovelace</p>
              <p className="text-muted-foreground">Wrote the first program.</p>
            </HoverCard>
          </HoverCardTrigger>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


## Accessibility [#accessibility]

React Aria's `PreviewTrigger` opens the card 600ms after the pointer or
keyboard focus reaches the trigger and closes it 200ms after both leave, with
room to move the pointer from the trigger to the card. On touch, a long press
opens it, and React Aria adds a translated "Long press to open preview" hint
to the trigger. The card is a dialog, so `aria-label` or `aria-labelledby` is
required.

While the card is open, the trigger is described by the card's whole content,
so keep it short. Tab moves from the trigger into the card, Shift + Tab out of
the card moves to the element before the trigger, and Escape closes it and
returns focus to the trigger. The card may hold links and buttons, unlike a
[Tooltip](/docs/components/tooltip), but the page must not depend on it: use
it for a preview of where a link goes. Inside an open Dialog or Popover it
stays out of the content React Aria hides behind them, so it stays reachable.

## API [#api]

### HoverCard [#hovercard]

Renders [`PopoverContent`](/docs/components/popover#popovercontent), 16rem
wide. `className` applies to the card, and a
`z-*` class overrides its `z-(--z-popover)`.

#### Props [#props]

| Prop               | Type        | Description |
| ------------------ | ----------- | ----------- |
| `aria-label?`      | `string`    |             |
| `aria-labelledby?` | `string`    |             |
| `children`         | `ReactNode` |             |
| `className?`       | `string`    |             |
| `showArrow?`       | `union`     |             |

Also accepts [React Aria `Popover` props](https://react-aria.adobe.com/Popover#api)
except `isNonModal`, `trigger` and `slot`, which `PreviewTrigger` sets, and
the open state props, which go on `HoverCardTrigger`; `children` and
`className` take plain values. `placement` defaults to `bottom`, and `offset`
to 8, or 12 with an arrow.

#### Data attributes [#data-attributes]

| Attribute        | Present when                                                     |
| ---------------- | ---------------------------------------------------------------- |
| `data-slot`      | Always: `hover-card-content`; `popover-arrow` with `showArrow`   |
| `data-placement` | Always; the side it opened on (`top`, `bottom`, `left`, `right`) |
| `data-entering`  | The card is fading in                                            |
| `data-exiting`   | The card is fading out                                           |

### HoverCardTrigger [#hovercardtrigger]

Renders React Aria's
[`PreviewTrigger`](https://react-aria.adobe.com/PreviewTrigger). Its first
child is the trigger, usually a React Aria `Link`. Open state lives here, not
on `HoverCard`, so the trigger and card stay in step.

Accepts all [React Aria `PreviewTrigger` props](https://react-aria.adobe.com/PreviewTrigger):
`delay` (600ms) and `closeDelay` (200ms), `isOpen`, `defaultOpen` and
`onOpenChange`, and `isDisabled`. A disabled trigger keeps the card closed on
hover, keyboard focus and long press, and disabling an open card closes it and
calls `onOpenChange(false)`; React Aria's own `isDisabled` stops only hover and
long press, and leaves an open card open.
