# Overlay

A layer over the whole window that holds the focus: the page behind inert, Tab inside, Escape to close, the focus given back.

Live variants: https://design.arcadia.is/components/overlay

```tsx
import { Overlay } from "@arcadia/ui";
```

```tsx
import { useEffect, useState } from "react";
import { Button, IconButton, Overlay } from "@arcadia/ui";

function Opening({ initiallyOpen = false }: { initiallyOpen?: boolean } = {}) {
  const [open, setOpen] = useState(false);
  useEffect(() => { if (initiallyOpen) setOpen(true); }, [initiallyOpen]);
  return (
    <>
      <Button variant="secondary" onClick={() => setOpen(true)}>Open the layer</Button>
      {open && (
        <Overlay label="A layer over the window" onClose={() => setOpen(false)} rootStyle={OverlayStyles.ground}>
          <div {...sx("cmp-overlay-layout", OverlayStyles.layout)}>
            <span {...sx("cmp-overlay-words", OverlayStyles.words)}>The page behind is inert</span>
            <Button variant="secondary" onClick={() => setOpen(false)}>Done</Button>
          </div>
          <span {...sx("cmp-overlay-bar", OverlayStyles.bar)}><IconButton icon="x" label="Close" size="md" onClick={() => setOpen(false)} /></span>
        </Overlay>
      )}
    </>
  );
}
```

## Accessibility

The platform's modal `dialog`, in the browser's top layer, named by `label` in the product's words. Everything else on the page is inert by the browser while it stands, so a reader stays inside it too, and the toast stack moves into it. With `onClose`, Escape closes it, unless something opened from it takes the key first; without it, Escape is left to what it holds.

- Escape: Closes it, with `onClose`
- Tab: Moves round what it holds, never out of it

## API reference

### Overlay

- `label` (`string`, required): Its name for assistive technology, in the product's words.
- `onClose` (`(event: Event) => void`): Escape closes it, when nothing opened from it takes the key first; it is handed that key's event. Without it, Escape is left to what it holds.
- `initialFocus` (`RefObject<HTMLElement | null>`): What takes the focus as it opens. Default: the layer itself, so its keys work at once.
- `finalFocus` (`() => HTMLElement | null | undefined`): Where the focus goes as it goes; asked then. Default, or when it answers nothing: what had the focus before.
- `rootStyle` (`RootStyle<OverlayKnobs>`): Typed StyleX override: layout and `--overlay-bg`.

Rest props go to `<div>` props.
