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

100% Statements 140/140
83.33% Branches 10/12
100% Functions 0/0
100% Lines 140/140

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 1411x 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 1x 1x 1x 1x 1x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 3449x 54x 54x 54x 54x 54x 3449x 3395x 3395x 3395x 3395x 3395x 3449x 3449x 3449x 3449x 1x 1x 1x 1x 1x  
import React from 'react';
import { cn } from '@/lib/core';
import { ButtonProps } from '@/types/ui';
import { Icon } from '@/components/ui/icons';
 
/**
 * Button Component
 *
 * A flexible, accessible button component with multiple variants and sizes.
 * Built with design system tokens for consistency.
 *
 * Supports both legacy props (loading, disabled) and standard props
 * (isLoading, isDisabled) for backward compatibility.
 *
 * @example
 * ```tsx
 * // Standard props (recommended)
 * <Button variant="primary" size="md" isLoading>
 *   Processing...
 * </Button>
 *
 * <Button variant="outline" leftIcon={<Icon name="plus" />} isDisabled>
 *   Add Item
 * </Button>
 *
 * // Legacy props (still supported)
 * <Button variant="danger" loading>
 *   Processing...
 * </Button>
 * ```
 */
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  (
    {
      variant = 'primary',
      size = 'md',
      fullWidth,
      // Standard props
      isLoading,
      isDisabled,
      // Legacy props
      loading,
      disabled,
      // Common props
      leftIcon,
      rightIcon,
      className,
      children,
      ...props
    },
    ref
  ) => {
    // Resolve loading state (prefer standard prop)
    const resolvedLoading = isLoading ?? loading;
 
    // Resolve disabled state (prefer standard prop)
    const resolvedDisabled = isDisabled ?? disabled;
 
    const baseStyles = `
      inline-flex items-center justify-center gap-2
      font-medium rounded-lg
      transition-all duration-200
      focus:outline-none focus:ring-2 focus:ring-offset-2
      disabled:opacity-50 disabled:cursor-not-allowed
    `;
 
    const variantStyles = {
      primary: `
        bg-blue text-white
        hover:bg-blue-dark hover:text-white active:bg-blue-darker
        focus:ring-blue/50
      `,
      secondary: `
        bg-gray-600 text-white
        hover:bg-gray-700 active:bg-gray-800
        focus:ring-gray-500/50
      `,
      outline: `
        border-2 border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-200
        hover:bg-gray-50 dark:hover:bg-gray-700 hover:border-gray-400
        active:bg-gray-100 dark:active:bg-gray-600
        focus:ring-gray-500/50
      `,
      ghost: `
        bg-transparent text-gray-700 dark:text-gray-200
        hover:bg-gray-100 dark:hover:bg-gray-700 active:bg-gray-200 dark:active:bg-gray-600
        focus:ring-gray-500/50
      `,
      danger: `
        bg-red text-white
        hover:bg-red-600 active:bg-red-700
        focus:ring-red/50
      `};
 
    const sizeStyles = {
      sm: 'px-4 py-2.5 text-sm min-h-[44px]',   // WCAG 2.5 minimum touch target
      md: 'px-5 py-3 text-base min-h-[44px]',   // WCAG 2.5 minimum touch target
      lg: 'px-6 py-3.5 text-lg min-h-[48px]'    // Comfortable touch target
    };
 
    const widthStyles = fullWidth ? 'w-full' : '';
 
    const isButtonDisabled = resolvedDisabled || resolvedLoading;
 
    return (
      <button
        ref={ref}
        className={cn(
          baseStyles,
          variantStyles[variant],
          sizeStyles[size],
          widthStyles,
          className
        )}
        disabled={isButtonDisabled}
        aria-disabled={isButtonDisabled || undefined}
        aria-busy={resolvedLoading || undefined}
        {...props}
      >
        {resolvedLoading ? (
          <>
            <Icon name="spinner" size={16} className="animate-spin" aria-hidden="true" />
            <span className="sr-only">Loading</span>
            <span aria-hidden="true">Loading...</span>
          </>
        ) : (
          <>
            {leftIcon && <span className="inline-flex">{leftIcon}</span>}
            {children}
            {rightIcon && <span className="inline-flex">{rightIcon}</span>}
          </>
        )}
      </button>
    );
  }
);
 
Button.displayName = 'Button';
 
export default Button;