# Overlay

> A fullscreen backdrop overlay for creating modal backgrounds, loading screens, and lightbox effects. Renders via portal with click-to-close and optional blur.

Source: https://vaneui.com/docs/overlay-components/overlay

Import: `import { Overlay, Button, Card, Img, Modal, Row, Text } from "@vaneui/ui"`

Use Overlay for a dimmed or transparent backdrop that needs no dialog role. It renders a plain `<div>` with the layout defaults `fixed`, `flex`, `itemsCenter` and `justifyCenter`. For a dialog, use [Modal](/docs/overlay-components/modal), which provides its own backdrop with the same layout defaults; for a panel attached to a trigger, use a [Popup](/docs/overlay-components/popup).

## Basic usage

A fullscreen semi-transparent backdrop. Click the overlay background to dismiss.


```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Overlay</Button>
    <Overlay open={open} onClose={() => setOpen(false)}>
      <Text>Click the background to close</Text>
    </Overlay>
  </>
);
```

## Overlay with content

Place content inside the overlay. Click handlers on the overlay only fire when the event target is the overlay itself, so children receive their own clicks without dismissing the overlay.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Overlay</Button>
    <Overlay open={open} onClose={() => setOpen(false)}>
      <Card>
        <Text>Overlay content here</Text>
        <Button onClick={() => setOpen(false)}>Close</Button>
      </Card>
    </Overlay>
  </>
);
```

## Blur effect

Add `backdropBlur` for a backdrop-filter blur behind the overlay. Blur intensity follows the `--overlay-blur` CSS variable.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Blur Overlay</Button>
    <Overlay open={open} onClose={() => setOpen(false)} backdropBlur>
      <Card>
        <Text>Blurred background</Text>
      </Card>
    </Overlay>
  </>
);
```

## Custom backdrop color

The backdrop paints the `--overlay-bg` scrim by default. Pass a `className` with a background utility to override it, or `transparent` to drop the scrim entirely.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Tinted Overlay</Button>
    <Overlay open={open} onClose={() => setOpen(false)} className="bg-red-500/40">
      <Card>
        <Text>Tinted overlay</Text>
      </Card>
    </Overlay>
  </>
);
```

Use `className` for gradients or backgrounds the appearance system doesn't cover:

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Gradient Overlay</Button>
    <Overlay
      open={open}
      onClose={() => setOpen(false)}
      className="bg-gradient-to-br from-black/80 to-transparent"
    >
      <Card>
        <Text>Custom gradient backdrop</Text>
      </Card>
    </Overlay>
  </>
);
```

## Non-dismissible Overlay

Use `pointerEventsNone` for loading states where the overlay should pass clicks through and never close on its own. Useful for full-screen spinners that sit above the page.

```tsx demo
const [loading, setLoading] = useState(false);

return (
  <>
    <Button onClick={() => setLoading(!loading)}>Toggle Loading</Button>
    <Overlay open={loading} pointerEventsNone>
      <Text>Loading...</Text>
    </Overlay>
  </>
);
```

## Transparent Overlay

Use `transparent` to create an invisible backdrop that still captures clicks. Useful for click-away dismissal without visually dimming the page.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Transparent Overlay</Button>
    <Overlay open={open} onClose={() => setOpen(false)} transparent>
      <Card>
        <Text>Content on a transparent overlay</Text>
        <Button onClick={() => setOpen(false)}>Close</Button>
      </Card>
    </Overlay>
  </>
);
```

## Foundation for Modal

`Modal` layers a dialog surface over its own portal-rendered backdrop, which handles portaling, backdrop click, and stacking (the same mechanics as `Overlay`). For accessible dialogs with focus trap, scroll lock, and ARIA wiring, prefer `Modal`. Reach for `Overlay` directly when you need a custom backdrop layer (lightbox, loading shade, drawer scrim) without dialog semantics.

```tsx
{/* High-level: use Modal for dialogs */}
<Modal open={open} onClose={() => setOpen(false)} aria-label="Details">
  <Text>Accessible dialog with focus trap</Text>
</Modal>

{/* Low-level: use Overlay for custom backdrops */}
<Overlay open={open} onClose={() => setOpen(false)} backdropBlur>
  <Img src="/photo.jpg" className="max-h-[90vh]" />
</Overlay>
```

## Portal target

Overlay renders into `document.body` by default via React portal. Set `portal={false}` to render in-place, useful when the overlay must be scoped to a positioned ancestor (e.g., scrim over a single card instead of the whole viewport).

```tsx demo
const [open, setOpen] = useState(false);

return (
  <Card relative className="h-64">
    <Text>Parent card</Text>
    <Button onClick={() => setOpen(true)}>Show scoped overlay</Button>
    <Overlay open={open} onClose={() => setOpen(false)} portal={false} absolute>
      <Text>Scoped to this card</Text>
    </Overlay>
  </Card>
);
```

When `portal={false}` and `absolute` is set, the overlay anchors to the nearest `relative`/`absolute` ancestor instead of the viewport.

## Z-index / stacking

Overlay reads its `z-index` from a managed stacking context (`--z-index`), so multiple overlays stack in mount order automatically. Override per instance with an inline `style={{ zIndex: 9999 }}` when you need to layer above a specific element (toasts, page chrome). A class that sets `--z-index` loses to the managed inline value.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Overlay</Button>
    <Overlay
      open={open}
      onClose={() => setOpen(false)}
      style={{ zIndex: 9999 }}
    >
      <Card>
        <Text>Above everything</Text>
      </Card>
    </Overlay>
  </>
);
```

## Custom animation duration

Control how the overlay transitions in and out. Use `transitionDuration` to set a custom duration in milliseconds (default `200`), or `noAnimation` to disable transitions entirely for an instant open/close.

```tsx demo
const [openSlow, setOpenSlow] = useState(false);
const [openInstant, setOpenInstant] = useState(false);

return (
  <>
    <Row>
      <Button onClick={() => setOpenSlow(true)}>Slow fade (500ms)</Button>
      <Button onClick={() => setOpenInstant(true)}>Instant</Button>
    </Row>

    {/* Slow fade - 500ms */}
    <Overlay open={openSlow} onClose={() => setOpenSlow(false)} transitionDuration={500}>
      <Card>Slow transition content</Card>
    </Overlay>

    {/* Instant - no animation */}
    <Overlay open={openInstant} onClose={() => setOpenInstant(false)} noAnimation>
      <Card>Instant content</Card>
    </Overlay>
  </>
);
```

## Lifecycle and mounting

| Prop | Default | Description |
|------|---------|-------------|
| `open` | `true` | Whether the overlay is visible |
| `onClose` | - | Called when the overlay is dismissed (backdrop click or `Escape`) |
| `portal` | `true` | Render into `document.body` via React portal |
| `keepMounted` | `false` | Keep DOM mounted when closed (useful for transition state preservation) |
| `noAnimation` | `false` | Disable enter/exit transitions |
| `transitionDuration` | `200` | Animation duration in ms |
| `onEnterComplete` | - | Called when enter transition completes |
| `onExitComplete` | - | Called when exit transition completes |

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Show Overlay</Button>
    <Overlay
      open={open}
      onClose={() => setOpen(false)}
      keepMounted
      transitionDuration={300}
      onExitComplete={() => console.log('overlay closed')}
    >
      <Card>Persistent DOM, custom timing</Card>
    </Overlay>
  </>
);
```

## Frequently asked questions

### Does Overlay trap focus or lock page scrolling?

No. Overlay closes on `Escape` and on a backdrop click when it has an `onClose`, but it does not move focus, trap `Tab`, lock the page scroll, make the page inert or set a dialog role. Keyboard users can still tab to the page behind it. For content that takes input or must be dismissed first, use [Modal](/docs/overlay-components/modal), which does all of these.

### How do I show a backdrop that blocks clicks but never closes?

Leave out `onClose`. Without it the Overlay ignores backdrop clicks and `Escape`, while still covering the page, so a click cannot reach the content behind it. `pointerEventsNone`, used under [Non-dismissible Overlay](#non-dismissible-overlay), does the opposite: clicks pass through to the page. Keyboard focus can still reach the page in both cases, since Overlay makes nothing inert.

### Why is my Overlay missing from the server-rendered HTML?

A portaled Overlay renders nothing on the server and while the page hydrates, because `document.body` does not exist on the server, and it appears right after hydration. This keeps the server and client markup the same, so React reports no hydration mismatch. An Overlay with `portal={false}` renders in place, so it is part of the server HTML.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `xs` | Size |  | Extra small size |
| `sm` | Size |  | Small size |
| `md` | Size |  | Medium size (default) |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |

Layout and utility props (Blur, Display, Height, Hide, Items, Justify, Overflow, Pointer Events, Position, Responsive Sizing, Responsive Width, Show, Transparent, Width) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
