浮動通知
浮在頁面上方、可自動消失的簡短訊息。
本頁內容
浮動通知是在頁面上方短暫顯示的訊息,用來告知使用者剛發生的事(例如已儲存、已部署或已複製),或告知正在進行、之後會回報結果的工作。每則通知都是固定定位的 toast-region 內的非模態 <dialog>,可透過對話框的原生機制關閉,無須腳本,進出場動畫則由 CSS 樣式表處理。
浮動通知
<div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast toast-success" aria-labelledby="toast-deployed"> <svg class="icon toast-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8" cy="8" r="6" /><path d="M5 8L7.1041 9.8703A0.2 0.2 0 0 0 7.3832 9.8573L10.75 6.25" /> </svg> <div class="toast-body"> <p class="toast-title" id="toast-deployed">Deployed to production</p> <p class="toast-description">atlas.dev is live in 14 regions.</p> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> </dialog></div>import CheckCircleIcon from '@master/icons-react/native/16/soft/check-circle'import XIcon from '@master/icons-react/native/16/soft/x'<div className="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open className="toast toast-success" aria-labelledby="toast-deployed"> <CheckCircleIcon className="icon toast-icon" /> <div className="toast-body"> <p className="toast-title" id="toast-deployed">Deployed to production</p> <p className="toast-description">atlas.dev is live in 14 regions.</p> </div> <form method="dialog" className="toast-close"> <button className="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon className="icon" /> </button> </form> </dialog></div><script> import CheckCircleIcon from '@master/icons-svelte/native/16/soft/check-circle' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast toast-success" aria-labelledby="toast-deployed"> <CheckCircleIcon class="icon toast-icon" /> <div class="toast-body"> <p class="toast-title" id="toast-deployed">Deployed to production</p> <p class="toast-description">atlas.dev is live in 14 regions.</p> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog></div><script setup>import CheckCircleIcon from '@master/icons-vue/native/16/soft/check-circle'import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast toast-success" aria-labelledby="toast-deployed"> <CheckCircleIcon class="icon toast-icon" /> <div class="toast-body"> <p class="toast-title" id="toast-deployed">Deployed to production</p> <p class="toast-description">atlas.dev is live in 14 regions.</p> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> </div></template>#顯示與關閉
- 帶有
open屬性的通知會在頁面載入時顯示,適合在表單以 POST 送出後,由伺服器直接算繪訊息。 - 在腳本中,在區域內附加新的
<dialog open class="toast">,或為區域內現有的通知設定open。兩種做法都會透過@starting-style設定通知進場時的起始樣式。請勿呼叫show():它會把焦點移入通知,讓使用者離開正在進行的操作。 - 將按鈕放在
<form method="dialog">裡,或在按鈕上設定commandfor和command="close",都能關閉通知。display透過allow-discrete進行轉場,讓退場動畫播完後,通知才停止占用空間。 - 離開後的通知仍留在文件中。若頁面為每則訊息附加一個新通知,
toast行為會在通知到期,或被關閉並退場後將它移除:toast(region)。attach()不會掛載它,因為重複開啟同一個通知的頁面需要把它留著。
#限時
toast-timed 會讓通知在六秒後自動消失。計時由 CSS 動畫控制,動畫結束時套用 display: none。指標停在通知上或鍵盤焦點位於通知內時,計時會暫停,讓使用者有足夠時間閱讀。通知底部的細線會隨剩餘時間縮短,並與計時同步暫停。用 --toast-duration 調整顯示時間。忙碌中的通知不會計時;工作結束後才開始計時。
限時
<div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast toast-timed" aria-label="Link copied"> <div class="toast-body"> <p class="toast-description">Link copied to the clipboard.</p> </div> </dialog></div><div className="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open className="toast toast-timed" aria-label="Link copied"> <div className="toast-body"> <p className="toast-description">Link copied to the clipboard.</p> </div> </dialog></div><div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast toast-timed" aria-label="Link copied"> <div class="toast-body"> <p class="toast-description">Link copied to the clipboard.</p> </div> </dialog></div><template> <div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast toast-timed" aria-label="Link copied"> <div class="toast-body"> <p class="toast-description">Link copied to the clipboard.</p> </div> </dialog> </div></template>#附操作按鈕
通知可以在關閉按鈕旁提供一個操作,例如「復原」或「檢視」。toast-action 放在文字之後:用 <form method="dialog"> 包住一個 btn btn-sm,讓操作執行後關閉通知。請為按鈕設定 value:對話框的 returnValue 就會指出是哪個按鈕關閉了它,只需一個 close 監聽器即可執行操作。操作是真正的按鈕,可用 Tab 移到它;當操作取得焦點或指標停在通知上時,限時通知會暫停計時。
通知會自動消失,使用者可能根本沒看到,因此通知中的操作只是捷徑,絕不能是唯一的途徑。重要的操作(例如還原已刪除的項目、重試儲存失敗的內容)也要放在會持續存在的地方:項目本身的選單、垃圾桶,或失敗表單旁的提示訊息。狀態修飾類別只改變圖示的顏色,文字維持原本的前景色。
附操作按鈕
<div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast" aria-labelledby="toast-archived"> <div class="toast-body"> <p class="toast-title" id="toast-archived">Project archived</p> <p class="toast-description">Atlas is hidden from the dashboard.</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="undo">Undo</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> </dialog> <dialog open class="toast toast-danger" aria-labelledby="toast-failed"> <svg class="icon toast-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8" cy="8" r="6" transform="rotate(180 8 8)" /><circle cx="8" cy="5" r="0.5" fill="currentColor" stroke="none" transform="rotate(180 8 8)" /><path d="M8 8L8 11" transform="rotate(180 8 8)" /> </svg> <div class="toast-body"> <p class="toast-title" id="toast-failed">The build failed</p> <p class="toast-description">The install step exited with code 1.</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="logs">View logs</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> </dialog></div>import XIcon from '@master/icons-react/native/16/soft/x'import AlertCircleIcon from '@master/icons-react/native/16/soft/alert-circle'<div className="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open className="toast" aria-labelledby="toast-archived"> <div className="toast-body"> <p className="toast-title" id="toast-archived">Project archived</p> <p className="toast-description">Atlas is hidden from the dashboard.</p> </div> <form method="dialog" className="toast-action"> <button className="btn btn-sm" value="undo">Undo</button> </form> <form method="dialog" className="toast-close"> <button className="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon className="icon" /> </button> </form> </dialog> <dialog open className="toast toast-danger" aria-labelledby="toast-failed"> <AlertCircleIcon className="icon toast-icon" /> <div className="toast-body"> <p className="toast-title" id="toast-failed">The build failed</p> <p className="toast-description">The install step exited with code 1.</p> </div> <form method="dialog" className="toast-action"> <button className="btn btn-sm" value="logs">View logs</button> </form> <form method="dialog" className="toast-close"> <button className="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon className="icon" /> </button> </form> </dialog></div><script> import XIcon from '@master/icons-svelte/native/16/soft/x' import AlertCircleIcon from '@master/icons-svelte/native/16/soft/alert-circle'</script><div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast" aria-labelledby="toast-archived"> <div class="toast-body"> <p class="toast-title" id="toast-archived">Project archived</p> <p class="toast-description">Atlas is hidden from the dashboard.</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="undo">Undo</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> <dialog open class="toast toast-danger" aria-labelledby="toast-failed"> <AlertCircleIcon class="icon toast-icon" /> <div class="toast-body"> <p class="toast-title" id="toast-failed">The build failed</p> <p class="toast-description">The install step exited with code 1.</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="logs">View logs</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog></div><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'import AlertCircleIcon from '@master/icons-vue/native/16/soft/alert-circle'</script><template> <div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast" aria-labelledby="toast-archived"> <div class="toast-body"> <p class="toast-title" id="toast-archived">Project archived</p> <p class="toast-description">Atlas is hidden from the dashboard.</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="undo">Undo</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> <dialog open class="toast toast-danger" aria-labelledby="toast-failed"> <AlertCircleIcon class="icon toast-icon" /> <div class="toast-body"> <p class="toast-title" id="toast-failed">The build failed</p> <p class="toast-description">The install step exited with code 1.</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="logs">View logs</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> </div></template>#先顯示進行中,再原地更新
通知可以在工作進行時回報狀態,並在同一個位置說明結果。工作進行期間,通知帶有 aria-busy="true",並在圖示的位置放一個帶有 toast-icon 的旋轉指示器;無論工作進行多久,計時都不會開始。工作結束時,請直接修改同一則通知:更換文字、狀態和圖示,並移除 aria-busy。通知不會再次進場,在堆疊中的位置也不變;只有新的圖示會放大出現。
進行中
<button class="btn" type="button" id="deploy-run">Deploy</button><div class="toast-region" id="deploy-region" role="region" aria-label="Notifications" aria-live="polite"></div><template id="deploy-pending"> <dialog open class="toast" aria-labelledby="deploy-title" aria-busy="true" aria-atomic="true"> <span class="spinner spinner-inline toast-icon" aria-hidden="true"></span> <div class="toast-body"> <p class="toast-title" id="deploy-title">Deploying Atlas</p> <p class="toast-description">Building for 14 regions.</p> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> <template> <svg class="icon toast-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8" cy="8" r="6" /><path d="M5 8L7.1041 9.8703A0.2 0.2 0 0 0 7.3832 9.8573L10.75 6.25" /> </svg> </template> </dialog></template><script>var region = document.getElementById('deploy-region')var count = 0document.getElementById('deploy-run').addEventListener('click', function () { var toast = document.getElementById('deploy-pending').content.firstElementChild.cloneNode(true) var title = toast.querySelector('.toast-title') title.id = 'deploy-title-' + ++count toast.setAttribute('aria-labelledby', title.id) region.append(toast) // When the work ends, the same toast says so: its icon, its words and its status change, and it is no longer busy. setTimeout(function () { toast.querySelector('.toast-icon').replaceWith(toast.querySelector('template').content.firstElementChild.cloneNode(true)) title.textContent = 'Deployed to production' toast.querySelector('.toast-description').textContent = 'atlas.dev is live in 14 regions.' toast.classList.add('toast-success', 'toast-timed') toast.removeAttribute('aria-busy') }, 2400)})// A toast that has run out, or was dismissed and has left, is removed.MasterUI.toast(region)</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>同一則通知可以再次變更,例如在上傳檔案時從一個數量更新到下一個,或從失敗變成重試。每次變更都會重新開始計時,讓使用者有完整的時間閱讀新文字。忙碌中的通知不適合用來追蹤重要的長時間工作:放在工作旁邊的進度會持續留在原處。
#豐富內容
浮動通知可以搭配既有的元件,容納文字以外的內容:開頭的頭像表示是誰做了某件事,內文中的檔案表示收到了什麼,檔案的進度則表示仍在進行中的上傳。浮動通知維持自身的幾何結構:圖片放在原本狀態圖示的位置,檔案則與文字同寬。
仍在進行中的上傳是不帶 toast-timed 的浮動通知,因此會一直顯示到工作結束;說明上傳完成的浮動通知,則是就地更新的同一則通知,如上方所述。請讓重要的內容也能在其他地方找到:檔案在它的資料夾中,提及在它的討論串中。
豐富內容
<div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast" aria-labelledby="toast-mention"> <span class="avatar avatar-sm"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span> <div class="toast-body"> <p class="toast-title" id="toast-mention">Ada mentioned you</p> <p class="toast-description">“Can you look at the token audit before Friday?”</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="reply">Reply</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> </dialog> <dialog open class="toast" aria-labelledby="toast-upload" aria-busy="true"> <div class="toast-body"> <p class="toast-title" id="toast-upload">Uploading 1 file</p> <div class="file file-sm mt-xs"> <span class="file-page" aria-hidden="true">pdf</span> <div class="file-body"> <p class="file-name">Q3 report.pdf</p> <p class="file-meta">1.6 of 2.4 MB</p> </div> <progress class="file-progress" value="66" max="100" aria-label="Uploading Q3 report.pdf"> 66% </progress> </div> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> </dialog> <dialog open class="toast toast-success" aria-labelledby="toast-shared"> <svg class="icon toast-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8" cy="8" r="6" /><path d="M5 8L7.1041 9.8703A0.2 0.2 0 0 0 7.3832 9.8573L10.75 6.25" /> </svg> <div class="toast-body"> <p class="toast-title" id="toast-shared">Photo shared with Design</p> <div class="file mt-xs"> <img class="file-thumb" src="/demo/harbor.svg" alt> <div class="file-body"> <p class="file-name">harbor-at-dusk.jpg</p> <p class="file-meta">1600 × 1000 · 480 KB</p> </div> </div> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <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> </form> </dialog></div>import XIcon from '@master/icons-react/native/16/soft/x'import CheckCircleIcon from '@master/icons-react/native/16/soft/check-circle'<div className="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open className="toast" aria-labelledby="toast-mention"> <span className="avatar avatar-sm"> <img className="avatar-image" src="/demo/portrait-1.svg" alt="" /> </span> <div className="toast-body"> <p className="toast-title" id="toast-mention">Ada mentioned you</p> <p className="toast-description">“Can you look at the token audit before Friday?”</p> </div> <form method="dialog" className="toast-action"> <button className="btn btn-sm" value="reply">Reply</button> </form> <form method="dialog" className="toast-close"> <button className="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon className="icon" /> </button> </form> </dialog> <dialog open className="toast" aria-labelledby="toast-upload" aria-busy="true"> <div className="toast-body"> <p className="toast-title" id="toast-upload">Uploading 1 file</p> <div className="file file-sm mt-xs"> <span className="file-page" aria-hidden="true">pdf</span> <div className="file-body"> <p className="file-name">Q3 report.pdf</p> <p className="file-meta">1.6 of 2.4 MB</p> </div> <progress className="file-progress" value="66" max="100" aria-label="Uploading Q3 report.pdf"> 66% </progress> </div> </div> <form method="dialog" className="toast-close"> <button className="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon className="icon" /> </button> </form> </dialog> <dialog open className="toast toast-success" aria-labelledby="toast-shared"> <CheckCircleIcon className="icon toast-icon" /> <div className="toast-body"> <p className="toast-title" id="toast-shared">Photo shared with Design</p> <div className="file mt-xs"> <img className="file-thumb" src="/demo/harbor.svg" alt="" /> <div className="file-body"> <p className="file-name">harbor-at-dusk.jpg</p> <p className="file-meta">1600 × 1000 · 480 KB</p> </div> </div> </div> <form method="dialog" className="toast-close"> <button className="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon className="icon" /> </button> </form> </dialog></div><script> import XIcon from '@master/icons-svelte/native/16/soft/x' import CheckCircleIcon from '@master/icons-svelte/native/16/soft/check-circle'</script><div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast" aria-labelledby="toast-mention"> <span class="avatar avatar-sm"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span> <div class="toast-body"> <p class="toast-title" id="toast-mention">Ada mentioned you</p> <p class="toast-description">“Can you look at the token audit before Friday?”</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="reply">Reply</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> <dialog open class="toast" aria-labelledby="toast-upload" aria-busy="true"> <div class="toast-body"> <p class="toast-title" id="toast-upload">Uploading 1 file</p> <div class="file file-sm mt-xs"> <span class="file-page" aria-hidden="true">pdf</span> <div class="file-body"> <p class="file-name">Q3 report.pdf</p> <p class="file-meta">1.6 of 2.4 MB</p> </div> <progress class="file-progress" value="66" max="100" aria-label="Uploading Q3 report.pdf"> 66% </progress> </div> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> <dialog open class="toast toast-success" aria-labelledby="toast-shared"> <CheckCircleIcon class="icon toast-icon" /> <div class="toast-body"> <p class="toast-title" id="toast-shared">Photo shared with Design</p> <div class="file mt-xs"> <img class="file-thumb" src="/demo/harbor.svg" alt> <div class="file-body"> <p class="file-name">harbor-at-dusk.jpg</p> <p class="file-meta">1600 × 1000 · 480 KB</p> </div> </div> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog></div><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'import CheckCircleIcon from '@master/icons-vue/native/16/soft/check-circle'</script><template> <div class="toast-region" role="region" aria-label="Notifications" aria-live="polite"> <dialog open class="toast" aria-labelledby="toast-mention"> <span class="avatar avatar-sm"> <img class="avatar-image" src="/demo/portrait-1.svg" alt> </span> <div class="toast-body"> <p class="toast-title" id="toast-mention">Ada mentioned you</p> <p class="toast-description">“Can you look at the token audit before Friday?”</p> </div> <form method="dialog" class="toast-action"> <button class="btn btn-sm" value="reply">Reply</button> </form> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> <dialog open class="toast" aria-labelledby="toast-upload" aria-busy="true"> <div class="toast-body"> <p class="toast-title" id="toast-upload">Uploading 1 file</p> <div class="file file-sm mt-xs"> <span class="file-page" aria-hidden="true">pdf</span> <div class="file-body"> <p class="file-name">Q3 report.pdf</p> <p class="file-meta">1.6 of 2.4 MB</p> </div> <progress class="file-progress" value="66" max="100" aria-label="Uploading Q3 report.pdf"> 66% </progress> </div> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> <dialog open class="toast toast-success" aria-labelledby="toast-shared"> <CheckCircleIcon class="icon toast-icon" /> <div class="toast-body"> <p class="toast-title" id="toast-shared">Photo shared with Design</p> <div class="file mt-xs"> <img class="file-thumb" src="/demo/harbor.svg" alt> <div class="file-body"> <p class="file-name">harbor-at-dusk.jpg</p> <p class="file-meta">1600 × 1000 · 480 KB</p> </div> </div> </div> <form method="dialog" class="toast-close"> <button class="btn btn-quiet btn-square btn-sm" aria-label="Dismiss"> <XIcon class="icon" /> </button> </form> </dialog> </div></template>#通知區域
toast-region 固定在底部靠末端的一角,將通知堆疊起來,最新一則排在最下方。區域本身不接收指標事件,透過區域的空白處仍可操作被覆蓋的頁面。在窄螢幕上,通知區域會橫跨整個寬度。
| 類別 | 位置 |
|---|---|
toast-region | 底部,靠末端邊緣 |
toast-region-top | 頂部,通知向下進場,最新一則排在最上方 |
toast-region-center | 水平置中 |
通知區域是設有 z-index 的一般固定定位元素。要讓通知顯示在已開啟的模態對話框上方,請為區域設定 popover="manual",再對它呼叫一次 showPopover(),即可將它放入頂層。
#無障礙
- 用
aria-live="polite"將通知區域設為即時區域,讓通知出現時能由輔助技術朗讀,並以aria-label為區域命名。 - 每則通知都應設定名稱,可用
aria-labelledby指向標題,或直接設定aria-label。 - 為原地更新文字的通知設定
aria-atomic="true",讓每次變更都以整則通知的形式播報一次,而不是逐一播報片段。通知忙碌期間,螢幕閱讀器可能會等到工作結束才播報。 - 浮動通知用來確認結果。使用者即使錯過通知,也應能取得所需資訊或完成操作,因此請在通知之外保留操作入口。
- 通知出現時不會取得焦點。它的操作按鈕和關閉按鈕都在 Tab 順序中,其中任一個取得焦點時,限時通知會暫停計時。
- 啟用減少動態效果時,通知會在原地淡入淡出,新的圖示也會以原尺寸淡入。
#React、Svelte 和 Vue
toast 元件以佇列管理通知。你可以從任何地方呼叫 toast() 函式,只需算繪一次通知區域,即可顯示佇列中的通知。使用 npx master-ui add toast 將元件複製到專案中,並參閱 React、Svelte 和 Vue 瞭解元件的建構方式和用法。
toast(message)會顯示一則通知並傳回一個控制代碼。update(message)會在同一則通知中顯示其他內容,不會再次進場,並重新開始計時;dismiss()會讓通知像按下它自己的關閉按鈕一樣離開。訊息包含title、description、status、以毫秒為單位的duration(設為false會讓通知保留)以及action;傳入字串時視為 description。status: 'pending'會顯示旋轉指示器並設定aria-busy,通知會保留到被更新為止。action: { label, onClick }定義通知可在關閉按鈕之外提供的單一操作按鈕。它會執行onClick,接著關閉通知,除非處理函式呼叫了event.preventDefault()。toast.promise(promise, { loading, success, error })會以進行中的通知顯示loading,再把同一則通知變成success,或變成帶有danger狀態的失敗訊息。兩者都可以是函式,成功回呼接收 Promise 的完成值,失敗回呼接收 Promise 的拒絕原因。它會傳回該 promise,可供 await 或串接。
toast({ title: 'Project archived', action: { label: 'Undo', onClick: () => restore(project) } })await toast.promise(deploy(project), { loading: { title: 'Deploying Atlas' }, success: (release) => ({ title: 'Deployed to production', description: `${release.url} is live.` }), error: (error) => ({ title: 'The deploy failed', description: String(error), duration: false }),})#類別
| 類別 | 說明 |
|---|---|
toast-region | 固定在底部末端的堆疊,最新的 toast 排在最後。使用 aria-live="polite" 將它設為即時區域,並以 aria-label 命名。堆疊本身不接收指標事件,在窄螢幕上會占滿寬度 |
toast-region-top | 浮動通知堆疊在頂端,最新一則位於最上方,其餘依序向下排列。搭配 toast-region 使用 |
toast-region-center | 將浮動通知堆疊置中,可搭配 toast-region-top 使用。搭配 toast-region 使用 |
toast | 在區域內的 dialog 元素上呈現訊息,使用指向標題的 aria-labelledby 或直接使用 aria-label 提供名稱。文字會原地更新時,請設定 aria-atomic="true",讓變更以整體只播報一次 |
toast-timed | 經過 --toast-duration 後自動消失。指標停留在同一區域的任一則浮動通知上,或任一則通知取得焦點時,倒數會暫停;通知處於忙碌狀態時不會倒數。底部的細線會隨剩餘時間縮短。搭配 toast 使用 |
toast-info | 圖示使用資訊色,文字維持原色搭配 toast 使用 |
toast-success | 圖示使用成功色,文字維持原色搭配 toast 使用 |
toast-warning | 圖示使用警告色,文字維持原色搭配 toast 使用 |
toast-danger | 圖示使用危險色,文字維持原色搭配 toast 使用 |
toast-icon | 與 icon 一同用於狀態圖示,或與 spinner 一同用於等待中通知的旋轉指示器:讓它與第一行文字置中對齊,並套用狀態色。取代舊圖示的新圖示會以放大方式出現 |
toast-body | 容納文字並占滿剩餘寬度,接在後方的按鈕會靠齊列尾 |
toast-title | 以醒目的簡短文字交代發生的事。 |
toast-description | 標題下方以較低調的樣式呈現的一句說明。浮動通知沒有標題時,這句話就是完整訊息。 |
toast-action | 浮動通知可提供的唯一操作(例如「復原」、「檢視」),位於內文之後、關閉按鈕之前:使用包住 btn btn-sm 的 method="dialog" 表單,或直接使用設有 commandfor 和 command="close" 的按鈕,讓操作執行後即關閉通知 |
toast-close | 用來關閉浮動通知的表單(method="dialog")或按鈕,向末端移入內距區域。 |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--toast-duration | toast-timed 的顯示時間,預設為 6s設定於 toast | |
--toast-inset | 1rem | 區域與檢視區邊緣的距離設定於 toast-region |
--toast-gap | .5rem | 浮動通知之間的間距設定於 toast-region |