Confirm dialog
Overlay: dialog, drawer, popover and menu
Says exactly what disappears and what stays, whether it can be undone, and for irreversible actions asks the name to be typed.
실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.
내 앱에 넣기
아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.
git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이
src/components/taster에 들어갑니다.node ../taster/packages/engine/cli.mjs add blocks/confirm-dialog --from https://taster-six.vercel.app --dir src/components/taster부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고,
tokens.css를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.// tailwind.config.cjs module.exports = { presets: [require('./src/components/taster/tailwind-preset.cjs')], content: ['./index.html', './src/**/*.{ts,tsx}'], };Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는
src/의 스타일시트 맨 위에 이 두 줄을 넣습니다.@import "tailwindcss"; @config "../tailwind.config.cjs";tokens.css는:root에--bg,--text,--border,--accent를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.
언제 쓰나요
- Confirm destructive dialogSays exactly what disappears and what stays, whether it can be undone, and for irreversible actions asks the name to be typed.
대신 없애 주는 AI 티
- Generated copy voice (CPY-01)
정보
코드 보기
'use client'
import * as React from 'react'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import { cn } from './_cn'
// ConfirmDialog. Asks before something that cannot be undone. The body says exactly what disappears and what
// stays; for irreversible actions `confirmText` asks the person to type a name before the button wakes up.
// Focus starts on Cancel, the dialog is an alertdialog, and while onConfirm's promise runs the button says so and
// the dialog cannot be dismissed. The destructive button is outlined in the danger token, not a red slab.
export interface ConfirmDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
title: React.ReactNode
/** What goes, what stays, and whether it can be undone. */
description: React.ReactNode
confirmLabel?: string
pendingLabel?: string
cancelLabel?: string
/** If set, the person must type this exactly to enable the confirm button. */
confirmText?: string
confirmPrompt?: (text: string) => React.ReactNode
onConfirm: () => void | Promise<void>
}
export function ConfirmDialog({
open,
onOpenChange,
title,
description,
confirmLabel = 'Delete',
pendingLabel = 'Deleting...',
cancelLabel = 'Cancel',
confirmText,
confirmPrompt = (t) => (
<>
Type <span className="font-medium text-text">{t}</span> to confirm.
</>
),
onConfirm,
}: ConfirmDialogProps) {
const [typed, setTyped] = React.useState('')
const [pending, setPending] = React.useState(false)
const [error, setError] = React.useState<string | null>(null)
const cancelRef = React.useRef<HTMLButtonElement>(null)
const inputId = React.useId()
const ready = !confirmText || typed === confirmText
React.useEffect(() => {
if (!open) {
setTyped('')
setError(null)
setPending(false)
}
}, [open])
const run = async () => {
if (!ready || pending) return
setPending(true)
setError(null)
try {
await onConfirm()
onOpenChange(false)
} catch (e) {
setError(e instanceof Error ? e.message : 'Something went wrong. Nothing was deleted.')
} finally {
setPending(false)
}
}
return (
<DialogPrimitive.Root open={open} onOpenChange={(o) => !pending && onOpenChange(o)}>
<DialogPrimitive.Portal>
<DialogPrimitive.Overlay className="fixed inset-0 z-50" style={{ background: 'var(--scrim)' }} />
<DialogPrimitive.Content
role="alertdialog"
onOpenAutoFocus={(e) => {
e.preventDefault()
cancelRef.current?.focus()
}}
className={cn(
'fixed left-1/2 top-1/2 z-50 flex w-[calc(100%-2rem)] max-w-md -translate-x-1/2 -translate-y-1/2 flex-col gap-4',
'rounded-lg border border-border bg-bg-popover p-6 text-text shadow-pop outline-none',
)}
>
<div className="flex flex-col gap-2">
<DialogPrimitive.Title className="text-title font-semibold text-text">{title}</DialogPrimitive.Title>
<DialogPrimitive.Description asChild>
<div className="text-sm text-text-muted">{description}</div>
</DialogPrimitive.Description>
</div>
{confirmText && (
<div className="flex flex-col gap-1.5">
<label htmlFor={inputId} className="text-sm text-text-muted">
{confirmPrompt(confirmText)}
</label>
<input
id={inputId}
type="text"
autoComplete="off"
spellCheck={false}
value={typed}
onChange={(e) => setTyped(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') run()
}}
className={cn(
'h-11 w-full rounded-md border border-border-input bg-transparent px-3 text-sm text-text desk:h-9',
'outline-none focus-visible:ring-2 focus-visible:ring-ring',
)}
/>
</div>
)}
{error && <p className="text-sm text-text">{error}</p>}
<div className="flex flex-wrap justify-end gap-2">
<DialogPrimitive.Close asChild>
<button
ref={cancelRef}
type="button"
disabled={pending}
className={cn(
'inline-flex h-11 items-center rounded-md border border-border px-4 text-sm font-medium text-text outline-none desk:h-9',
'transition-colors duration-press hover:border-text-subtle focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50',
)}
>
{cancelLabel}
</button>
</DialogPrimitive.Close>
<button
type="button"
onClick={run}
disabled={!ready || pending}
aria-busy={pending || undefined}
className={cn(
'inline-flex h-11 items-center rounded-md border border-danger px-4 text-sm font-medium text-danger outline-none desk:h-9',
'transition-[colors,transform] duration-press ease-out active:scale-press',
'focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40',
)}
>
{pending ? pendingLabel : confirmLabel}
</button>
</div>
</DialogPrimitive.Content>
</DialogPrimitive.Portal>
</DialogPrimitive.Root>
)
}