> Native Solid 2 fork. Original documentation and designs © WorkOS; historical posts retain their original context.

# Popover

Displays rich content in a portal, triggered by a button.

## Features

- Can be controlled or uncontrolled.

- Customize side, alignment, offsets, collision handling.

- Optionally render a pointing arrow.

- Focus is fully managed and customizable.

- Supports modal and non-modal modes.

- Dismissing and layering behavior is highly customizable.

## Anatomy

Import all parts and piece them together.

```jsx
import { Popover } from "@radix-solid/primitives";

export default () => <Popover.Root>

		<Popover.Trigger />

		<Popover.Anchor />

		<Popover.Portal>

			<Popover.Content>

				<Popover.Close />

				<Popover.Arrow />

			</Popover.Content>

		</Popover.Portal>

	</Popover.Root>;
```

## API Reference

### Root

Contains all the parts of a popover.

| Prop                                                                                                                                                                   | Type       | Default |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ------- |
| `defaultOpen`The open state of the popover when it is initially rendered. Use when you do not need to control its open state.                                          | `boolean`  | —       |
| `open`The controlled open state of the popover. Must be used in conjunction with `onOpenChange`.                                                                       | `boolean`  | —       |
| `onOpenChange`Event handler called when the open state of the popover changes.                                                                                         | `function` | —       |
| `modal`The modality of the popover. When set to `true`, interaction with outside elements will be disabled and only popover content will be visible to screen readers. | `boolean`  | `false` |

### Trigger

The button that toggles the popover. By default, the `Popover.Content` will position itself against the trigger.

| Prop                                                                                                                                                                    | Type      | Default |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------- |
| `asChild`Render through a single-target factory, merging props and behavior with the chosen host. Read our [Composition](../guides/composition) guide for more details. | `boolean` | `false` |

| Data attribute | Values               |
| -------------- | -------------------- |
| `[data-state]` | `"open" \| "closed"` |

### Anchor

An optional element to position the `Popover.Content` against. If this part is not used, the content will position alongside the `Popover.Trigger`.

| Prop                                                                                                                                                                    | Type      | Default |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------- |
| `asChild`Render through a single-target factory, merging props and behavior with the chosen host. Read our [Composition](../guides/composition) guide for more details. | `boolean` | `false` |

### Portal

When used, portals the content part into the `body`.

| Prop                                                                                                                                                                                                          | Type          | Default         |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | --------------- |
| `forceMount`Used to force mounting when more control is needed. Useful when controlling animation with Solid-compatible animation libraries. If used on this part, it will be inherited by `Popover.Content`. | `boolean`     | —               |
| `container`Specify a container element to portal the content into.                                                                                                                                            | `HTMLElement` | `document.body` |

### Content

The component that pops out when the popover is open.

| Prop                                                                                                                                                                                                                              | Type                | Default     |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ----------- |
| `asChild`Render through a single-target factory, merging props and behavior with the chosen host. Read our [Composition](../guides/composition) guide for more details.                                                           | `boolean`           | `false`     |
| `onOpenAutoFocus`Event handler called when focus moves into the component after opening. It can be prevented by calling `event.preventDefault`.                                                                                   | `function`          | —           |
| `onCloseAutoFocus`Event handler called when focus moves to the trigger after closing. It can be prevented by calling `event.preventDefault`.                                                                                      | `function`          | —           |
| `onEscapeKeyDown`Event handler called when the escape key is down. It can be prevented by calling `event.preventDefault`.                                                                                                         | `function`          | —           |
| `onPointerDownOutside`Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling `event.preventDefault`.                                                                | `function`          | —           |
| `onFocusOutside`Event handler called when focus moves outside the bounds of the component. It can be prevented by calling `event.preventDefault`.                                                                                 | `function`          | —           |
| `onInteractOutside`Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling `event.preventDefault`.                                          | `function`          | —           |
| `forceMount`Used to force mounting when more control is needed. Useful when controlling animation with Solid-compatible animation libraries. It inherits from `Popover.Portal`.                                                   | `boolean`           | —           |
| `side`The preferred side of the anchor to render against when open. Will be reversed when collisions occur and `avoidCollisions` is enabled.                                                                                      | `enum`              | `"bottom"`  |
| `sideOffset`The distance in pixels from the anchor.                                                                                                                                                                               | `number`            | `0`         |
| `align`The preferred alignment against the anchor. May change when collisions occur.                                                                                                                                              | `enum`              | `"center"`  |
| `alignOffset`An offset in pixels from the `"start"` or `"end"` alignment options.                                                                                                                                                 | `number`            | `0`         |
| `avoidCollisions`When `true`, overrides the `side` and`align` preferences to prevent collisions with boundary edges.                                                                                                              | `boolean`           | `true`      |
| `collisionBoundary`The element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check.                                                        | `Boundary`          | `[]`        |
| `collisionPadding`The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: `{ top: 20, left: 20 }`.                | `number \| Padding` | `0`         |
| `arrowPadding`The padding between the arrow and the edges of the content. If your content has `border-radius`, this will prevent it from overflowing the corners.                                                                 | `number`            | `0`         |
| `sticky`The sticky behavior on the align axis. `"partial"` will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst `"always"` will keep the content in the boundary regardless. | `enum`              | `"partial"` |
| `hideWhenDetached`Whether to hide the content when the trigger becomes fully occluded.                                                                                                                                            | `boolean`           | `false`     |

| Data attribute | Values                                   |
| -------------- | ---------------------------------------- |
| `[data-state]` | `"open" \| "closed"`                     |
| `[data-side]`  | `"left" \| "right" \| "bottom" \| "top"` |
| `[data-align]` | `"start" \| "end" \| "center"`           |

| CSS Variable                               | Description                                                                  |
| ------------------------------------------ | ---------------------------------------------------------------------------- |
| `--radix-popover-content-transform-origin` | The `transform-origin` computed from the content and arrow positions/offsets |
| `--radix-popover-content-available-width`  | The remaining width between the trigger and the boundary edge                |
| `--radix-popover-content-available-height` | The remaining height between the trigger and the boundary edge               |
| `--radix-popover-trigger-width`            | The width of the trigger                                                     |
| `--radix-popover-trigger-height`           | The height of the trigger                                                    |

### Arrow

An optional arrow element to render alongside the popover. This can be used to help visually link the anchor with the `Popover.Content`. Must be rendered inside `Popover.Content`.

| Prop                                                                                                                                                                    | Type      | Default |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------- |
| `asChild`Render through a single-target factory, merging props and behavior with the chosen host. Read our [Composition](../guides/composition) guide for more details. | `boolean` | `false` |
| `width`The width of the arrow in pixels.                                                                                                                                | `number`  | `10`    |
| `height`The height of the arrow in pixels.                                                                                                                              | `number`  | `5`     |

### Close

The button that closes an open popover.

| Prop                                                                                                                                                                    | Type      | Default |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------- |
| `asChild`Render through a single-target factory, merging props and behavior with the chosen host. Read our [Composition](../guides/composition) guide for more details. | `boolean` | `false` |

## Examples

### Constrain the content size

You may want to constrain the width of the content so that it matches the trigger width. You may also want to constrain its height to not exceed the viewport.

We expose several CSS custom properties such as `--radix-popover-trigger-width` and `--radix-popover-content-available-height` to support this. Use them to constrain the content dimensions.

```jsx
// index.tsx

import { Popover } from "@radix-solid/primitives";

import "./styles.css";

export default () => <Popover.Root>

		<Popover.Trigger>…</Popover.Trigger>

		<Popover.Portal>

			<Popover.Content class="PopoverContent" sideOffset={5}>

				…

			</Popover.Content>

		</Popover.Portal>

	</Popover.Root>;
```

```css
/* styles.css */

.PopoverContent {

	width: var(--radix-popover-trigger-width);

	max-height: var(--radix-popover-content-available-height);

}
```

### Origin-aware animations

We expose a CSS custom property `--radix-popover-content-transform-origin`. Use it to animate the content from its computed origin based on `side`, `sideOffset`, `align`, `alignOffset` and any collisions.

```jsx
// index.tsx

import { Popover } from "@radix-solid/primitives";

import "./styles.css";

export default () => <Popover.Root>

		<Popover.Trigger>…</Popover.Trigger>

		<Popover.Portal>

			<Popover.Content class="PopoverContent">…</Popover.Content>

		</Popover.Portal>

	</Popover.Root>;
```

```css
/* styles.css */

.PopoverContent {

	transform-origin: var(--radix-popover-content-transform-origin);

	animation: scaleIn 0.5s ease-out;

}



@keyframes scaleIn {

	from {

		opacity: 0;

		transform: scale(0);

	}

	to {

		opacity: 1;

		transform: scale(1);

	}

}
```

### Collision-aware animations

We expose `data-side` and `data-align` attributes. Their values will change at runtime to reflect collisions. Use them to create collision and direction-aware animations.

```jsx
// index.tsx

import { Popover } from "@radix-solid/primitives";

import "./styles.css";

export default () => <Popover.Root>

		<Popover.Trigger>…</Popover.Trigger>

		<Popover.Portal>

			<Popover.Content class="PopoverContent">…</Popover.Content>

		</Popover.Portal>

	</Popover.Root>;
```

```css
/* styles.css */

.PopoverContent {

	animation-duration: 0.6s;

	animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);

}

.PopoverContent[data-side="top"] {

	animation-name: slideUp;

}

.PopoverContent[data-side="bottom"] {

	animation-name: slideDown;

}



@keyframes slideDown {

	from {

		opacity: 0;

		transform: translateY(-10px);

	}

	to {

		opacity: 1;

		transform: translateY(0);

	}

}



@keyframes slideUp {

	from {

		opacity: 0;

		transform: translateY(10px);

	}

	to {

		opacity: 1;

		transform: translateY(0);

	}

}
```

### With custom anchor

You can anchor the content to another element if you do not want to use the trigger as the anchor.

```jsx
// index.tsx

import { Popover } from "@radix-solid/primitives";

import "./styles.css";

export default () => <Popover.Root>

		<Popover.Anchor asChild>{target => target("div", {

      "class": "Row",

      children: <>

				Row as anchor <Popover.Trigger>Trigger</Popover.Trigger>

			</>

    })}</Popover.Anchor>



		<Popover.Portal>

			<Popover.Content>…</Popover.Content>

		</Popover.Portal>

	</Popover.Root>;
```

```css
/* styles.css */

.Row {

	background-color: gainsboro;

	padding: 20px;

}
```

## Accessibility

Adheres to the [Dialog WAI-ARIA design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal).

### Keyboard Interactions

| Key           | Description                                              |
| ------------- | -------------------------------------------------------- |
| `Space`       | Opens/closes the popover.                                |
| `Enter`       | Opens/closes the popover.                                |
| `Tab`         | Moves focus to the next focusable element                |
| `Shift + Tab` | Moves focus to the previous focusable element            |
| `Esc`         | Closes the popover and moves focus to `Popover.Trigger`. |

## Custom APIs

Create your own API by abstracting the primitive parts into your own component.

#### Abstract the arrow and set default configuration

This example abstracts the `Popover.Arrow` part and sets a default `sideOffset` configuration.

#### Usage

```jsx
import { Popover, PopoverTrigger, PopoverContent } from "./your-popover";

export default () => <Popover>

		<PopoverTrigger>Popover trigger</PopoverTrigger>

		<PopoverContent>Popover content</PopoverContent>

	</Popover>;
```

#### Implementation

```jsx
import { omit, type ComponentProps } from "solid-js";

// your-popover.tsx



import { Popover as PopoverPrimitive } from "@radix-solid/primitives";

export const Popover = PopoverPrimitive.Root;

export const PopoverTrigger = PopoverPrimitive.Trigger;

export const PopoverContent = (p: Extract<ComponentProps<typeof PopoverPrimitive.Content>, {

  asChild?: false;

}>) => <PopoverPrimitive.Portal>

			<PopoverPrimitive.Content sideOffset={5} {...omit(p, "children", "class")} ref={p.ref}>

				{p.children}

				<PopoverPrimitive.Arrow />

			</PopoverPrimitive.Content>

		</PopoverPrimitive.Portal>;
```
