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 && (
e.stopPropagation()} + className="fixed left-0 top-[80px] w-screen bg-white shadow-lg z-40 overflow-y-auto p-6 transition-all" role="menu" aria-label="Shop categories" > -
- {/* SHOP ALL Section */} -
-
-

- SHOP ALL -

- -
-
- - {/* Mega Menu Grid */} -
- {/* Column 1: SHOP ALL */} -
-

- SHOP ALL -

-
    -
  • {createMenuItem('all-products', 'All products', 0)}
  • -
  • {createMenuItem('best-sellers', 'Best Sellers', 1)}
  • -
  • {createMenuItem('garage-sale', 'Garage Sale', 2)}
  • -
  • {createMenuItem('apparel-gear', 'Apparel & Gear', 3)}
  • -
-
- - {/* Column 2: SPORT NUTRITION PROTEIN */} -
-
- {/* SPORT NUTRITION Sub-column */} -
-

- SPORT NUTRITION -

-
    -
  • {createMenuItem('pre-workout', 'Pre-Workout', 4)}
  • -
  • - {createMenuItem('intra-workout', 'Intra-Workout', 5)} -
  • -
  • - {createMenuItem( - 'muscle-recovery', - 'Muscle Recovery', - 6 - )} -
  • -
  • {createMenuItem('supplements', 'Supplements', 7)}
  • -
-
- - {/* PROTEIN Sub-column */} -
-

- PROTEIN -

-
    -
  • - {createMenuItem('lactose-free', 'Lactose Free', 8)} -
  • -
  • - {createMenuItem('whey-protein', 'Whey Protein', 9)} -
  • -
  • - {createMenuItem('iso-protein', 'ISO Protein', 10)} -
  • -
  • - {createMenuItem('vegan-protein', 'Vegan Protein', 11)} -
  • -
-
+
+ {menuData.map((section, sectionIndex) => ( +
+

{section.title}

+
+ {section.items.map((item, idx) => + createMenuItem(item.name, item.label, sectionIndex * 10 + idx) + )}
- - {/* Column 3: AMINO ACIDS */} -
-

- AMINO ACIDS -

-
    -
  • {createMenuItem('bcaas', 'BCAAs', 12)}
  • -
  • {createMenuItem('creatine', 'Creatine', 13)}
  • -
  • {createMenuItem('glutamine', 'Glutamine', 14)}
  • -
  • {createMenuItem('eaas', 'EAAs', 15)}
  • -
-
- - {/* Column 4: HEALTH & WELLNESS */} -
-

- HEALTH & WELLNESS -

-
    -
  • - {createMenuItem('multivitamins', 'Multivitamins', 16)} -
  • -
  • - {createMenuItem('greens-and-reds', 'Greens and Reds', 17)} -
  • -
  • {createMenuItem('joint-health', 'Joint Health', 18)}
  • -
-
-
- - {/* Bottom Section */} -
- {/* Column 1: WEIGHT MANAGEMENT */} -
-

- WEIGHT MANAGEMENT -

-
    -
  • - {createMenuItem('meal-replacement', 'Meal Replacement', 19)} -
  • -
  • {createMenuItem('fat-burner', 'Fat Burner', 20)}
  • -
  • - {createMenuItem( - 'weight-management-supplements', - 'Supplements', - 21 - )} -
  • -
-
- - {/* Column 2: HORMONE HEALTH */} -
-

- HORMONE HEALTH -

-
    -
  • - {createMenuItem( - 'testosterone-booster', - 'Testosterone Booster', - 22 - )} -
  • -
-
- - {/* Column 3: SHOP BY GOAL */} -
-

- SHOP BY GOAL -

-
    -
  • {createMenuItem('build-mass', 'Build Mass', 23)}
  • -
  • {createMenuItem('endurance', 'Endurance', 24)}
  • -
  • - {createMenuItem( - 'athletic-performance', - 'Athletic Performance', - 25 - )} -
  • -
  • - {createMenuItem('health-wellness', 'Health & Wellness', 26)} -
  • -
-
- - {/* Column 4: APPAREL & GEAR */} -
-

- APPAREL & GEAR -

-
    -
  • {createMenuItem('apparel', 'Apparel', 27)}
  • -
  • {createMenuItem('gear', 'Gear', 28)}
  • -
  • {createMenuItem('merchandise', 'Merchandise', 29)}
  • -
-
-
+ ))}
)}