Tableau UI for Angular

Snackbar

A snack shows a short message above the page, at the top or the bottom of the window, for a while or until the user closes it.

Import

Inject SnackService from tableau-ui-angular/snack: one for the app, with no module to import. On the server it shows nothing.

MethodWhat the snack shows
openSnack(message)A message
openSnackWithAction(message, label, action)A message and a button that runs the action
openSnackFromTemplate(template, context)A template, with its context
openSnackComponent(component, data)A component, with its data

Each then takes the duration in milliseconds, 5000 by default and 0 for until the user closes it; the type, info, error or success; and the location, top or bottom. A timed snack waits while the pointer or focus is on it, and screen readers announce each one. While a modal is open the snacks sit in it, above its content, and they go back to the page when it closes; the dialogs page's Overlays in a modal shows one. Each method returns a SnackRef: close() closes the snack, and closed$ gives its result.

A message

import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { SnackService } from 'tableau-ui-angular/snack';

@Component({
  selector: 'app-snack-message-example',
  imports: [TableauUiButtonModule, TableauUiCheckboxModule],
  templateUrl: './snack-message.example.html',
  styleUrl: './snack-message.example.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SnackMessageExampleComponent {
  private readonly snacks = inject(SnackService);

  protected readonly $bottom = signal(false);
  protected readonly $untilClosed = signal(false);

  // A duration of 0 keeps the snack until the user closes it
  protected open(type: 'error' | 'info' | 'success') {
    const duration = this.$untilClosed() ? 0 : 5000;
    this.snacks.openSnack(`A snack of type ${type}`, duration, type, this.$bottom() ? 'bottom' : 'top');
  }
}

An action

Running the action doesn't close the snack: the action closes it, as Undo does here. The duration still applies.

import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import type { SnackRef } from 'tableau-ui-angular/snack';
import { SnackService } from 'tableau-ui-angular/snack';

@Component({
  selector: 'app-snack-action-example',
  imports: [TableauUiButtonModule],
  templateUrl: './snack-action.example.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SnackActionExampleComponent {
  private readonly snacks = inject(SnackService);

  protected delete() {
    this.snacks.openSnackWithAction(
      'Deleted the report',
      'Undo',
      snack => {
        this.undo(snack);
      },
      5000,
      'success',
    );
  }

  // Running the action doesn't close the snack: the action closes it, as here. The duration still applies
  private undo(snack: SnackRef<boolean>) {
    snack.close();
    this.snacks.openSnack('Restored the report', 3000, 'info');
  }
}

A template

The context is the template's, which its let- variables read.

import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import type { TemplateRef } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { SnackService } from 'tableau-ui-angular/snack';

interface Upload {
  file: string;
}

@Component({
  selector: 'app-snack-template-example',
  imports: [TableauUiButtonModule, TableauUiCheckboxModule],
  templateUrl: './snack-template.example.html',
  styleUrl: './snack-template.example.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SnackTemplateExampleComponent {
  private readonly snacks = inject(SnackService);

  // The template and its context, which its let- variables read
  protected open(template: TemplateRef<Upload>, type: 'error' | 'info' | 'success') {
    this.snacks.openSnackFromTemplate(template, { file: `report-${type}.csv` }, 5000, type);
  }
}

A component

The component reads its data with injectSnackData(), and closes the snack with a result through injectSnackRef().

No snack closed yet
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { injectSnackData, injectSnackRef, SnackService } from 'tableau-ui-angular/snack';

interface Invite {
  team: string;
}

// The snack's content: any component, which gets its data and its snack through inject
@Component({
  selector: 'app-invite-snack',
  imports: [TableauUiButtonModule],
  template: `
    <p>You are invited to {{ invite.team }}.</p>
    <button color="primary" (click)="snack.close('Joined')">Join</button>
  `,
  styles: `
    :host {
      display: block;
      padding: 0.5rem 1rem;
    }

    p {
      margin: 0 0 0.5rem;
    }
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class InviteSnackComponent {
  protected readonly snack = injectSnackRef<string>();
  protected readonly invite = injectSnackData<Invite>();
}

@Component({
  selector: 'app-snack-component-example',
  imports: [TableauUiButtonModule],
  templateUrl: './snack-component.example.html',
  styleUrl: './snack-component.example.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SnackComponentExampleComponent {
  private readonly snacks = inject(SnackService);

  protected readonly $result = signal('No snack closed yet');

  // closed$ gives the result the component closed with, or undefined when the time ran out
  protected open(type: 'error' | 'info' | 'success') {
    const snack = this.snacks.openSnackComponent<InviteSnackComponent, Invite, string>(InviteSnackComponent, { team: 'Sales' }, 5000, type);
    snack.closed$.subscribe(result => {
      this.$result.set(`The last snack closed with: ${result ?? 'no result'}`);
    });
  }
}

Accessibility

  • Screen readers announce each snack through a live region: a status, polite, for info and success, and an alert, assertive, for error.
  • A timed snack waits while the pointer or focus is on it, so the user can read it and reach its buttons. Its action is a button, and its close button is named Close.
  • While a modal is open, the snacks sit in it, in the browser's top layer above the page and any header the app fixes there, where they take focus and clicks while the rest of the page is inert.