feat: use vueuse to lock scrolling
This commit is contained in:
parent
574ecff12d
commit
f9b7e2fd76
1 changed files with 7 additions and 19 deletions
|
@ -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,
|
||||
|
@ -78,32 +80,18 @@ const props = withDefaults(defineProps<{
|
|||
|
||||
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>
|
||||
|
|
Loading…
Reference in a new issue