通知

通知コンポーネントを使用すると、プッシュ(またはローカル)システム通知のように見える必要なメッセージを表示できます。

通知アプリメソッド

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

app.notification.create(parameters)- 通知インスタンスを作成する

  • parameters - object。通知パラメータを持つオブジェクト

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

app.notification.destroy(el)- 通知インスタンスを破棄する

  • el - HTMLElement または string (CSSセレクター付き) または object。破棄する通知要素または通知インスタンス。

app.notification.get(el)- HTML要素から通知インスタンスを取得する

  • el - HTMLElement または string (CSSセレクター付き)。通知要素。

メソッドは通知のインスタンスを返します

app.notification.open(el, animate)- 通知を開く

  • el - HTMLElement または string (CSSセレクター付き)。開く通知要素。
  • animate - boolean。アニメーション付きで通知を開きます。

メソッドは通知のインスタンスを返します

app.notification.close(el, animate)- 通知を閉じる

  • el - HTMLElement または string (CSSセレクター付き)。閉じる通知要素。
  • animate - boolean。アニメーション付きで通知を閉じます。

メソッドは通知のインスタンスを返します

通知パラメータ

次に、通知を作成するために必要な使用可能なパラメータのリストを見てみましょう

パラメータタイプデフォルト説明
elHTMLElement
string
通知要素。すでにHTMLに通知要素があり、この要素を使用して新しいインスタンスを作成したい場合に役立ちます
iconstring通知アイコンのHTMLレイアウト。例えば <i class="f7-icons">house</i> や画像 <img src="path/to/icon.png" />
titlestring通知タイトル
titleRightTextstringタイトルの右側に追加のテキスト
subtitlestring通知サブタイトル
textstring通知の内部テキスト
closeButtonbooleanfalse通知の閉じるボタンを追加します
closeTimeoutnumber通知を自動的に閉じるまでのタイムアウト遅延 (ミリ秒単位)
closeOnClickbooleanfalse有効にすると、通知をクリックすると通知が閉じられます
swipeToClosebooleantrue有効にすると、スワイプジェスチャで通知を閉じることができます
cssClassstring追加する追加のcssクラス
renderfunction通知をレンダリングするカスタム関数。通知htmlを返す必要があります
onobject

イベントハンドラを持つオブジェクト。例えば

var notification = app.notification.create({
  title: 'John Doe',
  text: 'Hello, how are you?',
  on: {
    opened: function () {
      console.log('Notification opened')
    }
  }
})

次のすべてのパラメータは、すべての通知のデフォルトを設定するために、グローバルアプリパラメータのnotificationプロパティで使用できることに注意してください。例えば

var app = new Framework7({
  notification: {
    title: 'My App',
    closeTimeout: 3000,
  }
});

通知メソッドとプロパティ

通知を作成するには、次のように呼び出す必要があります

var notification = app.notification.create({ /* parameters */ })

その後、便利なメソッドとプロパティを備えた初期化されたインスタンス(上記の例のnotification変数など)が作成されます

プロパティ
notification.appグローバルアプリインスタンスへのリンク
notification.el通知HTML要素
notification.$el通知HTML要素を持つDom7インスタンス
notification.params通知パラメータ
メソッド
notification.open()通知を開きます
notification.close()通知を閉じます
notification.on(event, handler)イベントハンドラを追加します
notification.once(event, handler)発生後に削除されるイベントハンドラを追加します
notification.off(event, handler)イベントハンドラを削除します
notification.off(event)指定されたイベントのすべてのハンドラを削除します
notification.emit(event, ...args)インスタンスでイベントを発生させます

通知イベント

通知は、通知要素で次のDOMイベントと、アプリおよび通知インスタンスでのイベントを発生させます

DOMイベント

イベントターゲット説明
notification:open通知要素<div class="notification">通知がオープニングアニメーションを開始すると、イベントがトリガーされます
notification:opened通知要素<div class="notification">通知がオープニングアニメーションを完了すると、イベントがトリガーされます
notification:close通知要素<div class="notification">通知がクロージングアニメーションを開始すると、イベントがトリガーされます
notification:closed通知要素<div class="notification">通知がクロージングアニメーションを完了すると、イベントがトリガーされます

アプリと通知インスタンスのイベント

通知インスタンスは、自身のインスタンスとアプリインスタンスの両方でイベントを発生させます。アプリインスタンスのイベントは、notificationというプレフィックスが付いた同じ名前を持ちます。

イベント引数ターゲット説明
clicknotificationnotificationユーザーが通知要素をクリックすると、イベントがトリガーされます。引数として、イベントハンドラは通知インスタンスを受け取ります
notificationClicknotificationapp
opennotificationnotification通知がオープニングアニメーションを開始すると、イベントがトリガーされます。引数として、イベントハンドラは通知インスタンスを受け取ります
notificationOpennotificationapp
openednotificationnotification通知がオープニングアニメーションを完了すると、イベントがトリガーされます。引数として、イベントハンドラは通知インスタンスを受け取ります
notificationOpenednotificationapp
closenotificationnotification通知がクロージングアニメーションを開始すると、イベントがトリガーされます。引数として、イベントハンドラは通知インスタンスを受け取ります
notificationClosenotificationapp
closednotificationnotification通知がクロージングアニメーションを完了すると、イベントがトリガーされます。引数として、イベントハンドラは通知インスタンスを受け取ります
notificationClosednotificationapp
beforeDestroynotificationnotification通知インスタンスが破棄される直前にイベントがトリガーされます。引数として、イベントハンドラは通知インスタンスを受け取ります
notificationBeforeDestroynotificationapp

CSS 変数

以下は、関連するCSS変数 (CSSカスタムプロパティ) のリストです。

:root {
  --f7-notification-max-width: 568px;
  --f7-notification-subtitle-text-transform: none;
  --f7-notification-subtitle-line-height: 1.35;
  --f7-notification-text-text-transform: none;
  --f7-notification-text-font-weight: 400;
}
.ios {
  --f7-notification-margin: 8px;
  --f7-notification-padding-horizontal: 10px;
  --f7-notification-padding-vertical: 10px;
  --f7-notification-border-radius: 12px;
  --f7-notification-box-shadow: 0px 5px 25px -10px rgba(0, 0, 0, 0.7);
  --f7-notification-icon-size: 20px;
  --f7-notification-title-font-size: 13px;
  --f7-notification-title-text-transform: uppercase;
  --f7-notification-title-line-height: 1.4;
  --f7-notification-title-font-weight: 400;
  --f7-notification-title-letter-spacing: 0.02em;
  --f7-notification-title-right-font-size: 13px;
  --f7-notification-subtitle-font-size: 15px;
  --f7-notification-subtitle-font-weight: 600;
  --f7-notification-text-font-size: 15px;
  --f7-notification-text-line-height: 1.2;
  --f7-notification-bg-color: rgba(250, 250, 250, 0.95);
  --f7-notification-bg-color-rgb: 255, 255, 255;
  --f7-notification-title-color: #000;
  --f7-notification-title-right-color: rgba(0, 0, 0, 0.45);
  --f7-notification-subtitle-color: #000;
  --f7-notification-text-color: #000;
}
.ios .dark,
.ios.dark {
  --f7-notification-bg-color: rgba(30, 30, 30, 0.95);
  --f7-notification-bg-color-rgb: 30, 30, 30;
  --f7-notification-title-color: #fff;
  --f7-notification-text-color: #fff;
  --f7-notification-subtitle-color: #fff;
  --f7-notification-title-right-color: rgba(255, 255, 255, 0.55);
}
.md {
  --f7-notification-margin: 16px;
  --f7-notification-padding-vertical: 20px;
  --f7-notification-padding-horizontal: 16px;
  --f7-notification-border-radius: 16px;
  --f7-notification-box-shadow: none;
  --f7-notification-icon-size: 24px;
  --f7-notification-title-font-size: 16px;
  --f7-notification-title-text-transform: none;
  --f7-notification-title-line-height: 1.35;
  --f7-notification-title-font-weight: 500;
  --f7-notification-title-right-font-size: 12px;
  --f7-notification-subtitle-font-size: 14px;
  --f7-notification-subtitle-font-weight: 400;
  --f7-notification-text-font-size: 14px;
  --f7-notification-text-line-height: 1.35;
}
.md,
.md .dark,
.md [class*='color-'] {
  --f7-notification-title-color: var(--f7-md-on-surface);
  --f7-notification-bg-color: var(--f7-md-surface-5);
  --f7-notification-text-color: var(--f7-md-on-surface-variant);
  --f7-notification-title-right-color: var(--f7-md-on-surface-variant);
  --f7-notification-subtitle-color: var(--f7-md-on-surface);
}

notifications.html
<template>
  <div class="page">
    <div class="navbar">
      <div class="navbar-bg"></div>
      <div class="navbar-inner sliding">
        <div class="title">Notifications</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block block-strong-ios block-outline-ios">
        <p>Framework7 comes with simple Notifications component that allows you to show some useful messages to user and
          request basic actions.</p>
        <p><a class="button button-fill" @click=${showNotificationFull}>Full layout notification</a></p>
        <p><a class="button button-fill" @click=${showNotificationWithButton}>With close button</a></p>
        <p><a class="button button-fill" @click=${showNotificationCloseOnClick}>Click to close</a></p>
        <p><a class="button button-fill" @click=${showNotificationCallbackOnClose}>Callback on close</a></p>
      </div>
    </div>
  </div>
</template>
<script>
  export default (props, { $f7, $el, $on }) => {
    let notificationFull;
    let notificationWithButton;
    let notificationCloseOnClick;
    let notificationCallbackOnClose;

    const showNotificationFull = () => {
      // Create notification
      if (!notificationFull) {
        notificationFull = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          titleRightText: 'now',
          subtitle: 'This is a subtitle',
          text: 'This is a simple notification message',
          closeTimeout: 3000,
        });
      }
      // Open it
      notificationFull.open();
    }
    const showNotificationWithButton = () => {
      // Create notification
      if (!notificationWithButton) {
        notificationWithButton = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          subtitle: 'Notification with close button',
          text: 'Click (x) button to close me',
          closeButton: true,
        });
      }
      // Open it
      notificationWithButton.open();
    }
    const showNotificationCloseOnClick = () => {
      // Create notification
      if (!notificationCloseOnClick) {
        notificationCloseOnClick = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          titleRightText: 'now',
          subtitle: 'Notification with close on click',
          text: 'Click me to close',
          closeOnClick: true,
        });
      }
      // Open it
      notificationCloseOnClick.open();
    }
    const showNotificationCallbackOnClose = () => {
      // Create notification
      if (!notificationCallbackOnClose) {
        notificationCallbackOnClose = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          titleRightText: 'now',
          subtitle: 'Notification with close on click',
          text: 'Click me to close',
          closeOnClick: true,
          on: {
            close: function () {
              $f7.dialog.alert('Notification closed');
            },
          },
        });
      }
      // Open it
      notificationCallbackOnClose.open();
    }

    $on('pageBeforeRemove', () => {
      $f7.notification.close();
      if (notificationFull) notificationFull.destroy();
      if (notificationWithButton) notificationWithButton.destroy();
      if (notificationCloseOnClick) notificationCloseOnClick.destroy();
      if (notificationCallbackOnClose) notificationCallbackOnClose.destroy();
    });

    return $render;
  };
</script>