Interactive
Toast manager
Monsoon.toast("Saved", { tone: "success" }) shows a toast in a polite live region. Toasts stack per position, pause while hovered or focused, can carry an action and can be swiped away. A button can show one with data-m-toast. Needs monsoon.js.
Needs monsoon.js. The styles work on their own, and the behavior comes from the optional interactive layer: add <script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script> or import 'monsooncss/js'. See the interactive layer.
Example
<div class="flex flex-wrap gap-2">
<button type="button" class="btn btn-sm" data-m-toast="Changes saved" data-m-toast-tone="success" data-m-toast-description="Your profile is up to date.">Success</button>
<button type="button" class="btn btn-sm btn-outline" data-m-toast="Payment failed" data-m-toast-tone="danger" data-m-toast-description="Check the card details and try again.">Error</button>
<button type="button" class="btn btn-sm btn-outline" data-m-toast="Storage almost full" data-m-toast-tone="warning" data-m-toast-position="top-center">Top center</button>
<button type="button" class="btn btn-sm btn-ghost" onclick="Monsoon.toast('Conversation archived', { action: { label: 'Undo', onClick: () => Monsoon.toast('Restored', { tone: 'success' }) } })">With action</button>
</div>JavaScript API
Attribute
data-m-toast="Message"
Options
| Option | Description |
|---|---|
data-m-toast-tone="success" (info, warning, danger) | |
data-m-toast-description | |
data-m-toast-position="top-center" (top or bottom, start, center or end) | |
data-m-toast-duration="0" (stays until dismissed) |
Methods
| Method | Description |
|---|---|
Monsoon.toast(message, { title, description, tone, position, duration, action: { label, onClick }, dismissible, max }) | |
returns { el, close(), update(message, options) } |
Events
| Event | Description |
|---|---|
monsoon:toast { toast } | |
monsoon:toast-close { toast } |
Classes
7 classes. Click a name to copy it. Utilities on the same element always win over component defaults, so any of them can be adjusted with plain utility classes.
| Class | Type | What it does |
|---|---|---|
| Base | Base class of the toast manager. Not used in the example above. Sets fixed position, stacking order, offsets, flex display, alignment and gap.CSS |
|
| Modifier | Modifier, combined with the toast class. Not used in the example above. Sets stacking order, alignment and transition.CSS |
|
| Modifier | Modifier, combined with the toast class. Not used in the example above. Sets hidden by default, flex display, alignment, gap, width and padding, and 4 custom properties.CSS |
|
| Modifier | Modifier, combined with the toast class. Not used in the example above. Sets grid display, alignment, width, height, margin and text color.CSS |
|
| Modifier | Modifier, combined with the toast class. Not used in the example above. Sets alignment and width.CSS |
|
| Modifier | Modifier, combined with the toast class. Not used in the example above. Sets alignment.CSS |
|
| Modifier | Modifier, combined with the toast class. Not used in the example above. Sets grid display, alignment, width, height, margin and text color.CSS |
Usage
The styles ship inside the same stylesheet as the utilities, and the behavior comes from the optional monsoon.js script, which enhances any element with a matching data-m attribute, including markup added later.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/monsooncss">
<script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script>
<!-- Toast manager: start from the base class, then add modifiers -->
<div class="flex flex-wrap gap-2">
<button type="button" class="btn btn-sm" data-m-toast="Changes saved" data-m-toast-tone="success" data-m-toast-description="Your profile is up to date.">Success</button>
<button type="button" class="btn btn-sm btn-outline" data-m-toast="Payment failed" data-m-toast-tone="danger" data-m-toast-description="Check the card details and try again.">Error</button>
<button type="button" class="btn btn-sm btn-outline" data-m-toast="Storage almost full" data-m-toast-tone="warning" data-m-toast-position="top-center">Top center</button>
<button type="button" class="btn btn-sm btn-ghost" onclick="Monsoon.toast('Conversation archived', { action: { label: 'Undo', onClick: () => Monsoon.toast('Restored', { tone: 'success' }) } })">With action</button>
</div>Theme it with --seed on any ancestor, for example style="--seed: #0d9488", and force a scheme with class="dark" or class="light" on a wrapper.
Accessibility
- Use
buttonfor actions andafor navigation. Icon only buttons need anaria-label; decorative icons carryaria-hidden="true". - Every interactive part shows a visible focus ring with
:focus-visible, and tone colors pair with-foregroundtokens that keep text at 4.5:1 or better in light and dark mode.