Header.jsx 13.3 KB
Newer Older
fisherdaddy's avatar
fisherdaddy committed
1
// src/components/Header.jsx
fisherdaddy's avatar
fisherdaddy committed
2
import React, { useState, useEffect, useRef } from 'react';
fisherdaddy's avatar
fisherdaddy committed
3
import { NavLink, useNavigate } from 'react-router-dom';
fisherdaddy's avatar
fisherdaddy committed
4 5
import LanguageSelector from './LanguageSelector';
import { useTranslation } from '../js/i18n';
fisherdaddy's avatar
fisherdaddy committed
6
import logo from '/assets/logo.png';
fisherdaddy's avatar
fisherdaddy committed
7 8 9

function Header() {
  const { t } = useTranslation();
fisherdaddy's avatar
fisherdaddy committed
10 11
  const navigate = useNavigate();
  const user = JSON.parse(localStorage.getItem('user'));
fisherdaddy's avatar
fisherdaddy committed
12
  const [menuOpen, setMenuOpen] = useState(false);
13
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
fisherdaddy's avatar
fisherdaddy committed
14
  const menuRef = useRef(null);
fisherdaddy's avatar
fisherdaddy committed
15

fisherdaddy's avatar
fisherdaddy committed
16
  const toggleMenu = () => {
17
    setMenuOpen(!menuOpen);
fisherdaddy's avatar
fisherdaddy committed
18 19 20 21 22 23 24 25
  };

  useEffect(() => {
    const handleClickOutside = (event) => {
      if (menuRef.current && !menuRef.current.contains(event.target)) {
        setMenuOpen(false);
      }
    };
26

fisherdaddy's avatar
fisherdaddy committed
27 28 29 30
    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  }, [menuRef]);

  const handleLogout = () => {
    localStorage.removeItem('user');
    navigate('/login');
    setMobileMenuOpen(false);
  };

  const toggleMobileMenu = () => {
    setMobileMenuOpen(!mobileMenuOpen);
  };

  const handleNavClick = () => {
    setMobileMenuOpen(false);
  };
fisherdaddy's avatar
fisherdaddy committed
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
  // 添加键盘导航支持
  const handleKeyDown = (e) => {
    if (e.key === 'Escape') {
      setMenuOpen(false);
      setMobileMenuOpen(false);
    }
  };

  // 添加焦点管理
  useEffect(() => {
    const handleFocusTrap = (e) => {
      if (!mobileMenuOpen) return;
      
      const mobileMenu = document.querySelector('[role="dialog"]');
      const focusableElements = mobileMenu.querySelectorAll(
        'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
      );
      
      const firstElement = focusableElements[0];
      const lastElement = focusableElements[focusableElements.length - 1];

      if (e.key === 'Tab') {
        if (e.shiftKey && document.activeElement === firstElement) {
          e.preventDefault();
          lastElement.focus();
        } else if (!e.shiftKey && document.activeElement === lastElement) {
          e.preventDefault();
          firstElement.focus();
        }
      }
    };

    document.addEventListener('keydown', handleKeyDown);
    document.addEventListener('keydown', handleFocusTrap);
    
    return () => {
      document.removeEventListener('keydown', handleKeyDown);
      document.removeEventListener('keydown', handleFocusTrap);
    };
  }, [mobileMenuOpen]);

  // 处理滚动锁定
  useEffect(() => {
    if (mobileMenuOpen) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = '';
    }
    return () => {
      document.body.style.overflow = '';
    };
  }, [mobileMenuOpen]);

fisherdaddy's avatar
fisherdaddy committed
100
  return (
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
    <header className="fixed top-0 left-0 right-0 z-50">
      <div className="absolute inset-0 bg-white/70 backdrop-blur-lg border-b border-gray-100"></div>
      
      <nav 
        className="relative max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"
        role="navigation"
        aria-label={t('navigation.main')}
      >
        <div className="flex justify-between items-center h-20">
          <div className="flex items-center">
            <div className="flex-shrink-0 mr-8">
              <NavLink 
                to="/" 
                className="flex items-center space-x-3 group"
                aria-label={t('navigation.home')}
              >
                <img 
                  src={logo} 
                  alt={t('logo.alt')} 
                  className="w-10 h-10 object-contain transition transform group-hover:scale-105"
                  width="40"
                  height="40"
                  loading="eager"
                />
                <span className="text-xl font-semibold bg-gradient-to-r from-indigo-500/90 to-indigo-600/80 bg-clip-text text-transparent">
                  {t('title')}
                </span>
              </NavLink>
            </div>
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
            <div className="hidden md:flex items-center space-x-6">
              <NavLink 
                to="/dev-tools"
                className={({isActive}) => 
                  `px-3 py-2 text-base font-medium transition-all duration-200 border-b-2 ${
                    isActive 
                      ? 'text-indigo-500 border-indigo-500' 
                      : 'text-gray-600 border-transparent hover:text-indigo-500 hover:border-indigo-500'
                  }`
                }
              >
                {t('dev-tools.title')}
              </NavLink>
              <NavLink 
                to="/image-tools" 
                className={({isActive}) => 
                  `px-3 py-2 text-base font-medium transition-all duration-200 border-b-2 ${
                    isActive 
                      ? 'text-indigo-500 border-indigo-500' 
                      : 'text-gray-600 border-transparent hover:text-indigo-500 hover:border-indigo-500'
                  }`
                }
                onClick={handleNavClick}
              >
                {t('image-tools.title')}
              </NavLink>
              <NavLink 
                to="/ai-products" 
                className={({isActive}) => 
                  `px-3 py-2 text-base font-medium transition-all duration-200 border-b-2 ${
                    isActive 
                      ? 'text-indigo-500 border-indigo-500' 
                      : 'text-gray-600 border-transparent hover:text-indigo-500 hover:border-indigo-500'
                  }`
                }
                onClick={handleNavClick}
              >
                {t('ai-products.title')}
              </NavLink>
              <NavLink 
                to="/blog" 
                className={({isActive}) => 
                  `px-3 py-2 text-base font-medium transition-all duration-200 border-b-2 ${
                    isActive 
                      ? 'text-indigo-500 border-indigo-500' 
                      : 'text-gray-600 border-transparent hover:text-indigo-500 hover:border-indigo-500'
                  }`
                }
                onClick={handleNavClick}
              >
                {t('blog.title')}
              </NavLink>
            </div>
184
          </div>
185 186

          <div className="flex items-center space-x-4">
187
            <LanguageSelector />
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            {user ? (
              <div className="relative" ref={menuRef}>
                <button
                  onClick={toggleMenu}
                  className="flex items-center space-x-3 focus:outline-none"
                >
                  <img
                    src={user.picture}
                    alt="User Avatar"
                    className="w-8 h-8 sm:w-10 sm:h-10 rounded-full ring-2 ring-offset-2 ring-indigo-500 transition transform hover:scale-105"
                  />
                </button>
                
                {menuOpen && (
                  <div className="absolute right-0 mt-2 w-48 rounded-lg shadow-lg bg-white ring-1 ring-black ring-opacity-5 py-1 focus:outline-none">
                    <button
                      onClick={handleLogout}
                      className="w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-indigo-50 hover:text-indigo-600 transition-colors duration-150"
                    >
                      {t('logout')}
                    </button>
209
                  </div>
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                )}
              </div>
            ) : (
              <NavLink
                to="/login"
                className="hidden sm:inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 transition-colors duration-200 shadow-sm hover:shadow-md"
              >
                {t('login')}
              </NavLink>
            )}
          </div>

          <button 
            className="md:hidden p-2 rounded-md text-gray-600 hover:text-gray-900 hover:bg-gray-100 transition-colors duration-200"
            onClick={toggleMobileMenu}
            aria-expanded={mobileMenuOpen}
            aria-controls="mobile-menu"
            aria-label={mobileMenuOpen ? t('navigation.close') : t('navigation.menu')}
          >
            <span className="sr-only">
              {mobileMenuOpen ? t('navigation.close') : t('navigation.menu')}
            </span>
            <svg 
              className="w-6 h-6" 
              fill="none" 
              stroke="currentColor" 
              viewBox="0 0 24 24"
              aria-hidden="true"
            >
              {mobileMenuOpen ? (
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
241
              ) : (
242
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
243
              )}
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            </svg>
          </button>

          <div
            id="mobile-menu"
            role="dialog"
            aria-label={t('navigation.mobile_menu')}
            aria-modal="true"
            className={`md:hidden fixed inset-0 z-40 transform ${
              mobileMenuOpen ? 'translate-x-0' : 'translate-x-full'
            } transition-transform duration-300 ease-in-out`}
          >
            <div 
              className="fixed inset-0 bg-gray-600 bg-opacity-75 transition-opacity"
              aria-hidden="true"
              onClick={() => setMobileMenuOpen(false)}
            />
            <nav 
              className="relative flex flex-col h-full w-full max-w-sm ml-auto bg-white shadow-xl"
              role="navigation"
              aria-label={t('navigation.mobile')}
            >
              <div className="flex items-center justify-between p-4 border-b border-gray-200">
                <span className="text-lg font-semibold text-gray-800">{t('navigation.menu')}</span>
                <button
                  onClick={() => setMobileMenuOpen(false)}
                  className="p-2 rounded-md text-gray-600 hover:text-gray-900 hover:bg-gray-100"
                  aria-label={t('navigation.close')}
                >
                  <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                  </svg>
                </button>
              </div>

              <div className="flex-1 overflow-y-auto">
                <div className="px-2 pt-2 pb-3 space-y-1">
                  <NavLink 
                    to="/dev-tools"
                    className={({isActive}) => 
                      `block px-4 py-3 rounded-lg text-base font-medium transition-colors duration-200 ${
                        isActive 
                          ? 'bg-indigo-50 text-indigo-600' 
                          : 'text-gray-700 hover:bg-gray-50 hover:text-indigo-600'
                      }`
                    }
                    onClick={() => setMobileMenuOpen(false)}
                  >
                    {t('dev-tools.title')}
                  </NavLink>
                  <NavLink 
                    to="/image-tools" 
                    className={({isActive}) => 
                      `block px-4 py-3 rounded-lg text-base font-medium transition-colors duration-200 ${
                        isActive 
                          ? 'bg-indigo-50 text-indigo-600' 
                          : 'text-gray-700 hover:bg-gray-50 hover:text-indigo-600'
                      }`
                    }
                    onClick={() => setMobileMenuOpen(false)}
                  >
                    {t('image-tools.title')}
                  </NavLink>
                  <NavLink 
                    to="/ai-products" 
                    className={({isActive}) => 
                      `block px-4 py-3 rounded-lg text-base font-medium transition-colors duration-200 ${
                        isActive 
                          ? 'bg-indigo-50 text-indigo-600' 
                          : 'text-gray-700 hover:bg-gray-50 hover:text-indigo-600'
                      }`
                    }
                    onClick={() => setMobileMenuOpen(false)}
                  >
                    {t('ai-products.title')}
                  </NavLink>
                  <NavLink 
                    to="/blog" 
                    className={({isActive}) => 
                      `block px-4 py-3 rounded-lg text-base font-medium transition-colors duration-200 ${
                        isActive 
                          ? 'bg-indigo-50 text-indigo-600' 
                          : 'text-gray-700 hover:bg-gray-50 hover:text-indigo-600'
                      }`
                    }
                    onClick={() => setMobileMenuOpen(false)}
                  >
                    {t('blog.title')}
                  </NavLink>
                </div>
              </div>

              <div className="border-t border-gray-200 p-4">
                {!user && (
                  <NavLink
                    to="/login"
                    className="flex items-center justify-center px-4 py-2 border border-transparent text-base font-medium rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 transition-colors duration-200 shadow-sm hover:shadow-md w-full"
                    onClick={() => setMobileMenuOpen(false)}
                  >
                    {t('login')}
                  </NavLink>
                )}
              </div>
            </nav>
fisherdaddy's avatar
fisherdaddy committed
348 349
          </div>
        </div>
fisherdaddy's avatar
fisherdaddy committed
350 351 352 353
      </nav>
    </header>
  );
}
fisherdaddy's avatar
fisherdaddy committed
354
export default Header;