# Toast

A brief notification that appears in a corner of the screen and goes away on its own. Use it to confirm the outcome of an action without interrupting the user; keep anything they must act on in the page.

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

```typescript title="Import"
import { Toaster, Toast } from '@vigdx/ui/toast';
```

## Variants

Place `<vig-toaster />` once in the app shell (see the `app.ts` tab; this site already does). Then inject the `Toast` service anywhere and call `success()`, `info()`, `warning()` or `error()` with a message and an optional title.

```typescript title="toast-variants.ts"
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Toast } from '@vigdx/ui/toast';

@Component({
  selector: 'app-toast-variants',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap gap-3">
      <vig-button
        label="Success"
        variant="success"
        (click)="toast.success('Your changes were saved.', 'Saved')"
      />
      <vig-button label="Info" variant="info" (click)="toast.info('A new version is available.')" />
      <vig-button
        label="Warning"
        variant="warning"
        (click)="toast.warning('Your session expires in 2 minutes.', 'Heads up')"
      />
      <vig-button
        label="Error"
        variant="error"
        (click)="toast.error('The server did not respond. Try again.', 'Save failed')"
      />
    </div>
  `,
})
export class ToastVariantsDemo {
  protected readonly toast = inject(Toast);
}
```

```typescript title="app.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { Toaster } from '@vigdx/ui/toast';

/** The app shell: the host for every toast, placed once. */
@Component({
  selector: 'app-root',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RouterOutlet, Toaster],
  template: `
    <router-outlet />
    <vig-toaster position="top-end" />
  `,
})
export class App {}
```

## Duration

`show()` takes all options. `duration` is in milliseconds (default 5000); `0` keeps the toast until it is dismissed. Hovering a toast pauses its timer, and `clear()` removes every toast.

```typescript title="toast-duration.ts"
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Toast } from '@vigdx/ui/toast';

@Component({
  selector: 'app-toast-duration',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap gap-3">
      <vig-button label="Short (2 s)" variant="secondary" (click)="short()" />
      <vig-button label="Sticky" variant="secondary" (click)="sticky()" />
      <vig-button label="Clear all" appearance="text" variant="secondary" (click)="toast.clear()" />
    </div>
  `,
})
export class ToastDurationDemo {
  protected readonly toast = inject(Toast);

  protected short(): void {
    this.toast.show({
      message: 'Link copied to the clipboard.',
      variant: 'success',
      duration: 2000,
    });
  }

  // duration 0 keeps the toast until the user dismisses it.
  protected sticky(): void {
    this.toast.show({
      title: 'Import finished',
      message: '1,204 contacts imported, 3 skipped.',
      duration: 0,
    });
  }
}
```

## Dismiss from code

`show()` and the shortcuts return the toast's id; pass it to `dismiss()`, e.g. to replace a progress toast with the result. `closable: false` hides the close button.

```typescript title="toast-dismiss.ts"
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Toast } from '@vigdx/ui/toast';

@Component({
  selector: 'app-toast-dismiss',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap gap-3">
      <vig-button label="Start sync" [disabled]="syncing() !== null" (click)="start()" />
      <vig-button
        label="Finish sync"
        variant="secondary"
        [disabled]="syncing() === null"
        (click)="finish()"
      />
    </div>
  `,
})
export class ToastDismissDemo {
  private readonly toast = inject(Toast);
  /** Id of the progress toast while a sync runs. */
  protected readonly syncing = signal<number | null>(null);

  protected start(): void {
    const id = this.toast.show({
      message: 'Syncing your calendar…',
      duration: 0,
      closable: false,
    });
    this.syncing.set(id);
  }

  protected finish(): void {
    const id = this.syncing();
    if (id === null) return;
    this.toast.dismiss(id);
    this.syncing.set(null);
    this.toast.success('Calendar is up to date.');
  }
}
```

## 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="toaster"][data-part="region"] { … }`: 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="toaster"]`

| Part | Element | data-state |
| --- | --- | --- |
| `region` | The labelled region holding the stack. |   |
| `toast` | One toast card around its `vig-message`. |   |

| Attribute | Meaning |
| --- | --- |
| `data-toast-id` | On each toast card: the id `Toast.show()` returned, unique while it is shown. |

## Accessibility

- Toasts live in a `role="region"` landmark named by `ariaLabel` (localized “Notifications” by default), so users can find them again.
- Each toast is announced by the toaster itself, title and message together: `error` and `warning` assertively and after their severity (“Error: …”, translatable with `errorLabel` and `warningLabel`), the other variants politely. It uses `ariaNotify()` where the browser has it, otherwise its own live log, which is in the page from the start and only ever gains entries, so toasts arriving together are all read (a message inserted together with its live role is not announced reliably). A toast queued behind a modal dialog is announced once the dialog closes.
- Toasts never take focus. Hovering or keyboard focus inside a toast pauses its timer; for messages users need time to read or act on, use `duration: 0` or keep the information in the page.
- Dismissing the focused toast moves focus to the next toast, or back to where it was before entering the notifications.
- While a modal dialog or drawer is open the page behind it is inert, so toasts raised then wait and every timer stands still; they appear once the last modal closes.
- Close buttons are named by `closeAriaLabel` on the toaster (localized “Dismiss” by default).

## API

### Toaster

Selector: `vig-toaster` · Kind: component

Host for the Toast service — place once in the app shell. Each toast renders as a vig-message on an opaque card. A message inserted together with its live role is not announced reliably, so the toaster announces each new toast itself, errors and warnings assertively and after their severity (`errorLabel`, `warningLabel`: "Error: …"), the rest politely: through `ariaNotify()` where supported, otherwise by adding an entry to its own live log, which is in the page from the start. Entries are added, never replaced, so toasts that arrive together are all read, and the log is apart from the shared announcer that form errors use. Hover and keyboard focus pause a toast; dismissing the focused toast moves focus to the next one, or back to where it was before entering the region. A stack taller than the viewport scrolls, and the region then joins the tab order so keyboard users can scroll it. Each toast card carries `data-toast-id`, unique while it is shown. data-toast-id

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `ariaLabel` | `string \| undefined` | `undefined` | — |
| `closeAriaLabel` | `string \| undefined` | `undefined` | — |
| `errorLabel` | `string \| undefined` | `undefined` | Spoken before error toasts (i18n); defaults to `toastErrorLabel`. |
| `position` | `ToasterPosition` | `'top-right'` | Where the stack sits. The default is the physical top right corner (as in 2.0, until 3.0); prefer `top-end`, which follows the text direction. |
| `warningLabel` | `string \| undefined` | `undefined` | Spoken before warning toasts (i18n); defaults to `toastWarningLabel`. |

### Toast

Kind: injectable

Imperative notifications (PrimeNG MessageService territory): place `<vig-toaster />` once in the app shell, then `toast.success('Saved')`. Hovering or focusing a toast pauses its timer; leaving restarts the full delay.

While a modal vig-dialog / vig-drawer is open the page behind it is inert, so a toast there could be neither read nor dismissed: new toasts queue up and every timer stands still until the last modal closes.

#### Methods

| Signature | Description |
| --- | --- |
| `clear(): void` | — |
| `dismiss(id: number): void` | — |
| `error(message: string, title?: string): number` | — |
| `info(message: string, title?: string): number` | — |
| `pause(id: number): void` | Stops the auto-dismiss timer (used while hovered or focused). |
| `resume(id: number): void` | Restarts the full auto-dismiss delay (deferred while a modal is open). |
| `show(options: ToastOptions): number` | — |
| `success(message: string, title?: string): number` | — |
| `warning(message: string, title?: string): number` | — |
