1- import { useState } from 'react' ;
2- import { Link } from 'react-router-dom' ;
1+ import { useEffect , useRef , useState } from 'react' ;
2+ import { Link , useLocation } from 'react-router-dom' ;
33import { RetailIcon } from './retail' ;
44import { brandIcon , brandLogo , brandName } from '../lib/brand' ;
55import { CartIcon , SearchIcon } from './ui' ;
@@ -24,6 +24,38 @@ function fitDropdown(event) {
2424export default function StoreHeader ( { settings, name, departments, q, setQ, search, menuOpen, setMenuOpen, count, groups = [ ] , brands = [ ] , products = [ ] } ) {
2525 const [ expanded , setExpanded ] = useState ( { } ) ;
2626 const [ dismissed , setDismissed ] = useState ( false ) ;
27+ const [ searchOpen , setSearchOpen ] = useState ( false ) ;
28+ const menuToggle = useRef ( null ) ;
29+ const searchToggle = useRef ( null ) ;
30+ const searchInput = useRef ( null ) ;
31+
32+ useEffect ( ( ) => {
33+ const mobile = window . matchMedia ( '(max-width: 900px)' ) ;
34+ const closeOnDesktop = ( ) => {
35+ if ( ! mobile . matches ) {
36+ setMenuOpen ( false ) ;
37+ setSearchOpen ( false ) ;
38+ }
39+ } ;
40+ mobile . addEventListener ( 'change' , closeOnDesktop ) ;
41+ return ( ) => mobile . removeEventListener ( 'change' , closeOnDesktop ) ;
42+ } , [ setMenuOpen ] ) ;
43+
44+ useEffect ( ( ) => {
45+ if ( ! menuOpen ) return ;
46+ const root = document . documentElement ;
47+ root . classList . add ( 'retail-drawer-open' ) ;
48+ return ( ) => root . classList . remove ( 'retail-drawer-open' ) ;
49+ } , [ menuOpen ] ) ;
50+
51+ useEffect ( ( ) => {
52+ if ( searchOpen ) searchInput . current ?. focus ( ) ;
53+ } , [ searchOpen ] ) ;
54+
55+ function closeMenu ( ) {
56+ setMenuOpen ( false ) ;
57+ menuToggle . current ?. focus ( ) ;
58+ }
2759 const toggle = ( key ) => setExpanded ( ( current ) => ( { ...current , [ key ] : ! current [ key ] } ) ) ;
2860 const brandsByCategory = new Map ( ) ;
2961 for ( const product of products ) {
@@ -33,29 +65,38 @@ export default function StoreHeader({ settings, name, departments, q, setQ, sear
3365 brandsByCategory . get ( key ) . add ( product . brand_id ) ;
3466 }
3567 const logo = settings . header_logo || ( name === brandName ? brandLogo : '' ) ;
36- return < header className = "top retail-header retail-header-v2" onKeyDown = { ( event ) => {
68+ return < header className = "top retail-header retail-header-v2" data-search-open = { searchOpen } onKeyDown = { ( event ) => {
3769 if ( event . key !== 'Escape' ) return ;
3870 const group = event . target . closest ( '.retail-nav-group' ) ;
3971 if ( menuOpen ) {
40- setMenuOpen ( false ) ;
41- event . currentTarget . querySelector ( '.mobile-menu-toggle' ) ?. focus ( ) ;
72+ closeMenu ( ) ;
73+ } else if ( searchOpen ) {
74+ setSearchOpen ( false ) ;
75+ searchToggle . current ?. focus ( ) ;
4276 } else if ( group ) {
4377 setDismissed ( true ) ;
4478 group . querySelector ( 'a' ) ?. focus ( ) ;
4579 }
4680 } } >
4781 < div className = "wrap" >
48- < button type = "button" className = "mobile-menu-toggle" aria-label = "Toggle categories" aria-expanded = { menuOpen } aria-controls = "store-categories" onClick = { ( ) => setMenuOpen ( ( open ) => ! open ) } >
49- < RetailIcon name = "menu" size = { 22 } />
82+ < button ref = { menuToggle } type = "button" className = "mobile-menu-toggle" aria-label = "Toggle categories" aria-expanded = { menuOpen } aria-controls = "store-categories" onClick = { ( ) => {
83+ setSearchOpen ( false ) ;
84+ setMenuOpen ( ( open ) => ! open ) ;
85+ } } >
86+ < RetailIcon name = { menuOpen ? 'close' : 'menu' } size = { 22 } />
5087 </ button >
5188 < Link to = "/" className = "brand retail-brand" aria-label = { `${ name } home` } >
5289 { logo ? < img className = "retail-header-logo" src = { logo } alt = "" /> : < >
5390 < img className = "retail-brand-mark" src = { brandIcon } alt = "" width = "42" height = "42" />
5491 < span className = "retail-brand-name" > { name } </ span >
5592 </ > }
5693 </ Link >
57- < form className = "retail-search" role = "search" onSubmit = { search } >
58- < input type = "search" value = { q } onChange = { ( event ) => setQ ( event . target . value ) } placeholder = "Search products, brands or models" aria-label = "Search products" />
94+ < button ref = { searchToggle } type = "button" className = "retail-mobile-search-toggle" aria-label = { searchOpen ? 'Close search' : 'Open search' } aria-expanded = { searchOpen } aria-controls = "store-search" onClick = { ( ) => {
95+ setMenuOpen ( false ) ;
96+ setSearchOpen ( ( open ) => ! open ) ;
97+ } } > { searchOpen ? < RetailIcon name = "close" size = { 21 } /> : < SearchIcon /> } </ button >
98+ < form id = "store-search" className = "retail-search" role = "search" onSubmit = { ( event ) => { search ( event ) ; setSearchOpen ( false ) ; } } >
99+ < input ref = { searchInput } type = "search" value = { q } onChange = { ( event ) => setQ ( event . target . value ) } placeholder = "Search products, brands or models" aria-label = "Search products" />
59100 < button type = "submit" aria-label = "Submit product search" > < SearchIcon /> </ button >
60101 </ form >
61102 < div className = "retail-quick-actions" >
@@ -71,9 +112,10 @@ export default function StoreHeader({ settings, name, departments, q, setQ, sear
71112 </ div >
72113 < Link to = "/pc-builder" className = "retail-builder-link" > PC Builder</ Link >
73114 < Link to = "/cart" className = "icon-btn retail-top-cart" aria-label = { `Cart, ${ count } item${ count === 1 ? '' : 's' } ` } >
74- < CartIcon /> { count > 0 && < span className = "cart-count" > { count > 99 ? '99+' : count } </ span > }
115+ < CartIcon /> < span className = "cart-count" data-empty = { count === 0 } aria-hidden = "true" > { count > 99 ? '99+' : count } </ span >
75116 </ Link >
76117 </ div >
118+ { menuOpen && < button type = "button" className = "retail-drawer-backdrop" aria-label = "Close categories" tabIndex = { - 1 } onClick = { closeMenu } /> }
77119 < nav id = "store-categories" className = { `retail-nav ${ menuOpen ? 'menu-open' : '' } ` } aria-label = "Product categories" data-dismissed = { dismissed } onPointerMove = { ( ) => dismissed && setDismissed ( false ) } onBlur = { ( event ) => { if ( ! event . currentTarget . contains ( event . relatedTarget ) ) setDismissed ( false ) ; } } >
78120 < div className = "wrap" >
79121 < Link to = "/products" className = "retail-all" > < RetailIcon name = "menu" size = { 17 } /> All products</ Link >
@@ -83,7 +125,7 @@ export default function StoreHeader({ settings, name, departments, q, setQ, sear
83125 const columns = menuItems > 16 ? 2 : 1 ;
84126 return < div className = "retail-nav-group" key = { to } data-open = { ! ! expanded [ to ] } data-align = { index >= departments . length / 2 ? 'end' : 'start' } onPointerEnter = { fitDropdown } onFocus = { fitDropdown } >
85127 < div className = "retail-nav-row" > < Link to = { to } > < span className = "retail-nav-label" title = { name } > { name } </ span > </ Link >
86- { categories . length > 0 && < button type = "button" aria-label = { `Categories in ${ name } ` } aria-expanded = { ! ! expanded [ to ] } onClick = { ( ) => toggle ( to ) } > ⌄ </ button > }
128+ { categories . length > 0 && < button type = "button" aria-label = { `Categories in ${ name } ` } aria-expanded = { ! ! expanded [ to ] } onClick = { ( ) => toggle ( to ) } > { expanded [ to ] ? '−' : '+' } </ button > }
87129 </ div >
88130 { categories . length > 0 && < ul className = "retail-categories" style = { { '--nav-columns' : columns , '--nav-rows' : Math . ceil ( menuItems / columns ) } } >
89131 { categories . map ( ( category ) => {
@@ -92,7 +134,7 @@ export default function StoreHeader({ settings, name, departments, q, setQ, sear
92134 const matches = brands . filter ( ( brand ) => ids ?. has ( brand . id ) ) ;
93135 return < li key = { category . id } data-open = { ! ! expanded [ url ] } >
94136 < div className = "retail-nav-row" > < Link to = { url } > < span className = "retail-nav-label" title = { category . name } > { category . name } </ span > { matches . length > 0 && < span aria-hidden = "true" > ›</ span > } </ Link >
95- { matches . length > 0 && < button type = "button" aria-label = { `Brands in ${ category . name } ` } aria-expanded = { ! ! expanded [ url ] } onClick = { ( ) => toggle ( url ) } > ⌄ </ button > }
137+ { matches . length > 0 && < button type = "button" aria-label = { `Brands in ${ category . name } ` } aria-expanded = { ! ! expanded [ url ] } onClick = { ( ) => toggle ( url ) } > { expanded [ url ] ? '−' : '+' } </ button > }
96138 </ div >
97139 { matches . length > 0 && < ul className = "retail-brands" >
98140 { matches . map ( ( brand ) => < li key = { brand . id } > < Link to = { `${ url } &b=${ encodeURIComponent ( brand . id ) } ` } > < span className = "retail-nav-label" title = { brand . name } > { brand . name } </ span > </ Link > </ li > ) }
@@ -107,3 +149,21 @@ export default function StoreHeader({ settings, name, departments, q, setQ, sear
107149 </ nav >
108150 </ header > ;
109151}
152+
153+
154+ const mobileShortcuts = [
155+ { to : '/products?offer=1' , label : 'Offers' , icon : 'gift' } ,
156+ { to : '/track' , label : 'Track Order' , icon : 'box' } ,
157+ { to : '/pc-builder' , label : 'PC Builder' , icon : 'desktop' } ,
158+ { to : '/wishlist' , label : 'Wishlist' , icon : 'heart' } ,
159+ { to : '/account/login' , label : 'Account' , icon : 'user' } ,
160+ ] ;
161+
162+ export function MobileShopNav ( ) {
163+ const { pathname, search } = useLocation ( ) ;
164+ return < nav className = "retail-mobile-nav" aria-label = "Shopping navigation" >
165+ { mobileShortcuts . map ( ( { to, label, icon } ) => < Link key = { to } to = { to } aria-current = { pathname + search === to ? 'page' : undefined } >
166+ < RetailIcon name = { icon } size = { 20 } /> < span > { label } </ span >
167+ </ Link > ) }
168+ </ nav > ;
169+ }
0 commit comments