# Code coverage report for src/components/ui/dropdown-menu.tsx

# All files / src/components/ui dropdown-menu.tsx

60.57% Statements 169/279
88.88% Branches 16/18
42.1% Functions 8/19
60.57% Lines 169/279

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
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    2801x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    1x
    2x
    2x
    2x
    2x
    1x
    2x
    2x
    2x
    1x
    1x
    1x
    16x
    16x
    16x
    16x
    1x
    1x
    16x
    16x
    16x
    16x
    16x
    16x
    16x

    1x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    16x
    16x
    16x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    1x

    1x

    1x

    1x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    1x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    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 React, { ComponentProps, createContext, useContext, useState, useRef, useEffect } from "react"
    import { createPortal } from "react-dom"
    import { cn } from "@/lib/utils"

    interface DropdownMenuContextValue {
      open: boolean
      setOpen: (open: boolean) => void
    }

    const DropdownMenuContext = createContext<DropdownMenuContextValue | null>(null)

    function DropdownMenu({ children }: { children: React.ReactNode }) {
      const [open, setOpen] = useState(false)
      return (
        <DropdownMenuContext.Provider value={{ open, setOpen }}>
          {children}
        </DropdownMenuContext.Provider>
      )
    }

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

      useEffect(() => {
        setMounted(true)
      }, [])

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

    function DropdownMenuTrigger({ children, asChild, className, ...props }: ComponentProps<"button"> & { asChild?: boolean }) {
      const context = useContext(DropdownMenuContext)

      const handleClick = () => {
        context?.setOpen(!context.open)
      }

      if (asChild && React.isValidElement(children)) {
        return React.cloneElement(children as React.ReactElement, {
          onClick: handleClick,
          ...props,
        })
      }

      return (
        <button type="button" onClick={handleClick} className={className} {...props}>
          {children}
        </button>
      )
    }

    function DropdownMenuContent({
      className,
      children,
      ...props
    }: ComponentProps<"div"> & { align?: "start" | "center" | "end"; sideOffset?: number }) {
      const context = useContext(DropdownMenuContext)
      const contentRef = useRef<HTMLDivElement>(null)

      useEffect(() => {
        if (!context?.open) return

        const handleClickOutside = (e: MouseEvent) => {
          if (contentRef.current && !contentRef.current.contains(e.target as Node)) {
            context.setOpen(false)
          }
        }

        const handleEscape = (e: KeyboardEvent) => {
          if (e.key === "Escape") {
            context.setOpen(false)
          }
        }

        document.addEventListener("mousedown", handleClickOutside)
        document.addEventListener("keydown", handleEscape)

        return () => {
          document.removeEventListener("mousedown", handleClickOutside)
          document.removeEventListener("keydown", handleEscape)
        }
      }, [context])

      if (!context?.open) return null

      return (
        <DropdownMenuPortal>
          <div className="cdk-overlay-container">
            <div className="cdk-overlay-pane">
              <div
                ref={contentRef}
                className={cn("mat-mdc-menu-panel mat-menu-panel-animations-enabled", className)}
                role="menu"
                {...props}
              >
                <div className="mat-mdc-menu-content">
                  {children}
                </div>
              </div>
            </div>
          </div>
        </DropdownMenuPortal>
      )
    }

    function DropdownMenuGroup({ children }: { children: React.ReactNode }) {
      return <div role="group">{children}</div>
    }

    function DropdownMenuItem({
      className,
      variant = "default",
      onClick,
      children,
      ...props
    }: ComponentProps<"button"> & {
      inset?: boolean
      variant?: "default" | "destructive"
    }) {
      const context = useContext(DropdownMenuContext)

      const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
        onClick?.(e)
        context?.setOpen(false)
      }

      return (
        <button
          type="button"
          role="menuitem"
          className={cn(
            "mat-mdc-menu-item mdc-list-item",
            variant === "destructive" && "mat-warn",
            className
          )}
          onClick={handleClick}
          {...props}
        >
          <span className="mdc-list-item__primary-text">{children}</span>
          <div className="mat-mdc-menu-ripple mat-ripple" />
        </button>
      )
    }

    function DropdownMenuCheckboxItem({
      className,
      children,
      checked,
      ...props
    }: ComponentProps<"button"> & { checked?: boolean }) {
      return (
        <button
          type="button"
          role="menuitemcheckbox"
          aria-checked={checked}
          className={cn("mat-mdc-menu-item mdc-list-item", className)}
          {...props}
        >
          <span className="mdc-list-item__primary-text">{children}</span>
          {checked && (
            <span className="mdc-list-item__end">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z" />
              </svg>
            </span>
          )}
        </button>
      )
    }

    function DropdownMenuRadioGroup({ children }: { children: React.ReactNode }) {
      return <div role="radiogroup">{children}</div>
    }

    function DropdownMenuRadioItem({
      className,
      children,
      ...props
    }: ComponentProps<"button">) {
      return (
        <button
          type="button"
          role="menuitemradio"
          className={cn("mat-mdc-menu-item mdc-list-item", className)}
          {...props}
        >
          <span className="mdc-list-item__primary-text">{children}</span>
        </button>
      )
    }

    function DropdownMenuLabel({ className, ...props }: ComponentProps<"div"> & { inset?: boolean }) {
      return (
        <div
          className={cn("mat-mdc-optgroup-label", className)}
          {...props}
        />
      )
    }

    function DropdownMenuSeparator({ className, ...props }: ComponentProps<"hr">) {
      return (
        <hr
          className={cn("mat-divider", className)}
          role="separator"
          {...props}
        />
      )
    }

    function DropdownMenuShortcut({ className, ...props }: ComponentProps<"span">) {
      return (
        <span
          className={className}
          style={{ marginLeft: "auto", fontSize: "0.75rem", opacity: 0.6 }}
          {...props}
        />
      )
    }

    function DropdownMenuSub({ children }: { children: React.ReactNode }) {
      return <>{children}</>
    }

    function DropdownMenuSubTrigger({
      className,
      children,
      ...props
    }: ComponentProps<"button"> & { inset?: boolean }) {
      return (
        <button
          type="button"
          className={cn("mat-mdc-menu-item mdc-list-item", className)}
          {...props}
        >
          <span className="mdc-list-item__primary-text">{children}</span>
          <span className="mat-mdc-menu-submenu-icon">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
              <path d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z" />
            </svg>
          </span>
        </button>
      )
    }

    function DropdownMenuSubContent({ className, children, ...props }: ComponentProps<"div">) {
      return (
        <div
          className={cn("mat-mdc-menu-panel", className)}
          role="menu"
          {...props}
        >
          <div className="mat-mdc-menu-content">
            {children}
          </div>
        </div>
      )
    }

    export {
      DropdownMenu,
      DropdownMenuPortal,
      DropdownMenuTrigger,
      DropdownMenuContent,
      DropdownMenuGroup,
      DropdownMenuLabel,
      DropdownMenuItem,
      DropdownMenuCheckboxItem,
      DropdownMenuRadioGroup,
      DropdownMenuRadioItem,
      DropdownMenuSeparator,
      DropdownMenuShortcut,
      DropdownMenuSub,
      DropdownMenuSubTrigger,
      DropdownMenuSubContent,
    }