Mobile menu hamburger not working on direct mobile width loa

Created on 3 December 2024, 15 days ago

Problem/Motivation

The mobile hamburger menu is not functional when a page loads directly at mobile width (<1200px). This creates a poor user experience for mobile visitors who cannot access the navigation menu. The issue only affects direct mobile loads - the menu works correctly when resizing from desktop to mobile width, indicating a JavaScript initialization timing issue.

Steps to reproduce

  1. Load the site directly on mobile or at mobile width (<1200px)
  2. Click the hamburger menu - no response occurs
  3. Reload the same page at full desktop width
  4. Use browser's responsive tools to resize to mobile width
  5. Click hamburger menu - menu now toggles correctly
  6. Reload while still at mobile width - menu stops working again

Proposed resolution

document.addEventListener('DOMContentLoaded', function() {
var menuToggle = document.querySelector("#dxpr-theme-menu-toggle");

if (menuToggle) {
var handleMenuToggle = function() {
if (drupalSettings.dxpr_themeSettings.hamburgerAnimation === "cross") {
menuToggle.classList.toggle("navbar-toggle--active");
}
document.querySelector("#dxpr-theme-main-menu").classList.toggle("menu--open");
document.querySelector("html").classList.toggle("html--dxpr-theme-nav-mobile--open");
};

menuToggle.addEventListener("click", handleMenuToggle);
}
});

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

6.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mattyg77
  • Hello @mattyg77,

    I tried reproducing the issue by using : 'composer require 'drupal/dxpr_theme:^6.0@RC'' but I think the issue has been resolved in 6.0.0-rc2.
    As when I tried reproducing the issue using the above command, the version 6.0.0-rc2 was installed automatically and the issue was not found in that version.

    Please have a look on this, attaching a screenshot for the same.

  • Hi anish.ir,

    I updated to rc2 and took the same steps and it is still happening. I am not using any custom javascript.

    Here is an error in Dev Console:

    Uncaught TypeError: Cannot read properties of null (reading 'style')
    at js_xUvbOUb1aoazaSn0-…r6S9ceFLT:228:10232
    at js_xUvbOUb1aoazaSn0-…r6S9ceFLT:228:11717

    Gemini AI assitant

    The error "Uncaught TypeError: Cannot read properties of null (reading 'style')" means your JavaScript code is trying to access the style property of an element that doesn't exist (is null). In your provided code snippet, this happens in several places:

    document.querySelector(".dxpr-theme-boxed-container").style.overflow="hidden";
    This line attempts to find an element with the class dxpr-theme-boxed-container and then modify its overflow style. If no element with this class exists in the DOM when this code runs, document.querySelector() returns null. Trying to access .style of null causes the error.
    document.querySelector("#toolbar-bar").classList.add("header-mobile-fixed");
    Similar to the previous point, this line searches for an element with the ID toolbar-bar. If the element isn't found, document.querySelector() returns null, and attempting to use classList on null throws the error.
    var v=document.querySelector("#secondary-header"); v&&(v.style.marginTop="".concat(s,"px"))
    This line searches for an element with the ID secondary-header. If it doesn't exist, v will be null. The && operator short-circuits, so if v is null, the code after && is not executed, preventing an error in this specific line. However, if s is not defined correctly elsewhere in your code, attempting to concatenate with it could lead to separate errors in other parts of the code which may not be evident in the provided snippet.
    var y=document.querySelector("#navbar"); (and subsequent uses of y)
    If an element with the ID navbar is not present in the DOM when this line executes, y will be null. Any later attempt to use y.classList.add(...) or other properties will result in the error.
    How to Fix It:

    Check Element Existence: Before manipulating any element's style or class, always check if the element exists. Use optional chaining (?.) or a conditional check:

    const boxedContainer = document.querySelector(".dxpr-theme-boxed-container");
    if (boxedContainer) {
    boxedContainer.style.overflow = "hidden";
    } else {
    console.warn("Element with class 'dxpr-theme-boxed-container' not found.");
    // Handle the case where the element is missing – maybe add a default behavior or log a more detailed error
    }

    const toolbarBar = document.querySelector("#toolbar-bar");
    if (toolbarBar) {
    toolbarBar.classList.add("header-mobile-fixed");
    } else {
    console.warn("Element with id 'toolbar-bar' not found.");
    }

    // ... similar checks for #secondary-header and #navbar ...

Production build 0.71.5 2024