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

fix: fixed zIndex calculation logic #335

Merged
merged 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 18 additions & 2 deletions packages/vue-final-modal/src/components/CoreModal/CoreModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
const vfmRootEl = ref<HTMLDivElement>()

const { focus, focusLast, blur } = useFocusTrap(props, { focusEl: vfmRootEl, openedModals })
const { zIndex, refreshZIndex, resetZIndex } = useZIndex(props)
const { enableBodyScroll, disableBodyScroll } = useLockScroll(props, { lockScrollEl: vfmRootEl })
const { modelValueLocal } = useModelValue(props, emit)
const { emitEvent } = useEvent(emit)
Expand Down Expand Up @@ -83,6 +84,8 @@ const {
blur()
},
onLeave() {
deleteFromOpenedModals(getModalInstance())
resetZIndex()
emitEvent('closed')
resolveToggle('closed')
},
Expand Down Expand Up @@ -114,7 +117,20 @@ const modalInstance = computed<Modal>(() => ({
},
}))

const { zIndex } = useZIndex(props, { openedModals, modalInstance, visible })
function getModalInstance() {
return modalInstance
}

const index = computed(() => openedModals.indexOf(modalInstance))

function getIndex() {
return index.value
}

watch(() => [props.zIndexFn, index.value], () => {
if (visible.value)
refreshZIndex(getIndex())
hunterliu1003 marked this conversation as resolved.
Show resolved Hide resolved
})

onMounted(() => {
modals.push(modalInstance)
Expand All @@ -130,14 +146,14 @@ watch(modelValueLocal, (value) => {
async function open() {
emitEvent('beforeOpen')
moveToLastOpenedModals(modalInstance)
refreshZIndex(getIndex())
hunterliu1003 marked this conversation as resolved.
Show resolved Hide resolved
openLastOverlay()
enterTransition()
}

function close() {
emitEvent('beforeClose')
enableBodyScroll()
deleteFromOpenedModals(modalInstance)
focusLast()
openLastOverlay()
leaveTransition()
Expand Down
26 changes: 8 additions & 18 deletions packages/vue-final-modal/src/components/CoreModal/useZIndex.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import type { ComputedRef, Ref } from 'vue'
import { computed, ref, watch } from 'vue'
import { ref } from 'vue'
import type CoreModal from './CoreModal.vue'
import type { Modal } from '~/Modal'

export function useZIndex(
props: InstanceType<typeof CoreModal>['$props'],
options: {
openedModals: ComputedRef<Modal>[]
modalInstance: ComputedRef<Modal>
visible: Ref<boolean>
},
) {
const { openedModals, modalInstance, visible } = options

const zIndex = ref<undefined | number>()

const index = computed(() => openedModals.indexOf(modalInstance))

function refreshZIndex() {
zIndex.value = props.zIndexFn?.({ index: index.value })
function refreshZIndex(index: number) {
zIndex.value = props.zIndexFn?.({ index: index <= -1 ? 0 : index })
}

watch(() => [props.zIndexFn, index.value], () => {
if (visible.value)
refreshZIndex()
})
function resetZIndex() {
zIndex.value = undefined
}

return {
zIndex,
refreshZIndex,
resetZIndex,
}
}