All files / src/components dialog.tsx

100% Statements 29/29
100% Branches 0/0
100% Functions 6/6
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                        4x   4x   4x   4x   4x       25x                   25x               4x   4x       14x                                         14x                             14x                                 4x   4x       14x                 14x   4x   4x       12x             12x   4x   4x       10x             10x               4x   4x       3x         3x               4x                            
import type {
  ComponentPropsWithoutRef,
  ComponentRef,
  HTMLAttributes,
} from 'react';
import { forwardRef } from 'react';
import { X } from 'lucide-react';
 
import { cn } from '@/lib/utils';
import {
  Close,
  Content,
  Description,
  Overlay,
  Portal,
  Root,
  Title,
  Trigger,
} from '@radix-ui/react-dialog';
 
const Dialog = Root;
 
const DialogTrigger = Trigger;
 
const DialogPortal = Portal;
 
const DialogClose = Close;
 
const DialogOverlay = forwardRef<
  ComponentRef<typeof Overlay>,
  ComponentPropsWithoutRef<typeof Overlay>
>(({ className, ...props }, ref) => {
  const overlayStyles = [
    // Positioning
    'fixed inset-0 z-50',
    // Appearance
    'bg-black/80',
    // Animations
    'data-[state=open]:animate-in data-[state=closed]:animate-out',
    'data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
  ];
 
  return (
    <Overlay
      ref={ref}
      className={cn(overlayStyles.join(' '), className)}
      {...props}
    />
  );
});
DialogOverlay.displayName = Overlay.displayName;
 
const DialogContent = forwardRef<
  ComponentRef<typeof Content>,
  ComponentPropsWithoutRef<typeof Content>
>(({ className, children, ...props }, ref) => {
  const contentStyles = [
    // Positioning
    'fixed left-[50%] top-[50%] z-50',
    'translate-x-[-50%] translate-y-[-50%]',
    // Layout
    'grid w-full max-w-lg gap-4',
    // Appearance
    'border bg-background shadow-lg',
    // Spacing
    'p-6',
    // Animations
    'duration-200',
    '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',
    'data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%]',
    'data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%]',
    // Responsive
    'sm:rounded-lg',
  ];
 
  const closeButtonStyles = [
    // Positioning
    'absolute right-4 top-4',
    // Appearance
    'rounded-sm opacity-70',
    // Focus & interaction
    'ring-offset-background transition-opacity',
    'hover:opacity-100',
    'focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
    // Disabled state
    'disabled:pointer-events-none',
    // Data states
    'data-[state=open]:bg-accent data-[state=open]:text-muted-foreground',
  ];
 
  return (
    <DialogPortal>
      <DialogOverlay />
      <Content
        ref={ref}
        className={cn(contentStyles.join(' '), className)}
        {...props}
      >
        {children}
        <Close className={closeButtonStyles.join(' ')}>
          <X className="h-4 w-4" />
          <span className="sr-only">Close</span>
        </Close>
      </Content>
    </DialogPortal>
  );
});
DialogContent.displayName = Content.displayName;
 
const DialogHeader = ({
  className,
  ...props
}: HTMLAttributes<HTMLDivElement>) => {
  const headerStyles = [
    // Layout
    'flex flex-col',
    // Spacing
    'space-y-1.5',
    // Text alignment
    'text-center sm:text-left',
  ];
 
  return <div className={cn(headerStyles.join(' '), className)} {...props} />;
};
DialogHeader.displayName = 'DialogHeader';
 
const DialogFooter = ({
  className,
  ...props
}: HTMLAttributes<HTMLDivElement>) => {
  const footerStyles = [
    // Layout
    'flex flex-col-reverse',
    // Responsive layout
    'sm:flex-row sm:justify-end sm:space-x-2',
  ];
 
  return <div className={cn(footerStyles.join(' '), className)} {...props} />;
};
DialogFooter.displayName = 'DialogFooter';
 
const DialogTitle = forwardRef<
  ComponentRef<typeof Title>,
  ComponentPropsWithoutRef<typeof Title>
>(({ className, ...props }, ref) => {
  const titleStyles = [
    // Typography
    'text-lg font-semibold',
    // Line height & spacing
    'leading-none tracking-tight',
  ];
 
  return (
    <Title
      ref={ref}
      className={cn(titleStyles.join(' '), className)}
      {...props}
    />
  );
});
DialogTitle.displayName = Title.displayName;
 
const DialogDescription = forwardRef<
  ComponentRef<typeof Description>,
  ComponentPropsWithoutRef<typeof Description>
>(({ className, ...props }, ref) => {
  const descriptionStyles = [
    // Typography
    'text-sm text-muted-foreground',
  ];
 
  return (
    <Description
      ref={ref}
      className={cn(descriptionStyles.join(' '), className)}
      {...props}
    />
  );
});
DialogDescription.displayName = Description.displayName;
 
export {
  Dialog,
  DialogPortal,
  DialogOverlay,
  DialogClose,
  DialogTrigger,
  DialogContent,
  DialogHeader,
  DialogFooter,
  DialogTitle,
  DialogDescription,
};