|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Directionality} from '@angular/cdk/bidi'; |
| 10 | +import {Component, TemplateRef, ViewChild, ViewEncapsulation} from '@angular/core'; |
| 11 | +import {MatSnackBar} from '@angular/material-experimental/mdc-snack-bar'; |
| 12 | +import { |
| 13 | + MatSnackBarConfig, |
| 14 | + MatSnackBarHorizontalPosition, |
| 15 | + MatSnackBarVerticalPosition |
| 16 | +} from '@angular/material/snack-bar'; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + selector: 'mdc-snack-bar-demo', |
| 20 | + templateUrl: 'mdc-snack-bar-demo.html', |
| 21 | + styleUrls: ['mdc-snack-bar-demo.css'], |
| 22 | + encapsulation: ViewEncapsulation.None, |
| 23 | +}) |
| 24 | +export class MdcSnackBarDemo { |
| 25 | + |
| 26 | + @ViewChild('template') template: TemplateRef<any>; |
| 27 | + message = 'Snack Bar opened.'; |
| 28 | + actionButtonLabel = 'Retry'; |
| 29 | + action = false; |
| 30 | + setAutoHide = true; |
| 31 | + autoHide = 10000; |
| 32 | + addExtraClass = false; |
| 33 | + horizontalPosition: MatSnackBarHorizontalPosition = 'center'; |
| 34 | + verticalPosition: MatSnackBarVerticalPosition = 'bottom'; |
| 35 | + |
| 36 | + constructor(public snackBar: MatSnackBar, private _dir: Directionality) {} |
| 37 | + |
| 38 | + open() { |
| 39 | + const config = this._createConfig(); |
| 40 | + this.snackBar.open(this.message, this.action ? this.actionButtonLabel : undefined, config); |
| 41 | + } |
| 42 | + |
| 43 | + openTemplate() { |
| 44 | + const config = this._createConfig(); |
| 45 | + this.snackBar.openFromTemplate(this.template, config); |
| 46 | + } |
| 47 | + |
| 48 | + private _createConfig() { |
| 49 | + const config = new MatSnackBarConfig(); |
| 50 | + config.verticalPosition = this.verticalPosition; |
| 51 | + config.horizontalPosition = this.horizontalPosition; |
| 52 | + config.duration = this.setAutoHide ? this.autoHide : 0; |
| 53 | + config.panelClass = this.addExtraClass ? ['demo-party'] : undefined; |
| 54 | + config.direction = this._dir.value; |
| 55 | + return config; |
| 56 | + } |
| 57 | +} |
0 commit comments