トースト
トーストは、画面上のメッセージを通じて操作に関する簡単なフィードバックを提供します。
トーストアプリメソッド
トーストを操作するための関連するアプリメソッドを見てみましょう。
app.toast.create(パラメーター)- トーストインスタンスを作成します。
- パラメーター - オブジェクト。トーストパラメーターを含むオブジェクト。
メソッドは作成されたトーストのインスタンスを返します。
app.toast.destroy(el)- トーストインスタンスを破棄します。
- el - HTMLElement または 文字列(CSSセレクター付き)または オブジェクト。破棄するトースト要素またはトーストインスタンス。
app.toast.get(el)- HTML要素からトーストインスタンスを取得します。
- el - HTMLElement または 文字列(CSSセレクター付き)。トースト要素。
メソッドはトーストのインスタンスを返します。
app.toast.open(el, animate)- トーストを開きます。
- el - HTMLElement または 文字列(CSSセレクター付き)。開くトースト要素。
- animate - ブール値。アニメーション付きでトーストを開きます。
メソッドはトーストのインスタンスを返します。
app.toast.close(el, animate)- トーストを閉じます。
- el - HTMLElement または 文字列(CSSセレクター付き)。閉じるトースト要素。
- animate - ブール値。アニメーション付きでトーストを閉じます。
メソッドはトーストのインスタンスを返します。
app.toast.show(パラメーター)- トーストインスタンスを作成し、すぐに表示します。
- パラメーター - オブジェクト。トーストパラメーターを含むオブジェクト。
メソッドは作成されたトーストのインスタンスを返します。
トーストパラメーター
トーストを作成するために必要なパラメーターのリストを見てみましょう。
パラメーター | 型 | デフォルト | 説明 |
---|---|---|---|
el | HTMLElement | トースト要素。既にHTMLにトースト要素があり、この要素を使用して新しいインスタンスを作成する場合に便利です。 | |
icon | 文字列 | トーストアイコンのHTMLレイアウト、例:<i class="f7-icons">house</i> または画像<img src="path/to/icon.png" /> | |
text | 文字列 | トーストの内部テキスト | |
position | 文字列 | bottom | トーストの位置。bottom 、center 、top のいずれかになります。 |
horizontalPosition | 文字列 | left | ワイドスクリーンでのトーストの水平方向の配置。上と下のトーストにのみ影響します。left 、center 、right のいずれかになります。 |
closeButton | ブール値 | false | トーストの閉じるボタンを追加します。 |
closeButtonColor | 文字列 | デフォルトのカラースキームのいずれか。 | |
closeButtonText | 文字列 | Ok | 閉じるボタンのテキスト。 |
closeTimeout | 数値 | トーストを自動的に閉じるまでのタイムアウト遅延(ミリ秒)。 | |
cssClass | 文字列 | 追加するCSSクラス。 | |
destroyOnClose | ブール値 | false | 閉じるときにトーストインスタンスを破棄します。 |
render | 関数 | トーストをレンダリングするカスタム関数。トーストのHTMLを返す必要があります。 | |
containerEl | HTMLElement 文字列 | アプリのルート要素ではなく、カスタム要素にトーストをマウントできます。 | |
on | オブジェクト | イベントハンドラーを含むオブジェクト。例えば
|
これらのパラメーターはすべて、グローバルアプリパラメーターのtoast
プロパティで使用して、すべてのトーストのデフォルトを設定できます。例:
var app = new Framework7({
toast: {
closeTimeout: 3000,
closeButton: true,
}
});
トーストメソッドとプロパティ
トーストを作成するには、以下を呼び出します。
var toast = app.toast.create({ /* parameters */ })
その後、便利なメソッドとプロパティを持つ初期化されたインスタンス(上記の例ではtoast
変数)が得られます。
プロパティ | |
---|---|
toast.app | グローバルアプリインスタンスへのリンク。 |
toast.el | トーストHTML要素。 |
toast.$el | トーストHTML要素を含むDom7インスタンス。 |
toast.params | トーストパラメーター。 |
メソッド | |
toast.open() | トーストを開きます。 |
toast.close() | トーストを閉じます。 |
toast.on(event, handler) | イベントハンドラーを追加します。 |
toast.once(event, handler) | 発火後に削除されるイベントハンドラーを追加します。 |
toast.off(event, handler) | イベントハンドラーを削除します。 |
toast.off(event) | 指定されたイベントのすべてのハンドラーを削除します。 |
toast.emit(event, ...args) | インスタンスでイベントを発火します。 |
トーストイベント
トーストは、トースト要素で次のDOMイベント、アプリとトーストインスタンスでイベントを発火します。
DOMイベント
イベント | ターゲット | 説明 |
---|---|---|
toast:open | トースト要素<div class="toast"> | トーストが開くアニメーションを開始したときにトリガーされます。 |
toast:opened | トースト要素<div class="toast"> | トーストが開くアニメーションが完了した後にトリガーされます。 |
toast:close | トースト要素<div class="toast"> | トーストが閉じるアニメーションを開始したときにトリガーされます。 |
toast:closed | トースト要素<div class="toast"> | トーストが閉じるアニメーションが完了した後にトリガーされます。 |
アプリとトーストインスタンスのイベント
トーストインスタンスは、自身とアプリインスタンスの両方でイベントを発火します。アプリインスタンスのイベントは、toast
という接頭辞が付いた同じ名前です。
イベント | 引数 | ターゲット | 説明 |
---|---|---|---|
closeButtonClick | toast | toast | ユーザーがトーストの閉じるボタンをクリックしたときにトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。 |
toastCloseButtonClick | toast | app | |
open | toast | toast | トーストが開くアニメーションを開始したときにトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。 |
toastOpen | toast | app | |
opened | toast | toast | トーストが開くアニメーションが完了した後にトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。 |
toastOpened | toast | app | |
close | toast | toast | トーストが閉じるアニメーションを開始したときにトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。 |
toastClose | toast | app | |
closed | toast | toast | トーストが閉じるアニメーションが完了した後にトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。 |
toastClosed | toast | app | |
beforeDestroy | toast | toast | トーストインスタンスが破棄される直前にトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。 |
toastBeforeDestroy | toast | app |
CSS変数
関連するCSS変数(CSSカスタムプロパティ)のリストを以下に示します。
:root {
--f7-toast-font-size: 14px;
--f7-toast-icon-size: 48px;
--f7-toast-max-width: 568px;
}
.ios {
--f7-toast-text-color: #fff;
--f7-toast-bg-color: rgba(0, 0, 0, 0.75);
--f7-toast-bg-color-rgb: 0, 0, 0;
--f7-toast-padding-horizontal: 16px;
--f7-toast-padding-vertical: 12px;
--f7-toast-border-radius: 8px;
--f7-toast-button-min-width: 64px;
}
.md {
--f7-toast-padding-horizontal: 24px;
--f7-toast-padding-vertical: 14px;
--f7-toast-border-radius: 16px;
--f7-toast-button-min-width: 64px;
}
.md,
.md .dark,
.md [class*='color-'] {
--f7-toast-text-color: var(--f7-md-on-surface);
--f7-toast-bg-color: var(--f7-md-surface-5);
}
例
<template>
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner sliding">
<div class="title">Toast</div>
</div>
</div>
<div class="page-content">
<div class="block block-strong-ios block-outline-ios">
<p>Toasts provide brief feedback about an operation through a message on the screen.</p>
<p>
<a class="button button-fill" @click=${showToastBottom}>Toast on Bottom</a>
</p>
<p>
<a class="button button-fill" @click=${showToastTop}>Toast on Top</a>
</p>
<p>
<a class="button button-fill" @click=${showToastCenter}>Toast on Center</a>
</p>
<p>
<a class="button button-fill" @click=${showToastIcon}>Toast with icon</a>
</p>
<p>
<a class="button button-fill" @click=${showToastLargeMessage}>Toast with large message</a>
</p>
<p>
<a class="button button-fill" @click=${showToastWithButton}>Toast with close button</a>
</p>
<p>
<a class="button button-fill" @click=${showToastWithCustomButton}>Toast with custom button</a>
</p>
<p>
<a class="button button-fill" @click=${showToastWithCallback}>Toast with callback on close</a>
</p>
</div>
</div>
</div>
</template>
<script>
export default (props, { $f7, $theme, $on }) => {
let toastBottom;
let toastTop;
let toastCenter;
let toastIcon;
let toastLargeMessage;
let toastWithButton;
let toastWithCustomButton;
let toastWithCallback;
const showToastBottom = () => {
// Create toast
if (!toastBottom) {
toastBottom = $f7.toast.create({
text: 'This is default bottom positioned toast',
closeTimeout: 2000,
});
}
// Open it
toastBottom.open();
}
const showToastTop = () => {
// Create toast
if (!toastTop) {
toastTop = $f7.toast.create({
text: 'Top positioned toast',
position: 'top',
closeTimeout: 2000,
});
}
// Open it
toastTop.open();
}
const showToastCenter = () => {
// Create toast
if (!toastCenter) {
toastCenter = $f7.toast.create({
text: 'I\'m on center',
position: 'center',
closeTimeout: 2000,
});
}
// Open it
toastCenter.open();
}
const showToastIcon = () => {
// Create toast
if (!toastIcon) {
toastIcon = $f7.toast.create({
icon: $theme.ios ? '<i class="f7-icons">star_fill</i>' : '<i class="material-icons">star</i>',
text: 'I\'m with icon',
position: 'center',
closeTimeout: 2000,
});
}
// Open it
toastIcon.open();
}
const showToastLargeMessage = () => {
// Create toast
if (!toastLargeMessage) {
toastLargeMessage = $f7.toast.create({
text: 'This toast contains a lot of text. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil, quae, ab. Delectus amet optio facere autem sapiente quisquam beatae culpa dolore.',
closeTimeout: 2000,
});
}
// Open it
toastLargeMessage.open();
}
const showToastWithButton = () => {
// Create toast
if (!toastWithButton) {
toastWithButton = $f7.toast.create({
text: 'Toast with additional close button',
closeButton: true,
});
}
// Open it
toastWithButton.open();
}
const showToastWithCustomButton = () => {
// Create toast
if (!toastWithCustomButton) {
toastWithCustomButton = $f7.toast.create({
text: 'Custom close button',
closeButton: true,
closeButtonText: 'Close Me',
closeButtonColor: 'red',
});
}
// Open it
toastWithCustomButton.open();
}
const showToastWithCallback = () => {
// Create toast
if (!toastWithCallback) {
toastWithCallback = $f7.toast.create({
text: 'Callback on close',
closeButton: true,
on: {
close: function () {
$f7.dialog.alert('Toast closed');
},
}
});
}
// Open it
toastWithCallback.open();
}
$on('pageBeforeOut', () => {
$f7.toast.close();
})
$on('pageBeforeRemove', () => {
// Destroy toasts when page removed
if (toastBottom) toastBottom.destroy();
if (toastTop) toastTop.destroy();
if (toastCenter) toastCenter.destroy();
if (toastIcon) toastIcon.destroy();
if (toastLargeMessage) toastLargeMessage.destroy();
if (toastWithButton) toastWithButton.destroy();
if (toastWithCustomButton) toastWithCustomButton.destroy();
if (toastWithCallback) toastWithCallback.destroy();
})
return $render;
}
</script>