All files / src/components/ui/Dropdown index.tsx

100% Statements 165/165
93.54% Branches 29/31
100% Functions 7/7
100% Lines 165/165

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 1661x 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 1x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 1x 1x 1x 44x 44x 44x 13x 13x 44x 44x 44x 44x 44x 44x 44x 44x 44x 1x 1x 1x 44x 44x 44x 13x 13x 44x 44x 44x 44x 44x 44x 44x 2x 2x 2x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 4x 4x 4x 44x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 44x 44x 44x 44x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 13x 13x 44x 44x 44x 44x 1x 1x  
'use client';
 
import React, { useState, useRef, useEffect } from 'react';
import { cn } from '@/lib/core';
import { Button } from '@/components/ui/Button';
import { DropdownProps, DropdownItem } from '@/types/ui';
import { Icon } from '@/components/ui/icons';
 
/**
 * Dropdown Component
 *
 * An accessible dropdown menu with keyboard navigation.
 *
 * @example
 * ```tsx
 * const items = [
 *   { label: 'Edit', value: 'edit', icon: <Icon name="edit" /> },
 *   { label: 'Delete', value: 'delete', icon: <Icon name="trash" /> },
 * ];
 *
 * <Dropdown
 *   items={items}
 *   onChange={(value) => console.log(value)}
 *   placeholder="Select action"
 * />
 * ```
 */
export const Dropdown: React.FC<DropdownProps> = ({
  items,
  value,
  onChange,
  placeholder = 'Select...',
  size = 'md',
  fullWidth,
  disabled,
  trigger}) => {
  const [isOpen, setIsOpen] = useState(false);
  const dropdownRef = useRef<HTMLDivElement>(null);
 
  const selectedItem = items.find((item) => item.value === value);
 
  // Close on outside click
  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsOpen(false);
      }
    };
 
    if (isOpen) {
      document.addEventListener('mousedown', handleClickOutside);
    }
 
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [isOpen]);
 
  // Handle escape key
  useEffect(() => {
    const handleEscape = (event: KeyboardEvent) => {
      if (event.key === 'Escape') {
        setIsOpen(false);
      }
    };
 
    if (isOpen) {
      document.addEventListener('keydown', handleEscape);
    }
 
    return () => {
      document.removeEventListener('keydown', handleEscape);
    };
  }, [isOpen]);
 
  const handleItemClick = (item: DropdownItem) => {
    if (item.disabled) return;
    onChange?.(item.value);
    setIsOpen(false);
  };
 
  const sizeStyles = {
    sm: 'px-3 py-1.5 text-sm',
    md: 'px-4 py-2 text-base',
    lg: 'px-5 py-3 text-lg'};
 
  return (
    <div
      ref={dropdownRef}
      className={cn('relative', fullWidth && 'w-full')}
    >
      {/* Trigger */}
      {trigger ? (
        <div onClick={() => !disabled && setIsOpen(!isOpen)}>
          {trigger}
        </div>
      ) : (
        <Button
          type="button"
          onClick={() => setIsOpen(!isOpen)}
          disabled={disabled}
          variant="ghost"
          className={cn(
            'flex items-center justify-between gap-2 w-full',
            'bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg',
            'hover:border-gray-400 dark:hover:border-gray-500 focus:outline-none focus:ring-2 focus:ring-blue/20',
            'transition-all duration-200',
            'disabled:opacity-50 disabled:cursor-not-allowed',
            sizeStyles[size]
          )}
          aria-haspopup="listbox"
          aria-expanded={isOpen}
        >
          <span className="flex items-center gap-2">
            {selectedItem?.icon}
            <span className={!selectedItem ? 'text-gray-400 dark:text-gray-500' : 'dark:text-gray-100'}>
              {selectedItem?.label || placeholder}
            </span>
          </span>
          <Icon
            name="chevron-down"
            size={16}
            className={cn('transition-transform', isOpen && 'rotate-180')}
          />
        </Button>
      )}
 
      {/* Dropdown Menu */}
      {isOpen && (
        <div
          className={cn(
            'absolute z-10 mt-1 w-full',
            'bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg',
            'max-h-60 overflow-auto',
            'animate-fadeIn'
          )}
          role="listbox"
        >
          {items.map((item) => (
            <Button
              key={item.value}
              type="button"
              onClick={() => handleItemClick(item)}
              disabled={item.disabled}
              variant="ghost"
              className={cn(
                'flex items-center gap-2 w-full px-4 py-2 text-left dark:text-gray-100',
                'hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors',
                'disabled:opacity-50 disabled:cursor-not-allowed',
                value === item.value && 'bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400'
              )}
              role="option"
              aria-selected={value === item.value}
            >
              {item.icon}
              <span>{item.label}</span>
            </Button>
          ))}
        </div>
      )}
    </div>
  );
};
 
export default Dropdown;