Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(ui-components): Scroll jumps to top on Dialog open #2250

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions packages/ui-components/src/components/layout/Dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
leave-to="opacity-0"
>
<div
class="fixed inset-0 bg-neutral-100/70 dark:bg-neutral-900/70 transition-opacity backdrop-blur-xs"
class="fixed top-0 left-0 w-full h-full bg-neutral-100/70 dark:bg-neutral-900/70 transition-opacity backdrop-blur-xs"
/>
</TransitionChild>

<div class="fixed inset-0 z-10 h-[100dvh] w-screen">
<div class="fixed top-0 left-0 z-10 h-screen !h-[100dvh] w-screen">
<div class="flex justify-center items-center h-full w-full p-4 sm:p-0">
<TransitionChild
as="template"
Expand Down Expand Up @@ -94,8 +93,9 @@
import { Dialog, DialogPanel, TransitionChild, TransitionRoot } from '@headlessui/vue'
import { FormButton } from '~~/src/lib'
import { XMarkIcon } from '@heroicons/vue/24/outline'
import { computed, ref, useSlots } from 'vue'
import { computed, ref, useSlots, watch, onUnmounted } from 'vue'
import { throttle, noop } from 'lodash'
import { isClient } from '@vueuse/core'

type MaxWidthValue = 'sm' | 'md' | 'lg' | 'xl'

Expand Down Expand Up @@ -189,4 +189,33 @@ const onScroll = throttle((e: Event) => {
scrolledFromTop.value = scrollTop > 0
scrolledToBottom.value = scrollTop + offsetHeight >= scrollHeight
}, 60)

// Toggle 'dialog-open' class on <html> to prevent scroll jumping and disable background scroll.
// This maintains user scroll position when Headless UI dialogs are activated.
watch(open, (newValue) => {
if (isClient) {
const html = document.documentElement
if (newValue) {
html.classList.add('dialog-open')
} else {
html.classList.remove('dialog-open')
}
}
})

// Clean up when the component unmounts
onUnmounted(() => {
if (isClient) {
document.documentElement.classList.remove('dialog-open')
}
})
</script>

<style>
html.dialog-open {
overflow: visible !important;
}
html.dialog-open body {
overflow: hidden !important;
}
</style>