Banner.tsx 2.77 KB
import { useState } from 'react';
import type { BannerCapsuleTag } from '../types/home';

export interface BannerProps {
  title: string;
  subtitle: string;
  backgroundImage: string | null;
  backgroundBlurImage: string | null;
  capsuleTag: BannerCapsuleTag | null;
}

export function Banner({
  title,
  subtitle,
  backgroundImage,
  backgroundBlurImage,
  capsuleTag,
}: BannerProps) {
  const [isHovering, setIsHovering] = useState(false);
  const [imageError, setImageError] = useState(false);

  const shouldShowBlur = isHovering && backgroundBlurImage;
  const effectiveBackground = imageError
    ? null
    : shouldShowBlur
      ? backgroundBlurImage
      : backgroundImage;

  const handleMouseEnter = () => {
    setIsHovering(true);
  };

  const handleMouseLeave = () => {
    setIsHovering(false);
  };

  const handleImageError = () => {
    setImageError(true);
  };

  return (
    <section
      data-testid="banner-section"
      className="relative min-h-[500px] tablet:min-h-[582px] flex items-center justify-center overflow-hidden py-16 tablet:py-24 px-margin-mobile tablet:px-margin-desktop"
      onMouseEnter={handleMouseEnter}
      onMouseLeave={handleMouseLeave}
    >
      <div className="absolute inset-0 z-0">
        {effectiveBackground ? (
          <>
            <img
              src={effectiveBackground}
              alt="Banner 背景"
              className="w-full h-full object-cover"
              onError={handleImageError}
            />
            <div className="absolute inset-0 bg-white/40 backdrop-blur-[2px]" />
          </>
        ) : (
          <div className="absolute inset-0 bg-gradient-to-br from-primary/5 to-surface" />
        )}
      </div>

      <div className="relative z-10 text-center max-w-container-max mx-auto">
        {capsuleTag && (
          <div className="inline-flex items-center gap-2 px-4 py-1.5 rounded-full bg-primary/10 border border-primary/20 mb-6 tablet:mb-8">
            <svg className="w-[18px] h-[18px] text-primary" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12 2L14.09 8.26L20.18 8.63L15.54 12.74L17.27 18.82L12 15.27L6.73 18.82L8.46 12.74L3.82 8.63L9.91 8.26L12 2Z" />
            </svg>
            <span className="text-label-sm text-primary font-bold">{capsuleTag.text}</span>
          </div>
        )}

        <h1 className="text-display-lg-mobile tablet:text-display-lg font-bold mb-4 tablet:mb-6 leading-tight text-on-surface">
          {title.split(' ').map((word, index) => (
            index === 1 ? <span key={index} className="text-primary">{word} </span> : word + ' '
          ))}
        </h1>

        <p className="text-body-md tablet:text-body-lg text-on-surface-variant mb-8 tablet:mb-10 max-w-2xl mx-auto font-medium">
          {subtitle}
        </p>
      </div>
    </section>
  );
}