對話框
以覆疊面板呈現的原生模態對話框,無須腳本即可開啟和關閉。
本頁內容
對話框位於表面層級的最上層。面板下方的遮罩會讓頁面變暗、變模糊。請在原生 <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-body 以外的區域抓住面板,放開時移到它被甩向的停駐位置或較近的那一個;精確指標按住面板期間,軌道會停止貼齊。它會把剩下多少遮罩寫入--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 中以觸控驗證。捲動貼齊、反向 flex 捲動和 scrollend 在所有目前的瀏覽器中都可使用。
#動態效果
對話框從原尺寸的 97%、下移半個 rem 的位置開始淡入,進場耗時 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 也仍然有效。在有停駐高度的底部面板中,把手是具有可存取名稱(例如「底部面板高度」)的按鈕,可按下或使用鍵盤調整高度,效果與拖曳相同;沒有停駐高度時,它是對輔助技術隱藏的凹槽。它的操作目標為 64 × 24px,在觸控螢幕上高 44px。
#React、Svelte 和 Vue
dialog 元件會依應用程式的狀態開啟或關閉對話框,也會在瀏覽器開啟或關閉對話框時通知應用程式。觸發元件和對話框內的各個部分仍由頁面自己的標記定義。它會在每個對話框上掛載 drawer 行為,因此加上 dialog-swipe 和 dialog-panel 的抽屜或底部面板放開後即可關閉,並像其他關閉方式一樣回報;對沒有 dialog-swipe 的對話框,這個行為不會做任何事。執行 npx master-ui add dialog 將元件複製到專案中。React、Svelte 和 Vue 說明元件的建置與使用方式。
#類別
| 類別 | 說明 |
|---|---|
dialog | 覆疊面板使用 dialog 元素,並以 aria-labelledby 或 aria-label 命名。寬度為 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 會跟隨手指、觸控板、滾輪或滑鼠移動,放開即可關閉。請掛上抽屜行為搭配 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 | 標題,供對話框的 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。由抽屜行為寫入 |