轻提示
浮在页面上方的简短消息,可自动关闭。
本页内容
轻提示是浮在页面上的简短消息,用来告知用户刚刚发生的事(例如已保存、已部署或已复制),或告知正在进行、完成后会报告结果的工作。每条轻提示都是固定定位的 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属性的轻提示会在页面加载时显示,适用于表单提交后由服务器渲染的消息。 - 在脚本中,向该区域追加一个新的
<dialog open class="toast">,或为区域内已有的轻提示设置open。两种方式都由@starting-style定义轻提示入场时的起始帧。不要调用show():它会把焦点移入轻提示,打断用户正在进行的操作。 - 用
<form method="dialog">包裹按钮,或为按钮设置commandfor和command="close",都能关闭轻提示。display通过allow-discrete进行过渡,因此退场动画会在轻提示释放布局空间前播放完毕。 - 离开后的轻提示仍留在文档中。如果页面为每条消息追加一个新的轻提示,
toast行为脚本会在它到期,或被关闭并退场后将其移除:toast(region)。attach()不包含它,因为重复打开同一个轻提示的页面需要保留它。
#定时关闭
toast-timed 让轻提示在六秒后自动消失。计时由一个以 display: none 结束的 CSS 动画实现。指针悬停在轻提示上或键盘焦点位于其中时,计时会暂停,让用户有时间读完。轻提示底部的细线随倒计时逐渐缩短,并随计时一起暂停。设置 --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 位于文字之后:它是包裹 btn btn-sm 的 <form method="dialog">,因此操作执行后会关闭轻提示。为按钮设置 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,失败时显示 error 并设为danger状态。成功和失败的内容都可以由函数返回,函数分别接收结果值和失败原因。它返回该 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 一同使用 |
toast-info | 用于将图标设为信息色,文字保持墨色与 toast 一同使用 |
toast-success | 用于将图标设为成功色,文字保持墨色与 toast 一同使用 |
toast-warning | 用于将图标设为警告色,文字保持墨色与 toast 一同使用 |
toast-danger | 用于将图标设为危险色,文字保持墨色与 toast 一同使用 |
toast-icon | 与 icon 一同用于状态图标,或与 spinner 一同用于待处理 toast 的加载指示器:使其与第一行文字居中对齐,并使用状态色调。替换旧图标的新图标会放大进入 |
toast-body | 容纳文本并占据剩余宽度,紧随其后的按钮位于行尾 |
toast-title | 用醒目的简短文字说明发生了什么 |
toast-description | 标题下方较不醒目的一句说明,也可作为无标题轻提示的完整消息 |
toast-action | toast 可提供的唯一操作(撤销、查看),位于正文之后、关闭按钮之前:用 method="dialog" 的 form 包裹 btn btn-sm,或直接使用带 commandfor 和 command="close" 的按钮,使操作执行后关闭 toast |
toast-close | 将带有 method="dialog" 的 form 或关闭按钮移入末端内边距,用于关闭 toast |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--toast-duration | toast-timed 的显示时长,默认 6s在 toast 上设置 | |
--toast-inset | 1rem | 区域距视口边缘的距离在 toast-region 上设置 |
--toast-gap | .5rem | 轻提示间距在 toast-region 上设置 |