# Popover

A non-modal panel anchored to a trigger, for content that needs more than a tooltip: an explanation with a link, a small form or a preview. The rest of the page stays usable while it is open.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-popover`, `[vigPopoverTrigger]`, `[vigPopoverContent]`
- Source: [libs/ui/src/lib/organisms/popover/popover.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/organisms/popover/popover.ts)

```typescript title="Import"
import { Popover, PopoverTrigger, PopoverContent } from '@vigdx/ui/popover';
```

## Basic

Mark the trigger with `vigPopoverTrigger` and put the panel in an `ng-template vigPopoverContent`, which is created only while the popover is open. Name the panel with `ariaLabel`; otherwise the trigger labels it.

```typescript title="popover-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { Icon } from '@vigdx/ui/icon';
import { Link } from '@vigdx/ui/link';
import { Popover, PopoverContent, PopoverTrigger } from '@vigdx/ui/popover';

@Component({
  selector: 'app-popover-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Icon, Link, Popover, PopoverContent, PopoverTrigger],
  template: `
    <div class="flex items-center gap-1 text-sm">
      Monthly active users
      <vig-popover ariaLabel="About monthly active users">
        <vig-button
          vigPopoverTrigger
          appearance="text"
          variant="secondary"
          size="xs"
          ariaLabel="About monthly active users"
        >
          <vig-icon vigButtonIcon name="info" />
        </vig-button>
        <ng-template vigPopoverContent>
          <div class="flex max-w-64 flex-col gap-2">
            <p class="font-semibold">Monthly active users</p>
            <p class="text-foreground-soft">
              People who signed in at least once in the last 30 days. Service accounts are not
              counted.
            </p>
            <vig-link href="/docs/getting-started">How we measure</vig-link>
          </div>
        </ng-template>
      </vig-popover>
    </div>
  `,
})
export class PopoverBasicDemo {}
```

## Form content

Any content works, including form fields. Call `close()` on a template reference to close the popover from inside — it also returns focus to the trigger.

```typescript title="popover-form.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { InputField } from '@vigdx/ui/input-field';
import { Popover, PopoverContent, PopoverTrigger } from '@vigdx/ui/popover';
import { ToggleSwitch } from '@vigdx/ui/toggle-switch';

@Component({
  selector: 'app-popover-form',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, InputField, Popover, PopoverContent, PopoverTrigger, ToggleSwitch],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-popover #popover ariaLabel="Share document">
        <vig-button vigPopoverTrigger label="Share" />
        <ng-template vigPopoverContent>
          <form class="flex w-72 flex-col gap-3" (submit)="share($event, popover)">
            <vig-input label="Email" type="email" [(value)]="email" />
            <vig-toggle-switch label="Allow editing" [(value)]="canEdit" />
            <div class="flex justify-end gap-2">
              <vig-button label="Cancel" variant="secondary" size="sm" (click)="popover.close()" />
              <vig-button label="Send" type="submit" size="sm" [disabled]="!email()" />
            </div>
          </form>
        </ng-template>
      </vig-popover>
      <span class="text-sm text-muted-foreground" aria-live="polite">{{ sharedWith() }}</span>
    </div>
  `,
})
export class PopoverFormDemo {
  protected readonly email = signal('');
  protected readonly canEdit = signal(false);
  protected readonly sharedWith = signal('');

  protected share(event: Event, popover: Popover): void {
    event.preventDefault();
    const access = this.canEdit() ? 'can edit' : 'can view';
    this.sharedWith.set(`Shared with ${this.email()} (${access})`);
    this.email.set('');
    // close() also returns focus to the trigger.
    popover.close();
  }
}
```

## Open state

Two-way bind `[(open)]` to read whether the popover is open or to close it from code. The panel opens below the trigger and flips above it when there is no room.

```typescript title="popover-controlled.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';
import { Button } from '@vigdx/ui/button';
import { Popover, PopoverContent, PopoverTrigger } from '@vigdx/ui/popover';

@Component({
  selector: 'app-popover-controlled',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar, Button, Popover, PopoverContent, PopoverTrigger],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-popover [(open)]="open">
        <vig-button vigPopoverTrigger label="Ada Lovelace" appearance="text" variant="secondary" />
        <ng-template vigPopoverContent>
          <div class="flex w-64 items-center gap-3">
            <vig-avatar name="Ada Lovelace" />
            <div class="flex flex-col">
              <span class="font-semibold">Ada Lovelace</span>
              <span class="text-muted-foreground">Analyst · London</span>
            </div>
          </div>
        </ng-template>
      </vig-popover>
      <span class="text-sm text-muted-foreground">Open: {{ open() }}</span>
    </div>
  `,
})
export class PopoverControlledDemo {
  protected readonly open = signal(false);
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="popover"][data-part="panel"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="popover"]`

| Part | Element | data-state |
| --- | --- | --- |
| `panel` | The `role="dialog"` panel, rendered in the overlay container. |   |

| Attribute | Meaning |
| --- | --- |
| `data-opening` | On the overlay element (panel or `<dialog>`) while its entrance transition plays; removed once it settles (2s at most). Style the entrance with `data-opening:` utilities. |
| `data-closing` | On the overlay element while its exit transition plays, before it closes; removed once it settles (2s at most). Style the closed look with `data-closing:` utilities. |

## Accessibility

- The trigger gets `aria-haspopup="dialog"`, `aria-expanded` and, while open, `aria-controls`; the panel is a non-modal `role="dialog"`.
- Opening moves focus onto the panel, so screen readers announce it and Tab reaches its content next. Focus is not trapped.
- Escape closes the panel and returns focus to the trigger. Clicking outside or tabbing out of the panel closes it as well: the panel renders at the end of the page, so Tab past its last control continues from the trigger and Shift+Tab before its first control returns to the trigger.
- Opening or closing through a bound `[(open)]` manages focus the same way, and focus inside a panel that closes for any reason (scrolling, a click on a blank area) goes back to the trigger instead of being lost.
- Use a real button as the trigger (a `vig-button` or native `<button>`), and give icon-only triggers an `ariaLabel`.

## API

### Popover

Selector: `vig-popover` · Kind: component

Anchored non-modal panel for arbitrary content (PrimeNG p-popover / OverlayPanel territory). The content template renders only while open; focus moves onto the panel, Escape and outside interactions close it, and Tab past either end of the panel continues from the trigger. Driving `open` from outside behaves the same as the trigger.

**Example :**

```html
<vig-popover ariaLabel="Filters">
  <vig-button vigPopoverTrigger label="Filters" />
  <ng-template vigPopoverContent>…</ng-template>
</vig-popover>
```

While it plays its entrance or exit transition, the animating element carries `data-opening` or `data-closing` (stable hooks for styling and for waiting in tests; see the theming guide). data-opening data-closing

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `ariaLabel` | `string \| undefined` | `undefined` | — |
| `open` (model) | `boolean` | `false` | Two-way; also driven by the trigger. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `openChange` | `boolean` | Two-way; also driven by the trigger. |

#### Methods

| Signature | Description |
| --- | --- |
| `close(refocus = true): void` | — |
| `openPanel(): void` | — |
| `toggle(): void` | — |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigPopoverTrigger]` | `PopoverTrigger` | Wires a projected element as the popover trigger. Works on a native button or on a component wrapping one (e.g. vig-button): the ARIA state lands on the actual interactive element. |
| `[vigPopoverContent]` | `PopoverContent` | Marks the lazily instantiated panel content of a vig-popover. |
