Skip to content

Commit

Permalink
fix(UsaModal): delay visibility until component is mounted for ssr su…
Browse files Browse the repository at this point in the history
…pport
  • Loading branch information
patrickcate committed Feb 19, 2024
1 parent 7463111 commit 399093c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/components/UsaModal/UsaModal.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup>
import { computed, watch, ref, useSlots, onBeforeUnmount } from 'vue'
import { computed, watch, ref, useSlots, onMounted, onBeforeUnmount } from 'vue'
import { onKeyStroke, onClickOutside } from '@vueuse/core'
import { UseFocusTrap } from '@vueuse/integrations/useFocusTrap/component'
import { nextId } from '@/utils/unique-id.js'
Expand Down Expand Up @@ -72,9 +72,10 @@ const props = defineProps({
})
const modal = ref(null)
const isMounted = ref(false)
const isVisible = computed({
get: () => props.visible,
get: () => isMounted.value && props.visible,
set: currentlyVisibility => emit('update:visible', currentlyVisibility),
})
const classes = computed(() => {
Expand All @@ -94,6 +95,10 @@ const focusTrapClass = 'js-focus-trap-wrapper'
watch(
() => isVisible,
currentlyVisible => {
if (!isMounted.value) {
return
}
if (currentlyVisible.value) {
document.body.classList.add(modalBodyClass)
Expand Down Expand Up @@ -122,6 +127,10 @@ watch(
}
)
onMounted(() => {
isMounted.value = true
})
onBeforeUnmount(() => {
document.body.classList.remove(modalBodyClass)
Expand All @@ -148,7 +157,7 @@ onClickOutside(modal, () => {
</script>
<template>
<teleport to="body">
<Teleport to="body" :disabled="!isVisible">
<UseFocusTrap
v-if="isVisible"
:class="[`${focusTrapClass}`, customClasses?.focusTrap]"
Expand Down Expand Up @@ -212,5 +221,5 @@ onClickOutside(modal, () => {
</div>
</div>
</UseFocusTrap>
</teleport>
</Teleport>
</template>
3 changes: 2 additions & 1 deletion src/utils/unique-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export function nextId(componentName = '') {
const vm = getCurrentInstance()
const route = vm.appContext.config.globalProperties?.$route

const pathPrefix = route?.path ? kebabCase(route.path) : 'global'
const pathPrefix =
route?.path && kebabCase(route.path) ? kebabCase(route.path) : 'global'

if (!idRegistry[pathPrefix]) {
idRegistry[pathPrefix] = 0
Expand Down

0 comments on commit 399093c

Please sign in to comment.