彈出面板
錨定在開啟按鈕旁的彈出面板。
本頁內容
彈出面板依附於開啟它的按鈕。在任一元素加上 popover 屬性和 .popover,再用 popovertarget 讓按鈕指向該元素。瀏覽器會將面板移到頂層,點擊面板外部或按下 Esc 時關閉,並讓面板在 Tab 順序中緊接在按鈕之後。面板由 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 會將按鈕與面板連結,因此無須另外指定 CSS 錨點名稱。面板若會超出檢視區,就會翻到另一側。
| 類別 | 放置位置 |
|---|---|
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>瀏覽器若不支援錨點定位,彈出面板仍可正常開關,並會依照瀏覽器對彈出面板的預設定位顯示在檢視區中央。
#搭配表單
彈出面板可以容納控制項。面板開啟時,焦點會留在按鈕上。按 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>Chrome 和 Edge 已支援這類預覽觸發元素。其他瀏覽器仍可正常開啟連結,但不會顯示預覽。
#無障礙
- 瀏覽器會為按鈕設定
aria-expanded,請勿自行設定。 - 彈出面板是非模態的,開啟時使用者仍可操作頁面。如果必須先完成或取消一項任務,請使用對話框。
- 使用選單列出可執行的操作,使用工具提示顯示指標懸停時的標籤。
#React、Svelte 和 Vue
popover 元件會依應用程式狀態開關彈出面板,也會在瀏覽器開關面板時發出通知。觸發元素和內容仍由頁面自己的標記定義。它的 mode 可以是 auto、manual 或 hint:hint 是指標懸停時顯示的預覽,傳給觸發元素的屬性會是 interestfor 而不是 popovertarget,因此請將這些屬性展開套用到顯示預覽的連結或按鈕上。執行 npx master-ui add popover 即可將元件複製到專案中。請參閱 React、Svelte 和 Vue,了解元件的建構和使用方式。
若只有一個觸發元素指向彈出面板,受控開啟時會沿用按下該元素時的錨點。在自己的指令碼中,可用 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 |