Skip to content

ComponentsFeedback

SourceSource

Alert

An inline notice with an icon and a coloured edge, in info, success, warning and danger kinds.

Use it for a message that belongs to a place on the page and stays until the situation changes: a channel in slowmode, a bot that has gone offline, a form that saved. For something that happened a moment ago and can fade, use a Toast.

Pick the variant for what the message means. danger is for something broken or about to be lost, and warning for something the reader should know before going on.

Root class newt-alert
import {
  Alert,
  AlertActions,
  AlertDescription,
  AlertTitle,
} from "@/registry/bases/newt/ui/alert"
import { Button } from "@/registry/bases/newt/ui/button"

export default function AlertDemo() {
  return (
    <div className="flex w-full max-w-md flex-col gap-2">
      <Alert variant="info">
        <AlertTitle>A new version is ready</AlertTitle>
        <AlertDescription>
          Reload to pick up the latest commands.
        </AlertDescription>
      </Alert>
      <Alert variant="success">
        <AlertTitle>Roles saved</AlertTitle>
      </Alert>
      <Alert variant="warning">
        <AlertTitle>Slowmode is on</AlertTitle>
        <AlertDescription>
          Members can post one message every 30 seconds in this channel.
        </AlertDescription>
      </Alert>
      <Alert variant="danger">
        <AlertTitle>The bot is offline</AlertTitle>
        <AlertDescription>
          Its token was reset. Paste the new one to bring it back.
        </AlertDescription>
        <AlertActions>
          <Button size="sm" variant="secondary">
            Update token
          </Button>
        </AlertActions>
      </Alert>
    </div>
  )
}
<script setup lang="ts">
import {
  Alert,
  AlertActions,
  AlertDescription,
  AlertTitle,
} from "~~/registry/bases/newt/ui/alert"
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <div class="flex w-full max-w-md flex-col gap-2">
    <Alert variant="info">
      <AlertTitle>A new version is ready</AlertTitle>
      <AlertDescription
        >Reload to pick up the latest commands.</AlertDescription
      >
    </Alert>
    <Alert variant="success">
      <AlertTitle>Roles saved</AlertTitle>
    </Alert>
    <Alert variant="warning">
      <AlertTitle>Slowmode is on</AlertTitle>
      <AlertDescription>
        Members can post one message every 30 seconds in this channel.
      </AlertDescription>
    </Alert>
    <Alert variant="danger">
      <AlertTitle>The bot is offline</AlertTitle>
      <AlertDescription>
        Its token was reset. Paste the new one to bring it back.
      </AlertDescription>
      <AlertActions>
        <Button size="sm" variant="secondary">Update token</Button>
      </AlertActions>
    </Alert>
  </div>
</template>

Installation

npx newtui@latest add alertnpx shadcn@latest add @newtui/alertnpx shadcn-vue@latest add @newtui/alertpnpm dlx newtui@latest add alertpnpm dlx shadcn@latest add @newtui/alertpnpm dlx shadcn-vue@latest add @newtui/alertyarn dlx newtui@latest add alertyarn dlx shadcn@latest add @newtui/alertyarn dlx shadcn-vue@latest add @newtui/alertbunx newtui@latest add alertbunx shadcn@latest add @newtui/alertbunx shadcn-vue@latest add @newtui/alertdeno run -A npm:newtui@latest add alertdeno run -A npm:shadcn@latest add @newtui/alertdeno run -A npm:shadcn-vue@latest add @newtui/alertvlx newtui@latest add alertvlx shadcn@latest add @newtui/alertvlx shadcn-vue@latest add @newtui/alertvp dlx newtui@latest add alertvp dlx shadcn@latest add @newtui/alertvp dlx shadcn-vue@latest add @newtui/alertnubx newtui@latest add alertnubx shadcn@latest add @newtui/alertnubx shadcn-vue@latest add @newtui/alertnlx newtui@latest add alertnlx shadcn@latest add @newtui/alertnlx shadcn-vue@latest add @newtui/alertupx newtui@latest add alertupx shadcn@latest add @newtui/alertupx shadcn-vue@latest add @newtui/alert

The shadcn CLI needs the @newtui registry in your components.json. Set it up.

Install the dependencies

npm install class-variance-authoritypnpm add class-variance-authorityyarn add class-variance-authoritybun add class-variance-authoritydeno add npm:class-variance-authorityvlt install class-variance-authorityvp add class-variance-authoritynub add class-variance-authorityni class-variance-authorityupm add class-variance-authority

Copy the source into your project

import { cva, type VariantProps } from "class-variance-authority"
import * as React from "react"

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

const alertVariants = cva(
  "flex w-full items-start gap-3 rounded-[var(--newt-radius-sm)] border border-s-4 border-newt-border bg-newt-bg-surface px-3.5 py-3 text-sm leading-5 text-newt-text-secondary",
  {
    variants: {
      variant: {
        info: "border-s-newt-text-link [&>svg]:text-newt-text-link",
        success: "border-s-newt-online [&>svg]:text-newt-online",
        warning: "border-s-newt-idle [&>svg]:text-newt-idle",
        danger: "border-s-newt-danger [&>svg]:text-newt-danger",
      },
    },
    defaultVariants: { variant: "info" },
  }
)

/* One shape per kind, so the kind survives without its colour. */
const ICONS = {
  info: "M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 3.2a.9.9 0 1 1 0 1.8.9.9 0 0 1 0-1.8Zm-.8 3.4a.8.8 0 0 1 1.6 0v3.6a.8.8 0 0 1-1.6 0V7.6Z",
  success:
    "M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm3.2 5.5-3.6 3.9a.8.8 0 0 1-1.2 0L4.8 8.7a.8.8 0 1 1 1.2-1.1l1 1.1 3-3.3a.8.8 0 0 1 1.2 1.1Z",
  warning:
    "M8.9 1.5a1 1 0 0 0-1.8 0L.6 13.2A1 1 0 0 0 1.5 14.7h13a1 1 0 0 0 .9-1.5L8.9 1.5ZM8 5.5a.8.8 0 0 1 .8.8v3.2a.8.8 0 0 1-1.6 0V6.3a.8.8 0 0 1 .8-.8Zm0 7.2a.9.9 0 1 1 0-1.8.9.9 0 0 1 0 1.8Z",
  danger:
    "M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 3.5a.8.8 0 0 1 .8.8v3.4a.8.8 0 0 1-1.6 0V5.3a.8.8 0 0 1 .8-.8Zm0 7.4a.9.9 0 1 1 0-1.8.9.9 0 0 1 0 1.8Z",
} as const

export interface AlertProps
  extends React.ComponentProps<"div">, VariantProps<typeof alertVariants> {}

/*
 * A notice that sits in the page, where a toast floats over it. What kind of
 * notice it is shows three ways: the bar down its leading edge, the icon's
 * shape and the words. A `danger` alert is announced at once (`role="alert"`);
 * the others wait their turn (`role="status"`).
 */
const Alert = React.forwardRef<HTMLDivElement, AlertProps>(
  ({ className, variant, role, children, ...props }, ref) => {
    const kind = variant ?? "info"
    return (
      <div
        ref={ref}
        role={role ?? (kind === "danger" ? "alert" : "status")}
        data-variant={kind}
        className={cn(alertVariants({ variant }), className)}
        {...props}
      >
        <svg
          viewBox="0 0 16 16"
          aria-hidden="true"
          className="h-5 w-5 shrink-0"
        >
          <path fill="currentColor" d={ICONS[kind]} />
        </svg>
        <div className="flex min-w-0 flex-1 flex-col gap-0.5">{children}</div>
      </div>
    )
  }
)
Alert.displayName = "Alert"

const AlertTitle = React.forwardRef<
  HTMLDivElement,
  React.ComponentProps<"div">
>(({ className, ...props }, ref) => (
  <div
    ref={ref}
    className={cn("font-semibold text-newt-text-primary", className)}
    {...props}
  />
))
AlertTitle.displayName = "AlertTitle"

const AlertDescription = React.forwardRef<
  HTMLDivElement,
  React.ComponentProps<"div">
>(({ className, ...props }, ref) => (
  <div ref={ref} className={className} {...props} />
))
AlertDescription.displayName = "AlertDescription"

/** A row of buttons under the text. */
const AlertActions = React.forwardRef<
  HTMLDivElement,
  React.ComponentProps<"div">
>(({ className, ...props }, ref) => (
  <div
    ref={ref}
    className={cn("mt-2 flex flex-wrap gap-2", className)}
    {...props}
  />
))
AlertActions.displayName = "AlertActions"

export { Alert, AlertTitle, AlertDescription, AlertActions, alertVariants }
<script setup lang="ts">
import { computed, type HTMLAttributes } from "vue"

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

import { alertVariants, type AlertVariants } from "."

/*
 * A notice that sits in the page, where a toast floats over it. What kind of
 * notice it is shows three ways: the bar down its leading edge, the icon's
 * shape and the words. A `danger` alert is announced at once (`role="alert"`);
 * the others wait their turn (`role="status"`).
 */
const props = withDefaults(
  defineProps<{
    class?: HTMLAttributes["class"]
    variant?: NonNullable<AlertVariants["variant"]>
    /** Overrides the role the variant would pick. */
    role?: string
  }>(),
  { variant: "info" }
)

/* One shape per kind, so the kind survives without its colour. */
const ICONS = {
  info: "M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 3.2a.9.9 0 1 1 0 1.8.9.9 0 0 1 0-1.8Zm-.8 3.4a.8.8 0 0 1 1.6 0v3.6a.8.8 0 0 1-1.6 0V7.6Z",
  success:
    "M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm3.2 5.5-3.6 3.9a.8.8 0 0 1-1.2 0L4.8 8.7a.8.8 0 1 1 1.2-1.1l1 1.1 3-3.3a.8.8 0 0 1 1.2 1.1Z",
  warning:
    "M8.9 1.5a1 1 0 0 0-1.8 0L.6 13.2A1 1 0 0 0 1.5 14.7h13a1 1 0 0 0 .9-1.5L8.9 1.5ZM8 5.5a.8.8 0 0 1 .8.8v3.2a.8.8 0 0 1-1.6 0V6.3a.8.8 0 0 1 .8-.8Zm0 7.2a.9.9 0 1 1 0-1.8.9.9 0 0 1 0 1.8Z",
  danger:
    "M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 3.5a.8.8 0 0 1 .8.8v3.4a.8.8 0 0 1-1.6 0V5.3a.8.8 0 0 1 .8-.8Zm0 7.4a.9.9 0 1 1 0-1.8.9.9 0 0 1 0 1.8Z",
} as const

const resolvedRole = computed(
  () => props.role ?? (props.variant === "danger" ? "alert" : "status")
)
</script>

<template>
  <div
    :role="resolvedRole"
    :data-variant="props.variant"
    :class="cn(alertVariants({ variant: props.variant }), props.class)"
  >
    <svg viewBox="0 0 16 16" aria-hidden="true" class="h-5 w-5 shrink-0">
      <path fill="currentColor" :d="ICONS[props.variant]" />
    </svg>
    <div class="flex min-w-0 flex-1 flex-col gap-0.5"><slot /></div>
  </div>
</template>
<script setup lang="ts">
import type { HTMLAttributes } from "vue"

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

const props = defineProps<{ class?: HTMLAttributes["class"] }>()
</script>

<template>
  <div :class="cn('font-semibold text-newt-text-primary', props.class)">
    <slot />
  </div>
</template>
<script setup lang="ts">
import type { HTMLAttributes } from "vue"

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

const props = defineProps<{ class?: HTMLAttributes["class"] }>()
</script>

<template>
  <div :class="cn('', props.class)"><slot /></div>
</template>
<script setup lang="ts">
import type { HTMLAttributes } from "vue"

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

const props = defineProps<{ class?: HTMLAttributes["class"] }>()
</script>

<template>
  <div :class="cn('mt-2 flex flex-wrap gap-2', props.class)"><slot /></div>
</template>
import { cva, type VariantProps } from "class-variance-authority"

export { default as Alert } from "./Alert.vue"
export { default as AlertTitle } from "./AlertTitle.vue"
export { default as AlertDescription } from "./AlertDescription.vue"
export { default as AlertActions } from "./AlertActions.vue"

export const alertVariants = cva(
  "flex w-full items-start gap-3 rounded-[var(--newt-radius-sm)] border border-s-4 border-newt-border bg-newt-bg-surface px-3.5 py-3 text-sm leading-5 text-newt-text-secondary",
  {
    variants: {
      variant: {
        info: "border-s-newt-text-link [&>svg]:text-newt-text-link",
        success: "border-s-newt-online [&>svg]:text-newt-online",
        warning: "border-s-newt-idle [&>svg]:text-newt-idle",
        danger: "border-s-newt-danger [&>svg]:text-newt-danger",
      },
    },
    defaultVariants: { variant: "info" },
  }
)

export type AlertVariants = VariantProps<typeof alertVariants>

Update the import paths

The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Alert to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Alert to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

Usage

import { Alert } from "@/components/ui/alert"

export function Example() {
  return <Alert />
}
<script setup lang="ts">
import { Alert, AlertTitle, AlertDescription, AlertActions } from "@/components/ui/alert"
</script>

<template>
  <Alert />
</template>

Examples

With actions

AlertActions holds one or two small buttons under the text. Put the action that resolves the notice first.

Root class newt-alert
import {
  Alert,
  AlertActions,
  AlertDescription,
  AlertTitle,
} from "@/registry/bases/newt/ui/alert"
import { Button } from "@/registry/bases/newt/ui/button"

/* Two answers to one notice: the one that resolves it comes first. */
export default function AlertActionsDemo() {
  return (
    <Alert variant="warning" className="max-w-md">
      <AlertTitle>You have unsaved changes</AlertTitle>
      <AlertDescription>
        The server name and the welcome channel were edited.
      </AlertDescription>
      <AlertActions>
        <Button size="sm" variant="success">
          Save changes
        </Button>
        <Button size="sm" variant="secondary">
          Reset
        </Button>
      </AlertActions>
    </Alert>
  )
}
<script setup lang="ts">
import {
  Alert,
  AlertActions,
  AlertDescription,
  AlertTitle,
} from "~~/registry/bases/newt/ui/alert"
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <!-- Two answers to one notice: the one that resolves it comes first. -->
  <Alert variant="warning" class="max-w-md">
    <AlertTitle>You have unsaved changes</AlertTitle>
    <AlertDescription>
      The server name and the welcome channel were edited.
    </AlertDescription>
    <AlertActions>
      <Button size="sm" variant="success">Save changes</Button>
      <Button size="sm" variant="secondary">Reset</Button>
    </AlertActions>
  </Alert>
</template>

Dismissible

The alert keeps no open state: render it while it applies and stop rendering it when the reader dismisses it. Rendering it again is also what makes a screen reader announce it.

Root class newt-alert
"use client"

import * as React from "react"

import {
  Alert,
  AlertActions,
  AlertDescription,
  AlertTitle,
} from "@/registry/bases/newt/ui/alert"
import { Button } from "@/registry/bases/newt/ui/button"

/*
 * The alert keeps no state of its own: the page decides whether it applies,
 * and rendering it again is what makes a screen reader announce it.
 */
export default function AlertDismissibleDemo() {
  const [open, setOpen] = React.useState(true)

  return (
    <div className="flex w-full max-w-md flex-col items-start gap-2">
      {open ? (
        <Alert variant="info">
          <AlertTitle>Welcome to the server</AlertTitle>
          <AlertDescription>
            Read the rules channel before you post for the first time.
          </AlertDescription>
          <AlertActions>
            <Button
              size="sm"
              variant="secondary"
              onClick={() => setOpen(false)}
            >
              Got it
            </Button>
          </AlertActions>
        </Alert>
      ) : (
        <Button size="sm" variant="secondary" onClick={() => setOpen(true)}>
          Show the notice again
        </Button>
      )}
    </div>
  )
}
<script setup lang="ts">
import { ref } from "vue"
import {
  Alert,
  AlertActions,
  AlertDescription,
  AlertTitle,
} from "~~/registry/bases/newt/ui/alert"
import { Button } from "~~/registry/bases/newt/ui/button"

/*
 * The alert keeps no state of its own: the page decides whether it applies,
 * and rendering it again is what makes a screen reader announce it.
 */
const open = ref(true)
</script>

<template>
  <div class="flex w-full max-w-md flex-col items-start gap-2">
    <Alert v-if="open" variant="info">
      <AlertTitle>Welcome to the server</AlertTitle>
      <AlertDescription>
        Read the rules channel before you post for the first time.
      </AlertDescription>
      <AlertActions>
        <Button size="sm" variant="secondary" @click="open = false">
          Got it
        </Button>
      </AlertActions>
    </Alert>
    <Button v-else size="sm" variant="secondary" @click="open = true">
      Show the notice again
    </Button>
  </div>
</template>

API reference

Alert

PropType
variant?"info" | "success" | "warning" | "danger"

What kind of notice it is. Sets the edge colour, the icon and the default role.

Type"info" | "success" | "warning" | "danger"
Default"info"
role?string

Overrides the role the variant picks: `alert` for `danger`, `status` for the others.

Typestring

AlertTitle, AlertDescription and AlertActions are the parts inside it. A title alone is a complete alert; the actions row holds one or two buttons.

Accessibility

A danger alert is role="alert" and interrupts a screen reader the moment it appears; the others are role="status" and are read when the reader is idle. Only render an alert when it applies, because one that is on the page from the start is not announced at all. The kind is carried by the icon’s shape and by the words as well as the colour, so write a title that says what happened.

Tokens

Alert reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-alert.

  • --newt-border
  • --newt-text-link
  • --newt-radius-sm
  • --newt-bg-surface
  • --newt-text-secondary
  • --newt-online
  • --newt-idle
  • --newt-danger
  • --newt-text-primary