--- url: /reference/items/Toaster/examples.md --- # Examples ## Basic toaster Click a button to push a toast. Info and success toasts go to the polite region, errors to the assertive one, and each auto-dismisses after five seconds — hover or focus a toast to pause its timer. Fire **Stack 3** to see a burst coalesce into a single coordinated transition, then dismiss one in the middle: the survivors slide to their new slots without shimmying. :::code-group ```css /* Share one set of view-transition pseudos across every toast, while each element keeps its own unique `view-transition-name` (set by the class). */ .toast { view-transition-class: toast; } .toast[data-type='info'] { border-color: #3b82f6; } .toast[data-type='success'] { border-color: #10b981; } .toast[data-type='error'] { border-color: #ef4444; } /* Position/size interpolation for toasts that merely change slot. */ ::view-transition-group(.toast) { animation-duration: 300ms; animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1); } /* Slide ONLY genuinely entering/leaving toasts. `:only-child` matches when the image-pair holds just a new (entering) or just an old (leaving) snapshot; a persisting toast has both, so it keeps the default cross-fade and never shimmies as the stack reflows around it. */ ::view-transition-new(.toast):only-child { animation: toast-in 300ms ease-out both; } ::view-transition-old(.toast):only-child { animation: toast-out 200ms ease-in both; } @keyframes toast-in { from { opacity: 0; transform: translateY(12px); } } @keyframes toast-out { to { opacity: 0; transform: translateY(12px); } } /* Honour reduced-motion: drop the slide, keep the (instant) state change. */ @media (prefers-reduced-motion: reduce) { ::view-transition-group(.toast), ::view-transition-new(.toast):only-child, ::view-transition-old(.toast):only-child { animation: none; } } ``` ::: ## Positions A toaster's corner is nothing more than the region's flex alignment: `justify-start` / `justify-end` picks the top or bottom edge and the region's `items-start` / `items-end` the left or right one. Here four identical toasters are anchored to the four corners — click any button to pop a toast in that corner. :::code-group ```css .toast { view-transition-class: toast; } /* Toasts that merely change slot slide smoothly to their new position. */ ::view-transition-group(.toast) { animation-duration: 300ms; animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1); } /* A position-agnostic fade + scale for entering/leaving toasts, so the same animation reads well from any corner. Scope with `:only-child` so persisting toasts keep the default cross-fade and don't shimmy. */ ::view-transition-new(.toast):only-child { animation: toast-in 300ms ease-out both; } ::view-transition-old(.toast):only-child { animation: toast-out 200ms ease-in both; } @keyframes toast-in { from { opacity: 0; transform: scale(0.96); } } @keyframes toast-out { to { opacity: 0; transform: scale(0.96); } } @media (prefers-reduced-motion: reduce) { ::view-transition-group(.toast), ::view-transition-new(.toast):only-child, ::view-transition-old(.toast):only-child { animation: none; } } ``` :::