# Popup

> A floating element anchored to a trigger element using CSS Anchor Positioning. Supports 12 placement options, width matching, and click-outside dismissal.

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

Import: `import { Popup, Button, Divider, Input, PopupTrigger, Row, Text } from "@vaneui/ui"`

Popup holds content that floats beside the element that opened it, and Menu and Tooltip are built on it. It renders a `<div>` with `role="dialog"` unless `role` says otherwise. For a short hover hint, use a [Tooltip](/docs/overlay-components/tooltip); for a dialog that blocks the page, use a [Modal](/docs/overlay-components/modal). Its surface defaults are `md`, `flex column`, `padding`, `gap`, `rounded`, `border`, `shadow`, `primary`, `outline`, `wFit` and `placeBottom`.

> **Browser support:** Popup uses the CSS Anchor Positioning API (Chrome/Edge 129+, for the `span-*` alignment it relies on). Other browsers fall back to a JS positioning path that recomputes on scroll/resize.

## Basic usage

A controlled floating element anchored to a button. Closes on outside click or `Escape`. Pass an `anchorRef` pointing at the trigger element.

```tsx demo
const [open, setOpen] = useState(false);
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef} onClick={() => setOpen(!open)}>Toggle</Button>
    <Popup open={open} onClose={() => setOpen(false)} anchorRef={anchorRef}>
      <Text>Popup content</Text>
    </Popup>
  </>
);
```

## Uncontrolled with `defaultOpen`

Skip `open`/`onClose` and let Popup manage its own state. Use `onOpenChange` to react to opens/closes without owning the state.

```tsx demo
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef}>Trigger</Button>
    <Popup defaultOpen anchorRef={anchorRef} onOpenChange={(o) => console.log(o)}>
      <Text>Uncontrolled popup</Text>
    </Popup>
  </>
);
```

## PopupTrigger (click)

`PopupTrigger` is a convenience wrapper that manages open/close state and ref wiring automatically. No `useState` or `useRef` needed: wrap the trigger element and provide popup content via the `popup` prop. Default trigger mode is `"click"`.

```tsx demo
<PopupTrigger popup={<Text sm>Click-triggered popup</Text>}>
  <Button>Click Me</Button>
</PopupTrigger>
```

## PopupTrigger (hover)

Set `triggerOnHover` to show on mouse enter and hide on mouse leave. `openDelay` (default `0`) delays appearance; `closeDelay` (default `150`) delays dismissal so the user can move the cursor over the popup. Keyboard focus also opens it for accessibility.

```tsx demo
<PopupTrigger
  triggerOnHover
  openDelay={200}
  popup={<Text sm>Tooltip text</Text>}
>
  <Button>Hover Me</Button>
</PopupTrigger>
```

## PopupTrigger (focus)

Set `triggerOnFocus` to show on focus and hide on blur. Useful for search autocomplete, input hints, and dropdown suggestions.

```tsx demo
<PopupTrigger
  triggerOnFocus
  popup={<Text sm>Search suggestions...</Text>}
>
  <Input placeholder="Search..." />
</PopupTrigger>
```

## All 12 placements

Placement is set via boolean props: `placeTop`, `placeTopStart`, `placeTopEnd`, `placeBottom`, `placeBottomStart`, `placeBottomEnd`, `placeLeft`, `placeLeftStart`, `placeLeftEnd`, `placeRight`, `placeRightStart`, `placeRightEnd`. Exactly one wins (first-truthy per category). Default is `placeBottom`.

```tsx demo
const ref = useRef<HTMLButtonElement>(null);
const [open, setOpen] = useState(false);

return (
  <>
    <Button ref={ref} onClick={() => setOpen(!open)}>Anchor</Button>

    {/* Above the anchor, aligned to the anchor's left edge */}
    <Popup placeTopStart open={open} onClose={() => setOpen(false)} anchorRef={ref}>
      <Text>Top Start</Text>
    </Popup>

    {/* Centered to the right of the anchor */}
    <Popup placeRight open={open} onClose={() => setOpen(false)} anchorRef={ref}>
      <Text>Right</Text>
    </Popup>

    {/* Below the anchor, aligned to the anchor's right edge */}
    <Popup placeBottomEnd open={open} onClose={() => setOpen(false)} anchorRef={ref}>
      <Text>Bottom End</Text>
    </Popup>
  </>
);
```

When the requested placement would overflow the viewport, Popup falls back through `flip-block → flip-inline → shift → clamp` and exposes the resolved placement via the `data-placement` attribute.

## Size, shape, variant and appearance

Popup is `md`, `rounded`, `outline` and `primary` by default. Size drives the frame's padding, gap and radius together.

```tsx demo
const xsRef = useRef<HTMLButtonElement>(null);
const xlRef = useRef<HTMLButtonElement>(null);
const sharpRef = useRef<HTMLButtonElement>(null);
const filledRef = useRef<HTMLButtonElement>(null);

return (
  <Row flexWrap>
    <Button ref={xsRef}>xs</Button>
    <Popup xs defaultOpen anchorRef={xsRef}><Text xs>Extra small frame</Text></Popup>

    <Button ref={xlRef}>xl</Button>
    <Popup xl defaultOpen anchorRef={xlRef}><Text>Extra large frame</Text></Popup>

    <Button ref={sharpRef}>sharp</Button>
    <Popup sharp defaultOpen anchorRef={sharpRef}><Text>Sharp corners</Text></Popup>

    <Button ref={filledRef}>filled info</Button>
    <Popup filled info defaultOpen anchorRef={filledRef}><Text>Filled info surface</Text></Popup>
  </Row>
);
```

## Match anchor width

`matchWidth` makes the popup track the anchor's width, useful for select-like dropdowns. It sets the popup width to the anchor's width, overriding the default `wFit`.

```tsx demo
const [open, setOpen] = useState(false);
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef} onClick={() => setOpen(!open)} wFull>Select option</Button>
    <Popup matchWidth open={open} onClose={() => setOpen(false)} anchorRef={anchorRef}>
      <Text>Same width as the anchor</Text>
    </Popup>
  </>
);
```

## Rich content

Children render directly inside the Popup surface. Use layout primitives (`Col`, `Row`, `Stack`) to compose multi-element content. Avoid nesting another `Card` inside; Popup already provides the bordered, padded, shadowed surface.

```tsx demo
const [open, setOpen] = useState(false);
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef} onClick={() => setOpen(!open)}>Account</Button>
    <Popup open={open} onClose={() => setOpen(false)} anchorRef={anchorRef}>
      <Text fontBold>User Menu</Text>
      <Divider />
      <Button>Profile</Button>
      <Button danger>Sign Out</Button>
    </Popup>
  </>
);
```

## Tooltip pattern

Pair `triggerOnHover` with `arrow` for a classic tooltip. The arrow auto-rotates to match the resolved placement.

```tsx demo
<PopupTrigger
  triggerOnHover
  openDelay={300}
  popupProps={{ placeTop: true, arrow: true, role: "tooltip", sm: true }}
  popup={<Text sm>Save your changes to the server</Text>}
>
  <Button>Save</Button>
</PopupTrigger>
```

## Dropdown via Popup

Popup is a low-level primitive. For action menus with keyboard navigation, focus management, and item semantics, prefer the dedicated [`Menu`](./menu) component. It wraps Popup and adds `MenuItem`/`MenuLabel` with full arrow-key navigation. Reach for raw `Popup` only when you need a non-menu surface (filter panels, custom autocompletes, info cards).

```tsx demo
<PopupTrigger popup={
  <>
    <Button transparent justifyStart>Edit</Button>
    <Button transparent justifyStart>Duplicate</Button>
    <Divider />
    <Button transparent justifyStart danger>Delete</Button>
  </>
}>
  <Button>Actions</Button>
</PopupTrigger>
```

## Arrow indicator

Set `arrow` to render a pointer that visually links the popup to its anchor. The arrow is positioned automatically based on the resolved placement. With an arrow, `offset` is measured from the arrow tip, so the tip sits `offset` pixels from the anchor and never overlaps it (`@vaneui/ui` 1.5.0 or later).

```tsx demo
const [open, setOpen] = useState(false);
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef} onClick={() => setOpen(!open)}>Toggle Arrow Popup</Button>
    <Popup arrow open={open} onClose={() => setOpen(false)} anchorRef={anchorRef}>
      <Text sm>Arrow points at the anchor.</Text>
    </Popup>
  </>
);
```

## Click-outside behavior

By default, clicking anywhere outside the popup or anchor closes it. Set `closeOnClickOutside={false}` to require an explicit dismiss (e.g., a close button inside the popup). `closeOnEscape={false}` similarly disables the keyboard dismiss.

```tsx demo
const [open, setOpen] = useState(false);
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef} onClick={() => setOpen(true)}>Open Sticky Popup</Button>
    <Popup
      open={open}
      onClose={() => setOpen(false)}
      anchorRef={anchorRef}
      closeOnClickOutside={false}
      closeOnEscape={false}
    >
      <Text>Stays open until dismissed.</Text>
      <Button sm onClick={() => setOpen(false)}>Close</Button>
    </Popup>
  </>
);
```

## Disabled trigger

Set `disabled` on `Popup` (or `PopupTrigger`) to suppress opening regardless of `open` state. Useful for conditional UI where the trigger remains rendered but inactive.

```tsx demo
<PopupTrigger disabled popup={<Text>Hidden</Text>}>
  <Button disabled>Cannot open</Button>
</PopupTrigger>
```

## Advanced props

`Popup` supports additional configuration props:

| Prop | Default | Description |
|------|---------|-------------|
| `offset` | `4` | Distance from anchor in pixels; with `arrow`, measured from the arrow tip |
| `style` |   | Merged with the positioning styles, which win on a conflict (before 1.5.0 it replaced them) |
| `closeOnEscape` | `true` | Close on `Escape` key press |
| `closeOnClickOutside` | `true` | Close when clicking outside the popup or anchor |
| `portal` | `true` | Render via portal into `document.body` |
| `keepMounted` | `false` | Keep DOM node mounted when closed |
| `noAnimation` | `false` | Disable enter/exit transitions |
| `transitionDuration` | `200` | Animation duration in ms |
| `disabled` | `false` | Prevent popup from opening |
| `hideWhenDetached` | `false` | Hide when anchor scrolls out of view (uses `IntersectionObserver`) |
| `role` | `"dialog"` | ARIA role (set `"tooltip"` for tooltip patterns) |
| `arrow` | `false` | Render arrow pointer |
| `onEnterComplete` |   | Called when the enter transition finishes |
| `onExitComplete` |   | Called when the exit transition finishes |

`PopupTrigger` additionally accepts:

| Prop | Default | Description |
|------|---------|-------------|
| `triggerOnClick` | `true` (when no other trigger set) | Toggle on click |
| `triggerOnHover` | `false` | Open on mouse enter / focus |
| `triggerOnFocus` | `false` | Open on focus only |
| `openDelay` | `0` | ms before opening on hover |
| `closeDelay` | `150` | ms before closing on hover-out |
| `popupProps` |   | Props forwarded to the internal `Popup` |
| `popupId` | auto | Override generated id for `aria-controls` |

```tsx demo
const [open, setOpen] = useState(false);
const anchorRef = useRef<HTMLButtonElement>(null);

return (
  <>
    <Button ref={anchorRef} onClick={() => setOpen(!open)}>Custom Config</Button>
    <Popup
      open={open}
      onClose={() => setOpen(false)}
      anchorRef={anchorRef}
      offset={8}
      noAnimation
      closeOnEscape={false}
    >
      <Text>Custom config</Text>
    </Popup>
  </>
);
```

## Frequently asked questions

### Does focus move into a Popup when it opens?

Only when you ask for it. `PopupTrigger` in click mode moves focus to the first focusable element inside, but a Popup used on its own leaves focus on its trigger unless you set `autoFocus`. The Popup renders at the end of `<body>`, so `Tab` from the trigger does not reach it otherwise. `modal` also traps `Tab` inside the Popup, returns focus on close and sets `aria-modal`.

### Why don't the styles of the surrounding element reach my Popup?

The Popup renders through a portal at the end of `<body>`, so a selector such as `.sidebar .vane-popup`, the color or font a parent sets, and the `data-theme` of a wrapper do not apply to it. Style it with its own props and `className`. `portal={false}` renders it in place, but an ancestor with a `transform` or `filter` then becomes its containing block, which can shift or clip it.

### How do I close a PopupTrigger popup from a button inside it?

Control the PopupTrigger: keep the open state in your component, pass it as `open`, update it in `onOpenChange`, and set it to `false` in the button's `onClick`. A click on the trigger, an outside click and `Escape` still call `onOpenChange` with the new state, so setting your state there keeps them working. Uncontrolled, PopupTrigger offers no close function to call.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance | yes | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance |  | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family | yes | Sans-serif font family (default) — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight |  | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight |  | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `padding` | Padding | yes | Enable internal padding |
| `paddingX` | Padding |  | Enable only horizontal padding |
| `paddingY` | Padding |  | Enable only vertical padding |
| `noPadding` | Padding |  | Disable internal padding |
| `placeTop` | Placement |  | Position above anchor, centered horizontally (default) |
| `placeTopStart` | Placement |  | Position above anchor, aligned to start (left) |
| `placeTopEnd` | Placement |  | Position above anchor, aligned to end (right) |
| `placeBottom` | Placement | yes | Position below anchor, centered horizontally |
| `placeBottomStart` | Placement |  | Position below anchor, aligned to start (left) |
| `placeBottomEnd` | Placement |  | Position below anchor, aligned to end (right) |
| `placeLeft` | Placement |  | Position to the left of anchor, centered vertically |
| `placeLeftStart` | Placement |  | Position to the left of anchor, aligned to top |
| `placeLeftEnd` | Placement |  | Position to the left of anchor, aligned to bottom |
| `placeRight` | Placement |  | Position to the right of anchor, centered vertically |
| `placeRightStart` | Placement |  | Position to the right of anchor, aligned to top |
| `placeRightEnd` | Placement |  | Position to the right of anchor, aligned to bottom |
| `pill` | Shape |  | Fully rounded corners (circular) |
| `sharp` | Shape |  | No rounded corners (square) |
| `rounded` | Shape | yes | Medium rounded corners (default) |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size |  | Small size |
| `md` | Size | yes | 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 |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `truncate` | Truncate |  | Single line truncation with ellipsis |
| `lineClamp2` | Truncate |  | Truncate at 2 lines with ellipsis |
| `lineClamp3` | Truncate |  | Truncate at 3 lines with ellipsis |
| `lineClamp4` | Truncate |  | Truncate at 4 lines with ellipsis |
| `lineClamp5` | Truncate |  | Truncate at 5 lines with ellipsis |
| `noTruncate` | Truncate |  | Remove truncation |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text (default) |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |

Layout and utility props (Align Self, Border, Clamp Height, Constrain Width, Display, Flex Direction, Gap, Height, Hide, Items, Justify, Justify Self, Overflow, Pointer Events, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Transition, Transparent, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
