Skip to content

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/react

For 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.

Reference

Propsvariant, icon, dismissible… (7)

Props

Alert props
PropType
variantAlertVariant | null

The severity: info, success, warning or danger, the name Button, Badge and the menus use.

default 'info'

'info'
iconReactNode | false

Replace the variant's icon, or false for none.

–
dismissibleboolean

Show a dismiss button. Implied by onOpenChange.

–
openboolean

Controlled visibility.

–
defaultOpenboolean

Uncontrolled initial visibility.

default true

true
onOpenChange(open: boolean) => void

Called with false when dismissed.

–
dismissLabelstring

Accessible 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

Motion tokens
MomentTokens
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.standard

220ms · cubic-bezier(0.2, 0, 0, 1)

220mscubic-bezier(0.2, 0, 0, 1)
Dismiss, contentfades out ahead of the height
duration.fastease.exit

140ms · cubic-bezier(0.4, 0, 1, 1)

140mscubic-bezier(0.4, 0, 1, 1)
Reopenheight and content together
duration.baseease.standard

220ms · 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 have role="status" and wait for a pause. Pass role to override, such as role="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, like dismissLabel="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.