Chuyển tới nội dung
Component

Alert

An inline banner with a title, optional body and caller-supplied actions. Use info, success, warning or danger to communicate the kind of message. Static banners are named sections; opt into status or alert for dynamic updates.

Xem trước

Upload complete

Your files are ready to share.

Props

  • variant
  • dismissible
  • title
  • body
  • icon
  • action
import { Alert } from "@/components/ui/alert"

<Alert title="Upload complete">Your files are ready to share.</Alert>

Cách dùng

Khi nào dùng

  • Show a message that belongs to the current page, such as a saved setting or a failed upload.
  • Explain a condition and offer an optional next step beside it.

Khi nào không dùng

  • A destructive choice that requires confirmation. Use AlertDialog.
  • A short message that disappears after an action. Use Toast.
  • An empty successful result. Use EmptyState.

Biến thể

variant

The message type. Each value uses its status soft pair: info, success, warning or danger.

  • Upload complete

    Your files are ready to share.

    infomặc định

  • Upload complete

    Your files are ready to share.

    success

  • Upload complete

    Your files are ready to share.

    warning

  • Upload complete

    Your files are ready to share.

    danger

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

<Alert title="Upload complete">Your files are ready to share.</Alert>
<Alert variant="success" title="Upload complete">Your files are ready to share.</Alert>
<Alert variant="warning" title="Upload complete">Your files are ready to share.</Alert>
<Alert variant="danger" title="Upload complete">Your files are ready to share.</Alert>

Ví dụ

Inline guidance

Your workspace is ready

Invite a teammate to start reviewing projects together.
import { Alert } from "@/components/ui/alert"

<Alert title="Your workspace is ready">Invite a teammate to start reviewing projects together.</Alert>

With an action

Storage is nearly full

Remove unused files before your next upload.
import { Alert } from "@/components/ui/alert"
import { Link } from "@/components/ui/link"

<Alert variant="warning" title="Storage is nearly full" action={<Link href="#storage">Manage storage</Link>}>Remove unused files before your next upload.</Alert>

Dismissible update

The parent removes the banner when onDismiss runs. Supply a specific accessible name for the close button.

Changes saved

Your project settings are up to date.
import { Alert } from "@/components/ui/alert"

const dismissAlert = () => { /* Remove the alert from application state. */ }

<Alert variant="success" title="Changes saved" role="status" dismissible dismissLabel="Dismiss saved changes message" onDismiss={dismissAlert}>Your project settings are up to date.</Alert>

Nên và không nên

Nên

  • Write a short title and explain the next step in the body.
  • Use caller-supplied actions to resolve a warning or error.
  • Provide dismissLabel and remove the banner in onDismiss when dismissal is allowed.

Không nên

  • Do not use Alert for confirmation dialogs; use AlertDialog.
  • Do not announce every static banner as an alert.
  • Do not rely on the icon or color alone to communicate the message.

Props

TênKiểuGiá trịMặc địnhMô tả
variantenuminfo | success | warning | dangerinfoThe message type. Each value uses its status soft pair: info, success, warning or danger.
title bắt buộcnode--Short heading that names the banner.
iconnode--Decorative icon replacing the variant icon. null hides the icon.
actionnode--An action such as Button or Link, supplied by the caller.
dismissibleboolean-falseShows a dismissal button. true requires dismissLabel and onDismiss.
dismissLabelstring--Accessible name of the dismissal button, required when dismissible is true.
onDismissfunction--Called when the dismissal button is activated. The parent removes the banner. Required when dismissible is true.

Slot

TênBắt buộcMô tả
childrenkhôngOptional message body.

Cấu tạo

PhầnMô tả
rootNamed section with a soft status fill and border, and primary text.
iconDecorative 16px variant icon in the status color, in a box one line tall so it stays level with the first line of the title. Replaced by icon or hidden with icon={null}.
contentColumn that holds the title, body and optional actions.
titleHeading that names the section by default.
bodyOptional body in primary text, like the title.
actionsCaller-supplied action below the message.
closeOptional dismissal button, named by dismissLabel.
hitAreaTransparent minimum target around the dismissal button.

Khả năng truy cập

Role: region

PhímHành động
TabFocus a supplied action or the dismissal button.
Enter / SpaceActivate the focused dismissal button.
  • The title names the section unless aria-label or aria-labelledby supplies a different name.
  • Variant alone does not create a live region. Use role="status" for polite updates or role="alert" for urgent updates.
  • The icon is decorative; the title and body carry the message.
  • Dismissal remains under parent control. Alert never hides itself.

Cài đặt

npx shadcn add https://werewolf-ui.vercel.app/r/alert.json

Lệnh này thêm luôn styles/werewolf.css, bộ token mà mọi component dùng, và các component khác mà nó cần. Import file đó một lần trong CSS gốc, ngay sau @import "tailwindcss", với đường dẫn tính từ file CSS đó (từ app/globals.css là @import "../styles/werewolf.css").