Przejdź do treści

Alert

Wbudowane podpisy informujące o błędach w formularzu, ostrzeżeniach i potwierdzeniach pomyślnego zakończenia — kolorowe panele z obramowaniami w stylu neobrutalistycznym i ostrymi cieniami.

import { CheckCircle2Icon, InfoIcon } from "lucide-react"

import {

Alert to komunikat osadzony w treści strony: ikona, tytuł, opis i opcjonalna akcja, rozmieszczone na siatce CSS. To pozbawiony zależności div z role="alert" — bez prymitywu pod spodem, identyczny w obu backendach — ostylowany zgodnie z neobrutalistyczną recepturą: grube obramowania, twarde cienie i wyrazista typografia, z osią status odpowiadającą za kolory błędu, sukcesu, ostrzeżenia i informacji.

Sięgnij po ten komponent, gdy:

  • Błędy formularzy i żądań — pokaż błąd walidacji albo nieudane wywołanie API tuż obok miejsca, w którym coś się zepsuło.
  • Potwierdzenia sukcesu i statusu — „ustawienia zapisane”, „okres próbny dobiega końca”, „płatność otrzymana”, w kolorze sterowanym propem status.
  • Komunikaty z dalszym krokiem — ostrzeżenie lub informacja o ulepszeniu planu z przyciskiem AlertAction („Włącz”, „Cofnij”, „Ulepsz”).

Instalacja

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/alert.json
npx shadcn@latest add https://neobrutalism.com/r/base/alert.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/alert.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/alert.json

Skopiuj i wklej poniższy kod do swojego projektu.

components/ui/alert.tsx
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 }

Zaktualizuj ścieżki importów, aby pasowały do struktury twojego projektu.

Użycie

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>

Kompozycja

Użyj następującej kompozycji, aby zbudować Alert:

Alert
├── Icon
├── AlertTitle
├── AlertDescription
└── AlertAction

Przykłady

Podstawowy

Podstawowy alert z ikoną, tytułem i opisem.

import { CheckCircle2Icon } from "lucide-react"

import {

Destrukcyjny

Użyj variant="destructive", aby utworzyć destrukcyjny alert.

import { AlertCircleIcon } from "lucide-react"

import {

Akcja

Użyj AlertAction, aby dodać do alertu przycisk lub inny element akcji.

import {
  Alert,
  AlertAction,

Niestandardowe kolory

Możesz dostosować kolory alertu, dodając niestandardowe klasy, takie jak bg-amber-50 dark:bg-amber-950, do komponentu Alert.

import { AlertTriangleIcon } from "lucide-react"

import {

RTL

Aby włączyć obsługę RTL w Neobrutalism, zobacz przewodnik konfiguracji RTL.

"use client"

import * as React from "react"

Dostępność

Element główny alertu renderuje role="alert" — niejawny asertywny obszar live: czytniki ekranu odczytują jego treść w chwili wstawienia do DOM. Ogłoszenie pada wyłącznie przy dynamicznym wstawieniu — alert obecny już przy załadowaniu strony jest czytany jak zwykły tekst, więc renderuj go warunkowo, jeśli ma zostać usłyszany. role="alert" nigdy nie przenosi fokusu; jeśli użytkownik musi zareagować, zanim ruszy dalej, użyj zamiast tego dialogu alertu, a AlertAction traktuj jako skrót, nie jedyną drogę.

Referencja API

Alert

Komponent Alert wyświetla wyróżnienie, aby zwrócić uwagę użytkownika.

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

AlertTitle

Komponent AlertTitle wyświetla tytuł alertu.

PropTypeDefault
classNamestring-

AlertDescription

Komponent AlertDescription wyświetla opis lub treść alertu.

PropTypeDefault
classNamestring-

AlertAction

Komponent AlertAction wyświetla element akcji (taki jak przycisk) umieszczony absolutnie w prawym górnym rogu alertu.

PropTypeDefault
classNamestring-