ポップオーバー
開くボタンを基準に表示されるフローティングパネル。
このページの内容
ポップオーバーは、開くボタンにひも付くフローティングパネルです。任意の要素にpopover属性と.popoverを追加し、ボタンのpopovertargetでその要素を参照します。ブラウザーはポップオーバーをトップレイヤーに表示し、外側をクリックするかEscを押すと閉じます。タブ順ではボタンの直後に入ります。配置にはCSSアンカー位置指定を使うため、位置調整用のスクリプトは不要です。
ポップオーバー
<button type="button" class="btn" popovertarget="share">Share</button><div id="share" popover class="popover"> <div class="flex flex-col gap-sm w(260)"> <p class="m(0) fg-text-strong">Share this project</p> <div class="input-group input-group-sm"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div></div><> <button type="button" className="btn" popoverTarget="share">Share</button> <div id="share" popover="" className="popover"> <div className="flex flex-col gap-sm w(260)"> <p className="m(0) fg-text-strong">Share this project</p> <div className="input-group input-group-sm"> <input className="input-group-field" defaultValue="https://atlas.dev/p/9f2c" readOnly aria-label="Link" /> <button type="button" className="btn">Copy</button> </div> </div> </div></><button type="button" class="btn" popovertarget="share">Share</button><div id="share" popover class="popover"> <div class="flex flex-col gap-sm w(260)"> <p class="m(0) fg-text-strong">Share this project</p> <div class="input-group input-group-sm"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div></div><template> <button type="button" class="btn" popovertarget="share">Share</button> <div id="share" popover class="popover"> <div class="flex flex-col gap-sm w(260)"> <p class="m(0) fg-text-strong">Share this project</p> <div class="input-group input-group-sm"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> </div></template>#配置
ポップオーバーはボタンの6px下に開き、ボタンの開始端に揃います。Popover APIが両者を関連付けるため、アンカーとなるボタンに名前を付ける必要はありません。ビューポートからはみ出す場合は、反対側に表示されます。
| クラス | 配置 |
|---|---|
popover | ボタンの下、開始端揃え |
popover-end | 終了端揃え。行末のボタン向け |
popover-center | ボタンに対して中央揃え |
popover-top | ボタンの上。popover-endまたはpopover-centerと併用可能 |
配置
<button type="button" class="btn" popovertarget="place-start">Start</button><button type="button" class="btn" popovertarget="place-center">Center</button><button type="button" class="btn" popovertarget="place-end">End</button><button type="button" class="btn" popovertarget="place-top">Top</button><div id="place-start" popover class="popover">Lined up with the start edge.</div><div id="place-center" popover class="popover popover-center">Centered on the button.</div><div id="place-end" popover class="popover popover-end">Lined up with the end edge.</div><div id="place-top" popover class="popover popover-top popover-center">Above the button.</div><> <button type="button" className="btn" popoverTarget="place-start">Start</button> <button type="button" className="btn" popoverTarget="place-center">Center</button> <button type="button" className="btn" popoverTarget="place-end">End</button> <button type="button" className="btn" popoverTarget="place-top">Top</button> <div id="place-start" popover="" className="popover">Lined up with the start edge.</div> <div id="place-center" popover="" className="popover popover-center">Centered on the button.</div> <div id="place-end" popover="" className="popover popover-end">Lined up with the end edge.</div> <div id="place-top" popover="" className="popover popover-top popover-center"> Above the button. </div></><button type="button" class="btn" popovertarget="place-start">Start</button><button type="button" class="btn" popovertarget="place-center">Center</button><button type="button" class="btn" popovertarget="place-end">End</button><button type="button" class="btn" popovertarget="place-top">Top</button><div id="place-start" popover class="popover">Lined up with the start edge.</div><div id="place-center" popover class="popover popover-center">Centered on the button.</div><div id="place-end" popover class="popover popover-end">Lined up with the end edge.</div><div id="place-top" popover class="popover popover-top popover-center">Above the button.</div><template> <button type="button" class="btn" popovertarget="place-start">Start</button> <button type="button" class="btn" popovertarget="place-center">Center</button> <button type="button" class="btn" popovertarget="place-end">End</button> <button type="button" class="btn" popovertarget="place-top">Top</button> <div id="place-start" popover class="popover">Lined up with the start edge.</div> <div id="place-center" popover class="popover popover-center">Centered on the button.</div> <div id="place-end" popover class="popover popover-end">Lined up with the end edge.</div> <div id="place-top" popover class="popover popover-top popover-center">Above the button.</div></template>CSSアンカー位置指定に対応していないブラウザーでも、ポップオーバーは開閉できます。表示位置はブラウザーの既定どおり、ビューポートの中央になります。
#フォーム付き
ポップオーバーにはフォームコントロールを配置できます。開いた直後はボタンにフォーカスが残り、Tabを押すとポップオーバー内に移動します。開いたときに最初のフィールドへフォーカスを移すには、そのフィールドにautofocusを指定します。
フォーム付き
<button type="button" class="btn" popovertarget="rename">Rename</button><form id="rename" popover class="popover"> <div class="flex flex-col gap-sm w(240)"> <label class="field">Project name <input class="input input-sm" value="atlas" autofocus></label> <div class="flex justify-end gap-xs"> <button type="button" class="btn btn-sm" popovertarget="rename" popovertargetaction="hide"> Cancel </button> <button type="button" class="btn btn-sm btn-primary" popovertarget="rename" popovertargetaction="hide"> Save </button> </div> </div></form><> <button type="button" className="btn" popoverTarget="rename">Rename</button> <form id="rename" popover="" className="popover"> <div className="flex flex-col gap-sm w(240)"> <label className="field"> Project name <input className="input input-sm" defaultValue="atlas" autoFocus /> </label> <div className="flex justify-end gap-xs"> <button type="button" className="btn btn-sm" popoverTarget="rename" popoverTargetAction="hide"> Cancel </button> <button type="button" className="btn btn-sm btn-primary" popoverTarget="rename" popoverTargetAction="hide"> Save </button> </div> </div> </form></><button type="button" class="btn" popovertarget="rename">Rename</button><form id="rename" popover class="popover"> <div class="flex flex-col gap-sm w(240)"> <label class="field">Project name <input class="input input-sm" value="atlas" autofocus></label> <div class="flex justify-end gap-xs"> <button type="button" class="btn btn-sm" popovertarget="rename" popovertargetaction="hide"> Cancel </button> <button type="button" class="btn btn-sm btn-primary" popovertarget="rename" popovertargetaction="hide"> Save </button> </div> </div></form><template> <button type="button" class="btn" popovertarget="rename">Rename</button> <form id="rename" popover class="popover"> <div class="flex flex-col gap-sm w(240)"> <label class="field"> Project name <input class="input input-sm" value="atlas" autofocus> </label> <div class="flex justify-end gap-xs"> <button type="button" class="btn btn-sm" popovertarget="rename" popovertargetaction="hide"> Cancel </button> <button type="button" class="btn btn-sm btn-primary" popovertarget="rename" popovertargetaction="hide"> Save </button> </div> </div> </form></template>#ホバーで表示
リンクやボタンにホバーするかフォーカスを合わせたときに、遷移先のプレビューとしてポップオーバーを開くこともできます。ポップオーバーにpopover="hint"を指定し、トリガーのinterestforで参照します。ヒントを開いても、すでに開いているメニューや別のポップオーバーは閉じません。タッチ画面では長押しでしか表示できないため、内容はプレビューにとどめ、ここでしか得られない情報は載せないでください。
ホバーで表示
<p class="m(0) text-sm fg-text-body"> Deployed by <a class="link" href="#ada" interestfor="ada-card">Ada Lovelace</a> two hours ago.</p><div id="ada-card" popover="hint" class="popover"> <div class="flex items-center gap-sm"> <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span> <div class="flex flex-col"> <span class="fg-text-strong">Ada Lovelace</span> <span class="text-xs fg-text-muted">Owner · 412 deployments</span> </div> </div></div><> <p className="m(0) text-sm fg-text-body"> Deployed by <a className="link" href="#ada" {...{ interestfor: 'ada-card' }}>Ada Lovelace</a> two hours ago. </p> <div id="ada-card" popover="hint" className="popover"> <div className="flex items-center gap-sm"> <span className="avatar avatar-lg"> <img className="avatar-image" src="/demo/portrait-1.svg" alt="" /> </span> <div className="flex flex-col"> <span className="fg-text-strong">Ada Lovelace</span> <span className="text-xs fg-text-muted">Owner · 412 deployments</span> </div> </div> </div></><p class="m(0) text-sm fg-text-body"> Deployed by <a class="link" href="#ada" interestfor="ada-card">Ada Lovelace</a> two hours ago.</p><div id="ada-card" popover="hint" class="popover"> <div class="flex items-center gap-sm"> <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span> <div class="flex flex-col"> <span class="fg-text-strong">Ada Lovelace</span> <span class="text-xs fg-text-muted">Owner · 412 deployments</span> </div> </div></div><template> <p class="m(0) text-sm fg-text-body"> Deployed by <a class="link" href="#ada" interestfor="ada-card">Ada Lovelace</a> two hours ago. </p> <div id="ada-card" popover="hint" class="popover"> <div class="flex items-center gap-sm"> <span class="avatar avatar-lg"> <img class="avatar-image" src="/demo/portrait-1.svg" alt> </span> <div class="flex flex-col"> <span class="fg-text-strong">Ada Lovelace</span> <span class="text-xs fg-text-muted">Owner · 412 deployments</span> </div> </div> </div></template>Interest InvokersはChromeとEdgeで利用できます。その他のブラウザーでは、リンクは通常どおり機能し、プレビューは表示されません。
#アクセシビリティ
- ボタンの
aria-expandedはブラウザーが設定するため、手動では指定しないでください。 - ポップオーバーは非モーダルなので、開いている間もページを操作できます。完了するか閉じるまで他の操作に移れないようにするには、ダイアログを使います。
- 操作の一覧にはメニュー、ホバー時に表示するラベルにはツールチップを使います。
#React、Svelte、Vue
popoverコンポーネントは、アプリケーションの状態に応じてポップオーバーを開閉し、ブラウザー側で開閉が起きたときに通知します。トリガーと内容はページ側のマークアップで記述します。modeはauto、manual、hintのいずれかです。hintはホバーで表示するプレビューで、トリガーにはpopovertargetではなくinterestforが付きます。そのため、プレビューを表示するリンクやボタンにスプレッドで渡してください。npx master-ui add popoverでプロジェクトにコピーできます。コンポーネントの構成と使い方はReact、Svelte、Vueを参照してください。
ポップオーバーを指すトリガーが 1 つなら、アプリケーションの状態で開いた場合も押して開いた場合と同じアンカーを使います。独自のスクリプトでは popover.showPopover({ source: trigger }) でトリガーを渡せます。トリガーが複数ある場合は、実際に開いた要素を渡します。source が使えない場合も固定のトリガーを基準に配置するには、トリガーに details-trigger、ポップオーバーに details-popover を付け、同じアンカー名を指定します。
.details-trigger { anchor-name: --details; }.details-popover { position-anchor: --details; }#クラス
| クラス | 説明 |
|---|---|
popover | フローティングシート。popover 属性のある要素に適用し、属性がなければ表示されません。ボタンの始端に揃え、ボタンの下に --anchored-gap (6px) の間隔を空けて開きます。ビューポートの端では配置が反転します。 |
popover-end | 行末のボタン向け。ボタンの終端に揃えるpopover と併用します |
popover-center | ボタンの中央に揃えるpopover と併用します |
popover-top | ボタンの上に開く。popover-endまたはpopover-centerと併用可popover と併用します |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--popover-padding | 1rem | シートの内側のパディング |
--popover-max-width | 22rem | テキストが折り返す前にシートが広がる最大幅 |
--anchored-gap | サーフェスとそれを開いたコントロールの間隔。未設定時は 6px指定先:popover |