To add toast notifications in Astro, put one Toaster.astro component in your base layout that renders an empty aria-live region, and a small script that listens for a toast custom event and appends a message that removes itself after a few seconds. Any script or framework island can then show an Astro toast by dispatching that event, with no UI library and no island of its own.
Key Takeaways
- One live region in the layout, announced by screen readers with
role="status".- A
CustomEventonwindowlets plain scripts, React, Svelte and Vue islands all trigger toasts.- Auto-dismiss after about five seconds, but pause on hover and focus.
- Errors that need action belong inline next to the form, not in a toast.
The toaster component
---
// src/components/Toaster.astro
---
<div id="toaster" class="toaster" role="status" aria-live="polite"></div>
<script>
type ToastDetail = { message: string; type?: 'info' | 'success' | 'error'; timeout?: number };
const region = document.getElementById('toaster')!;
window.addEventListener('toast', (event) => {
const { message, type = 'info', timeout = 5000 } = (event as CustomEvent<ToastDetail>).detail;
const item = document.createElement('div');
item.className = `toast toast-${type}`;
item.textContent = message;
const close = document.createElement('button');
close.type = 'button';
close.setAttribute('aria-label', 'Dismiss notification');
close.textContent = '×';
close.addEventListener('click', () => item.remove());
item.append(close);
region.append(item);
let timer = setTimeout(() => item.remove(), timeout);
item.addEventListener('mouseenter', () => clearTimeout(timer));
item.addEventListener('mouseleave', () => (timer = setTimeout(() => item.remove(), timeout)));
});
</script>
<style>
.toaster {
position: fixed; inset: auto 1rem 1rem auto; z-index: 50;
display: grid; gap: 0.5rem; max-width: min(24rem, calc(100vw - 2rem));
}
:global(.toast) {
display: flex; gap: 0.75rem; align-items: start; justify-content: space-between;
padding: 0.75rem 1rem; border-radius: 0.5rem;
background: #1f2937; color: #fff; box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
}
:global(.toast-success) { background: #166534; }
:global(.toast-error) { background: #991b1b; }
:global(.toast button) { background: none; border: 0; color: inherit; font-size: 1.25rem; cursor: pointer; }
</style>
The toast elements are created by JavaScript, so they don’t get Astro’s scoped style attribute. That’s why their styles use :global().
Add it once to the layout, just before </body>:
---
// src/layouts/BaseLayout.astro
import Toaster from '../components/Toaster.astro';
---
<html lang="en">
<body>
<slot />
<Toaster />
</body>
</html>
Show a toast from anywhere
A tiny helper keeps the call sites short:
// src/lib/toast.ts
export function showToast(message: string, type: 'info' | 'success' | 'error' = 'info') {
window.dispatchEvent(new CustomEvent('toast', { detail: { message, type } }));
}
From a component script:
<button id="copy">Copy link</button>
<script>
import { showToast } from '../lib/toast';
document.getElementById('copy')!.addEventListener('click', async () => {
await navigator.clipboard.writeText(location.href);
showToast('Link copied', 'success');
});
</script>
From a React island it’s the same import inside an event handler. Because the toaster only listens for an event, islands don’t need to share a framework or a store. The same idea scales to other cross-island messages, as shown in islands talking with custom events.
Accessibility details
role="status"witharia-live="polite"makes screen readers announce new toasts without interrupting. The region must exist in the HTML before messages arrive, which is why it’s rendered by the component rather than created on demand.- Use
role="alert"only for urgent messages; it interrupts the user. - Give people time. WCAG asks that timed content can be paused or extended, so the hover pause matters. Keyboard users can reach the dismiss button.
- Don’t put the only copy of important information in a toast. A failed form submission should show its error next to the form as well.
With view transitions
If you use <ClientRouter />, the toaster’s script runs once and the listener on window survives navigation, but the region is replaced with each new page. Add transition:persist to the region so a toast shown just before navigating stays visible:
<div id="toaster" class="toaster" role="status" aria-live="polite" transition:persist></div>
See re-running scripts after view transitions for scripts that need to run on every page instead.
Our Astro components guide covers the component and script basics used here, and the styling docs explain where global styles live in a Komal theme. If you’d rather start from a finished site, browse our Astro themes.
FAQ
Does Astro have a built-in toast component?
No. Astro ships no UI components for toasts. A small Astro component with a script, as above, or a framework library inside an island both work.
Can I use react-hot-toast or Sonner in Astro?
Yes, inside a React island. The toaster and the code that calls it must be in the same React tree, so it suits sites built mostly from one large island. For mixed pages, an event-based toaster is simpler.
How do I show a toast after a form submission?
Dispatch the toast in the submit handler once the response comes back. For server-rendered form posts, render a status message on the returned page instead, because the page reloads.