NavigationMenu class is a custom web component that renders the theme’s main navigation menu with support for mega menus, responsive behavior, and an optional “More” dropdown for overflow items.
Overview
The component automatically fetches menu data from the Salla API, renders both mobile and desktop versions, and handles overflow menu items by moving them into a “More” dropdown when space is limited.Component definition
<custom-main-menu> and can be used in Twig templates.
Features
- API integration - Fetches menu structure from Salla
- Responsive design - Separate mobile and desktop rendering
- Mega menu support - Display products directly in menu dropdowns
- Overflow handling - Automatically moves items to “More” dropdown
- Menu images - Support for category images in mobile menu
- RTL/LTR support - Works with both text directions
Usage
src/views/components/header/header.twig).
Configuration
The overflow behavior is controlled by a global variable set inmaster.twig:
When enabled, menu items that don’t fit will be moved to a “More” dropdown. When disabled, all menu items remain visible without overflow handling.
Key methods
connectedCallback()
Initializes the component when added to the DOM. Fetches menu data and renders the navigation.
hasChildren(menu)
Checks if a menu item has child items.
The menu object to check
boolean - true if the menu has children
hasProducts(menu)
Checks if a menu item has associated products for mega menu display.
The menu object to check
boolean - true if the menu has products
getMobileMenu(menu, displayAllText)
Generates the HTML for mobile menu items.
The menu object to render
Text for “display all” link
string - HTML markup for mobile menu
getDesktopMenu(menu, isRootMenu, additionalClasses)
Generates the HTML for desktop menu items.
The menu object to render
Whether this is a root-level menu item
Additional CSS classes to apply
string - HTML markup for desktop menu
initializeResponsiveMenu()
Initializes the responsive overflow behavior. Only runs on desktop (screen width >= 1024px) and when enable_more_menu is enabled.
checkMenuOverflow()
Calculates which menu items fit in the available space and moves overflow items to the “More” dropdown.
The method:
- Resets visible and overflow menus
- Shows all menu items
- Calculates available width (container width - other elements - 300px buffer)
- Hides items that don’t fit
- Creates “More” dropdown if there are overflow items
createMoreDropdown()
Creates the HTML for the “More” dropdown menu containing overflow items.
Returns string - HTML markup for More dropdown
debounce(func, wait)
Utility function to limit how often the resize handler is called.
The function to debounce
Wait time in milliseconds
function - Debounced function
Menu object structure
Styling
The component uses these CSS classes:.main-menu- Main menu container.root-level- Root menu items.has-children- Menu items with submenu.mega-menu- Menu items with products.sub-menu- Submenu dropdown.mobile-menu- Mobile menu container.close-mobile-menu- Mobile menu close button
Example
A complete navigation menu with overflow handling:Performance considerations
- Menu data is cached after the initial API call
- Resize events are debounced (250ms) to prevent excessive calculations
- Overflow calculations only run on desktop (≥1024px width)
- Menu images are only rendered in mobile view
Browser compatibility
The component uses:- Custom Elements API
- ES6 classes
- Modern JavaScript features