トースト
ページに重ねて表示する短いメッセージです。自動で閉じることもできます。
このページの内容
トーストは、保存、デプロイ、コピーなど直前に起きたこと、または進行中で終了時に結果を知らせる作業について、ページ上で伝える短いメッセージです。各トーストは、固定配置の 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 を指定すると、トーストは 6 秒後に自動で閉じます。時間の計測には 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>#アクション付き
トーストには、閉じるボタンの横に「元に戻す」や「表示」などのアクションを 1 つ置けます。toast-action はテキストの後に置き、btn btn-sm を <form method="dialog"> で囲みます。これにより、アクションの実行後にトーストが閉じます。ボタンには value を指定してください。ダイアログの returnValue でどのボタンで閉じたかがわかるため、1 つの 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"を指定します。これにより、変更は部分ごとではなく、トースト全体として一度だけ読み上げられます。処理中の間は、スクリーンリーダーが作業の終了まで読み上げを保留することがあります。 - トーストは、操作の結果を知らせるために使います。見逃しても支障がないように、操作への唯一の導線をトーストに置かないでください。
- トーストは表示時にフォーカスを受け取りません。アクションボタンと閉じるボタンはタブ順序に含まれ、自動で閉じるトーストは、いずれかにフォーカスがある間は待機します。
- 動きを減らす設定では、トーストは位置を変えずにフェードし、新しいアイコンは元の大きさのままフェードインします。
#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 }で、トーストにアクションボタンを 1 つ追加できます。onClickを実行した後にトーストを閉じます。ただし、ハンドラーがevent.preventDefault()を呼び出した場合は閉じません。toast.promise(promise, { loading, success, error })は、loadingを処理中のトーストとして表示し、同じトーストをsuccess、または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 | 下部の終端側に固定したトーストのスタック。最新のトーストを最後に置きます。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" を付け、変更時にメッセージ全体が1回だけ読み上げられるようにします。 |
toast-timed | --toast-duration が経過すると自動で消えます。同じ領域のいずれかのトーストにポインターが重なっている間やフォーカスがある間はタイマーが一時停止し、トーストが処理中の間はタイマーが進みません。下端の極細線は残り時間に合わせて短くなります。toast と併用します |
toast-info | アイコンに情報色を適用。テキストはインク色のままtoast と併用します |
toast-success | アイコンに成功色を適用。テキストはインク色のままtoast と併用します |
toast-warning | アイコンに警告色を適用。テキストはインク色のままtoast と併用します |
toast-danger | アイコンに危険色を適用。テキストはインク色のままtoast と併用します |
toast-icon | ステータスアイコンに icon と併せて、または処理中のトーストのスピナーに spinner と併せて指定します。アイコンまたはスピナーの縦方向の中心をテキストの1行目の中心に合わせ、ステータスに対応する色を適用します。アイコンが置き換わると、新しいアイコンが拡大しながら現れます。 |
toast-body | テキストを収め、残りの幅を占める領域。直後のボタンは行末に配置 |
toast-title | 起きたことを端的に伝える、強調表示の短い文言です。 |
toast-description | タイトルの下に置く控えめな一文。タイトルのないトーストではメッセージ全文 |
toast-action | トーストに追加できるアクション(「元に戻す」「表示」など)は1つまでです。本文の後、閉じるボタンの前に置きます。btn btn-sm を method="dialog" の form で囲むか、ボタン自体に commandfor と command="close" を付け、アクションの実行後にトーストが閉じるようにします。 |
toast-close | トーストを閉じる method="dialog" 付きのフォーム、またはボタンです。終端側のパディング領域に入り込むように配置します。 |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--toast-duration | toast-timed の表示時間。未設定時は 6s指定先:toast | |
--toast-inset | 1rem | ビューポートの端から領域までの距離指定先:toast-region |
--toast-gap | .5rem | トースト同士の間隔指定先:toast-region |