弹出层
以触发按钮为锚点的浮动面板。
本页内容
弹出层是依附于触发按钮的浮动面板。为任意元素添加 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 将两者关联起来,按钮无需命名即可作为锚点。如果弹出层会超出视口,它就会翻转到按钮的另一侧。
| 类名 | 位置 |
|---|---|
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 已支持通过 interestfor 在悬停或聚焦时触发预览。在其他浏览器中,链接仍可正常使用,预览不会显示。
#无障碍
- 按钮的
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 上设置 |