Alert
A message that draws attention without interrupting the user.
Alert follows the Alert WAI-ARIA design pattern.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Alert } from '@leve-ui/alert';
import { LuAlertCircle } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Alert.Root class="alert-default">
<LuAlertCircle class="alert-default-icon" aria-hidden="true" />
<Alert.Title class="alert-default-title">Payment failed</Alert.Title>
<Alert.Description class="alert-default-description">
Check your card details and try again.
</Alert.Description>
</Alert.Root>
);
});.alert-default {
display: grid;
grid-template-columns: auto 1fr;
column-gap: 12px;
row-gap: 4px;
width: 320px;
padding: 14px 16px;
border: 1px solid light-dark(#fecaca, #f8717166);
border-radius: 6px;
background: light-dark(#fef2f2, #f871711f);
font-family: inherit;
}
/* Decorative icon spanning the title and description rows */
.alert-default-icon {
grid-row: span 2;
width: 18px;
height: 18px;
margin-top: 1px;
color: light-dark(#dc2626, #f87171);
}
.alert-default-title {
margin: 0;
font-size: 14px;
font-weight: 600;
color: light-dark(#dc2626, #f87171);
}
.alert-default-description {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/alertnpm install @leve-ui/alertyarn add @leve-ui/alertbun add @leve-ui/alertpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Alert } from '@leve-ui/alert'; // or '@leve-ui/core'
<Alert.Root>
<Alert.Title />
<Alert.Description />
</Alert.Root>
| Part | Element | Description |
|---|---|---|
Alert.Root | <div> | Wraps the message and has role="alert" (unless live={false}). |
Alert.Title | <div> | The heading of the message. |
Alert.Description | <div> | The body of the message. |
Examples#
On Submit#
Render Alert.Root at the moment the message should be announced, for example after a failed submit.
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Alert } from '@leve-ui/alert';
import { LuAlertCircle } from '@qwikest/icons/lucide';
import styles from './on-submit.css?inline';
export default component$(() => {
useStyles$(styles);
const error = useSignal(false);
return (
<div class="alert-on-submit-wrapper">
<button type="button" class="alert-on-submit-button" onClick$={() => (error.value = true)}>
Submit
</button>
{/* Render the alert when it should be announced */}
{error.value && (
<Alert.Root class="alert-on-submit">
<LuAlertCircle class="alert-on-submit-icon" aria-hidden="true" />
<Alert.Title class="alert-on-submit-title">Submission failed</Alert.Title>
<Alert.Description class="alert-on-submit-description">
Email is required.
</Alert.Description>
</Alert.Root>
)}
</div>
);
});.alert-on-submit-wrapper {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
font-family: inherit;
}
.alert-on-submit-button {
height: 32px;
padding: 0 12px;
border: none;
border-radius: 6px;
background: #009dfd;
color: #ffffff;
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
}
.alert-on-submit-button:hover {
background: #006ce9;
}
.alert-on-submit-button:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.alert-on-submit {
display: grid;
grid-template-columns: auto 1fr;
column-gap: 12px;
row-gap: 4px;
width: 320px;
padding: 14px 16px;
border: 1px solid light-dark(#fecaca, #f8717166);
border-radius: 6px;
background: light-dark(#fef2f2, #f871711f);
}
/* Decorative icon spanning the title and description rows */
.alert-on-submit-icon {
grid-row: span 2;
width: 18px;
height: 18px;
margin-top: 1px;
color: light-dark(#dc2626, #f87171);
}
.alert-on-submit-title {
margin: 0;
font-size: 14px;
font-weight: 600;
color: light-dark(#dc2626, #f87171);
}
.alert-on-submit-description {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| live | true | boolean Whether the alert is a live region ( |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-alert | Always present |
live (default true) decides whether the Root is a live region. Use live={false} for static notices.
Title#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-alert-title | Always present |
Description#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-alert-description | Always present |
Accessibility#
Complies with the Alert WAI-ARIA design pattern.
Alert.Rootrendersrole="alert", so screen readers announce its content.- An alert that is already in the page when it loads may not be announced. Render it when it should be shown.
- For a notice that is on the page from the start (for example a banner about the page itself), pass
live={false}: the Root renders noroleand is read as normal content. - Alert does not move focus and does not close itself. For temporary notifications, use a toast instead.
- Alert has no keyboard interaction.