All files / src/components action-menu.tsx

96.15% Statements 25/26
80% Branches 8/10
87.5% Functions 7/8
96.15% Lines 25/26

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                                  5x       45x                       5x   5x       45x                 5x   5x   5x       5x                                         5x       45x                   5x   5x       44x                 5x   5x   5x       45x                     5x   5x                             5x                                   5x   36x             72x                                                                                         5x                                      
import { memo, forwardRef } from 'react';
import type { ComponentRef, ComponentPropsWithoutRef } from 'react';
import {
  Root as NavigationMenuRoot,
  List as NavigationMenuList,
  Item as NavigationMenuItem,
  Trigger as NavigationMenuTrigger,
  Content as NavigationMenuContent,
  Link as NavigationMenuLink,
  Viewport as NavigationMenuViewport,
  Indicator as NavigationMenuIndicator,
} from '@radix-ui/react-navigation-menu';
import { cva } from 'class-variance-authority';
import { MoreHorizontal } from 'lucide-react';
 
import { cn } from '@/lib/utils';
 
const ActionMenu = forwardRef<
  ComponentRef<typeof NavigationMenuRoot>,
  ComponentPropsWithoutRef<typeof NavigationMenuRoot>
>(({ className, children, ...props }, ref) => (
  <NavigationMenuRoot
    ref={ref}
    className={cn(
      'relative flex max-w-max flex-1 items-center justify-center',
      className
    )}
    {...props}
  >
    {children}
    <ActionMenuViewport />
  </NavigationMenuRoot>
));
ActionMenu.displayName = NavigationMenuRoot.displayName;
 
const ActionMenuList = forwardRef<
  ComponentRef<typeof NavigationMenuList>,
  ComponentPropsWithoutRef<typeof NavigationMenuList>
>(({ className, ...props }, ref) => (
  <NavigationMenuList
    ref={ref}
    className={cn(
      'group flex flex-1 list-none items-center justify-center space-x-1',
      className
    )}
    {...props}
  />
));
ActionMenuList.displayName = NavigationMenuList.displayName;
 
const ActionMenuItem = NavigationMenuItem;
 
const actionMenuTriggerStyle = cva(
  'group inline-flex items-center justify-center text-sm font-medium transition-all duration-200 disabled:pointer-events-none disabled:opacity-50'
);
 
const actionMenuItemStyle = cva(
  'block leading-none no-underline outline-none transition-all duration-200 hover:bg-gradient-vertical hover:text-white focus:bg-gradient-vertical focus:text-white',
  {
    variants: {
      size: {
        default: 'h-[27px] pl-2 pr-4 py-1.5 text-xs',
        compact: 'h-[27px] pl-2 pr-4 px-2 py-1.5 text-xs',
      },
      variant: {
        default: '',
        destructive:
          'text-destructive hover:bg-destructive hover:text-destructive-foreground focus:bg-destructive focus:text-destructive-foreground',
      },
    },
    defaultVariants: {
      size: 'default',
      variant: 'default',
    },
  }
);
 
const ActionMenuTrigger = forwardRef<
  ComponentRef<typeof NavigationMenuTrigger>,
  ComponentPropsWithoutRef<typeof NavigationMenuTrigger>
>(({ className, children, ...props }, ref) => (
  <NavigationMenuTrigger
    ref={ref}
    className={cn(actionMenuTriggerStyle(), 'group', className)}
    aria-label="Open menu"
    {...props}
  >
    <MoreHorizontal className="h-4 w-4" aria-hidden="true" />
    <span className="sr-only">More options</span>
  </NavigationMenuTrigger>
));
ActionMenuTrigger.displayName = NavigationMenuTrigger.displayName;
 
const ActionMenuContent = forwardRef<
  ComponentRef<typeof NavigationMenuContent>,
  ComponentPropsWithoutRef<typeof NavigationMenuContent>
>(({ className, ...props }, ref) => (
  <NavigationMenuContent
    ref={ref}
    className={cn(
      'left-0 top-0 w-full data-[motion^=from-]:animate-in data-[motion^=to-]:animate-out data-[motion^=from-]:fade-in data-[motion^=to-]:fade-out data-[motion=from-end]:slide-in-from-right-52 data-[motion=from-start]:slide-in-from-left-52 data-[motion=to-end]:slide-out-to-right-52 data-[motion=to-start]:slide-out-to-left-52 md:absolute md:w-auto',
      className
    )}
    {...props}
  />
));
ActionMenuContent.displayName = NavigationMenuContent.displayName;
 
const ActionMenuLink = NavigationMenuLink;
 
const ActionMenuViewport = forwardRef<
  ComponentRef<typeof NavigationMenuViewport>,
  ComponentPropsWithoutRef<typeof NavigationMenuViewport>
>(({ className, ...props }, ref) => (
  <div className={cn('absolute left-0 top-full flex justify-center')}>
    <NavigationMenuViewport
      className={cn(
        'origin-top-center relative mt-1.5 h-[var(--radix-navigation-menu-viewport-height)] w-full overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-90 md:w-[var(--radix-navigation-menu-viewport-width)]',
        className
      )}
      ref={ref}
      {...props}
    />
  </div>
));
ActionMenuViewport.displayName = NavigationMenuViewport.displayName;
 
const ActionMenuIndicator = forwardRef<
  ComponentRef<typeof NavigationMenuIndicator>,
  ComponentPropsWithoutRef<typeof NavigationMenuIndicator>
>(({ className, ...props }, ref) => (
  <NavigationMenuIndicator
    ref={ref}
    className={cn(
      'top-full z-[1] flex h-1.5 items-end justify-center overflow-hidden data-[state=visible]:animate-in data-[state=hidden]:animate-out data-[state=hidden]:fade-out data-[state=visible]:fade-in',
      className
    )}
    {...props}
  >
    <div className="relative top-[60%] h-2 w-2 rotate-45 rounded-tl-sm bg-border shadow-md" />
  </NavigationMenuIndicator>
));
ActionMenuIndicator.displayName = NavigationMenuIndicator.displayName;
 
// Reusable menu item type with better typing
interface MenuItemConfig {
  label: string;
  href?: string;
  onClick?: () => void;
  variant?: 'default' | 'destructive';
  size?: 'default' | 'compact';
  disabled?: boolean;
}
 
// Reusable ActionMenuComponent with memoization for better performance
interface ActionMenuWrapperProps {
  items: MenuItemConfig[];
  className?: string;
}
 
const ActionMenuWrapper = memo<ActionMenuWrapperProps>(
  ({ items, className }) => (
    <ActionMenu className={className}>
      <ActionMenuList>
        <ActionMenuItem>
          <ActionMenuTrigger />
          <ActionMenuContent>
            <ul className="grid w-max">
              {items.map((item, index) => (
                <li key={item.label || index}>
                  <ActionMenuLink asChild>
                    {item.onClick ? (
                      <button
                        type="button"
                        onClick={item.onClick}
                        disabled={item.disabled}
                        className={cn(
                          actionMenuItemStyle({
                            variant: item.variant,
                            size: item.size,
                          }),
                          'w-full text-left',
                          item.disabled && 'opacity-50 cursor-not-allowed'
                        )}
                      >
                        {item.label}
                      </button>
                    ) : (
                      <a
                        href={item.href || '#'}
                        className={cn(
                          actionMenuItemStyle({
                            variant: item.variant,
                            size: item.size,
                          }),
                          item.disabled &&
                            'opacity-50 cursor-not-allowed pointer-events-none'
                        )}
                        aria-disabled={item.disabled}
                      >
                        {item.label}
                      </a>
                    )}
                  </ActionMenuLink>
                </li>
              ))}
            </ul>
          </ActionMenuContent>
        </ActionMenuItem>
      </ActionMenuList>
    </ActionMenu>
  )
);
 
ActionMenuWrapper.displayName = 'ActionMenuWrapper';
 
// Export styles as individual functions for better tree shaking
export { actionMenuTriggerStyle };
export { actionMenuItemStyle };
 
// Export components individually for better tree shaking
export { ActionMenu };
export { ActionMenuList };
export { ActionMenuItem };
export { ActionMenuContent };
export { ActionMenuTrigger };
export { ActionMenuLink };
export { ActionMenuIndicator };
export { ActionMenuViewport };
export { ActionMenuWrapper };
 
// Export types
export type { MenuItemConfig };