Przejdź do treści

Okno dialogowe

Okno modalne do potwierdzeń, szybkich formularzy i monitów o działania powodujące utratę danych — wyświetlane na stronie z grubymi ramkami i wyraźnym cieniem.

import { Button } from "@/components/ui/button"
import {
  Dialog,

Dialog otwiera okno modalne nad stroną — fokus przenosi się do środka, treść w tle staje się nieaktywna, a po zamknięciu wracasz dokładnie tam, gdzie wcześniej. Zbudowany na prymitywie Base UI Dialog i ostylowany zgodnie z neobrutalistyczną recepturą: grube obramowania, twarde cienie i wyrazista typografia.

Sięgnij po ten komponent, gdy:

  • Potwierdzenia — usuwanie, wylogowanie, obniżenie planu; wszystko, co destrukcyjne i zasługuje na twarde zatrzymanie.
  • Szybkie formularze — edycja profilu, zmiana nazwy projektu, zaproszenie osoby z zespołu bez opuszczania strony.
  • Skupione widoki szczegółów — podglądy obrazów, tekst licencji, informacje o wydaniu, które nie potrzebują własnej podstrony.

Instalacja

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

Zainstaluj następujące zależności:

pnpm add @base-ui/react
npm install @base-ui/react
yarn add @base-ui/react
bun add @base-ui/react

Skopiuj i wklej poniższy kod do swojego projektu.

components/ui/dialog.tsx
"use client"

import * as React from "react"
import { XIcon } from "lucide-react"
import { Dialog as DialogPrimitive } from "radix-ui"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"

function Dialog({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
  return <DialogPrimitive.Root data-slot="dialog" {...props} />
}

function DialogTrigger({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
  return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
}

function DialogPortal({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
  return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
}

function DialogClose({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
  return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
}

function DialogOverlay({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
  return (
    <DialogPrimitive.Overlay
      data-slot="dialog-overlay"
      className={cn(
        "fixed inset-0 isolate z-50 bg-foreground/20 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
        className
      )}
      {...props}
    />
  )
}

function DialogContent({
  className,
  children,
  showCloseButton = true,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
  showCloseButton?: boolean
}) {
  return (
    <DialogPortal>
      <DialogOverlay />
      <DialogPrimitive.Content
        data-slot="dialog-content"
        className={cn(
          "fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded border-2 bg-popover p-4 text-sm text-popover-foreground shadow-md duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
          className
        )}
        {...props}
      >
        {children}
        {showCloseButton && (
          <DialogPrimitive.Close data-slot="dialog-close" asChild>
            <Button
              variant="ghost"
              className="absolute top-2 right-2"
              size="icon-sm"
            >
              <XIcon />
              <span className="sr-only">Close</span>
            </Button>
          </DialogPrimitive.Close>
        )}
      </DialogPrimitive.Content>
    </DialogPortal>
  )
}

function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="dialog-header"
      className={cn("flex flex-col gap-2", className)}
      {...props}
    />
  )
}

function DialogFooter({
  className,
  showCloseButton = false,
  children,
  ...props
}: React.ComponentProps<"div"> & {
  showCloseButton?: boolean
}) {
  return (
    <div
      data-slot="dialog-footer"
      className={cn(
        "-mx-4 -mb-4 flex flex-col-reverse gap-2 rounded-b-xl border-t-2 bg-muted/50 p-4 sm:flex-row sm:justify-end",
        className
      )}
      {...props}
    >
      {children}
      {showCloseButton && (
        <DialogPrimitive.Close asChild>
          <Button variant="outline">Close</Button>
        </DialogPrimitive.Close>
      )}
    </div>
  )
}

function DialogTitle({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
  return (
    <DialogPrimitive.Title
      data-slot="dialog-title"
      className={cn(
        "font-head text-base leading-none font-medium",
        className
      )}
      {...props}
    />
  )
}

function DialogDescription({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
  return (
    <DialogPrimitive.Description
      data-slot="dialog-description"
      className={cn(
        "text-sm text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground",
        className
      )}
      {...props}
    />
  )
}

export {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogOverlay,
  DialogPortal,
  DialogTitle,
  DialogTrigger,
}

Zaktualizuj ścieżki importu, aby pasowały do konfiguracji twojego projektu.

Użycie

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
<Dialog>
  <DialogTrigger>Open</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you absolutely sure?</DialogTitle>
      <DialogDescription>
        This action cannot be undone. This will permanently delete your account
        and remove your data from our servers.
      </DialogDescription>
    </DialogHeader>
  </DialogContent>
</Dialog>

Kompozycja

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

Dialog
├── DialogTrigger
└── DialogContent
    ├── DialogHeader
    │   ├── DialogTitle
    │   └── DialogDescription
    └── DialogFooter

Przykłady

Niestandardowy przycisk zamykania

Zastąp domyślną kontrolkę zamykania własnym przyciskiem.

import { Button } from "@/components/ui/button"
import {
  Dialog,

Bez przycisku zamykania

Użyj showCloseButton={false}, aby ukryć przycisk zamykania.

import { Button } from "@/components/ui/button"
import {
  Dialog,

Przyklejona stopka

Utrzymuj akcje widoczne, gdy zawartość jest przewijana.

import { Button } from "@/components/ui/button"
import {
  Dialog,

Przewijalna zawartość

Długa zawartość może być przewijana, podczas gdy nagłówek pozostaje widoczny.

import { Button } from "@/components/ui/button"
import {
  Dialog,

RTL

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

"use client"

import {

Dostępność

Dialog realizuje wzorzec WAI-ARIA Dialog (Modal): treść renderuje się z role="dialog" i aria-modal, etykietę nadaje jej DialogTitle, a opis DialogDescription; póki okno jest otwarte, fokus pozostaje uwięziony w środku, a po zamknięciu wraca do wyzwalacza.

Obsługa klawiatury:

KlawiszDziałanie
Space / EnterOtwiera dialog, gdy fokus jest na wyzwalaczu
Tab / Shift + TabPrzenosi fokus na następny / poprzedni element wewnątrz dialogu (fokus jest uwięziony)
EscZamyka dialog i przywraca fokus na wyzwalacz

Referencja API

Więcej informacji znajdziesz w dokumentacji Base UI.