2022-12-02 10:02:44 +03:00
|
|
|
<script lang="ts" setup>
|
2022-12-16 22:55:31 +03:00
|
|
|
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
|
2022-11-27 04:52:46 +03:00
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
export interface Props {
|
|
|
|
/**
|
|
|
|
* level of depth
|
|
|
|
*
|
|
|
|
* @default 100
|
|
|
|
*/
|
|
|
|
zIndex?: number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* whether to allow close dialog by clicking mask layer
|
|
|
|
*
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
closeByMask?: boolean
|
|
|
|
|
|
|
|
/**
|
|
|
|
* use v-if, destroy all the internal elements after closed
|
|
|
|
*
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
useVIf?: boolean
|
|
|
|
|
|
|
|
/**
|
|
|
|
* keep the dialog opened even when in other views
|
|
|
|
*
|
|
|
|
* @default false
|
2022-12-02 10:27:44 +03:00
|
|
|
*/
|
2022-12-02 10:02:44 +03:00
|
|
|
keepAlive?: boolean
|
2022-12-16 22:55:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The aria-labelledby id for the dialog.
|
|
|
|
*/
|
|
|
|
dialogLabelledBy?: string
|
2022-12-02 10:02:44 +03:00
|
|
|
}
|
2022-12-02 10:27:44 +03:00
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
zIndex: 100,
|
|
|
|
closeByMask: true,
|
|
|
|
useVIf: true,
|
|
|
|
keepAlive: false,
|
|
|
|
})
|
2022-11-27 04:52:46 +03:00
|
|
|
|
2023-01-01 18:57:49 +03:00
|
|
|
const emit = defineEmits<{
|
2022-12-16 22:55:31 +03:00
|
|
|
/** v-model dialog visibility */
|
2023-01-01 18:57:49 +03:00
|
|
|
(event: 'close',): void
|
2022-11-23 06:48:01 +03:00
|
|
|
}>()
|
2022-11-24 11:04:53 +03:00
|
|
|
|
2023-04-12 15:35:35 +03:00
|
|
|
const { modelValue: visible } = defineModels<{
|
2023-01-06 18:46:36 +03:00
|
|
|
/** v-model dislog visibility */
|
|
|
|
modelValue: boolean
|
|
|
|
}>()
|
2022-11-27 04:52:46 +03:00
|
|
|
|
2023-01-15 09:06:12 +03:00
|
|
|
defineOptions({
|
|
|
|
inheritAttrs: false,
|
|
|
|
})
|
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
const deactivated = useDeactivated()
|
|
|
|
const route = useRoute()
|
2022-11-27 04:52:46 +03:00
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
/** scrollable HTML element */
|
|
|
|
const elDialogMain = ref<HTMLDivElement>()
|
|
|
|
const elDialogRoot = ref<HTMLDivElement>()
|
2022-11-27 04:52:46 +03:00
|
|
|
|
2022-12-16 22:55:31 +03:00
|
|
|
const { activate } = useFocusTrap(elDialogRoot, {
|
2022-12-21 17:41:27 +03:00
|
|
|
immediate: false,
|
2022-12-16 22:55:31 +03:00
|
|
|
allowOutsideClick: true,
|
|
|
|
clickOutsideDeactivates: true,
|
|
|
|
escapeDeactivates: true,
|
2023-01-03 16:02:54 +03:00
|
|
|
preventScroll: true,
|
|
|
|
returnFocusOnDeactivate: true,
|
2022-12-16 22:55:31 +03:00
|
|
|
})
|
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
defineExpose({
|
|
|
|
elDialogRoot,
|
|
|
|
elDialogMain,
|
|
|
|
})
|
|
|
|
|
|
|
|
/** close the dialog */
|
2022-11-27 06:13:39 +03:00
|
|
|
function close() {
|
2022-12-02 10:02:44 +03:00
|
|
|
visible.value = false
|
2023-01-01 18:57:49 +03:00
|
|
|
emit('close')
|
2022-12-02 10:02:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function clickMask() {
|
|
|
|
if (props.closeByMask)
|
|
|
|
close()
|
2022-11-27 06:13:39 +03:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
const routePath = ref(route.path)
|
|
|
|
watch(visible, (value) => {
|
|
|
|
if (value)
|
|
|
|
routePath.value = route.path
|
|
|
|
})
|
|
|
|
|
|
|
|
const notInCurrentPage = computed(() => deactivated.value || routePath.value !== route.path)
|
|
|
|
watch(notInCurrentPage, (value) => {
|
|
|
|
if (props.keepAlive)
|
|
|
|
return
|
|
|
|
if (value)
|
|
|
|
close()
|
|
|
|
})
|
|
|
|
|
|
|
|
// controls the state of v-if.
|
|
|
|
// when useVIf is toggled, v-if has the same state as modelValue, otherwise v-if is true
|
|
|
|
const isVIf = computed(() => {
|
|
|
|
return props.useVIf
|
|
|
|
? visible.value
|
|
|
|
: true
|
|
|
|
})
|
|
|
|
|
|
|
|
// controls the state of v-show.
|
|
|
|
// when useVIf is toggled, v-show is true, otherwise it has the same state as modelValue
|
|
|
|
const isVShow = computed(() => {
|
|
|
|
return !props.useVIf
|
|
|
|
? visible.value
|
|
|
|
: true
|
|
|
|
})
|
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
function bindTypeToAny($attrs: any) {
|
|
|
|
return $attrs as any
|
|
|
|
}
|
2022-12-02 10:02:44 +03:00
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
function trapFocusDialog() {
|
2022-12-16 22:55:31 +03:00
|
|
|
if (isVShow.value)
|
|
|
|
nextTick().then(() => activate())
|
|
|
|
}
|
|
|
|
|
2022-11-27 06:13:39 +03:00
|
|
|
useEventListener('keydown', (e: KeyboardEvent) => {
|
2022-12-02 10:02:44 +03:00
|
|
|
if (!visible.value)
|
2022-11-27 06:13:39 +03:00
|
|
|
return
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
close()
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
2022-11-24 11:04:53 +03:00
|
|
|
})
|
2022-12-02 10:02:44 +03:00
|
|
|
</script>
|
2022-11-27 06:13:39 +03:00
|
|
|
|
2022-11-23 06:48:01 +03:00
|
|
|
<template>
|
2022-12-21 17:41:27 +03:00
|
|
|
<Teleport to="body">
|
2022-12-02 10:02:44 +03:00
|
|
|
<!-- Dialog component -->
|
2022-12-21 17:41:27 +03:00
|
|
|
<Transition name="dialog-visible" @transitionend="trapFocusDialog">
|
2022-11-27 15:29:10 +03:00
|
|
|
<div
|
2022-12-02 10:02:44 +03:00
|
|
|
v-if="isVIf"
|
|
|
|
v-show="isVShow"
|
|
|
|
ref="elDialogRoot"
|
2022-12-16 22:55:31 +03:00
|
|
|
aria-modal="true"
|
|
|
|
:aria-labelledby="dialogLabelledBy"
|
2022-12-02 10:02:44 +03:00
|
|
|
:style="{
|
|
|
|
'z-index': zIndex,
|
|
|
|
}"
|
2022-12-05 16:20:23 +03:00
|
|
|
fixed inset-0 of-y-auto scrollbar-hide overscroll-none
|
2022-11-27 15:29:10 +03:00
|
|
|
>
|
2022-12-02 10:02:44 +03:00
|
|
|
<!-- The style `scrollbar-hide overscroll-none overflow-y-scroll` and `h="[calc(100%+0.5px)]"` is used to implement scroll locking, -->
|
|
|
|
<!-- corresponding to issue: #106, so please don't remove it. -->
|
|
|
|
|
|
|
|
<!-- Mask layer: blur -->
|
|
|
|
<div class="dialog-mask" absolute inset-0 z-0 bg-transparent opacity-100 backdrop-filter backdrop-blur-sm touch-none />
|
|
|
|
<!-- Mask layer: dimming -->
|
|
|
|
<div class="dialog-mask" absolute inset-0 z-0 bg-black opacity-48 touch-none h="[calc(100%+0.5px)]" @click="clickMask" />
|
|
|
|
<!-- Dialog container -->
|
|
|
|
<div class="p-safe-area" absolute inset-0 z-1 pointer-events-none opacity-100 flex>
|
2022-12-04 09:27:08 +03:00
|
|
|
<div flex-1 flex items-center justify-center p-4>
|
2022-12-02 11:06:52 +03:00
|
|
|
<!-- We use `class` here to make v-bind being able to be override them -->
|
2022-12-02 10:02:44 +03:00
|
|
|
<div
|
|
|
|
ref="elDialogMain"
|
2022-12-14 12:10:42 +03:00
|
|
|
class="dialog-main rounded shadow-lg pointer-events-auto isolate bg-base border-base border-1px border-solid w-full max-h-full of-y-auto overscroll-contain touch-pan-y touch-pan-x"
|
2022-12-02 10:02:44 +03:00
|
|
|
v-bind="bindTypeToAny($attrs)"
|
|
|
|
>
|
2022-12-04 09:27:08 +03:00
|
|
|
<slot />
|
2022-12-02 10:02:44 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-27 15:29:10 +03:00
|
|
|
</div>
|
2022-12-02 10:02:44 +03:00
|
|
|
</Transition>
|
2022-12-17 19:55:29 +03:00
|
|
|
</Teleport>
|
2022-11-23 06:48:01 +03:00
|
|
|
</template>
|
2022-11-27 11:02:09 +03:00
|
|
|
|
2022-12-02 10:02:44 +03:00
|
|
|
<style lang="postcss" scoped>
|
|
|
|
.dialog-visible-enter-active,
|
|
|
|
.dialog-visible-leave-active {
|
|
|
|
transition-duration: 0.25s;
|
|
|
|
|
|
|
|
.dialog-mask {
|
|
|
|
transition: opacity 0.25s ease;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dialog-main {
|
|
|
|
transition: opacity 0.25s ease, transform 0.25s ease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.dialog-visible-enter-from,
|
|
|
|
.dialog-visible-leave-to {
|
|
|
|
.dialog-mask {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dialog-main {
|
|
|
|
transform: translateY(50px);
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-safe-area {
|
|
|
|
padding-top: env(safe-area-inset-top);
|
|
|
|
padding-right: env(safe-area-inset-right);
|
|
|
|
padding-bottom: env(safe-area-inset-bottom);
|
|
|
|
padding-left: env(safe-area-inset-left);
|
|
|
|
}
|
2022-11-27 11:02:09 +03:00
|
|
|
</style>
|