'use client'; import throttle from 'lodash.throttle'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useBreakpoint } from '../../hook/breakpoints.hook.js'; import { resolveResponsiveVariant } from '../../utils/breakpoint.util.js'; import { ArrowLeftIcon, HamburgerMenuIcon } from '../icon/index.js'; import { Button, SkipLink } from '../index.js'; import { BOMMultibrandLargeLogo, BOMMultibrandSmallLogo, BSAMultibrandLargeLogo, BSAMultibrandSmallLogo, STGMultibrandLargeLogo, STGMultibrandSmallLogo, SymbolProps, WBCMultibrandLargeLogo, WBCMultibrandSmallLogo, } from '../symbol/index.js'; import { styles as headerStyles } from './header.styles.js'; import { type HeaderProps } from './header.types.js'; const LOGO_MAP = { wbc: { logo: (props: SymbolProps) => , largeLogo: (props: SymbolProps) => , }, stg: { logo: (props: SymbolProps) => , largeLogo: (props: SymbolProps) => , }, bom: { logo: (props: SymbolProps) => , largeLogo: (props: SymbolProps) => , }, bsa: { logo: (props: SymbolProps) => , largeLogo: (props: SymbolProps) => , }, } as const; export function Header({ anchorTarget, brand, className, children, disableLogoLink = false, fixed = false, fixedMaxWidth, isScrolled, leftIcon, leftOnClick, leftAssistiveText, logoAssistiveText, logoLink = '#', logoCenter = false, logoOnClick, skipLinkContent = 'Skip to main content', skipToContentId, ...props }: HeaderProps) { const [scrolled, setScrolled] = useState(false); const handleScroll = throttle(() => { let hasScrolled = false; if (window.scrollY > 5) { hasScrolled = true; } setScrolled(hasScrolled); }, 10); useEffect(() => { if (fixed) window.addEventListener('scroll', handleScroll); return () => { window.removeEventListener('scroll', handleScroll); }; }, [fixed, handleScroll]); const logoAlignment = logoCenter ? 'center' : 'left'; const finalBrand = useMemo(() => { // Due to brands like 'wbc-light' and 'stg-light' return brand.split('-')[0] as keyof typeof LOGO_MAP; }, [brand]); const SmallLogo = LOGO_MAP[finalBrand].logo; const LargeLogo = LOGO_MAP[finalBrand].largeLogo; const ButtonIcon = leftIcon === 'arrow' ? ArrowLeftIcon : HamburgerMenuIcon; const breakpoint = useBreakpoint(); const styles = headerStyles({ logoCenter: resolveResponsiveVariant(logoCenter, breakpoint), fixed: resolveResponsiveVariant(fixed, breakpoint), leftIcon: resolveResponsiveVariant(leftIcon, breakpoint), scrolled: isScrolled || scrolled, }); const HeaderLogo = useCallback( () => ( <> ), // eslint-disable-next-line react-hooks/exhaustive-deps [logoAlignment, logoAssistiveText, LargeLogo, SmallLogo], ); const defaultAssistiveText = leftIcon === 'arrow' ? 'Back' : 'Menu'; return (
{skipToContentId && {skipLinkContent}} {leftIcon && (
)} {/* useFocusRing was causing this link to need two clicks to activate so focus-visible styling is used instead */} {disableLogoLink ? (
) : ( )} {children &&
{children}
}
); }