アクションシート

アクションシートは、特定のタスクをどのように進めるかの選択肢をユーザーに提示するためのスライドアップパネルです。

アクションシートを使用して、潜在的に危険なアクションを確認するようにユーザーに促すこともできます。

アクションシートには、オプションのタイトルと、実行するアクションに対応する1つ以上のボタンが含まれています。

大きな画面(iPad)ではアクションシートを使用しないことをお勧めします。大きな画面では、代わりにポップオーバーを使用する必要があります。

アクションシートアプリメソッド

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

app.actions.create(parameters)- アクションシートインスタンスを作成します。

  • parameters - object。アクションシートのパラメーターを持つオブジェクト。

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

app.actions.destroy(el)- アクションシートインスタンスを破棄します。

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

app.actions.get(el)- HTML要素によるアクションシートインスタンスを取得します。

  • el - HTMLElement または string (CSSセレクター付き)。アクションシート要素。

メソッドは、アクションシートのインスタンスを返します。

app.actions.open(el, animate)- アクションシートを開きます。

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

メソッドは、アクションシートのインスタンスを返します。

app.actions.close(el, animate)- アクションシートを閉じます。

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

メソッドは、アクションシートのインスタンスを返します。

アクションシートのパラメーター

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

パラメータータイプデフォルト説明
elHTMLElement
string
アクションシート要素。HTMLに既にアクションシート要素があり、この要素を使用して新しいインスタンスを作成する場合に便利です。
contentstringアクションシートの完全なHTMLコンテンツ文字列。カスタムHTMLでアクションシート要素を作成する場合に便利です。
backdropbooleantrueアクションシートの背景(背後の暗い半透明レイヤー)を有効にします。
backdropElHTMLElement
string
カスタム背景要素のHTML要素または文字列CSSセレクター
backdropUniquebooleanfalse有効にすると、このモーダルのために排他的にユニークな背景要素が作成されます。
closeByBackdropClickbooleantrue有効にすると、アクションシートは背景クリックで閉じられます。
closeByOutsideClickbooleanfalse有効にすると、アクションシートの外側をクリックすると閉じられます。
closeOnEscapebooleanfalse有効にすると、アクションシートはESCキーの押下で閉じられます。
animatebooleantrueアクションシートをアニメーション付きで開閉するかどうか。.open()および.close()メソッドで上書きできます。
buttonsarrayアクションシートグループ/ボタン。この場合、アクションのレイアウトは、渡されたグループとボタンに基づいて動的に生成されます。groupsの場合、各項目がグループのボタンを持つ配列を表す配列である必要があります。
gridbooleanfalseグリッドボタンレイアウトを有効にします。
convertToPopoverbooleantrue有効にすると、アクションシートは大きな画面でポップオーバーに変換されます。
forceToPopoverbooleanfalse有効にすると、アクションシートは常にポップオーバーに変換されます。
targetElHTMLElement
string
ターゲット要素のHTML要素または文字列CSSセレクター。ポップオーバーへの変換を使用する場合は必須です。
targetXnumber画面の左側からの仮想ターゲット要素の水平方向オフセット。実際のターゲット要素(targetEl)を使用せずにポップオーバーへの変換を使用する場合は必須です。
targetYnumber画面の上からの仮想ターゲット要素の垂直方向オフセット。実際のターゲット要素(targetEl)を使用せずにポップオーバーへの変換を使用する場合は必須です。
targetWidthnumber0仮想ターゲット要素の幅(px単位)。実際のターゲット要素(targetEl)を使用せずにポップオーバーへの変換を使用する場合は必須です。
targetHeightnumber0仮想ターゲット要素の高さ(px単位)。実際のターゲット要素(targetEl)を使用せずにポップオーバーへの変換を使用する場合は必須です。
cssClassstringアクションシート要素に設定する追加のCSSクラス名
onClickfunction(actions, e)アクションシートボタンのクリック後に実行されるコールバック関数
renderfunctionアクションシートをレンダリングするためのカスタム関数。アクションシートのhtmlを返す必要があります。
renderPopoverfunctionポップオーバーへの変換を使用する場合にポップオーバーをレンダリングするためのカスタム関数。ポップオーバーのhtmlを返す必要があります。
containerElHTMLElement
string
モーダルをマウントする要素(デフォルトはアプリのルート要素)
onobject

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

var actions = app.actions.create({
  buttons: [ /* ... */ ],
  on: {
    opened: function () {
      console.log('Action Sheet opened')
    }
  }
})

次のすべてのパラメーターは、actionsプロパティの下のグローバルアプリパラメーターで使用して、すべてのアクションシートのデフォルトを設定できることに注意してください。例:

var app = new Framework7({
  actions: {
    convertToPopover: false,
    grid: true,
  }
});

ボタンパラメーター

buttons配列の各ボタンは、ボタンパラメーターを持つオブジェクトとして表示する必要があります。

パラメータータイプデフォルト説明
textstringボタンのテキスト(HTML文字列の場合もあります)を持つ文字列
iconstringアイコンのHTML文字列
strongbooleanfalseボタンテキストを太字にします。
colorstringボタンの色、デフォルトの色の1つ
bgstringボタンの背景色、デフォルトの色の1つ
labelbooleanfalse有効にすると、ボタンではなくラベルとしてレンダリングされます。
disabledbooleanfalseボタンが無効かどうかを定義します。
closebooleantrue有効にすると、ボタンをクリックするとアクションシートが閉じます。
onClickfunction(actions, e)このボタンのクリック後に実行されるコールバック関数

アクションシートのメソッドとプロパティ

アクションシートを作成するには、次のコードを呼び出す必要があります。

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

その後、便利なメソッドとプロパティを持つ初期化されたインスタンス(上記の例のactions変数など)があります。

プロパティ
actions.appグローバルアプリインスタンスへのリンク
actions.elアクションシートのHTML要素
actions.$elアクションシートのHTML要素を持つDom7インスタンス
actions.backdropEl背景HTML要素
actions.$backdropEl背景HTML要素を持つDom7インスタンス
actions.paramsアクションシートインスタンスのパラメーター
actions.openedアクションシートが開いているかどうかを示すブール値のプロパティ
メソッド
actions.open(animate)アクションシートを開きます。ここで
  • animate - boolean(デフォルトはtrue) - アニメーション付きで開くかどうかを定義します
actions.close(animate)アクションシートを閉じます。ここで
  • animate - boolean(デフォルトはtrue) - アニメーション付きで閉じるかどうかを定義します
actions.destroy()アクションシートを破棄します。
actions.on(event, handler)イベントハンドラーを追加します。
actions.once(event, handler)発生後に削除されるイベントハンドラーを追加します。
actions.off(event, handler)イベントハンドラーを削除します。
actions.off(event)指定されたイベントのすべてのハンドラーを削除します。
actions.emit(event, ...args)インスタンスでイベントを発生させます。

リンク上の特別なクラスとデータ属性を使用して、必要なアクションシート(DOMにある場合)を開閉できます。

  • アクションシートを開くには、「actions-open」クラスを任意のHTML要素(リンクに推奨)に追加する必要があります。

  • アクションシートを閉じるには、「actions-close」クラスを任意のHTML要素(リンクに推奨)に追加する必要があります。

  • DOMに複数のアクションシートがある場合は、このHTML要素の追加のdata-actions=".my-actions"属性を介して適切なアクションシートを指定する必要があります。

上記のメモに従って

<!-- In data-actions attribute we specify CSS selector of action sheet we need to open -->
<p><a href="#" data-actions=".my-actions" class="actions-open">Open Actions</a></p>

<!-- And somewhere in DOM -->
<div class="actions-modal my-actions">
  ...
</div>

アクションシートイベント

アクションシートは、アクションシート要素とアプリとアクションシートインスタンスのイベントで次のDOMイベントを発生させます。

DOMイベント

イベントターゲット説明
actions:openアクションシート要素<div class="actions-modal">アクションシートが開くアニメーションを開始するとイベントがトリガーされます。
actions:openedアクションシート要素<div class="actions-modal">アクションシートが開くアニメーションを完了するとイベントがトリガーされます。
actions:closeアクションシート要素<div class="actions-modal">アクションシートが閉じるアニメーションを開始するとイベントがトリガーされます。
actions:closedアクションシート要素<div class="actions-modal">アクションシートが閉じるアニメーションを完了するとイベントがトリガーされます。

アプリとアクションシートインスタンスのイベント

アクションシートインスタンスは、自己インスタンスとアプリインスタンスの両方でイベントを発生させます。アプリインスタンスのイベントには、actionsで始まる同じ名前が付いています。

イベント引数ターゲット説明
openactionsactionsアクションシートが開きのアニメーションを開始したときにイベントがトリガーされます。イベントハンドラーは、引数としてアクションシートのインスタンスを受け取ります。
actionsOpenactionsapp
openedactionsactionsアクションシートが開きのアニメーションを完了した後にイベントがトリガーされます。イベントハンドラーは、引数としてアクションシートのインスタンスを受け取ります。
actionsOpenedactionsapp
closeactionsactionsアクションシートが閉じのアニメーションを開始したときにイベントがトリガーされます。イベントハンドラーは、引数としてアクションシートのインスタンスを受け取ります。
actionsCloseactionsapp
closedactionsactionsアクションシートが閉じのアニメーションを完了した後にイベントがトリガーされます。イベントハンドラーは、引数としてアクションシートのインスタンスを受け取ります。
actionsClosedactionsapp
beforeDestroyactionsactionsアクションシートのインスタンスが破棄される直前にイベントがトリガーされます。イベントハンドラーは、引数としてアクションシートのインスタンスを受け取ります。
actionsBeforeDestroyactionsapp

CSS変数

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

コメントアウトされた変数はデフォルトでは指定されておらず、その値はこの場合のフォールバック値であることに注意してください。

:root {
  --f7-actions-grid-button-font-size: 12px;
  --f7-actions-grid-button-icon-size: 48px;
}
.ios {
  --f7-actions-transition-timing-function: initial;
  --f7-actions-border-radius: 13px;
  /*
  --f7-actions-button-text-color: var(--f7-theme-color);
  */
  --f7-actions-grid-button-text-color: #757575;
  --f7-actions-button-padding: 0px;
  --f7-actions-button-text-align: center;
  --f7-actions-button-height: 57px;
  --f7-actions-button-height-landscape: 44px;
  --f7-actions-button-font-size: 20px;
  --f7-actions-button-icon-size: 28px;
  --f7-actions-button-justify-content: center;
  --f7-actions-label-padding: 8px 10px;
  --f7-actions-label-font-weight: 400;
  --f7-actions-label-font-size: 13px;
  --f7-actions-label-justify-content: center;
  --f7-actions-group-border-color: transparent;
  --f7-actions-group-margin: 8px;
  --f7-actions-bg-color: rgba(255, 255, 255, 0.95);
  --f7-actions-bg-color-rgb: 255, 255, 255;
  --f7-actions-button-border-color: rgba(0, 0, 0, 0.2);
  --f7-actions-button-pressed-bg-color: rgba(230, 230, 230, 0.9);
  --f7-actions-button-pressed-bg-color-rgb: 230, 230, 230;
  --f7-actions-label-text-color: #8a8a8a;
}
.ios .dark,
.ios.dark {
  --f7-actions-bg-color: rgba(45, 45, 45, 0.95);
  --f7-actions-bg-color-rgb: 45, 45, 45;
  --f7-actions-button-border-color: rgba(255, 255, 255, 0.15);
  --f7-actions-button-pressed-bg-color: rgba(50, 50, 50, 0.9);
  --f7-actions-button-pressed-bg-color-rgb: 50, 50, 50;
  --f7-actions-label-text-color: rgba(255, 255, 255, 0.55);
}
.md {
  --f7-actions-transition-timing-function: cubic-bezier(0, 0.8, 0.34, 1);
  --f7-actions-border-radius: 16px;
  --f7-actions-button-border-color: transparent;
  --f7-actions-button-padding: 0 16px;
  --f7-actions-button-text-align: left;
  --f7-actions-button-height: 48px;
  --f7-actions-button-height-landscape: 48px;
  --f7-actions-button-font-size: 16px;
  --f7-actions-button-icon-size: 24px;
  --f7-actions-button-justify-content: space-between;
  --f7-actions-label-padding: 12px 16px;
  --f7-actions-label-font-size: 14px;
  --f7-actions-label-font-weight: 500;
  --f7-actions-label-justify-content: flex-start;
  --f7-actions-group-margin: 0px;
  --f7-actions-button-pressed-bg-color: #e5e5e5;
}
.md .dark,
.md.dark {
  --f7-actions-button-pressed-bg-color: #2e2e2e;
}
.md,
.md .dark,
.md [class*='color-'] {
  --f7-actions-button-text-color: var(--f7-md-on-surface);
  --f7-actions-bg-color: var(--f7-md-surface-3);
  --f7-actions-group-border-color: var(--f7-md-outline);
  --f7-actions-label-text-color: var(--f7-theme-color);
  --f7-actions-grid-button-text-color: var(--f7-md-on-surface);
}

action-sheet.html
<template>
  <div class="page">
    <div class="navbar">
      <div class="navbar-bg"></div>
      <div class="navbar-inner sliding">
        <div class="title">Action Sheet</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block block-strong inset">
        <p class="grid grid-cols-2 grid-gap">
          <a class="button button-fill" @click=${openDemo1}>One group</a>
          <a class="button button-fill" @click=${openDemo2}>Two groups</a>
        </p>
        <p>
          <a class="button button-fill" @click=${openGrid}>Action Grid</a>
        </p>
      </div>
      <div class="block-title">Action Sheet To Popover</div>
      <div class="block block-strong inset">
        <p>Action Sheet can be automatically converted to Popover (for tablets). This button will open Popover on
          tablets and Action Sheet on phones: <a style="display:inline-block"
            class="button button-to-popover button-small" @click=${openActionsPopover}>Actions</a></p>
      </div>
    </div>
  </div>
</template>
<script>
  export default (props, { $on, $f7, $el }) => {
    let actions1;
    let actions2;
    let actionsGrid;
    let actionsToPopover;

    const openDemo1 = () => {
      actions1.open();
    }
    const openDemo2 = () => {
      actions2.open();
    }
    const openGrid = () => {
      actionsGrid.open();
    }
    const openActionsPopover = () => {
      actionsToPopover.open();
    }

    $on('pageInit', () => {
      // 1 Group
      const buttons1 = [
        {
          text: 'Do something',
          label: true
        },
        {
          text: 'Button 1',
          strong: true
        },
        {
          text: 'Button 2',
        },
        {
          text: 'Cancel',
          color: 'red'
        },
      ];

      // 2 Groups
      const buttons2 = [
        // First Group
        [
          {
            text: 'Do something',
            label: true
          },
          {
            text: 'Button 1',
            strong: true
          },
          {
            text: 'Button 2',
          }
        ],
        // Second Group
        [
          {
            text: 'Cancel',
            color: 'red'
          }
        ]
      ]

      // Grid buttons with icons/images
      const gridButtons = [
        [
          {
            text: 'Button 1',
            icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-1.jpg" width="48" style="max-width: 100%; border-radius: 8px"/>'
          },
          {
            text: 'Button 2',
            icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-2.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
          {
            text: 'Button 3',
            icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-3.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
        ],
        [
          {
            text: 'Button 1',
            icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-4.jpg" width="48" style="max-width: 100%; border-radius: 8px"/>'
          },
          {
            text: 'Button 2',
            icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-5.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
          {
            text: 'Button 3',
            icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-6.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
        ],
      ]

      actions1 = $f7.actions.create({ buttons: buttons1 });
      actions2 = $f7.actions.create({ buttons: buttons2 });
      actionsGrid = $f7.actions.create({ buttons: gridButtons, grid: true });

      // Actions To Popover
      actionsToPopover = $f7.actions.create({
        buttons: buttons1,
        // Need to specify popover target
        targetEl: $el.value.find('.button-to-popover')
      });
    })

    $on('pageBeforeRemove', () => {
      actions1.destroy();
      actions2.destroy();
      actionsGrid.destroy();
      actionsToPopover.destroy();
    })

    return $render;
  }

</script>