# Hover Card

## Usage

`HoverCard` opens a card of richer content when the pointer hovers the trigger or keyboard focus lands on it. It differs from `Tooltip` in what it carries: a tooltip holds a line of plain text, while a hover card can hold an avatar, a name, a short bio, and action buttons. Use it for user profiles, link previews, and inline definitions.

Showing waits 300 milliseconds by default, so a pointer passing across the trigger does not flash the card. Hiding waits 200 milliseconds and is cancelled when the pointer enters the card, so the user can travel from the trigger into the card and keep reading or clicking without it vanishing on the way.

The trigger goes in the default slot and the card content goes in the `content` slot:

```vue
<script setup lang="ts">
import { HoverCard } from '@astryx-vue/core/HoverCard'
</script>

<template>
  <HoverCard label="Dana Whitfield">
    Dana Whitfield
    <template #content>
      <p>Design systems engineer, Amsterdam.</p>
    </template>
  </HoverCard>
</template>
```

When the trigger is plain text the component gives it a dashed underline and makes it focusable, so a keyboard user can open the card with Tab. When the trigger is an element such as a button or link, the component listens on that element and writes `aria-haspopup` and `aria-controls` onto it. Passing `label` exposes the card to assistive technology as a named `role="dialog"`; without it the card is a `role="group"` linked to the trigger by `aria-describedby`.

### Anatomy

- **Trigger**: The element that opens the hover card on hover or focus: a button, link, or inline text.
- **Card**: The floating overlay with the preview content, anchored to the trigger.
- **Body**: The main content area: profile info, link summary, or any rich content.
- **Actions**: Optional buttons inside the card for follow-up actions like Follow or Message.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Keep the card supplementary: understanding improves with it, but the task is still completable without it. |
| Do | Use plain text as the trigger so the dashed underline and keyboard focus work by default; users then see that it is hoverable. |
| Do | Pass `label` for content such as a profile, so the card enters the assistive-technology reading order as a named dialog. |
| Do | Leave `touchTrigger` on `auto`: a trigger that does nothing on its own opens on a tap, while a button or link keeps its tap. |
| Don't | Put critical actions or required information only inside the card; it disappears as soon as the pointer leaves. |
| Don't | Use a hover card where a tooltip or popover would do. A one-line hint belongs in `Tooltip`, and content the user must confirm belongs in `Popover`. |
| Don't | Put a long form inside the card. The pointer leaving closes it; that content belongs in a dialog. |
| Don't | Place it directly inside a `<p>`, a heading, or a link. Those placements work, but the layer's relationship to the document flow becomes hard to predict, so prefer a block context. |

## Examples

### Definition

Hover a term in a sentence to read a short definition.

```vue
<script setup lang="ts">
import { HoverCard } from '@astryx-vue/core/HoverCard'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Text type="body">
    The component uses a
    <HoverCard placement="above">
      focus trap
      <template #content>
        <VStack :gap="1" style="max-width: 200px">
          <Text type="label">
            Focus trap
          </Text>
          <Text type="body" color="secondary">
            A pattern that keeps keyboard focus inside a container, preventing it from moving to elements outside. Used in dialogs and modals to ensure accessibility.
          </Text>
        </VStack>
      </template>
    </HoverCard>
    to keep keyboard navigation inside the
    <HoverCard placement="above">
      modal dialog
      <template #content>
        <VStack :gap="1" style="max-width: 200px">
          <Text type="label">
            Modal dialog
          </Text>
          <Text type="body" color="secondary">
            An overlay that blocks interaction with the rest of the page until the user responds. Uses the native HTML dialog element for built-in accessibility and backdrop support.
          </Text>
        </VStack>
      </template>
    </HoverCard>.
  </Text>
</template>
```

### Link preview

Hover a documentation link to see its title, summary, and URL.

```vue
<script setup lang="ts">
import { HoverCard } from '@astryx-vue/core/HoverCard'
import { Icon } from '@astryx-vue/core/Icon'
import { Link } from '@astryx-vue/core/Link'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { h } from 'vue'

function LinkIcon() {
  return h('svg', {
    'viewBox': '0 0 24 24',
    'fill': 'none',
    'stroke': 'currentColor',
    'stroke-width': 1.5,
    'stroke-linecap': 'round',
    'stroke-linejoin': 'round',
  }, [
    h('path', { d: 'M10 13a5 5 0 0 0 7.07 0l2.12-2.12a5 5 0 0 0-7.07-7.07L10.5 5.43' }),
    h('path', { d: 'M14 11a5 5 0 0 0-7.07 0L4.8 13.12a5 5 0 0 0 7.07 7.07L13.5 18.57' }),
  ])
}
</script>

<template>
  <Text type="body">
    Read more in the
    <HoverCard placement="below">
      <Link href="#" has-underline>
        Getting Started Guide
      </Link>
      <template #content>
        <VStack :gap="2" style="max-width: 280px">
          <HStack align="start" :gap="2">
            <Icon :icon="LinkIcon" size="sm" color="secondary" />
            <VStack :gap="1">
              <Text type="label">
                Getting Started Guide
              </Text>
              <Text type="body" color="secondary">
                Learn how to set up your first project, invite team members, and configure your workspace.
              </Text>
              <Text type="supporting" color="secondary">
                docs.example.com/getting-started
              </Text>
            </VStack>
          </HStack>
        </VStack>
      </template>
    </HoverCard>.
  </Text>
</template>
```

### Profile preview

Hover a username to see an avatar, bio, and join date.

```vue
<script setup lang="ts">
import { Avatar } from '@astryx-vue/core/Avatar'
import { Button } from '@astryx-vue/core/Button'
import { Heading } from '@astryx-vue/core/Heading'
import { HoverCard } from '@astryx-vue/core/HoverCard'
import { Icon } from '@astryx-vue/core/Icon'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <HoverCard placement="below">
    <Button label="@janedoe" variant="ghost" />
    <template #content>
      <HStack align="start" :gap="3" style="max-width: 280px">
        <Avatar name="Jane Doe" :size="48" style="flex-shrink: 0" />
        <VStack :gap="1">
          <Heading :level="3">
            @janedoe
          </Heading>
          <Text type="body" color="secondary">
            Crafting accessible, scalable design systems for modern teams.
          </Text>
          <HStack align="center" :gap="1">
            <Icon icon="calendar" size="xsm" color="secondary" />
            <Text type="supporting" color="secondary">
              March 2024
            </Text>
          </HStack>
        </VStack>
      </HStack>
    </template>
  </HoverCard>
</template>
```

## Accessibility

The hover card is a separate layer, and the component relates it to the trigger with ARIA attributes so assistive technology can both learn that the trigger carries extra context and read the structure and controls inside the open card:

- The card has `role="group"`; with `label` it becomes a named `role="dialog"`, because an unnamed dialog is invalid while an unnamed group is not.
- Without `label`, the trigger carries `aria-describedby` pointing at the card.
- With `label`, the trigger carries `aria-haspopup="dialog"` instead, plus `aria-controls` while the card is open. `aria-expanded` is only written when the trigger's own role allows it; a plain-text `<span>` trigger has no such role, so it never gets the attribute.
- Attributes the trigger already carried — `aria-haspopup`, `aria-controls`, `aria-expanded`, `aria-describedby` — are preserved and restored when the component unmounts.

The keyboard path is complete: focusing the trigger opens the card immediately, and moving focus between the trigger and the card keeps it open. Pressing Escape while the card is showing closes it; if focus was inside the card it returns to the trigger, and that refocus does not reopen the card. In the controlled mode Escape only emits `openChange(false)` and the parent decides whether to close.

A press inside the open card acts only within the card: a clickable card, list row, or tree row around the trigger does not activate, while links and buttons inside the card keep working. Page-level listeners still receive the press.

A touch screen has no hover, so `touchTrigger` decides what a tap does. The default `auto` opens the card on a tap of a trigger that performs no action of its own, and leaves the tap to buttons, links, and form controls; once open, a tap outside the card dismisses it. Because the content appears only briefly, keep information a user must see to complete a task out of the card.

## HoverCard Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `content` | `string` | `''` | Plain-text card content. Use the `content` slot for rich content. |
| `placement` | `'above' \| 'below' \| 'start' \| 'end'` | `'above'` | Side of the trigger. The card flips to the opposite side when there is no room. |
| `alignment` | `'start' \| 'center' \| 'end'` | `'center'` | Alignment along the placement axis. |
| `delay` | `number` | `300` | Milliseconds before showing on hover. |
| `hideDelay` | `number` | `200` | Milliseconds before hiding. The pointer entering the card within this window cancels the hide. |
| `focusTrigger` | `'auto' \| 'always' \| 'never'` | `'auto'` | Whether focus opens the card. auto listens only when the trigger is itself focusable. |
| `touchTrigger` | `'auto' \| 'tap' \| 'none'` | `'auto'` | Tap behavior on touch. auto opens on a trigger that performs no action of its own and leaves the tap to buttons and links; tap always opens; none never opens. A tap-opened card closes on a tap outside. |
| `isEnabled` | `boolean` | `true` | Enables the hover and focus triggers. Setting it to false also closes an open card. |
| `label` | `string` | — | Accessible name for the card. With it the card is a named `role="dialog"`; without it, a `role="group"`. |
| `hasHoverIndication` | `'auto' \| boolean` | `'auto'` | Dashed underline on the trigger. auto applies it only to a text-only trigger. |
| `isOpen` | `boolean` | — | Controlled visibility. Hover and focus are ignored when set. |
| `isDefaultOpen` | `boolean` | `false` | Show the card on mount. Only applies when `isOpen` is unset. |

## Events

- `openChange(isOpen: boolean)`: fires when the card shows or hides. In the controlled mode, Escape emits `false` and the parent decides whether to close.

## Slots

| Name | Description |
| --- | --- |
| `default` | The trigger. Plain text gains a dashed underline and becomes focusable; an element becomes the trigger that receives the events and ARIA attributes. |
| `content` | The card content, rich markup included. Replaces the `content` prop. |

## Methods

- `show()`: shows the card immediately, skipping the hover delay.
- `hide()`: hides the card immediately.
- `element`: the card layer element, or `null` before it mounts.

## Native attributes

The component renders a fragment: a trigger wrapper `<span class="astryx-hover-card__trigger">` plus a layer teleported to `body`. Attributes land on the wrapper; the layer receives none.

- HTML attributes, `aria-*`, `data-*`, `class`, and `style` all go to the wrapper.
- The wrapper is a `display: contents` box when the trigger is an element, and the trigger itself, `display: inline`, when the trigger is plain text. Put layout attributes on the trigger element directly; on a text trigger you can put them on the component.
- The `aria-haspopup`, `aria-controls`, `aria-expanded`, and `aria-describedby` the component manages are written onto the real trigger element and restored to their previous values when it unmounts.

## Theme

```css
.my-hover-card {
  --astryx-hover-card-background: #fff;
  --astryx-hover-card-foreground: #171717;
}
```

- `--astryx-hover-card-background`: card background.
- `--astryx-hover-card-foreground`: card text color.
- `--astryx-hover-card-shadow`: card elevation.
- `--astryx-hover-card-radius`: card radius, defaulting to `--astryx-radius-container`.
- `--astryx-hover-card-indication-color`: dashed underline color.
- `--astryx-hover-card-z-index`: stacking order of the card.
