ToastToast is used to display messages in an overlay.

Toast - 图1

Documentation

Import

  1. import {ToastModule} from 'primeng/toast';
  2.  

Getting Started

A single message is specified by the Message interface having the following properties.

NameTypeDefaultDescription
severitystringnullSeverity level of the message, valid values are "success", "info", "warn" and "error".
summarystringnullSummary text of the message.
detailstringnullDetail text of the message.
idanynullIdentifier of the message.
keystringnullKey of the message in case message is targeted to a specific toast component.
lifenumber3000Number of time in milliseconds to wait before closing the message.
stickybooleanfalseWhether the message should be automatically closed based on life property or kept visible.
closablebooleantrueWhen enabled, displays a close icon to hide a message manually.
dataanynullArbitrary object to associate with the message.

A message is displayed using a MessageService, make sure your component has an injectable MessageService defined as a provider otherwise Toast cannot be utilized.

  1. <p-toast></p-toast>
  2.  
  1. import {Component} from '@angular/core';
  2. import {MessageService} from 'primeng/api';
  3. @Component({
  4. templateUrl: './my.component.html'
  5. })
  6. export class MyComponent {
  7. constructor(private messageService: MessageService) {}
  8. addSingle() {
  9. this.messageService.add({severity:'success', summary:'Service Message', detail:'Via MessageService'});
  10. }
  11. addMultiple() {
  12. this.messageService.addAll([{severity:'success', summary:'Service Message', detail:'Via MessageService'},
  13. {severity:'info', summary:'Info Message', detail:'Via MessageService'}]);
  14. }
  15. clear() {
  16. this.messageService.clear();
  17. }
  18. }
  19.  

Positions

Toast supports various positions where default is "top-right".

  1. <p-toast position="top-left"></p-toast>
  2.  

Valid values of the position property would be;

  • top-right
  • top-left
  • bottom-right
  • bottom-left
  • top-center
  • bottom-center
  • center

Targeting Messages

A page may have multiple toast components, in case you'd like to target a specific message to a particular toast, use the key property so that toast and the message can match.

  1. <p-toast key="myKey1"></p-toast>
  2. <p-toast key="myKey2"></p-toast>
  3.  
  1. this.messageService.add({key: 'myKey1': severity:'success', summary: 'Summary Text', detail: 'Detail Text'});
  2. this.messageService.add({key: 'myKey2': severity:'success', summary: 'Summary Text', detail: 'Detail Text'});
  3.  

Clearing Messages

Clicking the close icon on the message, removes it manually. Same can also be achieved programmatically using the clear function of the message service. Calling it without any arguments, removes all displayed messages whereas calling it with a key, removes the messages displayed on a toast having the same key.

  1. <p-toast key="myKey1"></p-toast>
  2. <p-toast key="myKey2"></p-toast>
  3.  
  1. this.messageService.clear(); //clears messages of both toast components
  2. this.messageService.clear('myKey1'); //clears messages of the first toast only
  3.  

Templating

Templating allows customizing the content where the message instance is available as the implicit variable.

  1. <p-toast position="center" key="c" (onClose)="onReject()" [modal]="true" [baseZIndex]="5000">
  2. <ng-template let-message pTemplate="message">
  3. <div style="text-align: center">
  4. <i class="pi pi-exclamation-triangle" style="font-size: 3em"></i>
  5. <h3>{{message.summary}}</h3>
  6. <p>{{message.detail}}</p>
  7. </div>
  8. <div class="ui-g ui-fluid">
  9. <div class="ui-g-6">
  10. <button type="button" pButton (click)="onConfirm()" label="Yes" class="ui-button-success"></button>
  11. </div>
  12. <div class="ui-g-6">
  13. <button type="button" pButton (click)="onReject()" label="No" class="ui-button-secondary"></button>
  14. </div>
  15. </div>
  16. </ng-template>
  17. </p-toast>
  18.  

Animation Configuration

Transition of the animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-toast [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'"></p-toast>
  2.  

Properties

NameTypeDefaultDescription
keystringnullKey to match the key of a message to display.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
positionstringtop-rightPosition of the component, valid values are "top-right", "top-left", "bottom-left", "bottom-right", "top-center, "bottom-center" and "center".
modalbooleanfalseDefines whether background should be blocked when a message is displayed.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
showTransitionOptionsstring300ms ease-outTransition options of the show animation.
hideTransitionOptionsstring250ms ease-inTransition options of the hide animation.

Events

NameParametersDescription
onCloseevent.message: Removed messageCallback to invoke when a message is closed.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-toastMain container element.
ui-toast-top-rightMain container element positioned at the top right of the screen.
ui-toast-top-leftMain container element positioned at the top right of the screen.
ui-toast-bottom-rightMain container element positioned at the top right of the screen.
ui-toast-bottom-leftMain container element positioned at the top right of the screen.
ui-toast-top-centerMain container element positioned at the top center of the screen.
ui-toast-bottom-centerMain container element positioned at the bottom center of the screen.
ui-toast-centerMain container element positioned at the center of the screen.
ui-toast-messageElement of a message.
ui-toast-message-infoElement of an info message.
ui-toast-message-successElement of an success message.
ui-toast-message-warnElement of an warn message.
ui-toast-message-errorElement of an error message.
ui-toast-message-contentContent element of a message element.
ui-toast-close-iconIcon to close a message.
ui-toast-message-text-contentContent element of the message text.

Dependencies

None.

Source

View on GitHub

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <p-toast [style]="{marginTop: '80px'}" position="top-left" key="tl"></p-toast>
  3. <p-toast [style]="{marginTop: '80px'}" position="top-center" key="tc"></p-toast>
  4. <p-toast [style]="{marginTop: '80px'}" styleClass="custom-toast" key="custom" position="bottom-center"></p-toast>
  5. <p-toast position="center" key="c" (onClose)="onReject()" [modal]="true" [baseZIndex]="5000">
  6. <ng-template let-message pTemplate="message">
  7. <div style="text-align: center">
  8. <i class="pi pi-exclamation-triangle" style="font-size: 3em"></i>
  9. <h3>{{message.summary}}</h3>
  10. <p>{{message.detail}}</p>
  11. </div>
  12. <div class="ui-g ui-fluid">
  13. <div class="ui-g-6">
  14. <button type="button" pButton (click)="onConfirm()" label="Yes" class="ui-button-success"></button>
  15. </div>
  16. <div class="ui-g-6">
  17. <button type="button" pButton (click)="onReject()" label="No" class="ui-button-secondary"></button>
  18. </div>
  19. </div>
  20. </ng-template>
  21. </p-toast>
  22. <h3 class="first">Severities</h3>
  23. <button type="button" pButton (click)="showSuccess()" label="Success" class="ui-button-success"></button>
  24. <button type="button" pButton (click)="showInfo()" label="Info" class="ui-button-info"></button>
  25. <button type="button" pButton (click)="showWarn()" label="Warn" class="ui-button-warning"></button>
  26. <button type="button" pButton (click)="showError()" label="Error" class="ui-button-danger"></button>
  27. <h3>Multiple</h3>
  28. <button type="button" pButton (click)="showMultiple()" label="Multiple"></button>
  29. <h3>Clear</h3>
  30. <button type="button" pButton (click)="clear()" icon="pi pi-times" label="Remove All"></button>
  31. <h3>Positions</h3>
  32. <button type="button" pButton (click)="showTopLeft()" label="Top Left" class="ui-button-success"></button>
  33. <button type="button" pButton (click)="showTopCenter()" label="Top Center" class="ui-button-info"></button>
  34. <h3>Template</h3>
  35. <button type="button" pButton (click)="showConfirm()" label="Confirm" class="ui-button-warning"></button>
  36.  
  1. @Component({
  2. templateUrl: './toastdemo.html',
  3. styles: [`
  4. :host ::ng-deep button {
  5. margin-right: .25em;
  6. }
  7. :host ::ng-deep .custom-toast .ui-toast-message {
  8. color: #ffffff;
  9. background: #FC466B;
  10. background: -webkit-linear-gradient(to right, #3F5EFB, #FC466B);
  11. background: linear-gradient(to right, #3F5EFB, #FC466B);
  12. }
  13. :host ::ng-deep .custom-toast .ui-toast-close-icon {
  14. color: #ffffff;
  15. }
  16. `],
  17. providers: [MessageService]
  18. })
  19. export class ToastDemo {
  20. constructor(private messageService: MessageService) {}
  21. showSuccess() {
  22. this.messageService.add({severity:'success', summary: 'Success Message', detail:'Order submitted'});
  23. }
  24. showInfo() {
  25. this.messageService.add({severity:'info', summary: 'Info Message', detail:'PrimeNG rocks'});
  26. }
  27. showWarn() {
  28. this.messageService.add({severity:'warn', summary: 'Warn Message', detail:'There are unsaved changes'});
  29. }
  30. showError() {
  31. this.messageService.add({severity:'error', summary: 'Error Message', detail:'Validation failed'});
  32. }
  33. showCustom() {
  34. this.messageService.add({key: 'custom', severity:'info', summary: 'Custom Toast', detail:'With a Gradient'});
  35. }
  36. showTopLeft() {
  37. this.messageService.add({key: 'tl', severity:'info', summary: 'Success Message', detail:'Order submitted'});
  38. }
  39. showTopCenter() {
  40. this.messageService.add({key: 'tc', severity:'warn', summary: 'Info Message', detail:'PrimeNG rocks'});
  41. }
  42. showConfirm() {
  43. this.messageService.clear();
  44. this.messageService.add({key: 'c', sticky: true, severity:'warn', summary:'Are you sure?', detail:'Confirm to proceed'});
  45. }
  46. showMultiple() {
  47. this.messageService.addAll([
  48. {severity:'info', summary:'Message 1', detail:'PrimeNG rocks'},
  49. {severity:'info', summary:'Message 2', detail:'PrimeUI rocks'},
  50. {severity:'info', summary:'Message 3', detail:'PrimeFaces rocks'}
  51. ]);
  52. }
  53. onConfirm() {
  54. this.messageService.clear('c');
  55. }
  56. onReject() {
  57. this.messageService.clear('c');
  58. }
  59. clear() {
  60. this.messageService.clear();
  61. }
  62. }
  63.