Skip to main content

Overview

The Alert component displays important messages to users with support for different types, variants, action buttons, and custom icons.

Basic Usage

import { NSAlert } from '@newtonschool/grauity';
import { NSTypography } from '@newtonschool/grauity';

function MyComponent() {
  return (
    <Alert
      variant="primary"
      type="filled"
      icon="auto"
      title={
        <NSTypography variant="paragraph-sb-l1" color="inherit">
          This is an alert title
        </NSTypography>
      }
      description={
        <NSTypography variant="paragraph-md-p3" color="inherit">
          This is the alert description with more details.
        </NSTypography>
      }
      showCloseButton={true}
      onClose={() => console.log('Alert closed')}
    />
  );
}

Props

type
string
default:"default"
Type/style of the alert.Available choices:
  • default - Colorless background, colored icon & text
  • outlined - Light colored background with outline, colored icon & text
  • filled - Colored background, white icon & text
variant
string
default:"primary"
Variant/color theme of the alert.Available choices: primary, success, warning, error, default
icon
grauityIconName | 'auto' | null
default:"null"
Alert icon. Use auto to automatically select the icon based on the variant (info, check, warning, or error icon).
title
React.ReactNode
default:""
Alert title content. Can be a string or React element (typically Typography).
description
React.ReactNode
default:""
Alert description content. Can be a string or React element.
showCloseButton
boolean
default:false
Show a close button in the alert.
onClose
function
Callback function when the close button is clicked.
actionButtons
ButtonProps[]
Array of action button configurations to display in the alert.
inlineButtons
boolean
default:false
Display action buttons inline with the content instead of stacked.
position
string
default:"static"
CSS position property for the alert.Available choices: static, fixed, absolute, relative
top
string
default:"null"
Top position value (useful for fixed positioning).
bottom
string
default:"null"
Bottom position value (useful for fixed positioning).
left
string
default:"null"
Left position value (useful for fixed positioning).
right
string
default:"null"
Right position value (useful for fixed positioning).
maxWidth
string
default:"440px"
Maximum width of the alert.
className
string
default:""
Additional CSS class name for styling.

Alert Types

<Alert
  type="default"
  variant="primary"
  icon="auto"
  title="Default Alert"
  description="Colorless background with colored icon and text."
/>

Alert Variants

<NSAlert variant="primary" type="filled" icon="auto" title="Primary" />
<NSAlert variant="success" type="filled" icon="auto" title="Success" />
<NSAlert variant="warning" type="filled" icon="auto" title="Warning" />
<NSAlert variant="error" type="filled" icon="auto" title="Error" />
<NSAlert variant="default" type="filled" icon="auto" title="Default" />

With Action Buttons

<Alert
  variant="primary"
  type="filled"
  title="Action Required"
  description="Please review and take action."
  actionButtons={[
    {
      children: 'Confirm',
      variant: 'primary',
      color: 'neutral',
      size: 'small',
      onClick: () => console.log('Confirmed'),
    },
    {
      children: 'Cancel',
      variant: 'tertiary',
      color: 'neutral',
      size: 'small',
      onClick: () => console.log('Cancelled'),
    },
  ]}
/>

Inline Buttons

<Alert
  variant="success"
  title="Success!"
  description="Operation completed."
  inlineButtons={true}
  actionButtons={[
    {
      children: 'View Details',
      variant: 'primary',
      size: 'small',
    },
  ]}
/>

With Close Button

<Alert
  variant="warning"
  icon="auto"
  title="Warning"
  description="This action cannot be undone."
  showCloseButton={true}
  onClose={() => console.log('Alert dismissed')}
/>

Custom Icon

<Alert
  variant="primary"
  icon="sparkle"
  title="Custom Icon"
  description="Using a custom icon instead of auto."
/>

Fixed Positioning

<Alert
  variant="success"
  title="Fixed Alert"
  position="fixed"
  top="20px"
  right="20px"
  maxWidth="400px"
/>

Constants

import { 
  ALERT_VARIANTS_ENUM,
  ALERT_TYPES_ENUM 
} from '@newtonschool/grauity';

// Usage
<NSAlert 
  variant={ALERT_VARIANTS_ENUM.SUCCESS} 
  type={ALERT_TYPES_ENUM.FILLED}
/>

Build docs developers (and LLMs) love