ポップオーバーとメニュー
ボタンのすぐ下に小さく開く面です。操作を並べるメニューにも使い、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 の呼び出し方
<>
<IconButton
label="家賃の明細の操作"
icon={<Ellipsis strokeWidth={1.5} aria-hidden="true" />}
aria-haspopup="menu"
aria-expanded="false"
popoverTarget="rent-menu"
style={{ anchorName: '--rent-menu' }}
/>
<Popover id="rent-menu" anchorName="--rent-menu">
<Menu aria-label="家賃の明細の操作">
<MenuItem>分類を変える</MenuItem>
<MenuItem>メモを書く</MenuItem>
<MenuItem>翌月にも入れる</MenuItem>
<MenuSeparator />
<MenuItem tone="danger">削除する</MenuItem>
</Menu>
</Popover>
</>実装を見る(react/Popover.tsx)
React の実装(react/Popover.tsx)
/**
* Popover — ポップオーバー(docs/spec/components.md「popover・menu」)。見た目は css/components/popover.css だけが持つ。
* - Popover API:開くボタンに popoverTarget={id} を付ける。Esc と外側の押下で閉じ、フォーカスはボタンへ戻る
* - 位置は CSS Anchor Positioning。ボタンに style={{ anchorName }} を付け、ここに同じ anchorName を渡す
* - Anchor Positioning が使えないブラウザでは、開いたときにボタンの下へ置く(--_x・--_y を渡すだけ)
* - 開閉に合わせて、開くボタンの aria-expanded を変える
*/
import {
forwardRef,
useImperativeHandle,
useRef,
type CSSProperties,
type HTMLAttributes,
type ToggleEvent,
} from 'react';
export interface PopoverProps extends Omit<HTMLAttributes<HTMLDivElement>, 'popover'> {
id: string;
/** 開くボタンの anchor-name(例:'--mz-menu-row')。ボタン側にも style={{ anchorName }} を付ける */
anchorName: string;
/** auto(既定)は外側の押下と Esc で閉じる */
mode?: 'auto' | 'manual';
}
export const Popover = forwardRef<HTMLDivElement, PopoverProps>(function Popover(
{ id, anchorName, mode = 'auto', className, style, onToggle, ...rest },
ref,
) {
const inner = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => inner.current as HTMLDivElement);
const handleToggle = (event: ToggleEvent<HTMLDivElement>) => {
const el = event.currentTarget;
const open = event.newState === 'open';
const trigger = document.querySelector<HTMLElement>(`[popovertarget="${CSS.escape(id)}"]`);
trigger?.setAttribute('aria-expanded', String(open));
if (open && trigger && !supportsAnchor()) placeBelow(el, trigger);
onToggle?.(event);
};
return (
<div
ref={inner}
id={id}
popover={mode}
className={className ? `mz-popover ${className}` : 'mz-popover'}
style={{ positionAnchor: anchorName, ...style } as CSSProperties}
onToggle={handleToggle}
{...rest}
/>
);
});
function supportsAnchor(): boolean {
return typeof CSS !== 'undefined' && CSS.supports('position-area: block-end');
}
/** Anchor Positioning が使えないときだけ:ボタンの下・左端そろえ。下に入らなければ上へ */
function placeBelow(el: HTMLElement, trigger: HTMLElement): void {
const r = trigger.getBoundingClientRect();
const h = el.offsetHeight;
// ボタンとの間(--_offset)は CSS の margin-block で付く。上に置くときは上下の margin の分だけ上へずらす
const offset = parseFloat(getComputedStyle(el).marginBlockStart) || 0;
const below = r.bottom + offset + h <= window.innerHeight;
el.style.setProperty('--_x', `${Math.max(0, Math.min(r.left, window.innerWidth - el.offsetWidth))}px`);
el.style.setProperty('--_y', `${below ? r.bottom : Math.max(0, r.top - h - offset * 2)}px`);
}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 ではなく ↑↓ で移るためです。
よい例と悪い例
更新日