پرش به محتوا

گفتگو

یک پنجرهٔ مدال برای تأییدها، فرم‌های سریع و هشدارهای اقدام مخرب — که روی صفحه با حاشیه‌های ضخیم و سایه‌ای تیره قرار گرفته است.

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

دیالوگ یک پنجرهٔ مدال روی صفحه باز می‌کند — فوکوس داخل می‌رود، محتوای پشت inert می‌شود، و بستن شما را دقیقاً به همان‌جا برمی‌گرداند. بر پایهٔ Base UI Dialog ساخته شده و با دستور پخت نئوبروتالیستی استایل شده: حاشیه‌های ضخیم، سایه‌های سخت و تایپوگرافی پررنگ.

برای این موارد از آن استفاده کنید:

  • تأییدها — حذف، خروج، کاهش پلن؛ هر اقدام مخربی که توقف سخت می‌خواهد.
  • فرم‌های سریع — ویرایش پروفایل، rename پروژه، دعوت هم‌تیمی بدون ترک صفحه.
  • نمای جزئیات متمرکز — پیش‌نمایش تصویر، متن لایسنس، یادداشت انتشار که route جدا نمی‌خواهد.

نصب

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

وابستگی‌های زیر را نصب کنید:

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

کد زیر را کپی کرده و در پروژهٔ خود جای‌گذاری کنید.

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,
}

مسیرهای import را مطابق با پیکربندی پروژهٔ خود به‌روزرسانی کنید.

استفاده

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>

ترکیب

برای ساخت یک Dialog از ترکیب زیر استفاده کنید:

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

نمونه‌ها

دکمهٔ بستن سفارشی

کنترل بستن پیش‌فرض را با دکمهٔ خودتان جایگزین کنید.

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

بدون دکمهٔ بستن

برای پنهان کردن دکمهٔ بستن از showCloseButton={false} استفاده کنید.

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

فوتر چسبان

هنگام اسکرول محتوا، اقدامات را قابل‌مشاهده نگه دارید.

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

محتوای قابل اسکرول

محتوای طولانی می‌تواند اسکرول شود، در حالی که سرصفحه قابل‌مشاهده باقی می‌ماند.

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

RTL

برای فعال‌سازی پشتیبانی RTL در Neobrutalism، راهنمای پیکربندی RTL را ببینید.

"use client"

import {

دسترسی‌پذیری

دیالوگ از الگوی WAI-ARIA Dialog (Modal) پیروی می‌کند: محتوا با role="dialog" و aria-modal رندر می‌شود، با DialogTitle برچسب و با DialogDescription توصیف می‌شود، تا باز بودن فوکوس گیر می‌افتد، و بستن فوکوس را به تریگر برمی‌گرداند.

تعاملات صفحه‌کلید:

کلیدعمل
Space / Enterباز کردن دیالوگ از تریگر فوکوس‌شده
Tab / Shift + Tabانتقال فوکوس به عنصر بعدی / قبلی داخل دیالوگ (فوکوس گیر است)
Escبستن دیالوگ و بازگرداندن فوکوس به تریگر

مرجع API

برای اطلاعات بیشتر، مستندات Base UI را ببینید.