Files
bakery-ia/frontend/src/components/layout/PublicHeader/PublicHeader.tsx

511 lines
17 KiB
TypeScript
Raw Normal View History

2025-10-31 11:54:19 +01:00
import React, { forwardRef, useState, useEffect, useCallback } from 'react';
2025-08-28 10:41:04 +02:00
import { clsx } from 'clsx';
import { Link } from 'react-router-dom';
2025-10-17 18:14:28 +02:00
import { useTranslation } from 'react-i18next';
2025-08-28 10:41:04 +02:00
import { Button, ThemeToggle } from '../../ui';
2025-09-25 12:14:46 +02:00
import { CompactLanguageSelector } from '../../ui/LanguageSelector';
2025-10-18 16:03:23 +02:00
import { getRegisterUrl, getLoginUrl } from '../../../utils/navigation';
2025-10-31 11:54:19 +01:00
import { X } from 'lucide-react';
2025-08-28 10:41:04 +02:00
export interface PublicHeaderProps {
className?: string;
/**
* Custom logo component
*/
logo?: React.ReactNode;
/**
* Show theme toggle
*/
showThemeToggle?: boolean;
/**
* Show authentication buttons (login/register)
*/
showAuthButtons?: boolean;
2025-09-25 12:14:46 +02:00
/**
* Show language selector
*/
showLanguageSelector?: boolean;
2025-08-28 10:41:04 +02:00
/**
* Custom navigation items
*/
navigationItems?: Array<{
id: string;
label: string;
href: string;
external?: boolean;
}>;
/**
* Header variant
*/
variant?: 'default' | 'transparent' | 'minimal';
}
export interface PublicHeaderRef {
scrollIntoView: () => void;
}
/**
* PublicHeader - Header component for public pages (landing, login, register)
*
* Features:
* - Clean, minimal design suitable for public pages
* - Integrated theme toggle for consistent theming
* - Authentication buttons (login/register)
* - Optional custom navigation items
* - Multiple visual variants
* - Responsive design with mobile optimization
* - Accessible navigation structure
*/
export const PublicHeader = forwardRef<PublicHeaderRef, PublicHeaderProps>(({
className,
logo,
showThemeToggle = true,
showAuthButtons = true,
2025-09-25 12:14:46 +02:00
showLanguageSelector = true,
2025-08-28 10:41:04 +02:00
navigationItems = [],
variant = 'default',
}, ref) => {
2025-10-17 18:14:28 +02:00
const { t } = useTranslation();
2025-08-28 10:41:04 +02:00
const headerRef = React.useRef<HTMLDivElement>(null);
2025-10-31 11:54:19 +01:00
// State for mobile menu
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
// State for sticky header
const [isScrolled, setIsScrolled] = useState(false);
// State for active section
const [activeSection, setActiveSection] = useState<string>('');
2025-08-28 10:41:04 +02:00
// Default navigation items
2025-10-31 11:54:19 +01:00
const defaultNavItems: Array<{id: string; label: string; href: string; external?: boolean}> = [];
2025-08-28 10:41:04 +02:00
const navItems = navigationItems.length > 0 ? navigationItems : defaultNavItems;
2025-10-31 11:54:19 +01:00
// Smooth scroll to section
const scrollToSection = useCallback((href: string) => {
if (href.startsWith('#')) {
const element = document.querySelector(href);
if (element) {
const headerHeight = headerRef.current?.offsetHeight || 0;
const elementPosition = element.getBoundingClientRect().top + window.pageYOffset;
const offsetPosition = elementPosition - headerHeight - 20; // 20px additional offset
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
// Update URL hash
window.history.pushState(null, '', href);
// Close mobile menu
setIsMobileMenuOpen(false);
}
}
}, []);
// Handle scroll for sticky header
useEffect(() => {
const handleScroll = () => {
setIsScrolled(window.scrollY > 20);
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);
}, []);
// Active section detection with Intersection Observer
useEffect(() => {
const observerOptions = {
rootMargin: '-100px 0px -66%',
threshold: 0
};
const observerCallback = (entries: IntersectionObserverEntry[]) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const id = entry.target.getAttribute('id');
if (id) {
setActiveSection(id);
}
}
});
};
const observer = new IntersectionObserver(observerCallback, observerOptions);
// Observe all sections that are navigation targets
navItems.forEach(item => {
if (item.href.startsWith('#')) {
const element = document.querySelector(item.href);
if (element) {
observer.observe(element);
}
}
});
return () => observer.disconnect();
}, [navItems]);
// Close mobile menu on ESC key
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape' && isMobileMenuOpen) {
setIsMobileMenuOpen(false);
}
};
document.addEventListener('keydown', handleEscape);
return () => document.removeEventListener('keydown', handleEscape);
}, [isMobileMenuOpen]);
// Prevent body scroll when mobile menu is open
useEffect(() => {
if (isMobileMenuOpen) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = '';
}
return () => {
document.body.style.overflow = '';
};
}, [isMobileMenuOpen]);
2025-08-28 10:41:04 +02:00
// Scroll into view
2025-10-31 11:54:19 +01:00
const scrollIntoView = useCallback(() => {
2025-08-28 10:41:04 +02:00
headerRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, []);
// Expose ref methods
React.useImperativeHandle(ref, () => ({
scrollIntoView,
}), [scrollIntoView]);
2025-10-31 11:54:19 +01:00
// Render navigation link with improved styles and active state
const renderNavLink = (item: typeof navItems[0], isMobile = false) => {
const isActive = activeSection === item.id || (item.href.startsWith('#') && item.href === `#${activeSection}`);
2025-08-28 10:41:04 +02:00
const linkContent = (
2025-10-31 11:54:19 +01:00
<span className={clsx(
"relative text-sm font-medium transition-all duration-200",
isMobile ? "text-base py-1" : "",
isActive
? "text-[var(--color-primary)]"
: "text-[var(--text-secondary)] hover:text-[var(--text-primary)]",
// Animated underline on hover
!isMobile && "after:content-[''] after:absolute after:bottom-[-4px] after:left-0 after:w-0 after:h-[2px] after:bg-[var(--color-primary)] after:transition-all after:duration-300 hover:after:w-full",
// Active state indicator
isActive && !isMobile && "after:w-full"
)}>
2025-08-28 10:41:04 +02:00
{item.label}
</span>
);
2025-10-31 11:54:19 +01:00
if (item.href.startsWith('#')) {
return (
<a
key={item.id}
href={item.href}
onClick={(e) => {
e.preventDefault();
scrollToSection(item.href);
}}
className={clsx(
"focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)] focus:ring-offset-2 rounded-sm",
isMobile && "block w-full py-3 px-4 hover:bg-[var(--bg-secondary)] transition-colors"
)}
aria-current={isActive ? 'page' : undefined}
>
{linkContent}
</a>
);
}
if (item.external || item.href.startsWith('http')) {
2025-08-28 10:41:04 +02:00
return (
<a
key={item.id}
href={item.href}
target={item.external ? '_blank' : undefined}
rel={item.external ? 'noopener noreferrer' : undefined}
2025-10-31 11:54:19 +01:00
className={clsx(
"focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)] focus:ring-offset-2 rounded-sm",
isMobile && "block w-full py-3 px-4 hover:bg-[var(--bg-secondary)] transition-colors"
)}
2025-08-28 10:41:04 +02:00
>
{linkContent}
</a>
);
}
return (
<Link
key={item.id}
to={item.href}
2025-10-31 11:54:19 +01:00
className={clsx(
"focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)] focus:ring-offset-2 rounded-sm",
isMobile && "block w-full py-3 px-4 hover:bg-[var(--bg-secondary)] transition-colors"
)}
2025-08-28 10:41:04 +02:00
>
{linkContent}
</Link>
);
};
return (
2025-10-31 11:54:19 +01:00
<>
{/* Skip to main content link for accessibility */}
<a
href="#main-content"
className="sr-only focus:not-sr-only focus:absolute focus:z-50 focus:top-4 focus:left-4 focus:px-4 focus:py-2 focus:bg-[var(--color-primary)] focus:text-white focus:rounded-md"
>
{t('common:header.skip_to_content', 'Saltar al contenido principal')}
</a>
<header
ref={headerRef}
className={clsx(
'w-full transition-all duration-300',
// Sticky header
'fixed top-0 left-0 right-0 z-40',
// Base styles with scroll effect
variant === 'default' && [
isScrolled
? 'bg-[var(--bg-primary)]/95 backdrop-blur-md border-b border-[var(--border-primary)] shadow-lg'
: 'bg-[var(--bg-primary)] border-b border-[var(--border-primary)] shadow-sm'
],
variant === 'transparent' && [
isScrolled
? 'bg-[var(--bg-primary)]/95 backdrop-blur-md shadow-lg'
: 'bg-transparent'
],
variant === 'minimal' && 'bg-[var(--bg-primary)]',
className
)}
role="banner"
aria-label="Navegación principal"
>
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div className={clsx(
"flex justify-between items-center transition-all duration-300",
isScrolled ? "py-3 lg:py-4" : "py-4 lg:py-6"
)}>
2025-08-28 10:41:04 +02:00
{/* Logo and brand */}
<div className="flex items-center gap-3 min-w-0">
<Link to="/" className="flex items-center gap-3 hover:opacity-80 transition-opacity">
{logo || (
<>
<div className="w-8 h-8 bg-gradient-to-br from-[var(--color-primary)] to-[var(--color-primary-dark)] rounded-lg flex items-center justify-center text-white font-bold text-sm">
PI
</div>
<h1 className="text-xl lg:text-2xl font-bold text-[var(--text-primary)]">
Panadería IA
</h1>
</>
)}
</Link>
</div>
{/* Desktop navigation */}
<nav className="hidden md:flex items-center space-x-8" role="navigation">
2025-10-31 11:54:19 +01:00
{navItems.map((item) => renderNavLink(item, false))}
2025-08-28 10:41:04 +02:00
</nav>
{/* Right side actions */}
2025-10-03 14:09:34 +02:00
<div className="flex items-center gap-2 lg:gap-3">
{/* Language selector - More compact */}
2025-09-25 12:14:46 +02:00
{showLanguageSelector && (
2025-10-03 14:09:34 +02:00
<div className="hidden sm:flex">
<CompactLanguageSelector className="w-[70px]" />
</div>
2025-09-25 12:14:46 +02:00
)}
2025-08-28 10:41:04 +02:00
{/* Theme toggle */}
{showThemeToggle && (
<ThemeToggle
variant="button"
size="md"
className="hidden sm:flex"
/>
)}
2025-10-03 14:09:34 +02:00
{/* Authentication buttons - Enhanced */}
2025-08-28 10:41:04 +02:00
{showAuthButtons && (
2025-10-03 14:09:34 +02:00
<div className="flex items-center gap-2 lg:gap-3">
2025-10-18 16:03:23 +02:00
<Link to={getLoginUrl()}>
2025-09-25 12:14:46 +02:00
<Button
variant="ghost"
2025-10-03 14:09:34 +02:00
size="md"
className="hidden sm:inline-flex font-medium hover:bg-[var(--bg-secondary)] transition-all duration-200"
2025-08-28 10:41:04 +02:00
>
2025-10-17 18:14:28 +02:00
{t('common:header.login')}
2025-08-28 10:41:04 +02:00
</Button>
</Link>
2025-10-18 16:03:23 +02:00
<Link to={getRegisterUrl()}>
2025-09-25 12:14:46 +02:00
<Button
2025-10-03 14:09:34 +02:00
size="md"
className="bg-gradient-to-r from-[var(--color-primary)] to-[var(--color-primary-dark)] hover:opacity-90 text-white font-semibold shadow-lg hover:shadow-xl transition-all duration-200 px-6"
2025-08-28 10:41:04 +02:00
>
2025-10-17 18:14:28 +02:00
<span className="hidden sm:inline">{t('common:header.start_free')}</span>
<span className="sm:hidden">{t('common:header.register')}</span>
2025-08-28 10:41:04 +02:00
</Button>
</Link>
</div>
)}
{/* Mobile theme toggle */}
{showThemeToggle && (
<ThemeToggle
variant="button"
size="sm"
className="sm:hidden"
/>
)}
{/* Mobile menu button */}
<div className="md:hidden">
<Button
variant="ghost"
size="sm"
2025-10-31 11:54:19 +01:00
className="p-2 min-h-[44px] min-w-[44px]"
aria-label={isMobileMenuOpen ? t('common:header.close_menu', 'Cerrar menú') : t('common:header.open_menu', 'Abrir menú')}
aria-expanded={isMobileMenuOpen}
aria-controls="mobile-menu"
onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)}
2025-08-28 10:41:04 +02:00
>
2025-10-31 11:54:19 +01:00
<svg
className={clsx("w-6 h-6 transition-transform duration-300", isMobileMenuOpen && "rotate-90")}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
{isMobileMenuOpen ? (
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
) : (
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
)}
2025-08-28 10:41:04 +02:00
</svg>
</Button>
</div>
</div>
2025-10-31 11:54:19 +01:00
</div>
2025-08-28 10:41:04 +02:00
</div>
2025-10-31 11:54:19 +01:00
</header>
2025-08-28 10:41:04 +02:00
2025-10-31 11:54:19 +01:00
{/* Spacer to prevent content from hiding under fixed header */}
<div className={clsx(
"transition-all duration-300",
isScrolled ? "h-[72px] lg:h-[80px]" : "h-[80px] lg:h-[96px]"
)} aria-hidden="true" />
2025-09-25 12:14:46 +02:00
2025-10-31 11:54:19 +01:00
{/* Mobile menu drawer */}
{isMobileMenuOpen && (
<>
{/* Backdrop */}
<div
className="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 md:hidden animate-in fade-in duration-200"
onClick={() => setIsMobileMenuOpen(false)}
aria-hidden="true"
/>
{/* Drawer */}
<div
id="mobile-menu"
className={clsx(
"fixed top-0 right-0 bottom-0 w-[85%] max-w-sm bg-[var(--bg-primary)] shadow-2xl z-50 md:hidden",
"animate-in slide-in-from-right duration-300",
"flex flex-col"
)}
role="dialog"
aria-modal="true"
aria-label={t('common:header.mobile_menu', 'Menú de navegación móvil')}
>
{/* Drawer header */}
<div className="flex items-center justify-between p-4 border-b border-[var(--border-primary)]">
<div className="flex items-center gap-2">
<div className="w-8 h-8 bg-gradient-to-br from-[var(--color-primary)] to-[var(--color-primary-dark)] rounded-lg flex items-center justify-center text-white font-bold text-sm">
PI
2025-09-25 12:14:46 +02:00
</div>
2025-10-31 11:54:19 +01:00
<h2 className="text-lg font-bold text-[var(--text-primary)]">
Panadería IA
</h2>
2025-09-25 12:14:46 +02:00
</div>
2025-10-31 11:54:19 +01:00
<Button
variant="ghost"
size="sm"
className="p-2 min-h-[44px] min-w-[44px]"
onClick={() => setIsMobileMenuOpen(false)}
aria-label={t('common:header.close_menu', 'Cerrar menú')}
>
<X className="w-6 h-6" />
</Button>
</div>
{/* Drawer content */}
<div className="flex-1 overflow-y-auto">
{/* Navigation items */}
<nav className="py-4" role="navigation">
<div className="flex flex-col">
{navItems.map((item) => (
<div key={item.id}>
{renderNavLink(item, true)}
</div>
))}
</div>
</nav>
{/* Mobile language selector */}
{showLanguageSelector && (
<div className="px-4 py-4 border-t border-[var(--border-primary)]">
<div className="text-sm font-medium text-[var(--text-secondary)] mb-3">
{t('common:header.language', 'Idioma')}
</div>
<CompactLanguageSelector className="w-full" />
</div>
)}
{/* Theme toggle for mobile */}
{showThemeToggle && (
<div className="px-4 py-4 border-t border-[var(--border-primary)]">
<div className="text-sm font-medium text-[var(--text-secondary)] mb-3">
{t('common:header.theme', 'Tema')}
</div>
<ThemeToggle variant="button" size="md" className="w-full" />
</div>
)}
</div>
2025-09-25 12:14:46 +02:00
2025-10-31 11:54:19 +01:00
{/* Drawer footer with auth buttons */}
2025-08-28 10:41:04 +02:00
{showAuthButtons && (
2025-10-31 11:54:19 +01:00
<div className="p-4 border-t border-[var(--border-primary)] bg-[var(--bg-secondary)]">
<div className="flex flex-col gap-3">
<Link to={getLoginUrl()} onClick={() => setIsMobileMenuOpen(false)}>
<Button
variant="outline"
size="lg"
className="w-full font-medium min-h-[48px]"
>
{t('common:header.login', 'Iniciar Sesión')}
</Button>
</Link>
<Link to={getRegisterUrl()} onClick={() => setIsMobileMenuOpen(false)}>
<Button
size="lg"
className="w-full bg-gradient-to-r from-[var(--color-primary)] to-[var(--color-primary-dark)] hover:opacity-90 text-white font-semibold shadow-lg min-h-[48px]"
>
{t('common:header.start_free', 'Comenzar Gratis')}
</Button>
</Link>
</div>
2025-08-28 10:41:04 +02:00
</div>
)}
</div>
2025-10-31 11:54:19 +01:00
</>
)}
</>
2025-08-28 10:41:04 +02:00
);
});
PublicHeader.displayName = 'PublicHeader';