Alert
A short, important message.
Kobalte's Alert
is one element with role="alert", which the Kobalte layer binds, so
it needs no class at all.
alpha The class names and states here are stable. The look is not: these styles can change in a minor release, without a major version bump.
| Class | Does what |
|---|---|
alert | The message box |
alert-error | Recolours it with --error |
Basic
<div class="alert" role="alert">Your changes have been saved.</div>
<div class="alert alert-error" role="alert">We could not reach the server.</div>
<div class="alert" role="alert">Your changes have been saved.</div>
<div class="alert alert-error" role="alert">We could not reach the server.</div>
import { Alert } from "@kobalte/core/alert";
<Alert>Your changes have been saved.</Alert>
<Alert class="alert-error">We could not reach the server.</Alert>
<div role="alert">Your changes have been saved.</div>
<div role="alert" class="alert-error">We could not reach the server.</div>
There is no info, success or warning variant yet. The framework derives
everything from one --primary plus --error, and
those variants need tokens it does not have.
Composition
Common ways to put this together with other components. The general rules are on Composition.
At the top of a form
A form-level error goes above the fields, inside the card, so it is read before the fields are.
<form class="card">
<div class="alert alert-error" role="alert">We could not send your message. Try again in a minute.</div>
<label class="label">
<p>Email</p>
<input class="input" type="email" />
</label>
<footer class="card-actions flex" style="justify-content: end">
<button class="btn">Send</button>
</footer>
</form>
- Boundary:
.alert-erroris an error boundary, so its border, surface and text all take the error colour from one class. - Spacing: the alert carries a
1rembottom margin like a field, so it stacks into the form with no wrapper.
A toast after a form is sent
The form clears and a toast confirms it, out of the way of the next thing the visitor does.
<form class="card" style="max-width: 24rem">
<label class="label"><p>Email</p><input class="input" type="email" /></label>
<button class="btn">Subscribe</button>
</form>
<ul class="toast" style="position: static; max-width: 22rem; margin-top: var(--padding)">
<li class="alert" role="status">
<span><span class="toast-title">Subscribed</span><br /><span class="toast-description">Check your inbox to confirm.</span></span>
</li>
</ul>
- Surface: each message is an
.alert, so a failure is.alert-errorwith no toast-specific class. - Layout:
.toastis fixed to the corner; it is static here so it shows in the preview.