Skip to content

Broken mobile menu when initializing multiple mage.menu widgets #5595

@bramvanderholst

Description

@bramvanderholst

Steps to reproduce

  1. Initialize a new mage.menu in a .phtml <ul data-mage-init='{"menu":{"responsive":false, "expanded":true, "delay":0, "position":{"my":"left top","at":"left bottom"}}}'>
    (I wanted to split mobile and desktop navigation into 2 templates)
  2. View website in < 768px browser
  3. Open and close default mobile menu.

Problem

Menu will close, but opens again right after.

Possible cause

This seems to be due to setting the same onClick event multiple times on $('[data-action="toggle-nav"]') and $('.nav-sections') (in lib/web/mage/menu.js) when initializing the widget.

Quick fix

Change

toggle: function () {
    if ($('html').hasClass('nav-open')) {
        $('html').removeClass('nav-open');
        setTimeout(function () {
            $('html').removeClass('nav-before-open');
        }, 300);
    } else {
        $('html').addClass('nav-before-open');
        setTimeout(function () {
            $('html').addClass('nav-open');
        }, 42);
    }
},

to

toggle: function () {
    if ($('html').hasClass('nav-open')) {
        $('html').removeClass('nav-open');
        setTimeout(function () {
            $('html').removeClass('nav-before-open');
        }, 300);
    } else if(!$('html').hasClass('nav-before-open')) {
        $('html').addClass('nav-before-open');
        setTimeout(function () {
            $('html').addClass('nav-open');
        }, 42);
    }
},

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions