トースト

トーストは、画面上のメッセージを通じて操作に関する簡単なフィードバックを提供します。

トーストアプリメソッド

トーストを操作するための関連するアプリメソッドを見てみましょう。

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(パラメーター)- トーストインスタンスを作成し、すぐに表示します。

  • パラメーター - オブジェクト。トーストパラメーターを含むオブジェクト。

メソッドは作成されたトーストのインスタンスを返します。

トーストパラメーター

トーストを作成するために必要なパラメーターのリストを見てみましょう。

パラメーターデフォルト説明
elHTMLElementトースト要素。既にHTMLにトースト要素があり、この要素を使用して新しいインスタンスを作成する場合に便利です。
icon文字列トーストアイコンのHTMLレイアウト、例:<i class="f7-icons">house</i>または画像<img src="path/to/icon.png" />
text文字列トーストの内部テキスト
position文字列bottomトーストの位置。bottomcentertopのいずれかになります。
horizontalPosition文字列leftワイドスクリーンでのトーストの水平方向の配置。上と下のトーストにのみ影響します。leftcenterrightのいずれかになります。
closeButtonブール値falseトーストの閉じるボタンを追加します。
closeButtonColor文字列デフォルトのカラースキームのいずれか。
closeButtonText文字列Ok閉じるボタンのテキスト。
closeTimeout数値トーストを自動的に閉じるまでのタイムアウト遅延(ミリ秒)。
cssClass文字列追加するCSSクラス。
destroyOnCloseブール値false閉じるときにトーストインスタンスを破棄します。
render関数トーストをレンダリングするカスタム関数。トーストのHTMLを返す必要があります。
containerElHTMLElement
文字列
アプリのルート要素ではなく、カスタム要素にトーストをマウントできます。
onオブジェクト

イベントハンドラーを含むオブジェクト。例えば

var toast = app.toast.create({
  text: 'Hello, how are you?',
  on: {
    opened: function () {
      console.log('Toast opened')
    }
  }
})

これらのパラメーターはすべて、グローバルアプリパラメーターの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という接頭辞が付いた同じ名前です。

イベント引数ターゲット説明
closeButtonClicktoasttoastユーザーがトーストの閉じるボタンをクリックしたときにトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。
toastCloseButtonClicktoastapp
opentoasttoastトーストが開くアニメーションを開始したときにトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。
toastOpentoastapp
openedtoasttoastトーストが開くアニメーションが完了した後にトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。
toastOpenedtoastapp
closetoasttoastトーストが閉じるアニメーションを開始したときにトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。
toastClosetoastapp
closedtoasttoastトーストが閉じるアニメーションが完了した後にトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。
toastClosedtoastapp
beforeDestroytoasttoastトーストインスタンスが破棄される直前にトリガーされます。引数として、イベントハンドラーはトーストインスタンスを受け取ります。
toastBeforeDestroytoastapp

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);
}

toast.html
<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>