Alert
Show a message that needs attention within the page, with optional dismissal.
import { Alert, AlertAction, AlertDescription, AlertTitle } from '@youj/react'
Installation & usage
npm i @youj/reactFor an app already set up with npx @youj/cli init, which installs the peers (@youj/core, @youj/motion, motion and Base UI) and wires the provider. New app? Start at Installation.
Use Alert for a condition that stays on the page until resolved or dismissed, such as a usage limit or failed payment. Use Toast for brief feedback, or Alert Dialog for a decision that must be made before continuing.
import { Alert, AlertAction, AlertDescription, AlertTitle, Button } from '@youj/react'
export function BandwidthAlert() {
return (
<Alert variant="warning" dismissible>
<AlertTitle>You’ve used 82% of this month’s bandwidth</AlertTitle>
<AlertDescription>Raise the limit or upgrade to avoid overage.</AlertDescription>
<AlertAction>
<Button size="sm">Upgrade</Button>
</AlertAction>
</Alert>
)
}The title is optional. A lone AlertDescription is enough for a one-line notice. AlertAction sets a button or two, or a link, under the message.
Description
A dismissible bandwidth warning with two actions; the card below glides up when it goes.
The four severities.
variant is info, the default, success, warning or danger. Each tints the surface and brings its own Youj icon, which plays once as the alert appears. Info draws its stem and drops the dot, success draws its ring and check, warning rocks on its base, and danger shakes.
danger is the same name Button, Badge, the menu items and the charts and meters (tone) use, and it is the name of the color role (--yj-danger), so one word means "failed, or can't be undone" everywhere.
Links in the title or description are underlined in the foreground color, so they read as links on every tint.
A set of alerts you can dismiss, with a button that resets them.
dismissible adds a dismiss button in the corner. The alert collapses through Collapse: its height eases to zero while the content fades out faster, so what follows glides up instead of jumping. If focus was inside the alert, it moves on to the next focusable element rather than dropping to the top of the page.
Passing onOpenChange also makes an alert dismissible. Control it with open to remember the choice or to bring the alert back.
import { useState } from 'react'
import { Alert, AlertDescription } from '@youj/react'
export function MaintenanceAlert() {
const [open, setOpen] = useState(true)
return (
<Alert variant="info" open={open} onOpenChange={setOpen}>
<AlertDescription>Maintenance on October 3, from 02:00 to 02:20 UTC.</AlertDescription>
</Alert>
)
}Give an alert its spacing as a margin on the alert itself, such as className="mb-4", rather than as a gap on its parent. The margin collapses with the alert, so no empty gap is left behind.
An alert with a custom icon that plays on mount, and one with the icon removed.
icon replaces the variant's icon with any node, and icon={false} removes it. A Youj icon with trigger="mount" plays as the alert appears, like the built-in ones. Any icon takes the variant's color.
One-line insights under a figure, each with a link to the reasoning.
A sparkle in place of the severity icon and a link at the row's end make an alert an inline insight. Lay the text and the link out in the AlertDescription. The second row sits on the band (bg-band, with --alert-icon set to keep the sparkle in the accent), for a panel that already carries color.
Code
alert-demo.tsx, the Demo story. The component itself comes from the package.
Reference
Propsvariant, icon, dismissible… (7)
Props
| Prop | Type | |
|---|---|---|
variant | AlertVariant | nullThe severity: default 'info' | 'info' |
icon | ReactNode | falseReplace the variant's icon, or | – |
dismissible | booleanShow a dismiss button. Implied by | – |
open | booleanControlled visibility. | – |
defaultOpen | booleanUncontrolled initial visibility. default true | true |
onOpenChange | (open: boolean) => voidCalled with | – |
dismissLabel | stringAccessible name of the dismiss button. default "Dismiss" | "Dismiss" |
Other props pass through: extends ComponentProps<'div'>. | ||
AlertTitle, AlertDescription and AlertAction take the props of a div.
MotionIcon, dismiss, height, dismiss, content… · reduced motion
Motion
| Moment | Tokens | ||
|---|---|---|---|
| Iconplays once as the alert mounts | no token each icon’s own choreography | each icon’s own choreography | |
| Dismiss, heightwhat follows glides up | duration.baseease.standard220ms · cubic-bezier(0.2, 0, 0, 1) | 220mscubic-bezier(0.2, 0, 0, 1) | |
| Dismiss, contentfades out ahead of the height | duration.fastease.exit140ms · cubic-bezier(0.4, 0, 1, 1) | 140mscubic-bezier(0.4, 0, 1, 1) | |
| Reopenheight and content together | duration.baseease.standard220ms · cubic-bezier(0.2, 0, 0, 1) | 220mscubic-bezier(0.2, 0, 0, 1) |
Reduced motion
Icons appear at rest. A dismissed alert leaves at once, and a reopened one appears at once.
An alert that mounts appears in place without expanding. Only its icon plays. The collapse animates height on purpose, because the content below has to move with it.
Reduced motion
The icon appears in its resting pose. A dismissed alert leaves at once, and the content below moves up in the same frame. The severity, the tint and the message don't change.
AccessibilityARIA, focus, announcements
Accessibility
- Warning and danger alerts have
role="alert", so screen readers announce them at once, interrupting. Info and success alerts haverole="status"and wait for a pause. Passroleto override, such asrole="status"on a danger alert that isn't urgent. - Screen readers announce an alert when it's added to the page. One that renders with the page is read in place, with the rest of the content.
- The dismiss button is named by
dismissLabel, "Dismiss" by default. With several alerts on screen, name each one, likedismissLabel="Dismiss payment alert". - When a dismissed alert collapses, focus inside it moves to the next focusable element, or to the previous one at the end of the page. It never falls to the body.
- The icon is decorative. The title and description carry the message.