# Popover

> A floating panel anchored to a trigger for explanatory, structured or interactive content.

```tsx title="hero.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Popover,
  PopoverClose,
  PopoverContent,
  PopoverDescription,
  PopoverTitle,
  PopoverTrigger,
} from '@langgenius/dify-ui/popover'

export function PopoverHeroDemo() {
  return (
    <Popover>
      <PopoverTrigger
        render={
          <Button className="data-popup-open:bg-components-button-secondary-bg-hover data-popup-open:inset-ring-components-button-secondary-border-hover" />
        }
      >
        Service API
      </PopoverTrigger>
      <PopoverContent className="flex w-80 max-w-[calc(100vw-2rem)] flex-col gap-3 p-4">
        <div className="flex flex-col gap-1">
          <PopoverTitle className="system-md-semibold text-text-primary">Service API</PopoverTitle>
          <PopoverDescription className="system-xs-regular text-text-tertiary">
            Authenticate requests with a workspace secret key in the Authorization header.
          </PopoverDescription>
        </div>
        <dl className="grid grid-cols-[max-content_1fr] gap-x-4 gap-y-1.5 border-t border-divider-subtle pt-3 system-xs-regular">
          <dt className="text-text-tertiary">Endpoint</dt>
          <dd className="truncate font-mono text-text-secondary">/v1/chat-messages</dd>
          <dt className="text-text-tertiary">Rate limit</dt>
          <dd className="text-text-secondary tabular-nums">60 requests / min</dd>
        </dl>
        <div className="flex items-center justify-between gap-2 pt-0.5">
          <a
            href="https://docs.dify.ai"
            target="_blank"
            rel="noreferrer"
            className="rounded-xs system-xs-medium text-text-accent underline decoration-text-accent/70 decoration-1 underline-offset-[0.2em] outline-offset-2 outline-state-accent-solid transition-colors hover:decoration-current hover:decoration-2 focus-visible:outline-2 focus-visible:outline-solid active:opacity-80"
          >
            API reference
          </a>
          <PopoverClose render={<Button size="small" />}>Done</PopoverClose>
        </div>
      </PopoverContent>
    </Popover>
  )
}
```

## Usage guidelines

- Use `Popover` for explanatory, structured or interactive content that users must reach on touch and with assistive technology. Use `Tooltip` for a short visual hint on a control that already has its own action and name, `Infotip` when an information icon only opens an explanation, `PreviewCard` for a non-interactive preview of a link destination, and `Dialog` when the task needs a modal surface with focus containment and scroll locking.
- Give the popup a `PopoverTitle`; it names the dialog role the popup exposes. Add a `PopoverDescription` when a sentence of context helps.
- Name the trigger. Visible text names a text trigger; an icon-only trigger renders `IconButton` through `render` and carries its own `aria-label`.
- Put a dedicated opener under the root as `PopoverTrigger`. When the trigger has to live outside the root, connect both with a handle from `createPopoverHandle`; `handle.open(triggerId)` opens the popover anchored to the registered trigger with that `id`.
- Several triggers can share one root. Each trigger passes its item as `payload`, and the root's function child reads the payload of the trigger that opened it.
- Control `open` only when another owner must observe or coordinate the open state. Analytics or cleanup alone does not need a controlled root.
- `openOnHover` adds hover on top of click and keyboard. Content that matters still has to work for users who never hover.
- Use `PopoverClose` with `render` for actions that finish the task, such as Cancel or Send.

## Anatomy

```tsx
import {
  Popover,
  PopoverClose,
  PopoverContent,
  PopoverDescription,
  PopoverTitle,
  PopoverTrigger,
} from '@langgenius/dify-ui/popover'
```

```tsx
<Popover>
  <PopoverTrigger />
  <PopoverContent>
    <PopoverTitle />
    <PopoverDescription />
    <PopoverClose />
  </PopoverContent>
</Popover>
```

`PopoverContent` bundles the portal, positioner and styled popup and takes `side`, `align` and their offsets. Compose `PopoverPortal`, `PopoverPositioner` and `PopoverPopup` directly, with `PopoverBackdrop`, `PopoverArrow` or `PopoverViewport`, only when a surface needs more than those options.

## Examples

### Opening on hover

`openOnHover` on the trigger also opens the popover on hover after `delay`. Click, tap and keyboard still open it.

```tsx title="open-on-hover.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Popover,
  PopoverContent,
  PopoverDescription,
  PopoverTitle,
  PopoverTrigger,
} from '@langgenius/dify-ui/popover'

const apps = [
  { name: 'Customer support bot', calls: '12.4k calls this month' },
  { name: 'Contract review workflow', calls: '3.1k calls this month' },
  { name: 'Internal search', calls: '860 calls this month' },
]

export function PopoverOpenOnHoverDemo() {
  return (
    <Popover>
      <PopoverTrigger
        openOnHover
        delay={300}
        render={
          <Button className="data-popup-open:bg-components-button-secondary-bg-hover data-popup-open:inset-ring-components-button-secondary-border-hover" />
        }
      >
        <span aria-hidden="true" className="i-ri-key-2-line size-4 text-text-tertiary" />
        Used by 3 apps
      </PopoverTrigger>
      <PopoverContent className="flex w-72 max-w-[calc(100vw-2rem)] flex-col gap-3 p-4">
        <div className="flex flex-col gap-1">
          <PopoverTitle className="system-md-semibold text-text-primary">
            Apps using this key
          </PopoverTitle>
          <PopoverDescription className="system-xs-regular text-text-tertiary">
            Revoking the key stops these apps from calling the model provider.
          </PopoverDescription>
        </div>
        <ul className="flex flex-col divide-y divide-divider-subtle border-t border-divider-subtle">
          {apps.map((app) => (
            <li key={app.name} className="flex flex-col gap-0.5 py-2 first:pt-2.5 last:pb-0">
              <span className="system-sm-medium text-text-secondary">{app.name}</span>
              <span className="system-xs-regular text-text-tertiary tabular-nums">{app.calls}</span>
            </li>
          ))}
        </ul>
      </PopoverContent>
    </Popover>
  )
}
```

### Trigger outside the root

The trigger sits outside the root and connects to it through a shared handle. The separate button calls `handle.open(triggerId)` to open the popover anchored to that registered trigger.

```tsx title="detached-trigger.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import {
  createPopoverHandle,
  Popover,
  PopoverContent,
  PopoverDescription,
  PopoverTitle,
  PopoverTrigger,
} from '@langgenius/dify-ui/popover'
import * as React from 'react'

const variables = [
  { name: 'OPENAI_API_BASE', value: 'https://api.openai.com/v1' },
  { name: 'SUPPORT_EMAIL', value: 'support@example.com' },
]

export function PopoverDetachedTriggerDemo() {
  const [handle] = React.useState(() => createPopoverHandle())
  const triggerId = React.useId()

  return (
    <React.Fragment>
      <div className="flex flex-wrap items-center justify-center gap-3">
        <Button onClick={() => handle.open(triggerId)}>Show variables</Button>
        <PopoverTrigger
          id={triggerId}
          handle={handle}
          render={
            <IconButton
              variant="secondary"
              size="lg"
              aria-label="Environment variables"
              className="data-popup-open:bg-components-button-secondary-bg-hover data-popup-open:inset-ring-components-button-secondary-border-hover"
            >
              <span aria-hidden="true" className="i-ri-key-2-line size-4" />
            </IconButton>
          }
        />
      </div>
      <Popover handle={handle}>
        <PopoverContent className="flex w-72 max-w-[calc(100vw-2rem)] flex-col gap-3 p-4">
          <div className="flex flex-col gap-1">
            <PopoverTitle className="system-md-semibold text-text-primary">
              Environment variables
            </PopoverTitle>
            <PopoverDescription className="system-xs-regular text-text-tertiary">
              Values are shared by every node in this workflow.
            </PopoverDescription>
          </div>
          <dl className="flex flex-col divide-y divide-divider-subtle border-t border-divider-subtle">
            {variables.map((variable) => (
              <div
                key={variable.name}
                className="flex flex-col gap-0.5 py-2 first:pt-2.5 last:pb-0"
              >
                <dt className="font-mono system-xs-medium text-text-secondary">{variable.name}</dt>
                <dd className="truncate font-mono system-xs-regular text-text-tertiary">
                  {variable.value}
                </dd>
              </div>
            ))}
          </dl>
        </PopoverContent>
      </Popover>
    </React.Fragment>
  )
}
```

### Multiple triggers

One root serves several triggers. Each trigger carries its item as `payload`, and the popover anchors to the trigger that opened it.

```tsx title="multiple-triggers.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Popover,
  PopoverContent,
  PopoverDescription,
  PopoverTitle,
  PopoverTrigger,
} from '@langgenius/dify-ui/popover'
import * as React from 'react'

type NodeInfo = {
  name: string
  icon: string
  description: string
}

const nodes: NodeInfo[] = [
  {
    name: 'Knowledge retrieval',
    icon: 'i-ri-book-open-line',
    description: 'Searches the selected knowledge bases before the model generates an answer.',
  },
  {
    name: 'HTTP request',
    icon: 'i-ri-global-line',
    description: 'Calls an external endpoint with values from earlier nodes.',
  },
  {
    name: 'Code',
    icon: 'i-ri-code-s-slash-line',
    description: 'Runs Python or JavaScript to transform variables between nodes.',
  },
]

export function PopoverMultipleTriggersDemo() {
  return (
    <Popover<NodeInfo>>
      {({ payload }) => (
        <React.Fragment>
          <div className="flex flex-wrap justify-center gap-2">
            {nodes.map((node) => (
              <PopoverTrigger<NodeInfo>
                key={node.name}
                payload={node}
                render={
                  <Button className="data-popup-open:bg-components-button-secondary-bg-hover data-popup-open:inset-ring-components-button-secondary-border-hover" />
                }
              >
                <span aria-hidden="true" className={`${node.icon} size-4 text-text-tertiary`} />
                {node.name}
              </PopoverTrigger>
            ))}
          </div>
          <PopoverContent className="flex w-72 max-w-[calc(100vw-2rem)] flex-col gap-1.5 p-4">
            <PopoverTitle className="system-md-semibold text-text-primary">
              {payload?.name}
            </PopoverTitle>
            <PopoverDescription className="system-xs-regular text-text-tertiary">
              {payload?.description}
            </PopoverDescription>
          </PopoverContent>
        </React.Fragment>
      )}
    </Popover>
  )
}
```

## Keyboard

| Key              | Action                                                                                                                              |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `Enter`, `Space` | Opens the popover from a focused trigger and moves focus to the first control inside it.                                            |
| `Tab`            | Moves to the next control in the popover. Past the last control, closes the popover and continues to the element after the trigger. |
| `Shift` + `Tab`  | Moves to the previous control. From the first control, returns focus to the trigger and keeps the popover open.                     |
| `Escape`         | Closes the popover and returns focus to the trigger.                                                                                |

## API reference

### Popover

Groups all parts of a floating popover anchored to a trigger. Doesn't render its own HTML element.

Pass `openOnHover` on `PopoverTrigger` to also reveal on hover while keeping click and keyboard access.

Inherits `defaultOpen`, `open`, `onOpenChange`, `onOpenChangeComplete`, `actionsRef`, `modal`, `triggerId`, `defaultTriggerId`, `handle`, `children` from [Base UI Popover.Root](https://base-ui.com/react/components/popover#root).

### PopoverArrow

Displays an element positioned against the popover anchor. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Popover.Arrow](https://base-ui.com/react/components/popover#arrow).

**Data attributes**

| Attribute         | Description                                                           |
| ----------------- | --------------------------------------------------------------------- |
| `data-open`       | Present when the popup is open.                                       |
| `data-closed`     | Present when the popup is closed.                                     |
| `data-side`       | Indicates which side the popup is positioned relative to the trigger. |
| `data-align`      | Indicates how the popup is aligned relative to specified side.        |
| `data-uncentered` | Present when the popover arrow is uncentered.                         |

### PopoverBackdrop

An overlay displayed beneath the popover. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Popover.Backdrop](https://base-ui.com/react/components/popover#backdrop).

**Data attributes**

| Attribute             | Description                                 |
| --------------------- | ------------------------------------------- |
| `data-open`           | Present when the popup is open.             |
| `data-closed`         | Present when the popup is closed.           |
| `data-starting-style` | Present when the popup begins animating in. |
| `data-ending-style`   | Present when the popup is animating out.    |

### PopoverClose

A button that closes the popover. Renders a `<button>` element.

Inherits `className`, `render`, `style`, `nativeButton` from [Base UI Popover.Close](https://base-ui.com/react/components/popover#close).

### PopoverContent

A styled popover surface bundled with a portal and positioner. Renders a `<div>` element.

**Props**

| Prop          | Type                       | Default | Description                                                                                             |
| ------------- | -------------------------- | ------- | ------------------------------------------------------------------------------------------------------- |
| `sideOffset?` | `number \| OffsetFunction` | `8`     | Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance. |
| `children`    | `React.ReactNode`          | —       | The popover content.                                                                                    |

Inherits `className`, `render`, `style`, `initialFocus`, `finalFocus`, `side`, `align`, `alignOffset` from [Base UI Popover.Popup](https://base-ui.com/react/components/popover#popup).

**Data attributes**

| Attribute             | Description                                                           |
| --------------------- | --------------------------------------------------------------------- |
| `data-open`           | Present when the popup is open.                                       |
| `data-closed`         | Present when the popup is closed.                                     |
| `data-starting-style` | Present when the popup begins animating in.                           |
| `data-ending-style`   | Present when the popup is animating out.                              |
| `data-side`           | Indicates which side the popup is positioned relative to the trigger. |
| `data-align`          | Indicates how the popup is aligned relative to specified side.        |
| `data-instant`        | Present if animations should be instant.                              |

**CSS variables**

| Variable              | Type     | Description                                                                                                                   |
| --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `--popup-width`       | —        | The width of the popup.                                                                                                       |
| `--popup-height`      | —        | The height of the popup.                                                                                                      |
| `--available-width`   | `number` | The available width between the trigger and the edge of the viewport.                                                         |
| `--available-height`  | `number` | The available height between the trigger and the edge of the viewport.                                                        |
| `--anchor-width`      | `number` | The anchor's width.                                                                                                           |
| `--anchor-height`     | `number` | The anchor's height.                                                                                                          |
| `--transform-origin`  | `string` | The coordinates that this element is anchored to. Used for animations and transitions.                                        |
| `--positioner-width`  | `number` | The width of the popover's positioner. It is important to set `width` to this value when using CSS to animate size changes.   |
| `--positioner-height` | `number` | The height of the popover's positioner. It is important to set `height` to this value when using CSS to animate size changes. |

### PopoverDescription

A paragraph with additional information about the popover. Renders a `<p>` element.

Inherits `className`, `render`, `style` from [Base UI Popover.Description](https://base-ui.com/react/components/popover#description).

### PopoverPopup

A container for the popover contents. Renders a `<div>` element.

Inherits `className`, `render`, `style`, `initialFocus`, `finalFocus` from [Base UI Popover.Popup](https://base-ui.com/react/components/popover#popup).

**Data attributes**

| Attribute             | Description                                                           |
| --------------------- | --------------------------------------------------------------------- |
| `data-open`           | Present when the popup is open.                                       |
| `data-closed`         | Present when the popup is closed.                                     |
| `data-starting-style` | Present when the popup begins animating in.                           |
| `data-ending-style`   | Present when the popup is animating out.                              |
| `data-side`           | Indicates which side the popup is positioned relative to the trigger. |
| `data-align`          | Indicates how the popup is aligned relative to specified side.        |
| `data-instant`        | Present if animations should be instant.                              |

**CSS variables**

| Variable         | Type | Description              |
| ---------------- | ---- | ------------------------ |
| `--popup-width`  | —    | The width of the popup.  |
| `--popup-height` | —    | The height of the popup. |

### PopoverPortal

A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to `<body>`. Renders a `<div>` element.

Inherits `keepMounted`, `container`, `className`, `render`, `style` from [Base UI Popover.Portal](https://base-ui.com/react/components/popover#portal).

### PopoverPositioner

Positions the popover against the trigger. Renders a `<div>` element.

**Props**

| Prop          | Type                       | Default | Description                                                                                             |
| ------------- | -------------------------- | ------- | ------------------------------------------------------------------------------------------------------- |
| `sideOffset?` | `number \| OffsetFunction` | `8`     | Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance. |

Inherits `className`, `render`, `style`, `anchor`, `positionMethod`, `side`, `align`, `alignOffset`, `collisionBoundary`, `collisionPadding`, `sticky`, `arrowPadding`, `disableAnchorTracking`, `collisionAvoidance` from [Base UI Popover.Positioner](https://base-ui.com/react/components/popover#positioner).

**Data attributes**

| Attribute            | Description                                                           |
| -------------------- | --------------------------------------------------------------------- |
| `data-open`          | Present when the popup is open.                                       |
| `data-closed`        | Present when the popup is closed.                                     |
| `data-anchor-hidden` | Present when the anchor is hidden.                                    |
| `data-side`          | Indicates which side the popup is positioned relative to the trigger. |
| `data-align`         | Indicates how the popup is aligned relative to specified side.        |

**CSS variables**

| Variable              | Type     | Description                                                                                                                   |
| --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `--available-width`   | `number` | The available width between the trigger and the edge of the viewport.                                                         |
| `--available-height`  | `number` | The available height between the trigger and the edge of the viewport.                                                        |
| `--anchor-width`      | `number` | The anchor's width.                                                                                                           |
| `--anchor-height`     | `number` | The anchor's height.                                                                                                          |
| `--transform-origin`  | `string` | The coordinates that this element is anchored to. Used for animations and transitions.                                        |
| `--positioner-width`  | `number` | The width of the popover's positioner. It is important to set `width` to this value when using CSS to animate size changes.   |
| `--positioner-height` | `number` | The height of the popover's positioner. It is important to set `height` to this value when using CSS to animate size changes. |

### PopoverTitle

A heading that labels the popover. Renders an `<h2>` element.

Inherits `className`, `render`, `style` from [Base UI Popover.Title](https://base-ui.com/react/components/popover#title).

### PopoverTrigger

A button that opens the popover. Renders a `<button>` element.

Inherits `nativeButton`, `id`, `className`, `render`, `style`, `handle`, `payload`, `openOnHover`, `delay`, `closeDelay` from [Base UI Popover.Trigger](https://base-ui.com/react/components/popover#trigger).

**Data attributes**

| Attribute         | Description                                     |
| ----------------- | ----------------------------------------------- |
| `data-popup-open` | Present when the corresponding popover is open. |
| `data-pressed`    | Present when the trigger is pressed.            |
| `data-disabled`   | Present when the trigger is disabled.           |

### PopoverViewport

A viewport for displaying content transitions. This component is only required if one popup can be opened by multiple triggers, its content changes based on the trigger, and switching between them is animated. Renders a `<div>` element.

Inherits `children`, `className`, `render`, `style` from [Base UI Popover.Viewport](https://base-ui.com/react/components/popover#viewport).

**Data attributes**

| Attribute                   | Description                                                                                                                                                                                                                |
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data-current`              | Applied to the direct child of the viewport when no transitions are present or the new content when it's entering.                                                                                                         |
| `data-previous`             | Applied to the direct child of the viewport that contains the exiting content when transitions are present.                                                                                                                |
| `data-activation-direction` | Indicates the direction from which the popup was activated. This can be used to create directional animations based on how the popup was triggered. Contains space-separated values for both horizontal and vertical axes. |
| `data-transitioning`        | Indicates that the viewport is currently transitioning between old and new content.                                                                                                                                        |
| `data-instant`              | Present if animations should be instant.                                                                                                                                                                                   |

**CSS variables**

| Variable         | Type | Description                                                                                                                                                                                                                                                   |
| ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--popup-width`  | —    | The width of the parent popup. This variable is placed on the 'previous' container and stores the width of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content.   |
| `--popup-height` | —    | The height of the parent popup. This variable is placed on the 'previous' container and stores the height of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content. |

### createPopoverHandle

Creates a new handle to connect a Popover.Root with detached Popover.Trigger components.

```ts
createPopoverHandle<Payload>(): BasePopover.Handle<Payload>
```

**Returns:** `BasePopover.Handle<Payload>`

**Return properties**

| Member   | Type                          | Description                                                                                                                                                        |
| -------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `open`   | `(triggerId: string) => void` | Opens the popover and associates it with the trigger with the given id. This method should only be called in an event handler or an effect (not during rendering). |
| `close`  | `() => void`                  | Closes the popover. This method should only be called in an event handler or an effect (not during rendering).                                                     |
| `isOpen` | `boolean`                     | Whether the popover is currently open. Returns `false` while no root is attached to the handle.                                                                    |
