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 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
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
dist
node_modules
coverage
.cache
deprecated
33 changes: 0 additions & 33 deletions .github/workflows/coveralls.yml

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"dev:vfm": "pnpm --filter vue-final-modal dev",
"build:vfm": "pnpm --filter vue-final-modal build",
"test:vfm": "pnpm --filter vue-final-modal cypress:run",
"coverage:vfm": "pnpm --filter vue-final-modal coverage",
"release:vfm": "pnpm --filter vue-final-modal release",
"lint": "eslint . --ext=.ts,.vue --fix",
"typecheck": "pnpm --parallel typecheck",
Expand Down
16 changes: 14 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,16 @@ const modalInstance = computed<Modal>(() => ({
},
}))

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

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

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

onMounted(() => {
modals.push(modalInstance)
Expand All @@ -130,14 +142,14 @@ watch(modelValueLocal, (value) => {
async function open() {
emitEvent('beforeOpen')
moveToLastOpenedModals(modalInstance)
refreshZIndex(index.value)
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,
}
}