Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created October 10, 2019 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/24ae29a059c96460371d70b97cee3c2d to your computer and use it in GitHub Desktop.
Save whoisryosuke/24ae29a059c96460371d70b97cee3c2d to your computer and use it in GitHub Desktop.
React / JS - Mobile sidebar - detects if window size is mobile (600px) and tracks sidebar visibility independently
import React, { useState, useEffect } from 'react'
import Header from './Header'
import debounce from '../../helpers/debounce'
const MobileHeader = () => {
const [isMobile, setMobile] = useState(false)
const [isVisible, setVisibility] = useState(false)
const resize = () => {
let currentHideNav = window.innerWidth <= 600
// Are we mobile?
setMobile(currentHideNav)
}
const toggleVisibility = () => {
setVisibility(!isVisible)
}
useEffect(() => {
window.addEventListener("resize", debounce(resize, 250))
resize()
})
return (
<Header mobile={isMobile} visible={isVisible} toggleVisibility={toggleVisibility} />
)
}
export default MobileHeader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment