shuding / nextra

Simple, powerful and flexible site generation framework with everything you love from Next.js.
https://nextra.site
MIT License
11.95k stars 1.29k forks source link

BUG: overflow-hidden not removed when resizing - making content unscrollable #482

Closed D3Portillo closed 1 month ago

D3Portillo commented 2 years ago

Preview

https://user-images.githubusercontent.com/26236985/173242429-b998a677-d2ce-4f91-b86b-714d7f888ccb.mp4

How to simulate

A Dirty Workaround

// _app.js
const ON_RESIZE = "resize"
useEffect(() => {
  function closeMobileMenu() {
    const navButton = document.querySelector("nav button.md\\:hidden")
    const isShowingMenu = document.body.classList.contains("overflow-hidden")
    if (isShowingMenu && navButton) {
      navButton.click()
    }
  }
  window.addEventListener(ON_RESIZE, closeMobileMenu)
  return () => {
    window.removeEventListener(ON_RESIZE, closeMobileMenu)
  }
}, [])
  1. Basically triggers click event over the burger button whenever a resize event occurs
  2. Did try just removing "overflow-hidden" in body classList but didn't function
  3. I'd like to work on a solution if you consider it's an issue and of course if no-one workin on it :)
D3Portillo commented 2 years ago

UPDATE The hotfix won't work when virtual keyboard pops in mobiles. To fix this I've just trigger the click event when innerWidth changes at resize

// _app.js
const ON_RESIZE = "resize"
useEffect(() => {
  let storedWidth = window.innerWidth
  function closeMobileMenu() {
    const newWidth = window.innerWidth
    const navButton = document.querySelector("nav button.md\\:hidden")
    const isResizeXEvent = storedWidth !== newWidth
    const isShowingMenu = document.body.classList.contains("overflow-hidden")
    if (isShowingMenu && navButton && isResizeXEvent) {
      navButton.click()
    }
    storedWidth = newWidth
  }
  window.addEventListener(ON_RESIZE, closeMobileMenu)
  return () => {
    window.removeEventListener(ON_RESIZE, closeMobileMenu)
  }
}, [])

Hope someone finds it useful

dimaMachina commented 1 month ago

closing due age, I think it was fixed in Nextra v2 or v3, try to upgrade