Topic: A mobile navigation overlay remains active after resizing to desktop

lunaScript49 priority asked 1 week ago


When the mobile menu is open and I widen the browser, the desktop navigation appears correctly but our overlay remains above the page. We also keep a body scroll-lock class while the mobile panel is open.

The desktop layout is controlled by CSS, whereas the overlay and scroll lock depend on a React boolean. I can reproduce it by opening the menu in a narrow window and then dragging the window wider without selecting a link.


Mateusz Lazaru staff answered 6 days ago


CSS can change the navigation layout without changing the React state that controls your custom overlay. Close that mobile state when the desktop media query becomes active, using the same breakpoint as the layout. The overlay and scroll-lock effect should then respond to that state change.

Remove the media-query listener in its cleanup, and ensure the scroll-lock effect also removes its class on unmount. Please share those two effects if the overlay disappears but the page still cannot scroll.

useEffect(() => {
  const desktop = window.matchMedia('(min-width: 992px)');
  const sync = () => { if (desktop.matches) setOpen(false); };
  sync();
  desktop.addEventListener('change', sync);
  return () => desktop.removeEventListener('change', sync);
}, []);

https://mdbootstrap.com/docs/react/navigation/navbar/



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: Desktop
  • Browser: Edge
  • OS: Windows 11
  • Provided sample code: No
  • Provided link: No