Alert

React Alert components are used to show important messages or feedback after user actions. They are commonly used to confirm actions, show errors, display warnings, or share system information.

This Alert component is built with React and styled using Tailwind CSS, allowing control over layout, content, and visual style.

Installation

Install the component using the Tailgrids CLI.

npx @tailgrids/cli@latest add alert

Anatomy

Import the parts and combine them. The AlertIndicator automatically renders an icon that matches the variant of the surrounding Alert.

import {
  Alert,
  AlertIndicator,
  AlertContent,
  AlertTitle,
  AlertDescription
} from "@/components/tailgrids/alert";

export default () => (
  <Alert variant="success">
    <AlertIndicator />
    <AlertContent>
      <AlertTitle>Success</AlertTitle>
      <AlertDescription>
        Your changes have been saved successfully.
      </AlertDescription>
    </AlertContent>
  </Alert>
);

Examples

Basic

Compose different parts to create an alert according to your needs.

Variants

The Alert component includes five semantic variants, each with their own color scheme: default, success, warning, destructive, and info.

With Actions

Render buttons or links inside <AlertContent> to give users a way to act on the alert.

Custom Icon

Pass any icon as a child of <AlertIndicator> to override the default variant icon.

Flexibility

As each part is a separate sub-component, you can customize the layout and content of the alert to suit your needs.

API Reference

Alert

The root container. Provides variant context to all child parts.

PropTypeDefaultDescription
variant'default' | 'success' | 'warning' | 'destructive' | 'info''default'Sets the alert style and tone
classNamestring-Additional CSS classes
childrenReact.ReactNode-AlertIndicator, AlertContent
Data AttributeValuesDescription
data-slot'alert'Identifies the alert root element
data-variant'default' | 'success' | 'warning' | 'destructive' | 'info'Reflects the current variant prop

AlertIndicator

Renders the variant-aware icon. Pass children to override the default icon.

PropTypeDefaultDescription
classNamestring-Additional CSS classes
childrenReact.ReactNodevariant iconOverride the default variant icon
Data AttributeValuesDescription
data-slot'alert-indicator'Identifies the alert indicator
data-variant'default' | 'success' | 'warning' | 'destructive' | 'info'Reflects the current variant prop

AlertContent

The container that groups the title, description and other content.

PropTypeDescription
classNamestringAdditional CSS classes
childrenReact.ReactNodeAlertTitle, AlertDescription, and any content
Data AttributeValuesDescription
data-slot'alert-content'Identifies the alert content row

AlertTitle

The alert heading.

PropTypeDefaultDescription
levelnumber4Heading level, from 1 to 6
render(props, renderProps) => ReactElement-Custom DOM renderer for the heading element
classNamestring-Additional CSS classes
styleReact.CSSProperties-Inline styles
childrenReact.ReactNode-Title text
Data AttributeValuesDescription
data-slot'alert-title'Identifies the alert title
data-variant'default' | 'success' | 'warning' | 'destructive' | 'info'Reflects the current variant prop

AlertAction

Optional slot pinned to the top-right corner of the alert, for a close button or another action. The root adds right padding when an AlertAction is present.

PropTypeDescription
classNamestringAdditional CSS classes
childrenReact.ReactNodeAction content
Data AttributeValuesDescription
data-slot'alert-action'Identifies the alert action

AlertDescription

The alert body.

PropTypeDescription
classNamestringAdditional CSS classes
childrenReact.ReactNodeDescription body
Data AttributeValuesDescription
data-slot'alert-description'Identifies the alert description
data-variant'default' | 'success' | 'warning' | 'destructive' | 'info'Reflects the current variant prop

Accessibility

  • Live announcement: The root renders with role="alert", so assistive technologies announce it immediately when it appears in the DOM.
  • Heading structure: AlertTitle uses React Aria's Heading component. It defaults to level={4}, and you can pass a different level when you need another heading depth.
  • Status is not color-only: Each variant is paired with a dedicated AlertIndicator icon, so the message still has a visual cue beyond color alone.
  • Indicator is decorative by default: The indicator is intended to be decorative, so it's been hidden from assistive technologies using aria-hidden="true" and role="presentation".
  • Content semantics: AlertDescription is a generic content wrapper in the implementation, so you can include plain text, links, lists, or action buttons inside it when needed.
  • Testing hooks: Every subcomponent exposes a data-slot attribute, and variant-aware parts also expose data-variant. These are useful for styling and for accessibility-related tests.