Skip to content
This repository has been archived by the owner on Jan 9, 2022. It is now read-only.

Commit

Permalink
fix: fix disabling scrolling on mobile devices
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGitArt committed Aug 21, 2021
1 parent fc28a29 commit 57d92f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/composable/scroll.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Ref } from 'vue'

import { noScrollableParent } from '../helper/scroll.helper'
import { noScrollableParent, getScrollbarWidth } from '../helper/scroll.helper'

type ScrollParams = {
overlay: Ref<Element | undefined>
Expand All @@ -12,6 +12,7 @@ export const useScroll = ({
content,
}: ScrollParams) => {
let disabled = false
let disableType: 'byEvents' | 'byOverflow' = null

const eventListener = (event: WheelEvent) => {
if(event.target === overlay.value
Expand All @@ -27,9 +28,19 @@ export const useScroll = ({
return
}

window.addEventListener('wheel', eventListener, {
passive: false,
})
const scrollbarWidth = getScrollbarWidth()

// The mobile has a scroll bar width of 0
if (scrollbarWidth === 0) {
disableType = 'byOverflow'
document.documentElement.classList.add('overflow-y-hidden')
} else {
disableType = 'byEvents'
window.addEventListener('wheel', eventListener, {
passive: false,
})
}

disabled = true
}

Expand All @@ -38,7 +49,12 @@ export const useScroll = ({
return
}

window.removeEventListener('wheel', eventListener)
if(disableType === 'byEvents') {
window.removeEventListener('wheel', eventListener)
}else if (disableType === 'byOverflow') {
document.documentElement.classList.remove('overflow-y-hidden')
}

disabled = false
}

Expand Down
14 changes: 14 additions & 0 deletions src/helper/scroll.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ export const noScrollableParent = (event: WheelEvent, content: Element | undefin

return true
}

export const getScrollbarWidth = () => {
const container = document.createElement('div')
container.style.visibility = 'hidden'
container.style.overflow = 'scroll'
const inner = document.createElement('div')

container.appendChild(inner)
document.body.appendChild(container)
const scrollbarWidth = container.offsetWidth - inner.offsetWidth
document.body.removeChild(container)

return scrollbarWidth
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './scss/main.scss'

// standalone component
export { default as GDialog } from './components/GDialog.vue'

Expand Down
3 changes: 3 additions & 0 deletions src/scss/main.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html.overflow-y-hidden {
overflow-y: hidden;
}

0 comments on commit 57d92f2

Please sign in to comment.