对话框
以浮层面板呈现的原生模态对话框,打开和关闭均无需脚本。
本页内容
对话框是层次最高的表面。浮层面板位于遮罩上方,遮罩压暗页面并弱化背景细节。在原生 <dialog> 上使用 .dialog。带有 commandfor 和 command="show-modal" 的按钮可以打开对话框,浏览器会将对话框置于顶层,将焦点限制在框内,并支持按 Esc 关闭,全程无需脚本。
对话框
<button type="button" class="btn" commandfor="delete-project" command="show-modal"> Delete project</button><dialog id="delete-project" class="dialog" closedby="any" aria-labelledby="delete-project-title"> <header class="dialog-header"> <h2 class="dialog-title" id="delete-project-title">Delete this project?</h2> <p class="dialog-description"> Atlas and its 14 deployments are removed, and its domains stop resolving at once. </p> </header> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="delete-project" command="close">Cancel</button> <button type="button" class="btn btn-primary" commandfor="delete-project" command="close"> Delete project </button> </footer></dialog><> <button type="button" className="btn" {...{ commandfor: 'delete-project', command: 'show-modal' }}> Delete project </button> <dialog id="delete-project" className="dialog" closedby="any" aria-labelledby="delete-project-title"> <header className="dialog-header"> <h2 className="dialog-title" id="delete-project-title">Delete this project?</h2> <p className="dialog-description"> Atlas and its 14 deployments are removed, and its domains stop resolving at once. </p> </header> <footer className="dialog-footer"> <button type="button" className="btn" {...{ commandfor: 'delete-project', command: 'close' }}> Cancel </button> <button type="button" className="btn btn-primary" {...{ commandfor: 'delete-project', command: 'close' }}> Delete project </button> </footer> </dialog></><button type="button" class="btn" commandfor="delete-project" command="show-modal"> Delete project</button><dialog id="delete-project" class="dialog" closedby="any" aria-labelledby="delete-project-title"> <header class="dialog-header"> <h2 class="dialog-title" id="delete-project-title">Delete this project?</h2> <p class="dialog-description"> Atlas and its 14 deployments are removed, and its domains stop resolving at once. </p> </header> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="delete-project" command="close">Cancel</button> <button type="button" class="btn btn-primary" commandfor="delete-project" command="close"> Delete project </button> </footer></dialog><template> <button type="button" class="btn" commandfor="delete-project" command="show-modal"> Delete project </button> <dialog id="delete-project" class="dialog" closedby="any" aria-labelledby="delete-project-title"> <header class="dialog-header"> <h2 class="dialog-title" id="delete-project-title">Delete this project?</h2> <p class="dialog-description"> Atlas and its 14 deployments are removed, and its domains stop resolving at once. </p> </header> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="delete-project" command="close">Cancel</button> <button type="button" class="btn btn-primary" commandfor="delete-project" command="close"> Delete project </button> </footer> </dialog></template>#打开和关闭
三个属性即可实现过去需要脚本处理的模态对话框行为:
- 在按钮上将
commandfor="id"与command="show-modal"搭配使用,即可将对话框以模态方式打开。command="close"关闭对话框,并将焦点还给打开它的按钮。 - 在对话框上添加
closedby="any"后,点击遮罩也能关闭对话框。按 Esc 始终可以关闭。如果误点可能导致工作成果丢失,请省略此属性。在不支持此属性的浏览器中,Esc 和关闭按钮仍然有效。 - 对话框内的
<form method="dialog">会在提交时关闭对话框,并通过对话框的returnValue报告使用了哪个按钮。
模态对话框打开时,后方页面无法交互,也不会滚动。
#包含表单
dialog-body 容纳头部和底部之间的内容,并占用剩余高度。当内容超出视口允许的高度时,可在面板内滚动。dialog-close 在角落放置一个低调的方形按钮,请为它提供无障碍名称。
包含表单
<button type="button" class="btn btn-primary" commandfor="invite" command="show-modal"> Invite member</button><dialog id="invite" class="dialog" aria-labelledby="invite-title"> <header class="dialog-header"> <h2 class="dialog-title" id="invite-title">Invite a member</h2> <p class="dialog-description">They receive an email with a link that expires in seven days.</p> </header> <form class="dialog-body flex flex-col gap-md" method="dialog" id="invite-form"> <label class="field"> Email <input class="input" type="email" placeholder="ada@example.com" required autofocus> </label> <label class="field"> Role <select class="select"> <option class="select-option">Member</option> <option class="select-option">Admin</option> <option class="select-option">Billing</option> </select> </label> </form> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="invite" command="close">Cancel</button> <button type="submit" class="btn btn-primary" form="invite-form">Send invite</button> </footer> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="invite" command="close" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button></dialog>import XIcon from '@master/icons-react/native/16/soft/x'<> <button type="button" className="btn btn-primary" {...{ commandfor: 'invite', command: 'show-modal' }}> Invite member </button> <dialog id="invite" className="dialog" aria-labelledby="invite-title"> <header className="dialog-header"> <h2 className="dialog-title" id="invite-title">Invite a member</h2> <p className="dialog-description"> They receive an email with a link that expires in seven days. </p> </header> <form className="dialog-body flex flex-col gap-md" method="dialog" id="invite-form"> <label className="field"> Email <input className="input" type="email" placeholder="ada@example.com" required autoFocus /> </label> <label className="field"> Role <select className="select"> <option className="select-option">Member</option> <option className="select-option">Admin</option> <option className="select-option">Billing</option> </select> </label> </form> <footer className="dialog-footer"> <button type="button" className="btn" {...{ commandfor: 'invite', command: 'close' }}> Cancel </button> <button type="submit" className="btn btn-primary" form="invite-form">Send invite</button> </footer> <button type="button" className="btn btn-quiet btn-square btn-sm dialog-close" {...{ commandfor: 'invite', command: 'close' }} aria-label="Close"> <XIcon className="icon" /> </button> </dialog></><script> import XIcon from '@master/icons-svelte/native/16/soft/x'</script><button type="button" class="btn btn-primary" commandfor="invite" command="show-modal"> Invite member</button><dialog id="invite" class="dialog" aria-labelledby="invite-title"> <header class="dialog-header"> <h2 class="dialog-title" id="invite-title">Invite a member</h2> <p class="dialog-description">They receive an email with a link that expires in seven days.</p> </header> <form class="dialog-body flex flex-col gap-md" method="dialog" id="invite-form"> <label class="field"> Email <input class="input" type="email" placeholder="ada@example.com" required autofocus> </label> <label class="field"> Role <select class="select"> <option class="select-option">Member</option> <option class="select-option">Admin</option> <option class="select-option">Billing</option> </select> </label> </form> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="invite" command="close">Cancel</button> <button type="submit" class="btn btn-primary" form="invite-form">Send invite</button> </footer> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="invite" command="close" aria-label="Close"> <XIcon class="icon" /> </button></dialog><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <button type="button" class="btn btn-primary" commandfor="invite" command="show-modal"> Invite member </button> <dialog id="invite" class="dialog" aria-labelledby="invite-title"> <header class="dialog-header"> <h2 class="dialog-title" id="invite-title">Invite a member</h2> <p class="dialog-description"> They receive an email with a link that expires in seven days. </p> </header> <form class="dialog-body flex flex-col gap-md" method="dialog" id="invite-form"> <label class="field"> Email <input class="input" type="email" placeholder="ada@example.com" required autofocus> </label> <label class="field"> Role <select class="select"> <option class="select-option">Member</option> <option class="select-option">Admin</option> <option class="select-option">Billing</option> </select> </label> </form> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="invite" command="close">Cancel</button> <button type="submit" class="btn btn-primary" form="invite-form">Send invite</button> </footer> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="invite" command="close" aria-label="Close"> <XIcon class="icon" /> </button> </dialog></template>#尺寸
对话框默认宽 28rem。dialog-sm 宽 22rem,适合一句话的确认提示。dialog-lg 宽 40rem。在窄屏上,各尺寸的对话框与视口左右边缘各留出 1rem。底部通过容器查询响应对话框自身的宽度。宽度小于 22rem 时,按钮纵向排列并占满可用宽度,主要操作排在最上方。
尺寸
<button type="button" class="btn" commandfor="size-sm" command="show-modal">Small</button><button type="button" class="btn" commandfor="size-lg" command="show-modal">Large</button><dialog id="size-sm" class="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title"> <header class="dialog-header"> <h2 class="dialog-title" id="size-sm-title">Sign out?</h2> <p class="dialog-description">Unsaved drafts stay on this device.</p> </header> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="size-sm" command="close">Stay</button> <button type="button" class="btn btn-primary" commandfor="size-sm" command="close"> Sign out </button> </footer></dialog><dialog id="size-lg" class="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title"> <header class="dialog-header"> <h2 class="dialog-title" id="size-lg-title">Release notes</h2> <p class="dialog-description"> Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips. </p> </header> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="size-lg" command="close">Done</button> </footer></dialog><> <button type="button" className="btn" {...{ commandfor: 'size-sm', command: 'show-modal' }}> Small </button> <button type="button" className="btn" {...{ commandfor: 'size-lg', command: 'show-modal' }}> Large </button> <dialog id="size-sm" className="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title"> <header className="dialog-header"> <h2 className="dialog-title" id="size-sm-title">Sign out?</h2> <p className="dialog-description">Unsaved drafts stay on this device.</p> </header> <footer className="dialog-footer"> <button type="button" className="btn" {...{ commandfor: 'size-sm', command: 'close' }}> Stay </button> <button type="button" className="btn btn-primary" {...{ commandfor: 'size-sm', command: 'close' }}> Sign out </button> </footer> </dialog> <dialog id="size-lg" className="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title"> <header className="dialog-header"> <h2 className="dialog-title" id="size-lg-title">Release notes</h2> <p className="dialog-description"> Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips. </p> </header> <footer className="dialog-footer"> <button type="button" className="btn btn-primary" {...{ commandfor: 'size-lg', command: 'close' }}> Done </button> </footer> </dialog></><button type="button" class="btn" commandfor="size-sm" command="show-modal">Small</button><button type="button" class="btn" commandfor="size-lg" command="show-modal">Large</button><dialog id="size-sm" class="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title"> <header class="dialog-header"> <h2 class="dialog-title" id="size-sm-title">Sign out?</h2> <p class="dialog-description">Unsaved drafts stay on this device.</p> </header> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="size-sm" command="close">Stay</button> <button type="button" class="btn btn-primary" commandfor="size-sm" command="close"> Sign out </button> </footer></dialog><dialog id="size-lg" class="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title"> <header class="dialog-header"> <h2 class="dialog-title" id="size-lg-title">Release notes</h2> <p class="dialog-description"> Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips. </p> </header> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="size-lg" command="close">Done</button> </footer></dialog><template> <button type="button" class="btn" commandfor="size-sm" command="show-modal">Small</button> <button type="button" class="btn" commandfor="size-lg" command="show-modal">Large</button> <dialog id="size-sm" class="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title"> <header class="dialog-header"> <h2 class="dialog-title" id="size-sm-title">Sign out?</h2> <p class="dialog-description">Unsaved drafts stay on this device.</p> </header> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="size-sm" command="close">Stay</button> <button type="button" class="btn btn-primary" commandfor="size-sm" command="close"> Sign out </button> </footer> </dialog> <dialog id="size-lg" class="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title"> <header class="dialog-header"> <h2 class="dialog-title" id="size-lg-title">Release notes</h2> <p class="dialog-description"> Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips. </p> </header> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="size-lg" command="close"> Done </button> </footer> </dialog></template>#抽屉
dialog-drawer 是位于行内结束侧的面板,适合在保持页面可见的同时提供筛选、详情和设置。它距视口边缘 8px,使圆角和阴影完整可见,并从所在边缘滑入。
抽屉
<button type="button" class="btn" commandfor="filters" command="show-modal">Filters</button><dialog id="filters" class="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title"> <header class="dialog-header"> <h2 class="dialog-title" id="filters-title">Filters</h2> <p class="dialog-description">Narrow the list of deployments.</p> </header> <div class="dialog-body flex flex-col gap-md"> <label class="field">Branch <input class="input" value="main"></label> <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label> </div> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="filters" command="close">Reset</button> <button type="button" class="btn btn-primary" commandfor="filters" command="close"> Apply </button> </footer></dialog><> <button type="button" className="btn" {...{ commandfor: 'filters', command: 'show-modal' }}> Filters </button> <dialog id="filters" className="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title"> <header className="dialog-header"> <h2 className="dialog-title" id="filters-title">Filters</h2> <p className="dialog-description">Narrow the list of deployments.</p> </header> <div className="dialog-body flex flex-col gap-md"> <label className="field">Branch <input className="input" defaultValue="main" /></label> <label className="check"> <input type="checkbox" className="checkbox" defaultChecked /> Production only </label> </div> <footer className="dialog-footer"> <button type="button" className="btn" {...{ commandfor: 'filters', command: 'close' }}> Reset </button> <button type="button" className="btn btn-primary" {...{ commandfor: 'filters', command: 'close' }}> Apply </button> </footer> </dialog></><button type="button" class="btn" commandfor="filters" command="show-modal">Filters</button><dialog id="filters" class="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title"> <header class="dialog-header"> <h2 class="dialog-title" id="filters-title">Filters</h2> <p class="dialog-description">Narrow the list of deployments.</p> </header> <div class="dialog-body flex flex-col gap-md"> <label class="field">Branch <input class="input" value="main"></label> <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label> </div> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="filters" command="close">Reset</button> <button type="button" class="btn btn-primary" commandfor="filters" command="close"> Apply </button> </footer></dialog><template> <button type="button" class="btn" commandfor="filters" command="show-modal">Filters</button> <dialog id="filters" class="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title"> <header class="dialog-header"> <h2 class="dialog-title" id="filters-title">Filters</h2> <p class="dialog-description">Narrow the list of deployments.</p> </header> <div class="dialog-body flex flex-col gap-md"> <label class="field">Branch <input class="input" value="main"></label> <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label> </div> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="filters" command="close">Reset</button> <button type="button" class="btn btn-primary" commandfor="filters" command="close"> Apply </button> </footer> </dialog></template>#行内起始侧
添加 dialog-drawer-start 后,抽屉位于行内起始侧,宽 18rem,适合放置导航。侧边栏在窄布局中收起后,可以此抽屉的形式重新打开。参见应用框架。
行内起始侧的抽屉
<button type="button" class="btn" commandfor="nav-drawer" command="show-modal"> Open navigation</button><dialog id="nav-drawer" class="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation"> <div class="dialog-body"> <nav class="nav" aria-label="Workspace"> <a class="nav-item" href="#" aria-current="page">Overview</a> <a class="nav-item" href="#">Projects</a> <a class="nav-item" href="#">Domains</a> <a class="nav-item" href="#">Settings</a> </nav> </div></dialog><> <button type="button" className="btn" {...{ commandfor: 'nav-drawer', command: 'show-modal' }}> Open navigation </button> <dialog id="nav-drawer" className="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation"> <div className="dialog-body"> <nav className="nav" aria-label="Workspace"> <a className="nav-item" href="#" aria-current="page">Overview</a> <a className="nav-item" href="#">Projects</a> <a className="nav-item" href="#">Domains</a> <a className="nav-item" href="#">Settings</a> </nav> </div> </dialog></><button type="button" class="btn" commandfor="nav-drawer" command="show-modal"> Open navigation</button><dialog id="nav-drawer" class="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation"> <div class="dialog-body"> <nav class="nav" aria-label="Workspace"> <a class="nav-item" href="#" aria-current="page">Overview</a> <a class="nav-item" href="#">Projects</a> <a class="nav-item" href="#">Domains</a> <a class="nav-item" href="#">Settings</a> </nav> </div></dialog><template> <button type="button" class="btn" commandfor="nav-drawer" command="show-modal"> Open navigation </button> <dialog id="nav-drawer" class="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation"> <div class="dialog-body"> <nav class="nav" aria-label="Workspace"> <a class="nav-item" href="#" aria-current="page">Overview</a> <a class="nav-item" href="#">Projects</a> <a class="nav-item" href="#">Domains</a> <a class="nav-item" href="#">Settings</a> </nav> </div> </dialog></template>#底部面板
dialog-sheet 从底边升起,位于小屏幕上拇指容易触及的位置,适合简短任务或选项列表。它铺满可用宽度,最大宽度由 --dialog-width 指定,底部两角以直角贴合边缘,并用 env(safe-area-inset-bottom) 为主屏幕指示条留出空间。
底部面板
<button type="button" class="btn" commandfor="share-sheet" command="show-modal">Share</button><dialog id="share-sheet" class="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title"> <header class="dialog-header"> <h2 class="dialog-title" id="share-sheet-title">Share this project</h2> <p class="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div class="dialog-body"> <div class="input-group"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="share-sheet" command="close"> Done </button> </footer></dialog><> <button type="button" className="btn" {...{ commandfor: 'share-sheet', command: 'show-modal' }}> Share </button> <dialog id="share-sheet" className="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title"> <header className="dialog-header"> <h2 className="dialog-title" id="share-sheet-title">Share this project</h2> <p className="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div className="dialog-body"> <div className="input-group"> <input className="input-group-field" defaultValue="https://atlas.dev/p/9f2c" readOnly aria-label="Link" /> <button type="button" className="btn">Copy</button> </div> </div> <footer className="dialog-footer"> <button type="button" className="btn btn-primary" {...{ commandfor: 'share-sheet', command: 'close' }}> Done </button> </footer> </dialog></><button type="button" class="btn" commandfor="share-sheet" command="show-modal">Share</button><dialog id="share-sheet" class="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title"> <header class="dialog-header"> <h2 class="dialog-title" id="share-sheet-title">Share this project</h2> <p class="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div class="dialog-body"> <div class="input-group"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="share-sheet" command="close"> Done </button> </footer></dialog><template> <button type="button" class="btn" commandfor="share-sheet" command="show-modal">Share</button> <dialog id="share-sheet" class="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title"> <header class="dialog-header"> <h2 class="dialog-title" id="share-sheet-title">Share this project</h2> <p class="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div class="dialog-body"> <div class="input-group"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="share-sheet" command="close"> Done </button> </footer> </dialog></template>#滑动关闭
为抽屉或底部面板添加 dialog-swipe 后,它会跟随手指、触控板或鼠标移动,松手即可关闭。对话框本身变为面板移动的轨道:透明,与视口一样大。将对话框原有的全部内容放进带有材质的 dialog-panel,并为底部面板添加 dialog-handle,即顶部边缘的一道短凹槽,提示从哪里拖动。挂载 drawer 行为脚本;attach() 会找到所有 dialog-swipe。
可滑动关闭的底部面板
<button type="button" class="btn" commandfor="share-swipe" command="show-modal">Share</button><dialog id="share-swipe" class="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title"> <div class="dialog-panel"> <div class="dialog-handle" aria-hidden="true"></div> <header class="dialog-header"> <h2 class="dialog-title" id="share-swipe-title">Share this project</h2> <p class="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div class="dialog-body"> <div class="input-group"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="share-swipe" command="close"> Done </button> </footer> </div></dialog><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <button type="button" className="btn" {...{ commandfor: 'share-swipe', command: 'show-modal' }}> Share </button> <dialog id="share-swipe" className="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title"> <div className="dialog-panel"> <div className="dialog-handle" aria-hidden="true"></div> <header className="dialog-header"> <h2 className="dialog-title" id="share-swipe-title">Share this project</h2> <p className="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div className="dialog-body"> <div className="input-group"> <input className="input-group-field" defaultValue="https://atlas.dev/p/9f2c" readOnly aria-label="Link" /> <button type="button" className="btn">Copy</button> </div> </div> <footer className="dialog-footer"> <button type="button" className="btn btn-primary" {...{ commandfor: 'share-swipe', command: 'close' }}> Done </button> </footer> </div> </dialog> </div> )}<script> import { attach } from '@master/ui/behaviors'</script><div {@attach attach}> <button type="button" class="btn" commandfor="share-swipe" command="show-modal">Share</button> <dialog id="share-swipe" class="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title"> <div class="dialog-panel"> <div class="dialog-handle" aria-hidden="true"></div> <header class="dialog-header"> <h2 class="dialog-title" id="share-swipe-title">Share this project</h2> <p class="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div class="dialog-body"> <div class="input-group"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="share-swipe" command="close"> Done </button> </footer> </div> </dialog></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <button type="button" class="btn" commandfor="share-swipe" command="show-modal">Share</button> <dialog id="share-swipe" class="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title"> <div class="dialog-panel"> <div class="dialog-handle" aria-hidden="true"></div> <header class="dialog-header"> <h2 class="dialog-title" id="share-swipe-title">Share this project</h2> <p class="dialog-description">Anyone with the link can view the latest deployment.</p> </header> <div class="dialog-body"> <div class="input-group"> <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link"> <button type="button" class="btn">Copy</button> </div> </div> <footer class="dialog-footer"> <button type="button" class="btn btn-primary" commandfor="share-swipe" command="close"> Done </button> </footer> </div> </dialog> </div></template>抽屉的滑动方式相同,朝其停靠的边缘滑动,不需要手柄:整个面板都可以拖动。
可滑动关闭的抽屉
<button type="button" class="btn" commandfor="filters-swipe" command="show-modal">Filters</button><dialog id="filters-swipe" class="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title"> <div class="dialog-panel"> <header class="dialog-header"> <h2 class="dialog-title" id="filters-swipe-title">Filters</h2> <p class="dialog-description">Narrow the list of deployments.</p> </header> <div class="dialog-body flex flex-col gap-md"> <label class="field">Branch <input class="input" value="main"></label> <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label> </div> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="filters-swipe" command="close">Reset</button> <button type="button" class="btn btn-primary" commandfor="filters-swipe" command="close"> Apply </button> </footer> </div></dialog><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <button type="button" className="btn" {...{ commandfor: 'filters-swipe', command: 'show-modal' }}> Filters </button> <dialog id="filters-swipe" className="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title"> <div className="dialog-panel"> <header className="dialog-header"> <h2 className="dialog-title" id="filters-swipe-title">Filters</h2> <p className="dialog-description">Narrow the list of deployments.</p> </header> <div className="dialog-body flex flex-col gap-md"> <label className="field">Branch <input className="input" defaultValue="main" /></label> <label className="check"> <input type="checkbox" className="checkbox" defaultChecked /> Production only </label> </div> <footer className="dialog-footer"> <button type="button" className="btn" {...{ commandfor: 'filters-swipe', command: 'close' }}> Reset </button> <button type="button" className="btn btn-primary" {...{ commandfor: 'filters-swipe', command: 'close' }}> Apply </button> </footer> </div> </dialog> </div> )}<script> import { attach } from '@master/ui/behaviors'</script><div {@attach attach}> <button type="button" class="btn" commandfor="filters-swipe" command="show-modal">Filters</button> <dialog id="filters-swipe" class="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title"> <div class="dialog-panel"> <header class="dialog-header"> <h2 class="dialog-title" id="filters-swipe-title">Filters</h2> <p class="dialog-description">Narrow the list of deployments.</p> </header> <div class="dialog-body flex flex-col gap-md"> <label class="field">Branch <input class="input" value="main"></label> <label class="check"> <input type="checkbox" class="checkbox" checked> Production only </label> </div> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="filters-swipe" command="close">Reset</button> <button type="button" class="btn btn-primary" commandfor="filters-swipe" command="close"> Apply </button> </footer> </div> </dialog></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <button type="button" class="btn" commandfor="filters-swipe" command="show-modal"> Filters </button> <dialog id="filters-swipe" class="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title"> <div class="dialog-panel"> <header class="dialog-header"> <h2 class="dialog-title" id="filters-swipe-title">Filters</h2> <p class="dialog-description">Narrow the list of deployments.</p> </header> <div class="dialog-body flex flex-col gap-md"> <label class="field">Branch <input class="input" value="main"></label> <label class="check"> <input type="checkbox" class="checkbox" checked> Production only </label> </div> <footer class="dialog-footer"> <button type="button" class="btn" commandfor="filters-swipe" command="close"> Reset </button> <button type="button" class="btn btn-primary" commandfor="filters-swipe" command="close"> Apply </button> </footer> </div> </dialog> </div></template>滑动关闭的实现方式:
- 由浏览器移动面板。 轨道是一个带有吸附点的滚动容器:一个是打开时的停靠位置,另一个是越过边缘、与视口一样长的区域,面板松手后会滑到那里。手指、触控板和滚轮都能滚动它,因此跟手移动、轻扫的惯性以及最终停在哪里,都由浏览器自行处理。轨道采用反向排列的 Flex 布局,面板位于其末端,因此无需脚本即可从打开时的停靠位置开始。
- 行为脚本处理滚动做不到的事。 无论面板以何种方式离开轨道,脚本都会在其离开后关闭对话框。它恢复
closedby="any"的效果:轨道覆盖了浏览器监听点击的遮罩,因此由行为脚本在点击面板旁边时关闭对话框。鼠标无法通过拖动来滚动,脚本让鼠标可以按住手柄或面板上任何不是控件或正文的部分来拖动面板,并在松手时让面板停到被甩向的那个停靠位置,或较近的那个;精确指针按住面板时,轨道停止吸附。脚本将遮罩剩余的比例写入--dialog-scrim,因此遮罩会随面板离开而逐渐变淡。 - 对话框仍是对话框。 Esc、关闭按钮、焦点限制、焦点返回触发按钮以及滑入滑出动画,都与其他抽屉或底部面板相同。
- 可滚动的正文保留自己的滚动。 使用鼠标或滚轮时,在
dialog-body中拖动会滚动正文或选中文字。在触摸屏上,拖动到正文顶部后继续拖动会移动底部面板。
滑动只是一种快捷方式:保留关闭按钮或能关闭对话框的操作,并保留 Esc。
#停靠高度
为可滑动的底部面板添加 dialog-detents 后,它会停在两个高度:打开时的半个视口高度,以及全高。拖动或轻扫可在两者之间切换;从全高向下轻扫会停在半高,只有从半高向下轻扫才会关闭。将手柄设为带有无障碍名称的 <button>:点击、按 Enter 或 Space 会将底部面板切换到另一个高度,按 ↑ 展开到全高,按 ↓ 收起到半高。其 aria-expanded 表示底部面板是否处于全高。
带停靠高度的底部面板
<button type="button" class="btn" commandfor="comments-sheet" command="show-modal">Comments</button><dialog id="comments-sheet" class="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title"> <div class="dialog-panel"> <button type="button" class="dialog-handle" aria-label="Sheet height"></button> <header class="dialog-header"> <h2 class="dialog-title" id="comments-sheet-title">Comments</h2> <p class="dialog-description">Five people reviewed this deployment.</p> </header> <div class="dialog-body flex flex-col gap-md"> <p class="m(0)">Ada: the build on main is green again.</p> <p class="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p> <p class="m(0)">Linus: the cache headers on the fonts are right now.</p> <p class="m(0)">Margaret: the rollback plan is in the runbook.</p> <p class="m(0)">Alan: ship it after the 3pm check.</p> </div> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="comments-sheet" command="close" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div></dialog><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <button type="button" className="btn" {...{ commandfor: 'comments-sheet', command: 'show-modal' }}> Comments </button> <dialog id="comments-sheet" className="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title"> <div className="dialog-panel"> <button type="button" className="dialog-handle" aria-label="Sheet height"></button> <header className="dialog-header"> <h2 className="dialog-title" id="comments-sheet-title">Comments</h2> <p className="dialog-description">Five people reviewed this deployment.</p> </header> <div className="dialog-body flex flex-col gap-md"> <p className="m(0)">Ada: the build on main is green again.</p> <p className="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p> <p className="m(0)">Linus: the cache headers on the fonts are right now.</p> <p className="m(0)">Margaret: the rollback plan is in the runbook.</p> <p className="m(0)">Alan: ship it after the 3pm check.</p> </div> <button type="button" className="btn btn-quiet btn-square btn-sm dialog-close" {...{ commandfor: 'comments-sheet', command: 'close' }} aria-label="Close"> <XIcon className="icon" /> </button> </div> </dialog> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div {@attach attach}> <button type="button" class="btn" commandfor="comments-sheet" command="show-modal"> Comments </button> <dialog id="comments-sheet" class="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title"> <div class="dialog-panel"> <button type="button" class="dialog-handle" aria-label="Sheet height"></button> <header class="dialog-header"> <h2 class="dialog-title" id="comments-sheet-title">Comments</h2> <p class="dialog-description">Five people reviewed this deployment.</p> </header> <div class="dialog-body flex flex-col gap-md"> <p class="m(0)">Ada: the build on main is green again.</p> <p class="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p> <p class="m(0)">Linus: the cache headers on the fonts are right now.</p> <p class="m(0)">Margaret: the rollback plan is in the runbook.</p> <p class="m(0)">Alan: ship it after the 3pm check.</p> </div> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="comments-sheet" command="close" aria-label="Close"> <XIcon class="icon" /> </button> </div> </dialog></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <button type="button" class="btn" commandfor="comments-sheet" command="show-modal"> Comments </button> <dialog id="comments-sheet" class="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title"> <div class="dialog-panel"> <button type="button" class="dialog-handle" aria-label="Sheet height"></button> <header class="dialog-header"> <h2 class="dialog-title" id="comments-sheet-title">Comments</h2> <p class="dialog-description">Five people reviewed this deployment.</p> </header> <div class="dialog-body flex flex-col gap-md"> <p class="m(0)">Ada: the build on main is green again.</p> <p class="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p> <p class="m(0)">Linus: the cache headers on the fonts are right now.</p> <p class="m(0)">Margaret: the rollback plan is in the runbook.</p> <p class="m(0)">Alan: ship it after the 3pm check.</p> </div> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="comments-sheet" command="close" aria-label="Close"> <XIcon class="icon" /> </button> </div> </dialog> </div></template>处于半高时,底部面板的下半部分(包括页脚)位于屏幕边缘之下:停靠高度适用于供浏览的底部面板,例如列表、评论或地图,其操作应放在顶部。低于半个视口高度的底部面板只有一个停靠位置,即全高,其手柄也会如实表明这一点。
#不支持某项特性时
- 脚本不可用时,越过边缘的区域不是停靠位置(
@media (scripting: enabled)):松手后面板会弹回,Esc 和关闭按钮仍可关闭对话框。只要脚本能运行,就要挂载行为脚本,否则松手后面板会停在屏幕外,而页面仍处于惰性状态。 - 没有行为脚本时,遮罩会保持完整,直到对话框关闭。遮罩的变淡没有使用滚动驱动动画:在反向滚动容器中,Chromium 和 WebKit 运行滚动时间轴和视图时间轴的方向相反。
- 在支持
scroll-initial-target的浏览器(Chromium)中,带停靠高度的底部面板在任何脚本运行之前就以半高打开;在其他浏览器中,它以全高打开,直到行为脚本将其移到半高。
滑动关闭已在 Chromium 和 WebKit 中验证,覆盖鼠标、滚轮,以及 Chromium 中的触摸操作。滚动吸附、反向弹性布局滚动和 scrollend 已在所有当前浏览器中可用。
#动效
对话框从正常尺寸的 97%、向下偏移 0.5rem 的位置淡入,入场耗时 280ms,退场耗时 160ms。样式表用 CSS 实现入场和退场。@starting-style 定义首个渲染帧,display 和 overlay 使用 allow-discrete 进行过渡,使退场动画在对话框离开顶层前播完。启用减少动态效果时,仅保留淡入淡出。
#无障碍
- 让
aria-labelledby指向对话框的dialog-title,为对话框提供无障碍名称。 - 通过
show-modal打开对话框时,焦点会移入对话框,关闭后则返回触发按钮。将autofocus加到应首先获得焦点的字段上。确认破坏性操作时,将它加到安全的操作上。 - 当前版本的 Chrome、Edge、Firefox 和 Safari 均支持
command和commandfor。在旧版浏览器中,从点击事件处理函数调用dialog.showModal(),继续使用同一份样式表。 - 滑动绝不能是唯一的关闭方式:保留关闭按钮或能关闭对话框的操作,Esc 仍然有效。在带停靠高度的底部面板中,手柄是带有无障碍名称(例如“底部面板高度”)的按钮,按键和点击都能像拖动一样改变高度;没有停靠高度时,手柄是对辅助技术隐藏的凹槽。手柄的命中区域宽 64px、高 24px,在触摸屏上高度增至 44px。
#React、Svelte 和 Vue
dialog 组件根据应用状态打开和关闭对话框,并在浏览器打开或关闭对话框时发出通知。触发控件和对话框内部各部分的标记由页面自行编写。组件为每个对话框挂载 drawer 行为脚本,因此带有 dialog-swipe 和 dialog-panel 的抽屉或底部面板松手即可关闭,并像其他关闭方式一样发出通知;对没有 dialog-swipe 的对话框,该行为脚本不做任何事。使用 npx master-ui add dialog 将组件复制到项目中。参见 React、Svelte 和 Vue 了解组件的实现和用法。
#类名
| 类名 | 说明 |
|---|---|
dialog | 浮层面板使用通过 aria-labelledby 或 aria-label 命名的 dialog 元素,宽 28rem,距视口边缘留出 1rem |
dialog-sm | 宽度为 22rem,用于一句话的确认提示。默认宽度为 28rem。与 dialog 一同使用 |
dialog-lg | 宽度为 40rem。默认宽度为 28rem。与 dialog 一同使用 |
dialog-drawer | 位于行内末端、宽 24rem 的面板,操作时页面仍然可见。面板浮在视口内侧,距边缘 8px,并从所在边缘滑入。与 dialog 一同使用 |
dialog-drawer-start | 与 dialog-drawer 搭配使用,将宽度为 18rem 的面板放在行内起始边缘,用于容纳导航。与 dialog 一同使用 |
dialog-sheet | 从底边升起的面板,下方两角为直角,并避开主屏幕指示条。与 dialog 一同使用 |
dialog-swipe | 搭配 dialog-drawer 或 dialog-sheet:对话框变为与视口同大的透明轨道,其中的 dialog-panel 跟随手指、触控板、滚轮或鼠标移动,松手即关闭。需挂载 drawer 行为脚本与 dialog 一同使用 |
dialog-detents | 搭配 dialog-sheet 和 dialog-swipe:底部面板达到允许的最大高度,打开时显示半个视口,可停在半高或全高处与 dialog 一同使用 |
dialog-panel | 在 dialog-swipe 中,即抽屉或底部面板本身:带有材质,容纳标题栏、主体、页脚和关闭按钮 |
dialog-handle | 拖动手柄:底部面板顶边上的一道短凹槽,位于其 dialog-panel 的开头。在带 dialog-detents 的底部面板中,使用以其作用命名的按钮,行为脚本借此移动面板并设置 aria-expanded;其他情况下使用带 aria-hidden="true" 的 div |
dialog-header | 在面板顶部放置标题和说明 |
dialog-title | 标题。将 dialog 的 aria-labelledby 指向该标题。 |
dialog-description | 标题下方解释标题含义的低强调说明 |
dialog-body | 头部和底部之间的内容。占据剩余高度,内容过长时在面板内滚动 |
dialog-footer | 操作区,主操作排在最后。对话框宽度小于 22rem 时,各操作占满宽度并纵向排列,主操作排在最前 |
dialog-close | 在面板顶部的行内末端放置一个视觉权重较低的方形按钮,并为按钮提供无障碍名称。 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--dialog-width | 28rem | 首选宽度 |
--dialog-padding | 1.5rem | 各组成部分外围的内边距 |
--dialog-radius | 1.25rem | 圆角半径 |
--dialog-scrim | 在 dialog-swipe 中遮罩剩余的程度:从 1(全部,未设置时)随面板离开降至 0。由 drawer 行为脚本写入 |