# Popover

## Usage

`Popover` opens a surface next to a trigger button. The surface holds short, interactive content such as a form, a short message, or an action list. The trigger is the default slot and the surface is the `content` slot.

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

<template>
  <Popover label="Project details">
    <Button label="Details" variant="secondary" />
    <template #content>
      <p>Short content goes here.</p>
    </template>
  </Popover>
</template>
```

### Anatomy

- **Trigger element**: Caller-supplied or externally referenced control that anchors and toggles the popover.
- **Popover surface**: Painted surface owned and themed by Popover through the canonical popover target.
- **Popover content**: Caller-supplied content rendered inside the surface.
- **Fallback close control**: Keyboard-reachable close affordance appended by usePopover when enabled.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Keep popover content focused on a single task or piece of information. |
| Do | Provide a clear way to close: either by clicking outside or with an explicit close button. |
| Don't | Nest popovers inside other popovers; it creates confusing focus and navigation. |
| Don't | Use a popover for content that requires heavy user input; use a Dialog instead. |
| Don't | Put too much content in a popover; if it needs scrolling, use a Dialog instead. |

## Examples

### Bottom sheet alternative

On a compact touch surface, project actions open in a bottom sheet instead of an anchored popover.

```vue
<script setup lang="ts">
import type { Component } from 'vue'
import { BottomSheet } from '@astryx-vue/core/BottomSheet'
import { Button } from '@astryx-vue/core/Button'
import { Heading } from '@astryx-vue/core/Heading'
import { Icon } from '@astryx-vue/core/Icon'
import { List, ListItem } from '@astryx-vue/core/List'
import { Section } from '@astryx-vue/core/Section'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { h, ref } from 'vue'

function svgIcon(paths: string[]): Component {
  return () => h('svg', {
    'viewBox': '0 0 24 24',
    'fill': 'none',
    'stroke': 'currentColor',
    'stroke-width': 1.5,
    'stroke-linecap': 'round',
    'stroke-linejoin': 'round',
  }, paths.map(d => h('path', { d })))
}

const UserIcon = svgIcon(['M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2', 'M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8'])
const TagIcon = svgIcon(['M20.6 13.4 12 22l-8.6-8.6a2 2 0 0 1-.6-1.4V4a2 2 0 0 1 2-2h8a2 2 0 0 1 1.4.6L20.6 8a2 2 0 0 1 0 2.8Z', 'M7.5 7.5h.01'])
const CalendarIcon = svgIcon(['M8 2v4', 'M16 2v4', 'M3 10h18', 'M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z'])

const actions = [
  { icon: UserIcon, label: 'Assign owner', description: 'Route follow-up to a teammate.' },
  { icon: TagIcon, label: 'Add label', description: 'Group this item with related work.' },
  { icon: CalendarIcon, label: 'Set due date', description: 'Pick a reminder for review.' },
]

const isOpen = ref(false)
</script>

<template>
  <Button label="Open project actions" @click="isOpen = true" />
  <BottomSheet v-model:is-open="isOpen" label="Project actions" height="hug">
    <Section :padding-block="4" :padding-inline="1">
      <VStack :gap="3">
        <VStack :gap="1" style="margin-inline-start: var(--astryx-spacing-3, 12px)">
          <Heading :level="3">
            Project actions
          </Heading>
          <Text type="supporting" color="secondary">
            Use this modal touch surface when the task should move away from its trigger and stay close to the bottom edge.
          </Text>
        </VStack>
        <List density="spacious">
          <ListItem
            v-for="action in actions"
            :key="action.label"
            :label="action.label"
            :description="action.description"
            :on-click="() => { isOpen = false }"
          >
            <template #startContent>
              <Icon :icon="action.icon" size="md" color="secondary" />
            </template>
          </ListItem>
        </List>
      </VStack>
    </Section>
  </BottomSheet>
</template>
```

### Confirm action

A destructive button opens a short confirmation with Delete and Cancel.

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

const isOpen = ref(false)
</script>

<template>
  <Popover
    :is-open="isOpen"
    placement="below"
    label="Confirm deletion"
    :width="300"
    @open-change="isOpen = $event"
  >
    <Button label="Delete project" variant="destructive" />
    <template #content>
      <VStack :gap="3">
        <Heading :level="4">
          Delete project?
        </Heading>
        <Text type="body">
          This will permanently delete the project and all its data. This action cannot be undone.
        </Text>
        <HStack h-align="end" :gap="2">
          <Button label="Delete" variant="destructive" @click="isOpen = false" />
          <Button label="Cancel" variant="ghost" @click="isOpen = false" />
        </HStack>
      </VStack>
    </template>
  </Popover>
</template>
```

### Filter panel

Status checkboxes with Apply and Reset.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { CheckboxInput } from '@astryx-vue/core/CheckboxInput'
import { Divider } from '@astryx-vue/core/Divider'
import { Heading } from '@astryx-vue/core/Heading'
import { Popover } from '@astryx-vue/core/Popover'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { reactive, ref } from 'vue'

const isOpen = ref(false)
const filters = reactive({
  active: true,
  archived: false,
  drafts: true,
  shared: false,
})

function reset() {
  filters.active = true
  filters.archived = false
  filters.drafts = true
  filters.shared = false
}
</script>

<template>
  <Popover
    :is-open="isOpen"
    placement="below"
    label="Filter"
    :width="240"
    @open-change="isOpen = $event"
  >
    <Button label="Filter" />
    <template #content>
      <VStack :gap="3">
        <Heading :level="4">
          Filter by status
        </Heading>
        <Divider />
        <CheckboxInput v-model="filters.active" label="Active" />
        <CheckboxInput v-model="filters.archived" label="Archived" />
        <CheckboxInput v-model="filters.drafts" label="Drafts" />
        <CheckboxInput v-model="filters.shared" label="Shared with me" />
        <Divider />
        <HStack h-align="end" :gap="2">
          <Button label="Apply" variant="primary" @click="isOpen = false" />
          <Button label="Reset" variant="ghost" @click="reset" />
        </HStack>
      </VStack>
    </template>
  </Popover>
</template>
```

### Keyboard shortcuts

A list of shortcut keys and what they do.

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

const shortcuts = [
  { key: '⌘K', action: 'Command palette' },
  { key: '⌘/', action: 'Toggle sidebar' },
  { key: '⌘.', action: 'Quick actions' },
]
</script>

<template>
  <Popover placement="below" label="Keyboard shortcuts" :width="260">
    <Button label="Shortcuts" />
    <template #content>
      <VStack :gap="2">
        <Heading :level="4">
          Keyboard shortcuts
        </Heading>
        <Divider />
        <HStack v-for="shortcut in shortcuts" :key="shortcut.key" :gap="3">
          <Text type="body" weight="bold">
            {{ shortcut.key }}
          </Text>
          <Text type="body">
            {{ shortcut.action }}
          </Text>
        </HStack>
      </VStack>
    </template>
  </Popover>
</template>
```

### Settings panel

Switches for notifications, dark mode, and sounds.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Divider } from '@astryx-vue/core/Divider'
import { Heading } from '@astryx-vue/core/Heading'
import { Popover } from '@astryx-vue/core/Popover'
import { VStack } from '@astryx-vue/core/Stack'
import { Switch } from '@astryx-vue/core/Switch'
import { ref } from 'vue'

const notifications = ref(true)
const darkMode = ref(false)
const sounds = ref(true)
</script>

<template>
  <Popover placement="below" label="Settings" :width="280">
    <Button label="Settings" />
    <template #content>
      <VStack :gap="3">
        <Heading :level="4">
          Settings
        </Heading>
        <Divider />
        <Switch v-model="notifications" label="Notifications" description="Receive push notifications" />
        <Switch v-model="darkMode" label="Dark mode" description="Use dark color theme" />
        <Switch v-model="sounds" label="Sounds" description="Play sounds for actions" />
      </VStack>
    </template>
  </Popover>
</template>
```

## Accessibility

A popover has to communicate its presence, its current open state, and its content to people who use a screen reader or a keyboard. The component links the trigger to the surface automatically and manages focus as the surface opens and closes, so you only need to supply the accessible name.

- The trigger receives `aria-haspopup`, `aria-expanded`, and `aria-controls`, so assistive technology knows it is a button that opens a surface and can announce whether that surface is currently expanded.
- A `dialog` surface has `role="dialog"`; when `isModal` is set it also has `aria-modal="true"`, and the `label` serves as its accessible name.
- Focus moves to the first focusable element inside the surface on open, and returns to the trigger when the user presses Escape or closes the surface.
- With `isModal` set, Tab focus stays inside the surface while it is open.
- The built-in close button is visually hidden until it receives focus, so it appears once the keyboard reaches it.

## Layout

The surface is positioned with JavaScript. It recalculates its position when the page scrolls or resizes, and it flips to the other side when the chosen side has no room.

## Popover Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `placement` | `'above' \| 'below' \| 'start' \| 'end'` | `'below'` | Side of the trigger. Flips when there is no room. |
| `alignment` | `'start' \| 'center' \| 'end'` | `'start'` | Alignment along the placement axis. |
| `isOpen` | `boolean` | — | Controlled visibility. When unset, the trigger manages visibility. |
| `isEnabled` | `boolean` | `true` | Prevents the trigger from opening the surface when false. |
| `width` | `number \| string` | Trigger width | Surface width. Numbers are pixels. When unset, the surface is at least as wide as the trigger and at most 320px (and never wider than the viewport). |
| `padding` | `0 \| 0.5 \| 1 \| 1.5 \| 2 \| 3 \| 4 \| 5 \| 6 \| 8 \| 10` | `3` | Inner padding, mapped to the `--astryx-spacing-*` tokens. |
| `label` | `string` | — | Accessible name of the dialog. Provide it when `role` is `dialog`. |
| `role` | `'dialog' \| 'none'` | `'dialog'` | Use `dialog` for interactive content. Use `none` when the content already has its own role. |
| `isModal` | `boolean` | `true` | Keeps Tab focus inside the surface while it is open. Applies to `dialog` only. |
| `hasCloseButton` | `boolean` | `true` | Shows a close button that is visible only when focused. |
| `closeButtonLabel` | `string` | `'Close'` | Text and accessible name of the close button. |
| `hasAutoFocus` | `boolean` | `true` | Moves focus into the surface when it opens. |
| `hasLightDismiss` | `boolean` | `true` | Closes when the user presses outside the surface and the trigger. |
| `hasEscapeDismiss` | `boolean` | `true` | Closes when the user presses Escape. |
| `anchor` | `HTMLElement \| null` | — | External element to anchor to. No trigger wrapper is rendered and the default slot can be omitted; click, keydown and ARIA wiring land on the button inside that element. |

## Events

- `openChange(isOpen: boolean)`: fires when the surface opens or closes, including when the user dismisses it.

## Slots

- `default`: the trigger. Use a `button` or an element with `role="button"`. Not needed when `anchor` is set.
- `content`: the surface content.

## Theme

```css
.my-popover {
  --astryx-popover-background: #fff;
  --astryx-popover-foreground: #000;
}
```

- `--astryx-popover-background`: surface background color.
- `--astryx-popover-foreground`: surface text color.
- `--astryx-popover-shadow`: surface shadow.
- `--astryx-popover-z-index`: stacking order of the surface.
