-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.tsx
More file actions
181 lines (168 loc) · 4.76 KB
/
Copy pathcomponent.tsx
File metadata and controls
181 lines (168 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use client"
import { useState, useEffect } from "react"
import { cn } from "@agent-patterns/core"
interface ConfirmDialogProps {
title: string
description: string
confirmLabel?: string
cancelLabel?: string
variant?: "default" | "destructive" | "warning"
icon?: React.ReactNode
open: boolean
onConfirm?: () => void
onCancel?: () => void
className?: string
}
export function ConfirmDialog({
title,
description,
confirmLabel = "Confirm",
cancelLabel = "Cancel",
variant = "default",
icon,
open,
onConfirm,
onCancel,
className,
}: ConfirmDialogProps) {
const [isOpen, setIsOpen] = useState(open)
const [isLoading, setIsLoading] = useState(false)
useEffect(() => {
setIsOpen(open)
}, [open])
const handleConfirm = async () => {
setIsLoading(true)
try {
await onConfirm?.()
setIsOpen(false)
} finally {
setIsLoading(false)
}
}
const handleCancel = () => {
onCancel?.()
setIsOpen(false)
}
const variantStyles = {
default: {
button: "bg-primary text-primary-foreground hover:bg-primary/90",
icon: "text-primary",
},
destructive: {
button: "bg-red-600 text-white hover:bg-red-700",
icon: "text-red-600",
},
warning: {
button: "bg-yellow-600 text-white hover:bg-yellow-700",
icon: "text-yellow-600",
},
}
if (!isOpen) return null
return (
<>
{/* Backdrop */}
<div
className="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm"
onClick={handleCancel}
aria-hidden="true"
/>
{/* Dialog */}
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
<div
className={cn(
"relative w-full max-w-md rounded-lg border border-border bg-card p-6 shadow-lg",
"animate-in fade-in-0 zoom-in-95",
className
)}
role="dialog"
aria-modal="true"
aria-labelledby="dialog-title"
aria-describedby="dialog-description"
>
{/* Icon */}
{icon && (
<div className="mb-4 flex justify-center">
<div
className={cn(
"flex h-12 w-12 items-center justify-center rounded-full bg-muted",
variantStyles[variant].icon
)}
>
{icon}
</div>
</div>
)}
{/* Content */}
<div className="text-center">
<h2
id="dialog-title"
className="mb-2 text-lg font-semibold text-foreground"
>
{title}
</h2>
<p
id="dialog-description"
className="text-sm text-muted-foreground"
>
{description}
</p>
</div>
{/* Actions */}
<div className="mt-6 flex gap-3">
<button
type="button"
onClick={handleCancel}
disabled={isLoading}
className={cn(
"flex-1 rounded-lg border border-border bg-background px-4 py-2 text-sm font-medium",
"hover:bg-accent hover:text-accent-foreground",
"disabled:opacity-50 disabled:cursor-not-allowed",
"transition-colors"
)}
>
{cancelLabel}
</button>
<button
type="button"
onClick={handleConfirm}
disabled={isLoading}
className={cn(
"flex-1 rounded-lg px-4 py-2 text-sm font-medium",
variantStyles[variant].button,
"disabled:opacity-50 disabled:cursor-not-allowed",
"transition-colors"
)}
>
{isLoading ? (
<span className="flex items-center justify-center gap-2">
<svg
className="h-4 w-4 animate-spin"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
Loading...
</span>
) : (
confirmLabel
)}
</button>
</div>
</div>
</div>
</>
)
}