Skip to content

Commit

Permalink
Disable scroll on menu iOS compatible solution
Browse files Browse the repository at this point in the history
relates #114
  • Loading branch information
maxgerber committed Feb 2, 2019
1 parent 6825af1 commit 9e42d7d
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/state/reducers.js
Expand Up @@ -3,24 +3,48 @@ import { TOGGLE_MENU } from './actions'
const menuReducer = (state, { type, payload }) => {
switch (type) {
case TOGGLE_MENU:
document.body.style.overflowY === 'hidden'
? (document.body.style.overflowY = 'visible')
: (document.body.style.overflowY = 'hidden')
return toggleMenu(state, payload)
default:
return state
}
}

const handleTouchEvent = e => {
e.preventDefault()
}

const disableScroll = () => {
document.body.style.overflowY = 'hidden'
return document.addEventListener('touchmove', handleTouchEvent, {
passive: false,
})
}

const enableScroll = () => {
document.body.style.overflowY = 'visible'
return document.removeEventListener('touchmove', handleTouchEvent, {
passive: false,
})
}

const toggleMenu = (state, payload) => {
if (payload && payload.pageChange) return { ...state, menuIsOpen: 'INIT' }
switch (true) {
case payload && payload.pageChange:
enableScroll()
return { ...state, menuIsOpen: 'INIT' }

case state.menuIsOpen === 'INIT':
disableScroll()
return { ...state, menuIsOpen: 'OPENED' }

const newMenuState =
state.menuIsOpen === 'INIT' || state.menuIsOpen === 'CLOSED'
? 'OPENED'
: 'CLOSED'
case state.menuIsOpen === 'CLOSED':
disableScroll()
return { ...state, menuIsOpen: 'OPENED' }

return { ...state, menuIsOpen: newMenuState }
default:
enableScroll()
return { ...state, menuIsOpen: 'CLOSED' }
}
}

export default menuReducer
export default menuReducer

0 comments on commit 9e42d7d

Please sign in to comment.