feat: use vueuse to lock scrolling
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
kolaente 2022-03-22 22:48:32 +01:00
parent c466b83bca
commit f92aeed540
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 7 additions and 19 deletions

View File

@ -8,6 +8,7 @@
{ 'has-overflow': overflow },
variant,
]"
ref="modal"
>
<div
class="modal-container"
@ -62,7 +63,8 @@
<script lang="ts" setup>
import BaseButton from '@/components/base/BaseButton.vue'
import {onUnmounted, watch} from 'vue'
import {ref, watch} from 'vue'
import {useScrollLock} from '@vueuse/core'
const props = withDefaults(defineProps<{
enabled?: boolean,
@ -76,34 +78,20 @@ const props = withDefaults(defineProps<{
variant: 'default',
})
defineEmits(['close', 'submit'])
defineEmits(['close', 'submit'])
// Based on https://css-tricks.com/prevent-page-scrolling-when-a-modal-is-open/
function resetScrolling() {
const body = document.body
const scrollY = body.style.top
body.style.position = ''
body.style.top = ''
window.scrollTo(0, parseInt(scrollY || '0') * -1)
}
const modal = ref<HTMLElement | null>(null)
const scrollLock = useScrollLock(modal)
watch(
() => props.enabled,
enabled => {
if (enabled) {
const scrollY = window.scrollY
document.body.style.position = 'fixed'
document.body.style.top = `-${scrollY}px`
} else {
resetScrolling()
}
scrollLock.value = enabled
},
{
immediate: true,
},
)
onUnmounted(resetScrolling)
</script>
<style lang="scss" scoped>