メニュー

ポップオーバーとメニュー

ボタンのすぐ下に小さく開く面です。操作を並べるメニューにも使い、Esc で閉じてボタンへ戻ります。

見本で種類と状態を確かめる

最小の幅 200、内側 4、角丸 md、raised の地に shadow-2 です。メニューの項目は高さ 40 で、危険な項目は danger の文字にして線で分け、一番下に置きます。「操作」の状態では、実際に開いて ↑↓ と Enter で選べます。

操作

家賃−78,000円

メニューを開いた

説明を開いた

給料日の20日から次の給料日の前日までを1か月として数えます。11月は10/20(火)〜11/19(木)です。

コードを写す

HTML

<button class="mz-icon-button" type="button" aria-label="家賃の明細の操作" aria-haspopup="menu" aria-expanded="false" popovertarget="menu-light" data-demo-open="menu" style="anchor-name: --menu-light"><svg class="" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/></svg></button>

<div class="mz-popover" id="menu-light" popover data-demo="menu" style="position-anchor: --menu-light">
  <div class="mz-menu" role="menu" aria-label="家賃の明細の操作">
      <button class="mz-menu__item" role="menuitem" type="button" tabindex="-1"><svg class="mz-menu__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42z"/><circle cx="7.5" cy="7.5" r=".5" fill="currentColor"/></svg>分類を変える</button>
      <button class="mz-menu__item" role="menuitem" type="button" tabindex="-1"><svg class="mz-menu__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/></svg>メモを書く</button>
      <button class="mz-menu__item" role="menuitem" type="button" tabindex="-1"><svg class="mz-menu__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>翌月にも入れる</button>
      <div class="mz-menu__separator" role="separator"></div>
      <button class="mz-menu__item" role="menuitem" type="button" data-tone="danger" tabindex="-1"><svg class="mz-menu__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/></svg>削除する</button>
  </div>
</div>

React のメニューは react/Menu.tsx(Menu・MenuItem・MenuSeparator)を Popover の中に置きます。開閉は Popover API(popover と popovertarget)、位置は CSS Anchor Positioning です。↑↓ の移動と、開いたときに最初の項目へフォーカスする処理は JS が持ちます(React の Menu は持っています)。

いつ使うか

  • 行ごとの操作(メモ・分類・削除)をまとめて、画面を静かに保つときにメニューを使います。よく使う操作は、メニューに隠さずボタンで出します。
  • 言葉の説明を、その場で短く見せるときにポップオーバーを使います。
  • ボタンの下に開き、下に入らないときは上に返します。

読み上げとキーボードにどう応えるか

  • 開くボタンに aria-haspopup="menu" と aria-expanded を付けます。メニューは role="menu"、項目は role="menuitem" です。
  • ↑↓ で移動し、Enter で選び、Esc で閉じてフォーカスをボタンへ戻します。
  • 項目の tabindex="-1" は、Tab ではなく ↑↓ で移るためです。

よい例と悪い例

メモを書く分類を変える削除する
よい例危険な項目は線で分けて一番下に置く
メモを書く削除する分類を変える
悪い例削除がほかの項目の間にあると、押し間違えやすい

更新日

ページの一覧

索引を読み込んでいます。

メニュー