# Toast notification

Toasts are short, time based notifications that slide in and out of a page. These are best to use for providing system level feedback and status.

## Usage

![Toast notification](<../images/toast-notification.png>)

### When to use

- Useful to show system or application level feedbacks and status. For example, lost network, number of items exceeded etc.
- Toasts display time based short and clear messages to user.
- Use these to give feedback based on `information`, `success`, `caution`, `warning`, `danger` and `critical`.

### Best practices

- Keep the message context-based and under two lines.
- Appearance should be on the bottom right corner.
- Make toasts last for minimum 4-5 seconds.
- Toast notifications have a fixed width.
- Do not use for critical feedbacks.
- Do not use toasts for synchronous/co-occurring tasks like showing progress.
- Success toasts should not be used for every minor level use cases.

## Design

![Toast notification specification](<../images/toast-notification-usage-construction.png>)

1. **Icon:** Informs users about the severity of a notification.
2. **Title:** Provides user with a quick overview about notification. Avoid words like `Information`, `Success`, `Caution`, `Warning`, `Danger` or `Critical`in the title, use a real subject as a label.
3. **Content:** Provides additional details of the notification.
4. **Close:** A button to manually dismiss the notification *(optional)*.
5. **Timer:** Timer visualization until the notification disappears *(with timer variant)*.

### Notification types

![Toast notification types](<../images/toast-notification-usage-types.png>)

### Notification variants

![Toast notification variations](<../images/toast-notification-usage-variations.png>)

## Code

Used to display a notification as a toast that pops up from the bottom-right corner of the screen.

### Usage

In any component the following steps need to be taken in order to display a toast notification:

- An instance of the `SiToastNotificationService` needs to get imported and instantiated in your component's `constructor`.
- A variable of type `SiToast` needs to get created.
- The `SiToastNotificationService` has a method called `showToastNotification`. Call this method, and pass the previously created `SiToast` as the argument.

> [!NOTE]
>
> **Toast notification drawer**
>
> It is not needed anymore to insert the `si-toast-notification-drawer` manually into a main template as this is now automatically appended to the `<​body​>` tag by the `SiToastNotificationService`.

See the comments below for an example component that satisfies the above criteria. Notice the `showToast` method that gets called when the *Show Toast* button is clicked.

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Component, inject, OnDestroy } from '@angular/core';
import { StatusType } from '@siemens/element-ng/common';
import { Link } from '@siemens/element-ng/link';
import { SiToastNotificationService } from '@siemens/element-ng/toast-notification';
import { LOG_EVENT } from '@siemens/live-preview';

@Component({
  selector: 'app-sample',
  templateUrl: './si-toast-notification.html',
  host: { class: 'p-5' }
})
export class SampleComponent implements OnDestroy {
  logEvent = inject(LOG_EVENT);

  private toastNotificationService = inject(SiToastNotificationService);

  ngOnDestroy(): void {
    this.hideAll();
  }

  showToast(
    state: StatusType,
    title: string,
    message: string,
    disableAutoClose?: boolean,
    disableManualClose?: boolean,
    timeout?: number,
    action?: Link
  ): void {
    const toast = this.toastNotificationService.showToastNotification({
      state,
      title,
      message,
      disableAutoClose,
      disableManualClose,
      timeout: timeout ? (navigator.webdriver ? timeout * 3 : timeout) : undefined,
      action
    });
    toast.hidden?.subscribe(() => this.logEvent('toast hidden'));
  }

  showActionableToast(state: StatusType, title: string, message: string, linkTitle: string): void {
    this.showToast(state, title, message, undefined, undefined, undefined, {
      title: linkTitle,
      action: () => this.onAction()
    });
  }

  onAction(): void {
    this.logEvent('onAction');
  }

  hideAll(): void {
    this.toastNotificationService.hideToastNotification();
  }
}
```

Template:

```html
<div class="d-flex flex-wrap gap-4 e2e-ignore">
  <button
    type="button"
    class="btn btn-secondary"
    (click)="
      showToast(
        'success',
        'Email sent',
        'Meeting minutes sent to Werner von Siemens',
        undefined,
        undefined,
        4000
      )
    "
  >
    Success toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="showToast('danger', 'Security breach', 'Lab doors opened forcefully')"
  >
    Danger toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="showToast('caution', 'Caution', 'Description of the message')"
  >
    Caution toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="showToast('critical', 'Critical', 'Description of the message')"
  >
    Critical toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="
      showToast(
        'info',
        'Window closed',
        'Window in Lab 5 closed.
    HVAC returned to normal operation'
      )
    "
  >
    Info toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="
      showToast('warning', 'Overtemperature', 'Room temperature exceeds healthy environment')
    "
  >
    Warning toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="
      showActionableToast(
        'info',
        'Window opened',
        'Window in Lab 5 opened on purpose',
        'Acknowledge'
      )
    "
  >
    Actionable toast
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="showToast('info', 'Filter dirty', 'Replace filter within 2 weeks', true)"
  >
    Manual close only
  </button>
  <button
    type="button"
    class="btn btn-secondary"
    (click)="showToast('info', 'Update installed', 'Computer restarts in 5 seconds', false, true)"
  >
    Auto close only
  </button>
  <button type="button" class="btn btn-tertiary" (click)="hideAll()"> Hide all </button>
</div>
```

### [SiToastNotificationService](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L28-L203>)

**Kind:** class

**Provided in:** `root`

```
import { SiToastNotificationService } from '@siemens/element-ng/toast-notification';
```

```
undecorated_class SiToastNotificationService implements OnDestroy
```

#### Attributes and Methods

##### activeToasts

[Source for activeToasts](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L32-L34>)

**Modifiers:** readonly

**Type:** `SiToast[]`

List of currently active toasts to see details or close them.

##### hideToastNotification

[Source for hideToastNotification](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L131-L150>)

Hide a toast notification

```
hideToastNotification(toast?: SiToast): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [toast](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L131-L131>) (optional) | `SiToast` |  | • The toast object of the toast to be hidden, can be retrieved from [activeToasts](<index.html#SiToastNotificationService.activeToasts>) and is returned by [queueToastNotification](<index.html#SiToastNotificationService.queueToastNotification>). |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### queueToastNotification

[Source for queueToastNotification](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L65-L85>)

Queue a new toast to be shown.

Returns: the toast object

```
queueToastNotification(state: StatusType, title: string, message: string, disableAutoClose?: boolean, disableManualClose?: boolean, action?: Link, translationParams?: { [key: string]: any; }): SiToast
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [state](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L66-L66>) | `StatusType` |  |  |
| [title](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L67-L67>) | `string` |  |  |
| [message](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L68-L68>) | `string` |  |  |
| [disableAutoClose](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L69-L69>) (optional) | `boolean` |  |  |
| [disableManualClose](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L70-L70>) (optional) | `boolean` |  |  |
| [action](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L71-L71>) (optional) | `Link` |  | • Passing a Link object will optionally add a clickable link to the toast which can contain an action. |
| [translationParams](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L72-L72>) (optional) | `{ [key: string]: any; }` |  |  |

**Returns:** [SiToast](<index.html#t:SiToast>)

##### showToastNotification

[Source for showToastNotification](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L91-L125>)

Show a toast notification

```
showToastNotification(toast: SiToast): SiToast
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [toast](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast-notification.service.ts#L91-L91>) | `SiToast` |  | • The toast object of the toast to be shown, can also be constructed while calling this. |

**Returns:** [SiToast](<index.html#t:SiToast>)

### Types Documentation

### [Link](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L8-L51>)

**Kind:** interface

```
import type { Link } from '@siemens/element-ng/link';
```

```
interface Link
```

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| [action](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L42-L42>) (optional) | `string \| ((param?: any) => void \| any)` | A function that will be invoked when clicking on the menu item. When passed as a string, use together with SiLinkActionService to receive events. This is meant for repetitive things in lists/tables/etc. |
| [disabled](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L50-L50>) (optional) | `boolean` | A boolean that lets the link know whether it is disabled or not. |
| [href](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L22-L22>) (optional) | `string` | Defines a href URI that the menu item will be linked to. Will be used to set the href attribute of the a element. Will only be used if link and function is not set. |
| [isActive](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L46-L46>) (optional) | `boolean` | A boolean that lets the link know whether it is active or not. It is useful when action() is executed instead of link route. |
| [link](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L32-L32>) (optional) | `string \| any[]` | Defines the link of the menu item within the application using the angular router link concept. Accepts an array of `any` as per \[routerLink API definition\][https://angular.dev/api/router/RouterLink](<https://angular.dev/api/router/RouterLink>). |
| [navigationExtras](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L36-L36>) (optional) | `NavigationExtras` | The navigation extras provide additional routing options when using the router link. |
| [target](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L27-L27>) (optional) | `string` | The target attribute specifies where to open the linked document. If no target is specified, the link will open in the current tab. |
| [title](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L12-L12>) (optional) | `TranslatableString` | The title of the menu item. Can be either the title to be displayed or the translation key. |
| [tooltip](<https://github.com/siemens/element/blob/main/projects/element-ng/link/link.model.ts#L17-L17>) (optional) | `TranslatableString` | The optional tooltip of the link. Can be either the text to be displayed or the translation key. |

### [SiToast](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L10-L22>)

**Kind:** interface

```
import type { SiToast } from '@siemens/element-ng/toast-notification';
```

```
interface SiToast
```

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| [action](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L17-L17>) (optional) | `Link` |  |
| [close](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L18-L18>) (optional) | `(() => void)` |  |
| [closeAriaLabel](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L21-L21>) (optional) | `TranslatableString` |  |
| [disableAutoClose](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L15-L15>) (optional) | `boolean` |  |
| [disableManualClose](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L14-L14>) (optional) | `boolean` |  |
| [hidden](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L20-L20>) (optional) | `Subject<void>` |  |
| [message](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L13-L13>) | `TranslatableString` |  |
| [state](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L11-L11>) | `StatusType` |  |
| [timeout](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L16-L16>) (optional) | `number` |  |
| [title](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L12-L12>) | `TranslatableString` |  |
| [translationParams](<https://github.com/siemens/element/blob/main/projects/element-ng/toast-notification/si-toast.model.ts#L19-L19>) (optional) | `{ [key: string]: any; }` |  |

### [StatusType](<https://github.com/siemens/element/blob/main/projects/element-ng/common/models/status-type.model.ts#L7-L7>)

**Kind:** type alias

```
import type { StatusType } from '@siemens/element-ng/common';
```

```
type StatusType = 'success' | 'info' | 'warning' | 'danger' | 'caution' | 'critical'
```

### [TranslatableString](<https://github.com/siemens/element/blob/main/projects/element-translate-ng/translate-types/global.types.ts#L18-L18>)

**Kind:** type alias

```
import type { TranslatableString } from '@siemens/element-translate-ng/translate-types';
```

Represents a translatable string. This can either be a translation key, e.g. `ACTIONS.EDIT` that will be automatically translated when displayed on the UI or a pre-translated string, e.g. `Edit`. Equivalent to a normal string in usage and functionality.

```
type TranslatableString = string & Translatable
```
