diff --git a/src/components/ShopMenu/ShopMenu.jsx b/src/components/ShopMenu/ShopMenu.jsx index 850cb54c..4c99f11d 100644 --- a/src/components/ShopMenu/ShopMenu.jsx +++ b/src/components/ShopMenu/ShopMenu.jsx @@ -1,3 +1,5 @@ +'use client'; + import React, { useState, useRef, useEffect } from 'react'; import { ChevronDown, ChevronUp, ArrowRight } from 'lucide-react'; @@ -9,22 +11,16 @@ const ShopMenu = ({ shopOpen, setShopOpen, onShopClick, onShopKeyDown }) => { // Close shop menu when clicking outside useEffect(() => { const handleClickOutside = (event) => { - if ( - shopButtonRef.current && - !shopButtonRef.current.contains(event.target) - ) { + if (shopButtonRef.current && !shopButtonRef.current.contains(event.target)) { setShopOpen(false); setFocusedIndex(-1); } }; - document.addEventListener('mousedown', handleClickOutside); - return () => { - document.removeEventListener('mousedown', handleClickOutside); - }; + return () => document.removeEventListener('mousedown', handleClickOutside); }, [setShopOpen]); - // Keyboard accessibility + // Keyboard navigation useEffect(() => { const handleKeyDown = (event) => { if (!shopOpen) return; @@ -37,62 +33,25 @@ const ShopMenu = ({ shopOpen, setShopOpen, onShopClick, onShopKeyDown }) => { break; case 'ArrowDown': event.preventDefault(); - setFocusedIndex((prev) => { - const nextIndex = prev + 1; - const totalItems = menuItemsRef.current.length; - return nextIndex >= totalItems ? 0 : nextIndex; - }); + setFocusedIndex((prev) => (prev + 1) % menuItemsRef.current.length); break; case 'ArrowUp': event.preventDefault(); - setFocusedIndex((prev) => { - const prevIndex = prev - 1; - const totalItems = menuItemsRef.current.length; - return prevIndex < 0 ? totalItems - 1 : prevIndex; - }); - break; - case 'ArrowRight': - event.preventDefault(); - setFocusedIndex((prev) => { - // Move to next column (every 4 items per column) - const itemsPerColumn = 4; - const currentColumn = Math.floor(prev / itemsPerColumn); - const nextColumn = (currentColumn + 1) % 8; // 8 total columns - const nextIndex = - nextColumn * itemsPerColumn + (prev % itemsPerColumn); - return nextIndex < menuItemsRef.current.length ? nextIndex : prev; - }); - break; - case 'ArrowLeft': - event.preventDefault(); - setFocusedIndex((prev) => { - // Move to previous column (every 4 items per column) - const itemsPerColumn = 4; - const currentColumn = Math.floor(prev / itemsPerColumn); - const prevColumn = currentColumn === 0 ? 7 : currentColumn - 1; // 8 total columns - const prevIndex = - prevColumn * itemsPerColumn + (prev % itemsPerColumn); - return prevIndex < menuItemsRef.current.length ? prevIndex : prev; - }); + setFocusedIndex((prev) => + prev - 1 < 0 ? menuItemsRef.current.length - 1 : prev - 1 + ); break; case 'Enter': case ' ': - if (focusedIndex >= 0) { - event.preventDefault(); - const focusedItem = menuItemsRef.current[focusedIndex]; - if (focusedItem) { - focusedItem.click(); - } + if (focusedIndex >= 0 && menuItemsRef.current[focusedIndex]) { + menuItemsRef.current[focusedIndex].click(); } break; } }; - document.addEventListener('keydown', handleKeyDown); - return () => { - document.removeEventListener('keydown', handleKeyDown); - }; - }, [shopOpen, focusedIndex, setShopOpen, shopButtonRef]); + return () => document.removeEventListener('keydown', handleKeyDown); + }, [shopOpen, focusedIndex]); // Focus management useEffect(() => { @@ -101,37 +60,78 @@ const ShopMenu = ({ shopOpen, setShopOpen, onShopClick, onShopKeyDown }) => { } }, [focusedIndex]); - // Handle navigation to collection const handleCollectionClick = (collectionName) => { - const url = `https://corexshoptest.onrender.com/api/collections/${collectionName.toLowerCase()}`; - window.open(url, '_blank'); + window.open( + `https://corexshoptest.onrender.com/api/collections/${collectionName.toLowerCase()}`, + '_blank' + ); setShopOpen(false); setFocusedIndex(-1); }; - // Helper function to create menu item with accessibility const createMenuItem = (collectionName, displayName, index) => ( ); + const menuData = [ + { + title: 'SHOP ALL', + items: [ + { name: 'all-products', label: 'All Products' }, + { name: 'best-sellers', label: 'Best Sellers' }, + { name: 'garage-sale', label: 'Garage Sale' }, + { name: 'apparel-gear', label: 'Apparel & Gear' }, + ], + }, + { + title: 'SPORT NUTRITION', + items: [ + { name: 'pre-workout', label: 'Pre-Workout' }, + { name: 'intra-workout', label: 'Intra-Workout' }, + { name: 'muscle-recovery', label: 'Muscle Recovery' }, + { name: 'supplements', label: 'Supplements' }, + ], + }, + { + title: 'PROTEIN', + items: [ + { name: 'lactose-free', label: 'Lactose Free' }, + { name: 'whey-protein', label: 'Whey Protein' }, + { name: 'iso-protein', label: 'ISO Protein' }, + { name: 'vegan-protein', label: 'Vegan Protein' }, + ], + }, + { + title: 'AMINO ACIDS', + items: [ + { name: 'bcaas', label: 'BCAAs' }, + { name: 'creatine', label: 'Creatine' }, + { name: 'glutamine', label: 'Glutamine' }, + { name: 'eaas', label: 'EAAs' }, + ], + }, + { + title: 'HEALTH & WELLNESS', + items: [ + { name: 'multivitamins', label: 'Multivitamins' }, + { name: 'greens-and-reds', label: 'Greens and Reds' }, + { name: 'joint-health', label: 'Joint Health' }, + ], + }, + ]; + return ( <> - {/* Shop Button - Updated to match other navigation buttons */} + {/* Shop Button */} {/* Mega Menu */} {shopOpen && (