Angular Snackbar (スナックバー) コンポーネントの概要
Ignite UI for Angular Snackbar コンポーネントは、アクションを含むことができる単一行のメッセージで操作のフィードバックを提供します。Snackbar メッセージがその他の画面要素の上に表示され、画面の中央下に配置されます。
Angular Snackbar の例
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxButtonModule,
IgxRippleModule,
IgxSnackbarModule
} from "igniteui-angular" ;
import { SnackbarSample1Component } from "./snackbar-sample-1/snackbar-sample-1.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
SnackbarSample1Component
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxButtonModule,
IgxRippleModule,
IgxSnackbarModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
@Component ({
selector : 'app-snackbar-sample-1' ,
styleUrls : ['./snackbar-sample-1.component.scss' ],
templateUrl : './snackbar-sample-1.component.html'
})
export class SnackbarSample1Component { }
ts コピー <button igxButton ="raised" (click )="snackbar.open()" > Delete Message</button >
<div >
<igx-snackbar #snackbar > Message deleted</igx-snackbar >
</div >
html コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
Ignite UI for Angular Snackbar を使用した作業の開始
Ignite UI for Angular Snackbar コンポーネントを使用した作業を開始するには、Ignite UI for Angular をインストールする必要があります。既存の Angular アプリケーションで、以下のコマンドを入力します。
ng add igniteui-angular
cmd
Ignite UI for Angular については、「はじめに 」トピックをご覧ください。
次に、app.module.ts ファイルに IgxSnackbarModule
をインポートします。
...
import { IgxSnackbarModule } from 'igniteui-angular' ;
@NgModule ({
...
imports : [..., IgxSnackbarModule],
...
})
export class AppModule {}
typescript
あるいは、16.0.0
以降、IgxSnackbarComponent
をスタンドアロンの依存関係としてインポートできます。
import { IgxSnackbarComponent, IgxButtonDirective } from 'igniteui-angular' ;
@Component ({
selector : 'app-home' ,
template : `
<button igxButton="contained" (click)="snackbar.open()">Delete Message</button>
<div>
<igx-snackbar #snackbar>Message deleted</igx-snackbar>
</div>
` ,
styleUrls : ['home.component.scss' ],
standalone : true ,
imports : [IgxSnackbarComponent, IgxButtonDirective]
})
export class HomeComponent {}
typescript
Ignite UI for Angular Snackbar モジュールまたはコンポーネントをインポートしたので、igx-snackbar
コンポーネントの使用を開始できます。
Angular Snackbar の使用
Snackbar の表示
Snackbar コンポーネントを表示するには、ボタン クリックで open()
メソッドを呼び出します。
<button igxButton ="contained" (click )="snackbar.open()" > Delete Message</button >
<div >
<igx-snackbar #snackbar > Message deleted</igx-snackbar >
</div >
html
サンプルが正しく構成された場合、デモ サンプルが表示されます。ボタン クリック時にテキスト メッセージを表示する Snackbar が表示されます。
以上のコード スニペットで示されるように、スナックバーに表示されるメッセージを設定する 1 つの方法は、コンテンツ プロジェクションを使用することです。ただし、カスタム ロジックに基づいてプログラムによって値を切り替える必要がある場合は、値をパラメーターとして open()
メソッドに渡すだけです。
<button igxButton ="contained" (click )="snackbar.open('Message deleted')" > Delete Message</button >
<button igxButton ="contained" (click )="snackbar.open('Message deletion was not successful. Please try again')" > Delete Message</button >
<div >
<igx-snackbar #snackbar > </igx-snackbar >
</div >
html
非表示/自動的に隠す
開いた後は、displayTime
入力によって指定した期間遅延後に非表示になります。デフォルト値は 4000 ミリ秒です。この動作はデフォルトで有効ですが、autoHide
を false に設定して変更できます。この場合、Snackbar は非表示になりません。Snackbar の close()
メソッドを使用して、コードでコンポーネントを閉じることができます。
<button igxButton ="contained" (click )="snackbar.open()" > Send message</button >
<div >
<igx-snackbar #snackbar [autoHide ]="false" actionText ="CLOSE" (clicked )="close(snackbar)" > Message sent</igx-snackbar >
</div >
html
public close (element ) {
element.close();
}
typescript
サンプルを正しく構成した後、ボタンをクリックするとメッセージおよびアクション ボタンを含む Snackbar が表示されます。自動的に隠す機能が無効で、[CLOSE] ボタンのクリックで Snackbar が非表示になります。別のスナックバーが open()
メソッドを介して別のメッセージを渡し、表示時間 が終了すると非表示にします。3 番目のコンポーネントは、メッセージをパラメーターとして open()
メソッドに渡し、コンテンツ プロジェクションを使用してアイコンを追加します。
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxButtonModule,
IgxRippleModule,
IgxIconModule,
IgxSnackbarModule
} from "igniteui-angular" ;
import { SnackbarSample2Component } from "./snackbar-sample-2/snackbar-sample-2.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
SnackbarSample2Component
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxButtonModule,
IgxRippleModule,
IgxSnackbarModule,
IgxIconModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
@Component ({
selector : 'app-snackbar-sample-2' ,
styleUrls : ['./snackbar-sample-2.component.scss' ],
templateUrl : './snackbar-sample-2.component.html'
})
export class SnackbarSample2Component {
public close (element ) {
element.close();
}
}
ts コピー <div class ="columnWrapper" >
<span >
<h6 > Snackbar Component that uses Content projection</h6 >
<button igxButton ="raised" (click )="snackbar.open()" > Send message</button >
<igx-snackbar #snackbar [autoHide ]="false" actionText ="CLOSE" (clicked )="close(snackbar)" > Message sent</igx-snackbar >
</span >
<span >
<h6 > Snackbar Component which dynamically changes the message text</h6 >
<button igxButton ="outlined" igxButtonColor ="blue" (click )="snackbar1.open('Hi! This is info message.')" > Info Message</button >
<button igxButton ="outlined" igxButtonColor ="green" (click )="snackbar1.open('Hi! This is success message.')" > Success Message</button >
<igx-snackbar #snackbar1 [autoHide ]="true" actionText ="CLOSE" (clicked )="close(snackbar1)" > </igx-snackbar >
</span >
<span >
<h6 > Snackbar Component that uses Content Projection and dynamically changes the message text</h6 >
<button igxButton ="raised" (click )="snackbar2.open('The message was not delivered successfully. Please contact our support team.')" > Send message</button >
<igx-snackbar #snackbar2 [autoHide ]="false" actionText ="CLOSE" (clicked )="close(snackbar2)" >
<igx-icon > support_agent</igx-icon >
</igx-snackbar >
</span >
</div >
html コピー .columnWrapper {
display : flex;
flex-flow : row;
padding-top : 30px ;
}
.columnWrapper span {
min-width : 250px ;
flex : 1 0 0px ;
justify-content : center;
align-items : center;
align-content : center;
text-align : center;
}
button {
margin-top : 20px ;
}
scss コピー
表示時間
displayTime
でミリ秒間隔に設定し、Snackbar コンポーネントが表示される時間を設定します。デフォルトでは 4000 ミリ秒に設定されています。
Snackbar のカスタマイズ
Snackbar の内容をカスタマイズして、メッセージやボタンよりも複雑な要素を表示することもできます。たとえば、ファイルの読み込み中にスナックバーを表示したい場合は、読み込みアニメーションをそのコンテンツに追加することができます。
<button igxButton ="contained" (click )="snackbar.open()" > Load file</button >
<div >
<igx-snackbar #snackbar displayTime ="5000" > File loading
<svg id ="dots" height ="20px" >
<g id ="dots" fill ="#FFFFFF" >
<circle id ="dot1" cx ="5" cy ="18" r ="2" > </circle >
<circle id ="dot2" cx ="15" cy ="18" r ="2" > </circle >
<circle id ="dot3" cx ="25" cy ="18" r ="2" > </circle >
</g >
</svg >
</igx-snackbar >
</div >
html
#dots #dot1 {
animation : load 1s infinite;
}
#dots #dot2 {
animation : load 1s infinite;
animation-delay : 0.2s ;
}
#dots #dot3 {
animation : load 1s infinite;
animation-delay : 0.4s ;
}
@keyframes load {
0% {
opacity : 0 ;
}
50% {
opacity : 1 ;
}
100% {
opacity : 0 ;
}
}
scss
結果としてメッセージと 3 つのローディング ドットがスナックバーに表示されます。
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxButtonModule,
IgxSnackbarModule
} from "igniteui-angular" ;
import { SnackbarSample5Component } from "./snackbar-sample-5/snackbar-sample-5.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
SnackbarSample5Component
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxButtonModule,
IgxSnackbarModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
@Component ({
selector : 'app-snackbar-sample-5' ,
styleUrls : ['./snackbar-sample-5.component.scss' ],
templateUrl : './snackbar-sample-5.component.html'
})
export class SnackbarSample5Component { }
ts コピー <button igxButton ="raised" (click )="snackbar.open()" > Load file</button >
<div >
<igx-snackbar #snackbar [displayTime ]="5000" > File loading
<svg id ="dots" height ="20px" >
<g id ="dots" fill ="#999999" >
<circle id ="dot1" cx ="5" cy ="18" r ="2" > </circle >
<circle id ="dot2" cx ="15" cy ="18" r ="2" > </circle >
<circle id ="dot3" cx ="25" cy ="18" r ="2" > </circle >
</g >
</svg >
</igx-snackbar >
</div >
html コピー #dots #dot1 {
animation : load 1s infinite;
}
#dots #dot2 {
animation : load 1s infinite;
animation-delay : 0.2s ;
}
#dots #dot3 {
animation : load 1s infinite;
animation-delay : 0.4s ;
}
@keyframes load {
0% {
opacity : 0 ;
}
50% {
opacity : 1 ;
}
100% {
opacity : 0 ;
}
}
scss コピー
リストの Snackbar
Snackbar の主な機能を説明しました。次の例はより複雑なサンプルにコンポーネントを追加します。通知およびアクションの元に戻す機能を提供する Snackbar を作成します。
削除可能な連絡先のリストを作成します。項目を削除後、メッセージおよびアクションを元に戻すボタンを含む Snackbar が表示されます。
<igx-list >
<igx-list-item [isHeader ]="true" > Contacts</igx-list-item >
<igx-list-item igxRipple ="pink" igxRippleTarget =".igx-list__item" *ngFor ="let item of navItems" >
<div class ="item-container" >
<div class ="contact" >
<igx-avatar [src ]="item.avatar" shape ="circle" > </igx-avatar >
<div class ="contact__info" >
<span class ="name" > {{item.text}}</span >
</div >
</div >
<span igxIconButton ="flat" igxRipple igxRippleCentered ="true" (click )="delete(item)" >
<igx-icon [style.color ]="'#ff5252'" > delete</igx-icon >
</span >
</div >
</igx-list-item >
<igx-snackbar actionText ="Undo" (clicked )="restore()" > Contact deleted</igx-snackbar >
</igx-list >
html
import { Component, OnInit, ViewChild } from '@angular/core' ;
import { IgxSnackbarComponent } from 'igniteui-angular' ;
...
@ViewChild (IgxSnackbarComponent)
public snackbar: IgxSnackbarComponent;
public navItems: any [];
public deletedItems = [];
constructor ( ) { }
public ngOnInit ( ) {
this .navItems = [
{ avatar : 'assets/images/avatar/2.jpg' , text : 'Richard Mahoney' },
{ avatar : 'assets/images/avatar/4.jpg' , text : 'Lisa Landers' },
{ avatar : 'assets/images/avatar/14.jpg' , text : 'Marianne Taylor' },
{ avatar : 'assets/images/avatar/17.jpg' , text : 'Ward Riley' }
];
}
public delete (item ) {
this .deletedItems.push([item, this .navItems.indexOf(item)]);
this .navItems.splice(this .navItems.indexOf(item), 1 );
this .snackbar.open();
}
public restore ( ) {
const [item, index] = this .deletedItems.pop();
this .navItems.splice(index, 0 , item);
this .snackbar.close();
}
typescript
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxAvatarModule,
IgxButtonModule,
IgxIconModule,
IgxListModule,
IgxRippleModule,
IgxSnackbarModule
} from "igniteui-angular" ;
import { SnackbarSample4Component } from "./snackbar-sample-4/snackbar-sample-4.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
SnackbarSample4Component
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxAvatarModule,
IgxButtonModule,
IgxIconModule,
IgxListModule,
IgxRippleModule,
IgxSnackbarModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, OnInit, ViewChild } from '@angular/core' ;
import { IgxSnackbarComponent } from 'igniteui-angular' ;
@Component ({
selector : 'app-snackbar-sample-4' ,
styleUrls : ['./snackbar-sample-4.component.scss' ],
templateUrl : './snackbar-sample-4.component.html'
})
export class SnackbarSample4Component implements OnInit {
@ViewChild (IgxSnackbarComponent, { static : true })
public snackbar: IgxSnackbarComponent;
public navItems: any [];
public deletedItems = [];
constructor ( ) { }
public ngOnInit ( ) {
this .navItems = [{
avatar : 'https://www.infragistics.com/angular-demos-lob/assets/images/avatar/2.jpg' ,
text : 'Richard Mahoney'
},
{
avatar : 'https://www.infragistics.com/angular-demos-lob/assets/images/avatar/4.jpg' ,
text : 'Lisa Landers'
},
{
avatar : 'https://www.infragistics.com/angular-demos-lob/assets/images/avatar/14.jpg' ,
text : 'Marianne Taylor'
}, {
avatar : 'https://www.infragistics.com/angular-demos-lob/assets/images/avatar/17.jpg' ,
text : 'Ward Riley'
}];
}
public delete (item ) {
this .deletedItems.push([item, this .navItems.indexOf(item)]);
this .navItems.splice(this .navItems.indexOf(item), 1 );
this .snackbar.open();
}
public restore ( ) {
const [item, index] = this .deletedItems.pop();
this .navItems.splice(index, 0 , item);
this .snackbar.close();
}
}
ts コピー <div class ="list-sample" >
<igx-list >
<igx-list-item [isHeader ]="true" > Contacts</igx-list-item >
<igx-list-item igxRipple ="pink" igxRippleTarget =".igx-list__item-content" *ngFor ="let item of navItems" >
<igx-avatar igxListThumbnail [src ]="item.avatar" [roundShape ]="true" > </igx-avatar >
<span igxListLineTitle class ="name" > {{item.text}}</span >
<span igxListAction igxButton ="icon" igxRipple [igxRippleCentered ]="true" (click )="delete(item)" >
<igx-icon [style.color ]="'#ff5252'" > delete</igx-icon >
</span >
</igx-list-item >
<igx-snackbar actionText ="Undo" (clicked )="restore()" > Contact deleted</igx-snackbar >
</igx-list >
</div >
html コピー :host {
display : block;
padding : 16px ;
}
.list-sample {
display : block;
position : relative;
overflow : hidden;
height : 280px ;
min-width : 300px ;
box-shadow :
0 1px 3px 0 rgba(0 , 0 , 0 , 0.2 ),
0 1px 1px 0 rgba(0 , 0 , 0 , 0.14 ),
0 2px 1px -1px rgba(0 , 0 , 0 , 0.12 );
}
scss コピー
配置
positionSettings
プロパティを使用すると、Snackbar の表示位置を構成します。デフォルトで、ページの下に表示されます。以下のサンプルで、通知が上位置に表示されます。
<button igxButton ="contained" (click )="open(snackbar)" > Show notification on top</button >
<igx-snackbar #snackbar > Notification displayed</igx-snackbar >
html
import { VerticalAlignment, HorizontalAlignment } from 'igniteui-angular' ;
...
public open (snackbar ) {
snackbar.positionSettings.verticalDirection = VerticalAlignment.Top;
snackbar.positionSettings.horizontalDirection = HorizontalAlignment.Right;
snackbar.open();
}
...
typescript
スタイル設定
スナックバーのスタイル設定を始めるには、すべてのテーマ関数とコンポーネント ミックスインが存在する index ファイルをインポートする必要があります。
@use "igniteui-angular/theming" as *;
scss
最も簡単な方法は、snackbar-theme
を拡張する新しいテーマを作成し、$text-color
、$background
、$button-color
、$border-radius
パラメーターを受け取る方法です。
$dark-snackbar : snackbar-theme(
$text-color : #ffcd0f ,
$background : #292826 ,
$button-color : #ffcd0f ,
$border-radius : 12px
);
scss
上記のようにカラーの値をハードコーディングする代わりに、palette
および color
関数を使用してカラーに関してより高い柔軟性を実現することができます。使い方の詳細についてはパレット
のトピックをご覧ください。
最後にコンポーネントのテーマをアプリケーションに含めます 。
@include css-vars($dark-snackbar );
scss
デモ
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxButtonModule,
IgxSnackbarModule
} from "igniteui-angular" ;
import { SnackbarStyleComponent } from "./snackbar-style/snackbar-style.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
SnackbarStyleComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxButtonModule,
IgxSnackbarModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
@Component ({
selector : 'app-snackbar-style' ,
styleUrls : ['./snackbar-style.component.scss' ],
templateUrl : './snackbar-style.component.html'
})
export class SnackbarStyleComponent {
constructor ( ) { }
public close (element ) {
element.close();
}
}
ts コピー <button igxButton ="raised" (click )="snackbar.open()" > Send message</button >
<div >
<igx-snackbar #snackbar [autoHide ]="false" actionText ="Close" (clicked )="close(snackbar)" > Message sent</igx-snackbar >
</div >
html コピー @use '../../variables' as *;
$dark-snackbar : snackbar-theme(
$text-color : #FFCD0F ,
$background : #292826 ,
$button-color : #FFCD0F ,
$border-radius : 12px
);
:host {
@include css-vars($dark-snackbar );
}
scss コピー
API リファレンス
このトピックでは、IgxSnackbarComponent
を使用と構成方法を説明しました。API の詳細については以下のリンク先を参照してください。
スタイル:
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。