# All files / src/components/ui dialog.tsx

24.27% Statements 42/173
50% Branches 1/2
10% Functions 1/10
24.27% Lines 42/173

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
    1741x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    31x
    31x
    31x

    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
     "use client"

    import * as React from "react"
    import { ComponentProps, useEffect, useRef } from "react"
    import { createPortal } from "react-dom"
    import { cn } from "@/lib/utils"

    interface DialogProps {
      open?: boolean
      onOpenChange?: (open: boolean) => void
      children: React.ReactNode
    }

    function Dialog({ open, children }: DialogProps) {
      // If open is explicitly false, don't render
      if (open === false) return null
      return <>{children}</>
    }

    interface DialogTriggerProps extends ComponentProps<"button"> {
      children?: React.ReactNode
    }

    function DialogTrigger({ children, onClick, ...props }: DialogTriggerProps) {
      return (
        <button
          type="button"
          onClick={onClick}
          {...props}
        >
          {children}
        </button>
      )
    }

    function DialogPortal({ children }: { children: React.ReactNode }) {
      const [mounted, setMounted] = React.useState(false)

      React.useEffect(() => {
        setMounted(true)
        return () => setMounted(false)
      }, [])

      if (!mounted) return null
      return createPortal(children, document.body)
    }

    function DialogOverlay({ className, onClick, ...props }: ComponentProps<"div">) {
      return (
        <div
          className={cn("cdk-overlay-backdrop cdk-overlay-dark-backdrop cdk-overlay-backdrop-showing", className)}
          onClick={onClick}
          aria-hidden="true"
          data-testid="dialog-overlay"
          {...props}
        />
      )
    }

    function DialogContent({
      className,
      children,
      onClose,
      ...props
    }: ComponentProps<"div"> & { onClose?: () => void }) {
      const dialogRef = useRef<HTMLDivElement>(null)

      useEffect(() => {
        const handleEscape = (e: KeyboardEvent) => {
          if (e.key === "Escape" && onClose) {
            onClose()
          }
        }

        document.addEventListener("keydown", handleEscape)
        return () => document.removeEventListener("keydown", handleEscape)
      }, [onClose])

      return (
        <DialogPortal>
          <div className="cdk-overlay-container">
            <div className="cdk-global-overlay-wrapper">
              <div className="cdk-overlay-pane">
                <DialogOverlay onClick={onClose} />
                <div className="mat-mdc-dialog-container mdc-dialog mdc-dialog--open">
                  <div className="mat-mdc-dialog-inner-container">
                    <div
                      ref={dialogRef}
                      className={cn("mat-mdc-dialog-surface mdc-dialog__surface", className)}
                      role="dialog"
                      aria-modal="true"
                      {...props}
                    >
                      {children}
                      <button
                        type="button"
                        className="mat-mdc-icon-button mdc-icon-button"
                        onClick={onClose}
                        style={{ position: "absolute", right: "16px", top: "16px" }}
                        aria-label="Close dialog"
                        data-testid="dialog-close-btn"
                      >
                        <span className="mdc-icon-button__ripple" />
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                          <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" />
                        </svg>
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </DialogPortal>
      )
    }

    function DialogHeader({ className, ...props }: ComponentProps<"div">) {
      return (
        <div
          className={cn("mat-mdc-dialog-title", className)}
          {...props}
        />
      )
    }

    function DialogFooter({ className, ...props }: ComponentProps<"div">) {
      return (
        <div
          className={cn("mat-mdc-dialog-actions", className)}
          {...props}
        />
      )
    }

    function DialogTitle({ className, ...props }: ComponentProps<"h2">) {
      return (
        <h2
          className={cn("mat-mdc-dialog-title", className)}
          {...props}
        />
      )
    }

    function DialogDescription({ className, ...props }: ComponentProps<"p">) {
      return (
        <p
          className={cn("mat-mdc-dialog-content", className)}
          {...props}
        />
      )
    }

    function DialogClose({ children, onClick }: ComponentProps<"button">) {
      return (
        <button type="button" onClick={onClick}>
          {children}
        </button>
      )
    }

    export {
      Dialog,
      DialogPortal,
      DialogOverlay,
      DialogClose,
      DialogTrigger,
      DialogContent,
      DialogHeader,
      DialogFooter,
      DialogTitle,
      DialogDescription,
    }