2.0.1 • Published 6 years ago

ngx-snackbar v2.0.1

Weekly downloads
370
License
MIT
Repository
github
Last release
6 years ago

ngx-snackbar

Angular 7 component for Snackbar (AKA Toast) notifications.

Demo

Installation

npm install --save ngx-snackbar

Usage

Import default styles

Import styles.css into your app. This step is optional, feel free to theme the snackbars to your liking.

index.html

<link rel="stylesheet" href="node_modules/ngx-snackbar/bundles/style.css">

Angular CLI

.angular-cli.json

...
  "styles": [
   "styles.css",
   "../node_modules/ngx-snackbar/bundles/style.css"
  ],
...

Import SnackbarModule

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {SnackbarModule} from 'ngx-snackbar';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    SnackbarModule.forRoot()
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

SystemJS

System.config({
  map: {
    'ngx-snackbar': 'node_modules/ngx-snackbar/bundles/ngx-snackbar.umd.js'
  }
});

Place the ngx-snackbar tag on your template

<ngx-snackbar></ngx-snackbar>

Options

Use these properties to customize the snackbar component.

NameDescriptionTypeDefaultOptional
positionThe position where the snackbar appearstop-left, top-center, top-right, bottom-left, bottom-center, bottom-rightbottom-righttrue
maxThe maximum number of snackbars allowed on screennumber(Infinite)true
timeoutNumber of milliseconds before the snackbar closesnumber(Infinite)true
colorText color in hexstringautotrue
backgroundBackground color in hexstring#343434true
customClassCustom class to append to the snackbarstringtrue
accentAction button color. Requires action.textstring#2196f3true

Events

NameDescriptionReturn
onAddCallback gets triggered on snackbar addObject
onRemoveCallback gets triggered on snackbar removeObject
onClearCallback gets triggered on snackbar clearboolean
Object: add method options plus id string.

Use the SnackbarService to control snackbars

Import SnakckbarService from ngx-snackbar:

import {Component} from '@angular/core';
import {SnackbarService} from 'ngx-snackbar';

@Component({
  selector: 'app-root',
  template: `
    <ngx-snackbar></ngx-snackbar>
  `
})
export class AppComponent {
  constructor(private snackbarService: SnackbarService) {}
}

Methods

  • add(options: Object)

All options will override global values set on ngx-snackbar.

NameDescriptionTypeDefaultOptional
msgMessage to display in the snackbar (HTML accepted)stringfalse
timeoutNumber of milliseconds before the snackbar closesnumber(Infinite)true
colorText color in hexstringautotrue
backgroundBackground color in hexstring#343434true
customClassCustom class to append to the snackbarstringtrue
action.textAction button text. Snackbar will automatically dismiss on clickstringtrue
action.colorAction button color. Requires action.textstring#2196f3true
action.onClickAction button callback. Requires action.textFunctiontrue
onAddCallback gets triggered on snackbar addFunctiontrue
onRemoveCallback gets triggered on snackbar removeFunctiontrue
  • remove(id: string)

Remove snackbar on screen by ID.

  • clear()

Clears all snackbars on screen.

Example

import {Component} from '@angular/core';
import {SnackbarService} from 'ngx-snackbar';

@Component({
  selector: 'app-root',
  template: `
      <button (click)="add()">Add Snackbar</button>
      <br>
      <button (click)="clear()">Clear</button>
      
      <ngx-snackbar [position]="'bottom-center'" [max]="3"></ngx-snackbar>
  `
})
export class AppComponent {
  constructor(private snackbarService: SnackbarService) {
  }

  add() {
    const _this = this;
    this.snackbarService.add({
      msg: '<strong>Message sent.</strong>',
      timeout: 5000,
      action: {
        text: 'Undo',
        onClick: (snack) => {
          console.log('dismissed: ' + snack.id);
          
          _this.undo();
        },
      },
      onAdd: (snack) => {
        console.log('added: ' + snack.id);
      },
      onRemove: (snack) => {
        console.log('removed: ' + snack.id);
      }
    });
  }

  clear() {
    this.snackbarService.clear();
  }
  
  undo() {
    ...
  }
}

Credits

Thanks angular-library-starter for the compilation scripts.

License

MIT

2.0.1

6 years ago

1.2.0-rc.2

6 years ago

1.2.0-rc.1

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0-rc.2

6 years ago

1.1.0-rc.1

6 years ago

1.0.0-rc.5

6 years ago

1.0.0-rc.4

6 years ago

1.0.0-rc.3

6 years ago

1.0.0-rc.2

6 years ago

1.0.0-rc.1

6 years ago

1.1.0

6 years ago

3.0.0

6 years ago

2.0.0

6 years ago

1.0.0

6 years ago