dialog.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use client"
  2. import * as React from "react"
  3. import * as DialogPrimitive from "@radix-ui/react-dialog"
  4. import { XIcon } from "lucide-react"
  5. import { cn } from "@/lib/utils"
  6. function Dialog({
  7. ...props
  8. }: React.ComponentProps<typeof DialogPrimitive.Root>) {
  9. return <DialogPrimitive.Root data-slot="dialog" {...props} />
  10. }
  11. function DialogTrigger({
  12. ...props
  13. }: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
  14. return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
  15. }
  16. function DialogPortal({
  17. ...props
  18. }: React.ComponentProps<typeof DialogPrimitive.Portal>) {
  19. return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
  20. }
  21. function DialogClose({
  22. ...props
  23. }: React.ComponentProps<typeof DialogPrimitive.Close>) {
  24. return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
  25. }
  26. function DialogOverlay({
  27. className,
  28. ...props
  29. }: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
  30. return (
  31. <DialogPrimitive.Overlay
  32. data-slot="dialog-overlay"
  33. className={cn(
  34. "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",
  35. className
  36. )}
  37. {...props}
  38. />
  39. )
  40. }
  41. function DialogContent({
  42. className,
  43. children,
  44. showCloseButton = true,
  45. ...props
  46. }: React.ComponentProps<typeof DialogPrimitive.Content> & {
  47. showCloseButton?: boolean
  48. }) {
  49. return (
  50. <DialogPortal data-slot="dialog-portal">
  51. <DialogOverlay />
  52. <DialogPrimitive.Content
  53. data-slot="dialog-content"
  54. className={cn(
  55. "bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border p-6 shadow-lg duration-200 sm:max-w-lg",
  56. className
  57. )}
  58. {...props}
  59. >
  60. {children}
  61. {showCloseButton && (
  62. <DialogPrimitive.Close
  63. data-slot="dialog-close"
  64. className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
  65. >
  66. <XIcon />
  67. <span className="sr-only">Close</span>
  68. </DialogPrimitive.Close>
  69. )}
  70. </DialogPrimitive.Content>
  71. </DialogPortal>
  72. )
  73. }
  74. function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
  75. return (
  76. <div
  77. data-slot="dialog-header"
  78. className={cn("flex flex-col gap-2 text-center sm:text-left", className)}
  79. {...props}
  80. />
  81. )
  82. }
  83. function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
  84. return (
  85. <div
  86. data-slot="dialog-footer"
  87. className={cn(
  88. "flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
  89. className
  90. )}
  91. {...props}
  92. />
  93. )
  94. }
  95. function DialogTitle({
  96. className,
  97. ...props
  98. }: React.ComponentProps<typeof DialogPrimitive.Title>) {
  99. return (
  100. <DialogPrimitive.Title
  101. data-slot="dialog-title"
  102. className={cn("text-lg leading-none font-semibold", className)}
  103. {...props}
  104. />
  105. )
  106. }
  107. function DialogDescription({
  108. className,
  109. ...props
  110. }: React.ComponentProps<typeof DialogPrimitive.Description>) {
  111. return (
  112. <DialogPrimitive.Description
  113. data-slot="dialog-description"
  114. className={cn("text-muted-foreground text-sm", className)}
  115. {...props}
  116. />
  117. )
  118. }
  119. export {
  120. Dialog,
  121. DialogClose,
  122. DialogContent,
  123. DialogDescription,
  124. DialogFooter,
  125. DialogHeader,
  126. DialogOverlay,
  127. DialogPortal,
  128. DialogTitle,
  129. DialogTrigger,
  130. }