import { CheckCircle2Icon, InfoIcon } from "lucide-react"
import {Alert はインラインのコールアウトです。アイコン、タイトル、説明、任意のアクションを CSS グリッド上に配置します。依存関係のない role="alert" の div で、下層にプリミティブはなく、両バックエンドで同一です。ネオブルータリストのレシピに沿ってスタイルしており、太い枠線、硬い影、太字のタイポグラフィに加え、error・success・warning・info の配色用に status 軸があります。
次のような場面で使います:
- フォームやリクエストのエラー — 検証失敗や API の失敗を、問題の近くにそのまま表示します。
- 成功・状態の確認 — 「設定を保存しました」「トライアル期限が近い」「支払いを受け取りました」など。
statusprop で色分けします。 - フォローアップ付きの案内 — 警告やアップグレード案内に
AlertActionボタン(「有効にする」「元に戻す」「アップグレード」)を添えます。
インストール#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/alert.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/alert.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/alert.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/alert.json
以下のコードをコピーしてプロジェクトに貼り付けます。
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
// CSS-grid layout (shadcn-style): drop an icon as a direct child and the alert
// auto-arranges into an icon column + a content column — no manual flex wrapper.
// `has-[>svg]` adds the icon column only when an icon is present. NeoBrutalist
// chrome: hard 2px border, square corners, hard offset shadow.
const alertVariants = cva(
cn(
"group/alert relative grid w-full grid-cols-[0_1fr] items-start gap-y-1 rounded border-2 px-4 py-3 text-left text-sm shadow-md",
"has-[>svg]:grid-cols-[1.25rem_1fr] has-[>svg]:gap-x-3",
"has-data-[slot=alert-action]:pr-14",
"[&>svg]:size-5 [&>svg]:translate-y-0.5 [&>svg]:shrink-0 [&>svg]:text-current"
),
{
variants: {
// shadcn's variant axis (kept for API compatibility). `solid` is a Neobrutalism
// addition.
variant: {
default:
"border-border bg-background text-foreground *:data-[slot=alert-description]:text-muted-foreground",
destructive:
"border-red-900 bg-red-300 text-red-900 *:data-[slot=alert-description]:text-red-900/80",
solid:
"border-border bg-foreground text-background *:data-[slot=alert-description]:text-background/80",
},
// Neobrutalism status axis — additive; overrides the variant's colors when set.
status: {
error:
"border-red-900 bg-red-300 text-red-900 *:data-[slot=alert-description]:text-red-900/80",
success:
"border-green-900 bg-green-300 text-green-900 *:data-[slot=alert-description]:text-green-900/80",
warning:
"border-yellow-900 bg-yellow-300 text-yellow-900 *:data-[slot=alert-description]:text-yellow-900/80",
info: "border-blue-900 bg-blue-300 text-blue-900 *:data-[slot=alert-description]:text-blue-900/80",
},
},
defaultVariants: {
variant: "default",
},
}
)
function Alert({
className,
variant,
status,
...props
}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
return (
<div
data-slot="alert"
role="alert"
className={cn(alertVariants({ variant, status }), className)}
{...props}
/>
)
}
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-title"
className={cn(
"col-start-2 min-h-5 font-head text-base leading-tight tracking-tight [&_a]:underline [&_a]:underline-offset-3",
className
)}
{...props}
/>
)
}
function AlertDescription({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-description"
className={cn(
"col-start-2 grid justify-items-start gap-1 text-sm [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p]:leading-relaxed [&_p:not(:last-child)]:mb-4",
className
)}
{...props}
/>
)
}
function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-action"
className={cn("absolute top-2 right-2", className)}
{...props}
/>
)
}
export { Alert, AlertTitle, AlertDescription, AlertAction, alertVariants }
プロジェクトの構成に合わせてインポートパスを更新します。
使い方#
import {
Alert,
AlertAction,
AlertDescription,
AlertTitle,
} from "@/components/ui/alert"<Alert>
<InfoIcon />
<AlertTitle>Heads up!</AlertTitle>
<AlertDescription>
You can add components and dependencies to your app using the cli.
</AlertDescription>
<AlertAction>
<Button variant="outline">Enable</Button>
</AlertAction>
</Alert>コンポジション#
Alert を構築するには、次のコンポジションを使用します:
Alert
├── Icon
├── AlertTitle
├── AlertDescription
└── AlertAction使用例#
基本#
アイコン、タイトル、説明を備えた基本的なアラートです。
import { CheckCircle2Icon } from "lucide-react"
import {破壊的#
variant="destructive" を使って破壊的なアラートを作成します。
import { AlertCircleIcon } from "lucide-react"
import {アクション#
AlertAction を使って、ボタンなどのアクション要素をアラートに追加します。
import {
Alert,
AlertAction,カスタムカラー#
Alert コンポーネントに bg-amber-50 dark:bg-amber-950 などのカスタムクラスを追加することで、アラートの色をカスタマイズできます。
import { AlertTriangleIcon } from "lucide-react"
import {RTL#
Neobrutalism で RTL を有効にする方法は、RTL 設定ガイドをご覧ください。
"use client"
import * as React from "react"アクセシビリティ#
Alert のルートは role="alert" を描画します。これは暗黙の assertive なライブリージョンで、DOM に挿入された瞬間にスクリーンリーダーが内容を読み上げます。読み上げは動的な挿入時にだけ行われます。ページ読み込み時から既に存在している Alert は通常のテキストとして読まれるため、聞かせたいタイミングで条件付きレンダリングしてください。role="alert" はフォーカスを移しません。続行前にユーザーの応答が必要な場合は Alert Dialog を使い、AlertAction は近道として扱い、唯一の手段にはしないでください。
APIリファレンス#
Alert#
Alert コンポーネントは、ユーザーの注意を促すコールアウトを表示します。
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "default" |
AlertTitle#
AlertTitle コンポーネントは、アラートのタイトルを表示します。
| Prop | Type | Default |
|---|---|---|
className | string | - |
AlertDescription#
AlertDescription コンポーネントは、アラートの説明や内容を表示します。
| Prop | Type | Default |
|---|---|---|
className | string | - |
AlertAction#
AlertAction コンポーネントは、アラートの右上隅に絶対配置されるアクション要素(ボタンなど)を表示します。
| Prop | Type | Default |
|---|---|---|
className | string | - |