dialog.tsx 3.9 KB

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