All files / components/three KoreanButton.tsx

72.97% Statements 27/37
69.23% Branches 27/39
55.55% Functions 5/9
72.97% Lines 27/37

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                                                                                            2x                     19x 19x     19x 19x   17x               1x               1x                                     19x 19x   1x           1x             17x               19x 1x 1x       19x           19x         19x           19x         19x 19x   19x   19x       19x                                                             19x                                                               2x  
/**
 * KoreanButton - Three.js-compatible button component with Korean theming
 * 
 * Provides bilingual button with cyberpunk Korean aesthetic
 * Supports both HTML overlay and 3D mesh rendering
 * 
 * @module components/three
 */
 
import { Html } from "@react-three/drei";
import React, { useCallback, useMemo, useState } from "react";
import { KOREAN_COLORS, FONT_FAMILY } from "../../types/constants";
import { hexToRgbaString } from "../../utils/colorUtils";
 
/**
 * Props for KoreanButton component
 */
export interface KoreanButtonProps {
  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;
}
 
/**
 * KoreanButton Component
 * 
 * A bilingual button component with Korean cyberpunk theming.
 * Uses @react-three/drei Html overlay for proper DOM interaction.
 * 
 * @example
 * ```tsx
 * <KoreanButton
 *   korean="공격"
 *   english="Attack"
 *   onClick={() => console.log("Attack clicked")}
 *   variant="primary"
 *   size="md"
 * />
 * ```
 */
export const KoreanButton: React.FC<KoreanButtonProps> = ({
  korean,
  english,
  onClick,
  disabled = false,
  variant = "primary",
  size = "md",
  position = [0, 0, 0],
  fullWidth = false,
  testId,
}) => {
  const [isHovered, setIsHovered] = useState(false);
  const [isPressed, setIsPressed] = useState(false);
 
  // Memoize variant colors for performance
  const variantColors = useMemo(() => {
    switch (variant) {
      case "primary":
        return {
          background: KOREAN_COLORS.UI_BACKGROUND_DARK,
          border: KOREAN_COLORS.PRIMARY_CYAN,
          text: KOREAN_COLORS.ACCENT_GOLD,
          hoverBg: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.1),
          activeBg: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.2),
        };
      case "secondary":
        return {
          background: KOREAN_COLORS.UI_BACKGROUND_MEDIUM,
          border: KOREAN_COLORS.ACCENT_GOLD,
          text: KOREAN_COLORS.TEXT_PRIMARY,
          hoverBg: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.1),
          activeBg: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.2),
        };
      case "danger":
        return {
          background: KOREAN_COLORS.UI_BACKGROUND_DARK,
          border: KOREAN_COLORS.ACCENT_RED,
          text: KOREAN_COLORS.ACCENT_RED,
          hoverBg: hexToRgbaString(KOREAN_COLORS.ACCENT_RED, 0.1),
          activeBg: hexToRgbaString(KOREAN_COLORS.ACCENT_RED, 0.2),
        };
      default:
        return {
          background: KOREAN_COLORS.UI_BACKGROUND_DARK,
          border: KOREAN_COLORS.PRIMARY_CYAN,
          text: KOREAN_COLORS.ACCENT_GOLD,
          hoverBg: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.1),
          activeBg: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.2),
        };
    }
  }, [variant]);
 
  // Memoize size dimensions for performance
  const sizeDimensions = useMemo(() => {
    switch (size) {
      case "sm":
        return {
          padding: "8px 16px",
          fontSize: "14px",
          borderWidth: "1px",
        };
      case "lg":
        return {
          padding: "16px 32px",
          fontSize: "20px",
          borderWidth: "3px",
        };
      case "md":
      default:
        return {
          padding: "12px 24px",
          fontSize: "16px",
          borderWidth: "2px",
        };
    }
  }, [size]);
 
  const handleClick = useCallback(() => {
    Eif (!disabled) {
      onClick();
    }
  }, [onClick, disabled]);
 
  const handleMouseEnter = useCallback(() => {
    if (!disabled) {
      setIsHovered(true);
    }
  }, [disabled]);
 
  const handleMouseLeave = useCallback(() => {
    setIsHovered(false);
    setIsPressed(false);
  }, []);
 
  const handleMouseDown = useCallback(() => {
    if (!disabled) {
      setIsPressed(true);
    }
  }, [disabled]);
 
  const handleMouseUp = useCallback(() => {
    setIsPressed(false);
  }, []);
 
  // Memoize button styles for performance
  const buttonStyle = useMemo<React.CSSProperties>(() => {
    let background = hexToRgbaString(variantColors.background, 0.9);
    
    Iif (isPressed) {
      background = variantColors.activeBg;
    I} else if (isHovered) {
      background = variantColors.hoverBg;
    }
 
    return {
      background,
      border: `${sizeDimensions.borderWidth} solid ${hexToRgbaString(variantColors.border)}`,
      color: hexToRgbaString(variantColors.text),
      padding: sizeDimensions.padding,
      fontSize: sizeDimensions.fontSize,
      fontFamily: FONT_FAMILY.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(variantColors.border, 0.5)}`
        : "none",
      transform: isPressed && !disabled ? "scale(0.98)" : "scale(1)",
      textShadow: `0 2px 4px ${hexToRgbaString(KOREAN_COLORS.BLACK_SOLID, 0.5)}`,
    };
  }, [
    variantColors,
    sizeDimensions,
    disabled,
    fullWidth,
    isHovered,
    isPressed,
  ]);
 
  return (
    <Html position={position} center>
      <button
        onClick={handleClick}
        onMouseEnter={handleMouseEnter}
        onMouseLeave={handleMouseLeave}
        onMouseDown={handleMouseDown}
        onMouseUp={handleMouseUp}
        disabled={disabled}
        style={buttonStyle}
        data-testid={testId ?? "korean-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>
  );
};
 
KoreanButton.displayName = "KoreanButton";