# Drawer

A modal panel that slides in from an edge of the screen. Use it for secondary content that needs room but should keep the page in context, such as filters, details or mobile navigation.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-drawer`, `[vigDrawerFooter]`, `ng-template[vigDrawerContent]`
- Source: [libs/ui/src/lib/organisms/drawer/drawer.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/organisms/drawer/drawer.ts)

```typescript title="Import"
import { Drawer, DrawerFooter, DrawerContent } from '@vigdx/ui/drawer';
```

## Basic

Bind `[(open)]` and give the drawer a `heading`; `position` `end` slides it in from the end edge (the right one in left-to-right text; unset, it is the physical right edge). A `vigDrawerFooter` element becomes the pinned action row. Select panels inside work thanks to `provideVigdxUi()`, as in the dialog.

```typescript title="drawer-basic.ts"
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Checkbox } from '@vigdx/ui/checkbox';
import { Drawer, DrawerFooter } from '@vigdx/ui/drawer';
import { Select, type SelectOption } from '@vigdx/ui/select';
import { ToggleSwitch } from '@vigdx/ui/toggle-switch';

@Component({
  selector: 'app-drawer-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Checkbox, Drawer, DrawerFooter, Select, ToggleSwitch],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Filters" variant="secondary" (click)="open.set(true)" />
      <span class="text-sm text-muted-foreground">{{ summary() }}</span>
    </div>

    <vig-drawer heading="Filters" [(open)]="open">
      <div class="flex flex-col gap-5">
        <vig-select label="Status" [options]="statuses" [clearable]="true" [(value)]="status" />
        <fieldset class="flex flex-col gap-2">
          <legend class="mb-2 text-sm font-medium">Payment method</legend>
          <vig-checkbox label="Card" [(value)]="card" />
          <vig-checkbox label="Bank transfer" [(value)]="transfer" />
        </fieldset>
        <vig-toggle-switch label="Only overdue invoices" [(value)]="overdue" />
      </div>
      <div vigDrawerFooter class="contents">
        <vig-button label="Reset" variant="secondary" (click)="reset()" />
        <vig-button label="Apply" (click)="open.set(false)" />
      </div>
    </vig-drawer>
  `,
})
export class DrawerBasicDemo {
  protected readonly open = signal(false);
  protected readonly status = signal<string | null>(null);
  protected readonly card = signal(true);
  protected readonly transfer = signal(true);
  protected readonly overdue = signal(false);

  protected readonly statuses: SelectOption[] = [
    { value: 'paid', label: 'Paid' },
    { value: 'pending', label: 'Pending' },
    { value: 'failed', label: 'Failed' },
  ];

  protected readonly summary = computed(() => {
    const active = [
      this.status(),
      !this.card() && 'no card',
      !this.transfer() && 'no transfer',
      this.overdue() && 'overdue',
    ].filter(Boolean);
    return active.length ? `Filters: ${active.join(', ')}` : 'No filters';
  });

  protected reset(): void {
    this.status.set(null);
    this.card.set(true);
    this.transfer.set(true);
    this.overdue.set(false);
  }
}
```

## Positions

`position` anchors the drawer to the `start`, `end`, `top` or `bottom` edge. `start` and `end` follow the text direction: in a right-to-left layout `start` is the right edge. The older `left` and `right` are deprecated: they are physical edges that never swap.

```typescript title="drawer-positions.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Drawer, type DrawerPosition } from '@vigdx/ui/drawer';

@Component({
  selector: 'app-drawer-positions',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Drawer],
  template: `
    <div class="flex flex-wrap gap-3">
      @for (edge of edges; track edge) {
        <vig-button [label]="edge" variant="secondary" (click)="show(edge)" />
      }
    </div>

    <vig-drawer
      [heading]="'Drawer at the ' + position()"
      [position]="position()"
      width="20rem"
      height="14rem"
      [(open)]="open"
    >
      <p>
        Side drawers take their size from <code>width</code>, top and bottom drawers from
        <code>height</code>. Both are capped at 90% of the viewport.
      </p>
    </vig-drawer>
  `,
})
export class DrawerPositionsDemo {
  protected readonly edges: DrawerPosition[] = ['start', 'end', 'top', 'bottom'];
  protected readonly position = signal<DrawerPosition>('end');
  protected readonly open = signal(false);

  protected show(edge: DrawerPosition): void {
    this.position.set(edge);
    this.open.set(true);
  }
}
```

## Navigation drawer

A headerless drawer needs an `ariaLabel`. `padding="none"` (formerly `[padded]="false"`) hands the body spacing to the content, e.g. a full-width navigation list. The X button stays unless `closable` is off; `closeOnBackdropClick` and `closeOnEscape` work as in the dialog.

```typescript title="drawer-navigation.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { Drawer } from '@vigdx/ui/drawer';
import { Icon } from '@vigdx/ui/icon';

@Component({
  selector: 'app-drawer-navigation',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Drawer, Icon],
  template: `
    <div>
      <vig-button label="Menu" variant="secondary" (click)="open.set(true)">
        <vig-icon vigButtonIcon name="more-horizontal" />
      </vig-button>
    </div>

    <vig-drawer
      position="start"
      width="16rem"
      ariaLabel="Main navigation"
      padding="none"
      [(open)]="open"
    >
      <nav aria-label="Main">
        <ul class="flex flex-col p-2">
          @for (link of links; track link.label) {
            <li>
              <button
                type="button"
                class="flex w-full cursor-pointer items-center gap-3 rounded-button px-3 py-2 text-start hover:bg-subtle focus:outline-hidden focus-visible:shadow-(--shadow-focus-ring)"
                [attr.aria-current]="link.label === current() ? 'page' : null"
                [class.font-semibold]="link.label === current()"
                (click)="go(link.label)"
              >
                <vig-icon [name]="link.icon" />
                {{ link.label }}
              </button>
            </li>
          }
        </ul>
      </nav>
    </vig-drawer>
  `,
})
export class DrawerNavigationDemo {
  protected readonly open = signal(false);
  protected readonly current = signal('Dashboard');
  protected readonly links = [
    { label: 'Dashboard', icon: 'home' },
    { label: 'Inbox', icon: 'inbox' },
    { label: 'Files', icon: 'file' },
    { label: 'Calendar', icon: 'calendar' },
    { label: 'Account', icon: 'user' },
  ];

  protected go(label: string): void {
    this.current.set(label);
    this.open.set(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="drawer"][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="drawer"]`

| Part | Element | data-state |
| --- | --- | --- |
| `panel` | The native `<dialog>`; style the backdrop with `::backdrop`. | `open, closed` |
| `header` | The header row: heading and close button. |   |
| `heading` | The `<h2>` of `heading`. |   |
| `close` | The close button (`closable`). |   |
| `body` | The scrolling body (`padding`). |   |
| `footer` | The `[vigDrawerFooter]` row. |   |

| 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

- Built on the native `<dialog>` with `showModal()`, like `vig-dialog`: the page behind is inert and Tab stays inside the drawer.
- The drawer is named by its `heading`; without one, pass `ariaLabel`.
- Escape, the X button (`closeAriaLabel`) and a backdrop click close it, each unless turned off; focus then returns to the element that opened it. A backdrop click counts only when the press and the release both land outside the drawer, so a text selection dragged out of it does not close it.
- Scrolling over the backdrop or past the end of the drawer does not scroll the page.
- The drawer slides in and out; when the user prefers reduced motion it fades instead.

## Lazy content

Projected content is created with the `vig-drawer`, even while it is closed. Wrap a heavy body (a form that loads data, a chart) in `<ng-template vigDrawerContent>` instead: it is created when the drawer opens and destroyed once it has closed, after the exit transition.

## API

### Drawer

Selector: `vig-drawer` · Kind: component

Edge-anchored modal panel (PrimeNG p-drawer/sidebar territory) on the native `<dialog>` element: focus trap, top-layer, Escape and focus restore come from the platform. Slides in from the chosen edge. 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` | — |
| `closable` | `boolean` | `true` | — |
| `closeAriaLabel` | `string \| undefined` | `undefined` | — |
| `closeOnBackdropClick` | `boolean \| undefined` | `undefined` | Close when the backdrop is clicked (press and release both outside the box, so dragging a text selection out never dismisses). Default `true`. |
| `closeOnEscape` | `boolean` | `true` | Close on Escape / the back gesture; off also disables backdrop dismissal natively. |
| `dismissable` | `boolean` | `true` | **Deprecated**: Use `closeOnBackdropClick`; still honored when it is unset. |
| `header` | `string \| undefined` | `undefined` | **Deprecated**: Use `heading`; still honored when `heading` is unset. |
| `heading` | `string \| undefined` | `undefined` | Visible title, which also names the panel (`aria-labelledby`). |
| `height` | `string` | `'24rem'` | CSS height of `top`/`bottom` drawers, capped at 90dvh. |
| `open` (model) | `boolean` | `false` | Two-way: drive with a signal, e.g. `[(open)]="filtersOpen"`. |
| `padded` | `boolean` | `true` | **Deprecated**: Use `padding` (`[padded]="false"` is `padding="none"`). |
| `padding` | `DrawerPadding \| undefined` | `undefined` | Inner spacing of the body: `none` lets the content own it (e.g. nav). Default `md`. |
| `position` | `DrawerPosition` | `'right'` | Edge the drawer slides in from. The default is the physical right edge (as in 2.0, until 3.0); prefer `end`, which follows the text direction. |
| `width` | `string` | `'24rem'` | CSS width of side (`start`/`end`) drawers, capped at 90vw. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `openChange` | `boolean` | Two-way: drive with a signal, e.g. `[(open)]="filtersOpen"`. |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigDrawerFooter]` | `DrawerFooter` | Marks the footer (action row) content of a vig-drawer. |
| `ng-template[vigDrawerContent]` | `DrawerContent` | Body content of a vig-drawer created only while it is shown: when it opens, and destroyed once it has closed (after the exit transition). Projected content, by contrast, lives as long as the vig-drawer does. **Example :** `<vig-drawer [(open)]="editing" heading="Edit"> <ng-template vigDrawerContent><app-editor /></ng-template> </vig-drawer>` |
