# Overlay

## Usage

`Overlay` lays one piece of content on top of another bounded piece — a photo, a video, a card, any block that owns its own edges — with a scrim between them that darkens or lightens what is underneath so the content on top stays readable. The content is usually a single action or a single label: a quick view button over a gallery thumbnail, a play button over a video poster, a title over a cover image.

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

<template>
  <Overlay>
    <img src="/fjord.jpg" alt="Fjord at dusk">
    <template #content>
      <Button label="Quick view" size="sm" variant="secondary" />
    </template>
  </Overlay>
</template>
```

The default slot is the base content and the `content` slot is what goes on top; without a `content` slot only the base content renders and no scrim appears. The scrim is dark by default, and it does more than paint a background: everything inside it switches to the dark surface theme, so text, icons, and buttons pick up the colors that belong on a dark surface. You do not write any white text yourself. `scrim="light"` does the same for a light scrim, and `scrim={false}` drops the background entirely while keeping the overlay content and its placement.

There are three ways to decide when the overlay shows. `showOn` is pure CSS: `always`, the default, keeps it visible; `hover` shows it while the pointer is over the surface; `focus` shows it only while something inside the surface holds focus. `isOpen` is controlled from JavaScript and wins over both, bound with `v-model:isOpen`:

```vue
<script setup lang="ts">
import { ref } from 'vue'

const isOpen = ref(false)
</script>

<template>
  <Overlay v-model:is-open="isOpen">
    <img src="/fjord.jpg" alt="Fjord at dusk">
    <template #content>
      <Button label="Close" size="sm" variant="secondary" @click="isOpen = false" />
    </template>
  </Overlay>
</template>
```

In hover mode, keyboard focus inside the surface reveals the overlay as well, so a keyboard-only user still reaches the action, and it settles back once focus leaves. A touch device has no hover to follow, so that mode becomes tap-to-toggle: one tap on the base content opens the overlay, another closes it. A tap that lands on a button or a link that was already inside the surface belongs to that element and does not toggle anything.

`position` decides how much of the surface the scrim covers. `fill` covers all of it, while `bottom` and `top` are strips pinned to one edge that slide in from that edge and slide back out. `align` places the content inside the scrim: `start` at the leading edge, `end` at the trailing edge, `center` on both axes.

The container copies the border radius of the base content and clips the scrim with the same radius, so the scrim never covers a rounded corner. An `Overlay` is a covering layer that belongs to its own content, not a floating layer over the page: reach for `Popover` when the surface is anchored to a trigger, and for `Dialog` when the user must answer before continuing.

When the container already exists and only the behavior is missing, use `useOverlay` directly. It returns the attributes for the container and the `OverlayScrim` to render inside it:

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

const { containerRef, containerProps, scrimProps } = useOverlay({ showOn: 'hover' })
</script>

<template>
  <div ref="containerRef" v-bind="containerProps">
    <img src="/fjord.jpg" alt="Fjord at dusk">
    <OverlayScrim v-bind="scrimProps">
      <Button label="Quick view" size="sm" variant="secondary" />
    </OverlayScrim>
  </div>
</template>
```

The container needs both `containerProps` — which bring the positioning, the clipping, and the tap toggle — and `containerRef`, and the scrim has to render inside that container for the hover and focus selectors to find it. The returned state is reactive: pass a `ref` or a `computed` for options such as `showOn` and `isOpen` and the scrim follows them, while plain values are read once at setup.

### Anatomy

- **Base content**: The media, card, or bounded surface that the overlay sits on top of.
- **Scrim**: Optional dark or light overlay background that improves content contrast.
- **Overlay content**: Actions, labels, or supporting content rendered above the base surface.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Keep it to the actions and labels that belong to this content: quick view, play, a cover title. |
| Do | Keep the overlay content short. The more you place on the scrim, the less of the base surface reads and the harder the text is to read. |
| Do | Use `showOn="hover"` when keyboard users need the overlay too: it answers to focus as well as to the pointer. |
| Do | Pick the scrim from what is underneath, not from the page theme. A dark photo wants a dark scrim, a bright one wants a light scrim. |
| Don't | Put content the user must answer before continuing on it. That belongs in a `Dialog`. |
| Don't | Pour long text or a multi-field form onto the scrim. The room over a surface is sized for one line or one button. |
| Don't | Use it as a floating layer anchored to a trigger. `Popover`, `Tooltip`, and `Dialog` are the tools for that. |

## Examples

### Bottom strip

A bottom strip carries a badge and two lines of text, and leaves the rest of the image visible.

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

<template>
  <Overlay position="bottom" align="start">
    <AspectRatio
      :ratio="16 / 9"
      style="width: 420px; max-width: 100%; border-radius: 12px; overflow: clip"
    >
      <img
        src="/template-assets/illustrative-horizontal-1.png"
        alt="Product highlight preview"
        style="width: 100%; height: 100%; object-fit: cover"
      >
    </AspectRatio>
    <template #content>
      <VStack :gap="1">
        <Badge variant="green">
          New
        </Badge>
        <Text type="body" weight="bold" color="inherit">
          Weekly product highlights
        </Text>
        <Text type="supporting" color="inherit">
          12 updates across templates and tokens
        </Text>
      </VStack>
    </template>
  </Overlay>
</template>
```

### Hover reveal

The Quick view button stays hidden until the image is hovered or focused.

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

<template>
  <Overlay show-on="hover" align="center">
    <AspectRatio
      :ratio="16 / 9"
      style="width: 420px; max-width: 100%; border-radius: 12px; overflow: clip"
    >
      <img
        src="/template-assets/light-working-horizontal-1.png"
        alt="Workspace preview"
        style="width: 100%; height: 100%; object-fit: cover"
      >
    </AspectRatio>
    <template #content>
      <Button label="Quick view" variant="secondary" size="sm" />
    </template>
  </Overlay>
</template>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `showOn` | `OverlayShowOn` | `'always'` | What reveals the overlay while `isOpen` stays unbound, driven entirely by CSS. `always` keeps it visible, `hover` reveals it on hover or when focus enters the container, `focus` only on focus, and `hover-or-focus` is an alias of `hover`. On a touch device `hover` becomes tap-to-toggle. |
| `isOpen` | `boolean` | - | Visibility controlled from JavaScript, taking precedence over `showOn` and over the tap toggle. Bind it with `v-model:isOpen`; leaving it unbound hands visibility back to `showOn`. While it is `false` the scrim is hidden and made `inert`, so its buttons leave the tab order. |
| `scrim` | `OverlayScrimMode` | `'dark'` | The scrim background, which also decides the surface theme the content inside it uses. `dark` and `light` resolve the content against a dark and a light surface respectively; `false` draws no background and leaves the content on the ambient theme. |
| `position` | `OverlayPosition` | `'fill'` | How much of the base surface the scrim covers. `fill` covers all of it, while `bottom` and `top` are strips pinned to one edge that slide in from and out to that edge. |
| `align` | `OverlayAlign` | `'end'` | Alignment of the content inside the scrim. The scrim lays content out in a column, so this is the horizontal axis: `start` at the leading edge, `end` at the trailing edge, and `center` on both axes. |

## Slots

| Name | Description |
| --- | --- |
| `default` | The base content: media, video, artwork, or any bounded block. The scrim covers it. |
| `content` | The content drawn inside the scrim. Keep it short — one action or one label. Without this slot the component renders only the base content and draws no scrim. |

## OverlayScrim

`OverlayScrim` is the rendering component of the scrim itself, exported from `@astryx-vue/core/Overlay` for the consumers of `useOverlay`. `Overlay` renders it internally, and the props mean the same as above.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `scrim` | `OverlayScrimMode` | `'dark'` | The scrim background and the surface theme its content uses. |
| `position` | `OverlayPosition` | `'fill'` | How much of the base surface the scrim covers. |
| `align` | `OverlayAlign` | `'end'` | Alignment of the content inside the scrim. |
| `showOn` | `OverlayShowOn` | `'always'` | What reveals the overlay while `isOpen` stays unbound. |
| `isOpen` | `boolean` | - | Controlled visibility. When set, `showOn` is ignored and a `false` value marks the scrim `inert`. |

Its default slot is the overlay content. The root carries <code>data-position</code>, <code>data-align</code>, <code>data-scrim</code>, and <code>data-visibility</code> for theming, and the content is wrapped in a <code>data-astryx-media="dark | light"</code> element that switches the surface theme without affecting layout.

## useOverlay

`useOverlay` adds the same overlay behavior to a container that already exists. Its options match the props of `Overlay`, except that it accepts a `ref` or a `computed` for any of them.

| Name | Type | Description |
| --- | --- | --- |
| `containerRef` | `Ref` | Attach it to the container element with `ref="containerRef"`. |
| `containerProps` | `ComputedRef` | The attributes the container needs (scope class, tap toggle), spread with `v-bind`. They bring the positioning, the clipping, and the tap toggle on a touch device. |
| `scrimProps` | `ComputedRef` | Pass them to the `OverlayScrim` rendered inside the container. |
| `isOpen` | `ComputedRef<boolean \| undefined>` | The visibility the scrim actually uses: the bound `isOpen`, the tap toggle state, or `undefined` when CSS decides. |
| `hasTouchToggle` | `ComputedRef<boolean>` | Whether the container currently toggles by tap (a touch device in hover mode). |
| `toggle` | `() => void` | Flips the tap toggle state. Ignored while `isOpen` is bound. |

## Native attributes

The root element is a `<div>`.

The following go to the root element:

- HTML attributes.
- `aria-*` attributes.
- `data-*` attributes.
- `class`.
- `style`.

`class` is merged with <code>astryx-overlay</code>, and `style` with the border radius the component writes. A click handler you pass runs alongside the tap toggle.
