All files / components/shared/base BaseButton.tsx

95.12% Statements 39/41
86.84% Branches 33/38
92.3% Functions 12/13
97.43% Lines 38/39

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                                                                                                                          13x                           41x 41x     41x               41x 37x     41x 37x   37x       37x 37x       41x 37x       41x 37x     41x 2x 2x       41x 1x 1x       41x 1x 1x     41x 1x 1x       41x 1x       41x 41x   41x 1x 40x 1x     41x                                                                       41x                                                                           13x  
/**
 * BaseButton - Enhanced button component with Korean theming
 * 
 * Builds on existing KoreanButton with extracted common logic
 * Provides consistent button styling across the application
 * 
 * Now with Html overlay positioning helpers for:
 * - Consistent z-index management
 * - Performance optimization with distanceFactor
 * - GPU acceleration
 * 
 * @module components/base
 */
 
import { Html } from "@react-three/drei";
import React, { useCallback, useMemo, useState, useEffect } from "react";
import { KOREAN_COLORS, UI_DIMENSIONS } from "../../../types/constants";
import { hexToRgbaString } from "../../../utils/colorUtils";
import { applyHtmlOverlayStyles, calculateDistanceFactor } from "../../../utils/htmlOverlayHelpers";
import type { HtmlOverlayLayer } from "../../../types/HtmlOverlayTypes";
import { useKoreanTheme } from "./useKoreanTheme";
 
/**
 * Props for BaseButton component
 */
export interface BaseButtonProps {
  readonly korean: string;
  readonly english: string;
  readonly onClick: () => void;
  readonly disabled?: boolean;
  readonly variant?: "primary" | "secondary" | "danger";
  readonly size?: "sm" | "md" | "lg";
  readonly position?: [number, number, number];
  readonly fullWidth?: boolean;
  readonly testId?: string;
  readonly isMobile?: boolean;
  /** Html overlay layer for z-index (default: 'hud') */
  readonly layer?: HtmlOverlayLayer;
  /** Whether button should occlude behind 3D objects (default: false) */
  readonly occlude?: boolean;
}
 
/**
 * BaseButton Component
 * 
 * Enhanced Korean-themed button with common functionality extracted.
 * Uses useKoreanTheme hook for consistent styling.
 * Now includes Html overlay positioning helpers for proper z-index and performance.
 * 
 * @example
 * ```tsx
 * <BaseButton
 *   korean="공격"
 *   english="Attack"
 *   onClick={() => console.log("Attack")}
 *   variant="primary"
 *   size="md"
 *   layer="hud"
 * />
 * ```
 */
export const BaseButton: React.FC<BaseButtonProps> = ({
  korean,
  english,
  onClick,
  disabled = false,
  variant = "primary",
  size = "md",
  position = [0, 0, 0],
  fullWidth = false,
  testId,
  isMobile = false,
  layer = "hud",
  occlude = false,
}) => {
  const [isHovered, setIsHovered] = useState(false);
  const [isPressed, setIsPressed] = useState(false);
 
  // Use Korean theme hook for consistent styling
  const { buttonVariant, buttonSize, fontFamily } = useKoreanTheme({
    variant,
    size,
    disabled,
    isMobile,
  });
 
  // Track screen width for responsive distance factor updates on resize
  const [screenWidth, setScreenWidth] = useState(() => 
    typeof window !== "undefined" ? window.innerWidth : UI_DIMENSIONS.DEFAULT_SCREEN_WIDTH
  );
 
  useEffect(() => {
    Iif (typeof window === "undefined") return;
 
    const handleResize = () => {
      setScreenWidth(window.innerWidth);
    };
 
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, []);
 
  // Calculate optimal distance factor for button
  const distanceFactor = useMemo(() => {
    return calculateDistanceFactor(screenWidth, "button", isMobile);
  }, [screenWidth, isMobile]);
 
  // Apply Html overlay styles with proper z-index
  const overlayStyle = useMemo(() => {
    return applyHtmlOverlayStyles(layer, true, distanceFactor, true, occlude);
  }, [layer, distanceFactor, occlude]);
 
  const handleClick = useCallback(() => {
    Eif (!disabled) {
      onClick();
    }
  }, [onClick, disabled]);
 
  const handleMouseEnter = useCallback(() => {
    Eif (!disabled) {
      setIsHovered(true);
    }
  }, [disabled]);
 
  const handleMouseLeave = useCallback(() => {
    setIsHovered(false);
    setIsPressed(false);
  }, []);
 
  const handleMouseDown = useCallback(() => {
    Eif (!disabled) {
      setIsPressed(true);
    }
  }, [disabled]);
 
  const handleMouseUp = useCallback(() => {
    setIsPressed(false);
  }, []);
 
  // Memoize button styles for performance
  const buttonStyle = useMemo<React.CSSProperties>(() => {
    let background = hexToRgbaString(buttonVariant.background, 0.9);
    
    if (isPressed) {
      background = buttonVariant.activeBg;
    } else if (isHovered) {
      background = buttonVariant.hoverBg;
    }
 
    return {
      background,
      border: `${buttonSize.borderWidth} solid ${hexToRgbaString(buttonVariant.border)}`,
      color: hexToRgbaString(buttonVariant.text),
      padding: buttonSize.padding,
      fontSize: buttonSize.fontSize,
      fontFamily: fontFamily.KOREAN,
      fontWeight: "bold",
      cursor: disabled ? "not-allowed" : "pointer",
      opacity: disabled ? 0.5 : 1,
      borderRadius: "4px",
      transition: "all 0.2s ease",
      textAlign: "center",
      userSelect: "none",
      WebkitUserSelect: "none",
      width: fullWidth ? "100%" : "auto",
      boxShadow: isHovered && !disabled
        ? `0 0 10px ${hexToRgbaString(buttonVariant.border, 0.5)}`
        : "none",
      transform: isPressed && !disabled ? "scale(0.98)" : "scale(1)",
      textShadow: `0 2px 4px ${hexToRgbaString(KOREAN_COLORS.BLACK_SOLID, 0.5)}`,
      // Apply GPU acceleration from overlay style
      WebkitTransform: overlayStyle.transform,
      zIndex: overlayStyle.zIndex,
    };
  }, [
    buttonVariant,
    buttonSize,
    fontFamily,
    disabled,
    fullWidth,
    isHovered,
    isPressed,
    overlayStyle,
  ]);
 
  return (
    <Html 
      position={position} 
      center={overlayStyle.center}
      distanceFactor={overlayStyle.distanceFactor}
      occlude={overlayStyle.occlude}
      style={{ pointerEvents: overlayStyle.pointerEvents }}
    >
      <button
        onClick={handleClick}
        onMouseEnter={handleMouseEnter}
        onMouseLeave={handleMouseLeave}
        onMouseDown={handleMouseDown}
        onMouseUp={handleMouseUp}
        disabled={disabled}
        style={buttonStyle}
        data-testid={testId ?? "base-button"}
      >
        <div style={{ 
          display: "flex", 
          flexDirection: "column", 
          alignItems: "center",
          gap: "2px"
        }}>
          <span style={{ fontSize: "1em" }}>{korean}</span>
          <span style={{ 
            fontSize: "0.75em", 
            opacity: 0.8,
            fontStyle: "italic"
          }}>
            {english}
          </span>
        </div>
      </button>
    </Html>
  );
};
 
BaseButton.displayName = "BaseButton";