Skip to content
2k
On This Page

Alert

Displays a callout for user attention.

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert"

export default function AlertDemo() {
  return (
    <div class="grid w-full max-w-md items-start gap-4">
      <Alert>
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          class="size-4"
        >
          <circle cx="12" cy="12" r="10" />
          <path d="m16 9-5.5 5.5L8 12" />
        </svg>
        <AlertTitle>Payment successful</AlertTitle>
        <AlertDescription>
          Your payment of $29.99 has been processed. A receipt has been sent to
          your email address.
        </AlertDescription>
      </Alert>
      <Alert>
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          class="size-4"
        >
          <circle cx="12" cy="12" r="10" />
          <path d="M12 16v-4" />
          <path d="M12 8h.01" />
        </svg>
        <AlertTitle>New feature available</AlertTitle>
        <AlertDescription>
          We&apos;ve added dark mode support. You can enable it in your account
          settings.
        </AlertDescription>
      </Alert>
    </div>
  )
}

Installation

pnpm dlx solidui-cli@latest add alert

Install the following dependencies:

pnpm add class-variance-authority

Copy and paste the following code into your project.

components/ui/alert.tsx
import { type ComponentProps } from "@solidjs/web"
import { cva, type VariantProps } from "class-variance-authority"
import { omit } from "solid-js"

import { cn } from "~/lib/utils"

const alertVariants = cva("group/alert grid gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4", {
  variants: {
    variant: {
      default: "bg-card text-card-foreground",
      destructive: "bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current"
    }
  },
  defaultVariants: {
    variant: "default"
  }
})

type AlertProps = ComponentProps<"div"> & VariantProps<typeof alertVariants>

const Alert = (props: AlertProps) => {
  const others = omit(props, "class", "variant")
  return (
    <div
      class={cn(alertVariants({ variant: props.variant }), props.class)}
      data-slot="alert"
      role="alert"
      {...others}
    />
  )
}

type AlertTitleProps = ComponentProps<"h5">

const AlertTitle = (props: AlertTitleProps) => (
  <h5
    data-slot="alert-title"
    {...props}
    class={cn("font-medium group-has-[>svg]/alert:col-start-2", props.class)}
  />
)

type AlertDescriptionProps = ComponentProps<"div">

const AlertDescription = (props: AlertDescriptionProps) => (
  <div
    data-slot="alert-description"
    {...props}
    class={cn("text-sm text-balance text-muted-foreground md:text-pretty [&_p:not(:last-child)]:mb-4", props.class)}
  />
)

type AlertActionProps = ComponentProps<"div">

const AlertAction = (props: AlertActionProps) => (
  <div
    data-slot="alert-action"
    {...props}
    class={cn("absolute top-2 right-2", props.class)}
  />
)

export {
  Alert,
  AlertAction,
  AlertDescription,
  AlertTitle,
  alertVariants,
  type AlertActionProps,
  type AlertDescriptionProps,
  type AlertProps,
  type AlertTitleProps
}

Update the import paths to match your project setup.

Usage

import {
  Alert,
  AlertAction,
  AlertDescription,
  AlertTitle
} from "~/components/ui/alert"
<Alert>
  <AlertTitle>Heads up!</AlertTitle>
  <AlertDescription>
    You can add components and dependencies to your app using the cli.
  </AlertDescription>
  <AlertAction>
    <button type="button">Enable</button>
  </AlertAction>
</Alert>

Composition

Use the following composition to build an Alert:

Alert
โ”œโ”€โ”€ Icon
โ”œโ”€โ”€ AlertTitle
โ”œโ”€โ”€ AlertDescription
โ””โ”€โ”€ AlertAction

Basic

A basic alert with an icon, title and description.

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert"

export default function AlertBasic() {
  return (
    <Alert class="max-w-md">
      <svg
        width="24"
        height="24"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        class="size-4"
      >
        <circle cx="12" cy="12" r="10" />
        <path d="m16 9-5.5 5.5L8 12" />
      </svg>
      <AlertTitle>Account updated successfully</AlertTitle>
      <AlertDescription>
        Your profile information has been saved. Changes will be reflected
        immediately.
      </AlertDescription>
    </Alert>
  )
}

Destructive

Use variant="destructive" to create a destructive alert.

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert"

export default function AlertDestructive() {
  return (
    <Alert class="max-w-md" variant="destructive">
      <svg
        width="24"
        height="24"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        class="size-4"
      >
        <circle cx="12" cy="12" r="10" />
        <line x1="12" x2="12" y1="8" y2="12" />
        <line x1="12" x2="12.01" y1="16" y2="16" />
      </svg>
      <AlertTitle>Payment failed</AlertTitle>
      <AlertDescription>
        Your payment could not be processed. Please check your payment method
        and try again.
      </AlertDescription>
    </Alert>
  )
}

Action

Use AlertAction to add a button or other action element to the alert.

import {
  Alert,
  AlertAction,
  AlertDescription,
  AlertTitle
} from "~/components/ui/alert"
import { Button } from "~/components/ui/button"

export default function AlertActionExample() {
  return (
    <Alert class="max-w-md">
      <AlertTitle>Dark mode is now available</AlertTitle>
      <AlertDescription>
        Enable it under your profile settings to get started.
      </AlertDescription>
      <AlertAction>
        <Button size="xs" variant="default">
          Enable
        </Button>
      </AlertAction>
    </Alert>
  )
}

Custom Colors

You can customize the alert colors by adding custom classes such as bg-amber-50 dark:bg-amber-950 to the Alert component.

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert"

export default function AlertColors() {
  return (
    <Alert class="max-w-md border-amber-200 bg-amber-50 text-amber-900 dark:border-amber-900 dark:bg-amber-950 dark:text-amber-50">
      <svg
        width="24"
        height="24"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        class="size-4"
      >
        <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" />
        <path d="M12 9v4" />
        <path d="M12 17h.01" />
      </svg>
      <AlertTitle>Your subscription will expire in 3 days.</AlertTitle>
      <AlertDescription>
        Renew now to avoid service interruption or upgrade to a paid plan to
        continue using the service.
      </AlertDescription>
    </Alert>
  )
}

API Reference

Alert

The Alert component displays a callout for user attention.

PropTypeDefault
variant"default" | "destructive""default"

AlertTitle

The AlertTitle component displays the title of the alert.

PropTypeDefault
classstring-

AlertDescription

The AlertDescription component displays the description or content of the alert.

PropTypeDefault
classstring-

AlertAction

The AlertAction component displays an action element (like a button) positioned absolutely in the top-right corner of the alert.

PropTypeDefault
classstring-