192 lines
6.4 KiB
Vue
192 lines
6.4 KiB
Vue
<script setup lang="ts">
|
||
import { NModal, NScrollbar } from 'naive-ui'
|
||
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
||
|
||
/** 公共弹窗的标题、说明、操作状态和宽度。 */
|
||
withDefaults(
|
||
defineProps<{
|
||
/** 弹窗标题,同时作为可访问名称。 */
|
||
title: string
|
||
/** 标题下的默认说明,可使用 description slot 覆盖。 */
|
||
description?: string
|
||
/** 提交期间锁定关闭操作。 */
|
||
busy?: boolean
|
||
/** 使用宽弹窗布局。 */
|
||
wide?: boolean
|
||
/** 指定弹窗宽度,单位为像素;实际宽度仍受视口限制。 */
|
||
width?: number
|
||
}>(),
|
||
{ description: '', busy: false, wide: false }
|
||
)
|
||
/** 弹窗显隐状态,关闭行为由 Naive 管理。 */
|
||
const open = defineModel<boolean>('open', { default: false })
|
||
/** 正文容器隔离子元素外边距,保留完整的自然内容高度。 */
|
||
const bodyContent = ref<HTMLElement | null>(null)
|
||
/** 正文最大可用高度;未挂载时不限制自然高度。 */
|
||
const maxBodyHeight = ref<number | undefined>(undefined)
|
||
/** 监听正文和固定区域的尺寸变化,适配异步内容与换行。 */
|
||
let layoutObserver: ResizeObserver | undefined
|
||
|
||
/** 扣除视口上下间距、标题、底栏、正文内边距和边框,只限制正文最大高度。 */
|
||
function updateBodyHeight() {
|
||
const card = bodyContent.value?.closest('.app-dialog')
|
||
if (!card) return
|
||
const header = card.querySelector<HTMLElement>('.n-card-header')
|
||
const footer = card.querySelector<HTMLElement>('.n-card__footer')
|
||
const content = card.querySelector<HTMLElement>('.n-card-content')
|
||
const contentStyle = content ? getComputedStyle(content) : null
|
||
const cardStyle = getComputedStyle(card)
|
||
maxBodyHeight.value = Math.max(
|
||
0,
|
||
(window.visualViewport?.height ?? window.innerHeight) -
|
||
32 -
|
||
(header?.offsetHeight ?? 0) -
|
||
(footer?.offsetHeight ?? 0) -
|
||
(contentStyle ? parseFloat(contentStyle.paddingTop) + parseFloat(contentStyle.paddingBottom) : 0) -
|
||
parseFloat(cardStyle.borderTopWidth) -
|
||
parseFloat(cardStyle.borderBottomWidth)
|
||
)
|
||
}
|
||
watch(
|
||
[open, bodyContent],
|
||
([shown]) => {
|
||
layoutObserver?.disconnect()
|
||
if (!shown) {
|
||
maxBodyHeight.value = undefined
|
||
return
|
||
}
|
||
updateBodyHeight()
|
||
const card = bodyContent.value?.closest('.app-dialog')
|
||
if (!card || !bodyContent.value) return
|
||
layoutObserver = new ResizeObserver(updateBodyHeight)
|
||
const header = card.querySelector('.n-card-header')
|
||
const footer = card.querySelector('.n-card__footer')
|
||
if (header) layoutObserver.observe(header)
|
||
if (footer) layoutObserver.observe(footer)
|
||
layoutObserver.observe(bodyContent.value)
|
||
},
|
||
{ flush: 'post', immediate: true }
|
||
)
|
||
onMounted(() => {
|
||
window.addEventListener('resize', updateBodyHeight)
|
||
window.visualViewport?.addEventListener('resize', updateBodyHeight)
|
||
})
|
||
onBeforeUnmount(() => {
|
||
layoutObserver?.disconnect()
|
||
window.removeEventListener('resize', updateBodyHeight)
|
||
window.visualViewport?.removeEventListener('resize', updateBodyHeight)
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<slot name="trigger" />
|
||
<!-- 以下是关闭打开时自动聚焦的公共弹窗模块 -->
|
||
<NModal
|
||
v-model:show="open"
|
||
preset="card"
|
||
:auto-focus="false"
|
||
:closable="!busy"
|
||
:mask-closable="!busy"
|
||
:close-on-esc="!busy"
|
||
:style="{ width: `${width ?? (wide ? 1040 : 600)}px` }"
|
||
class="app-dialog"
|
||
role="dialog"
|
||
:aria-label="title"
|
||
>
|
||
<!-- 以下是固定标题与说明模块,说明位于标题下并可自定义 -->
|
||
<template #header>
|
||
<div class="min-w-0">
|
||
<div class="text-lg font-semibold">{{ title }}</div>
|
||
<div v-if="description || $slots.description" class="mt-[2px] text-sm leading-6 font-normal text-muted">
|
||
<slot name="description">{{ description }}</slot>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
<!-- 以下是自然高度正文模块,仅超过剩余视口高度时滚动 -->
|
||
<NScrollbar
|
||
class="dialog-body-scroll"
|
||
:style="{ height: 'auto', maxHeight: maxBodyHeight === undefined ? undefined : maxBodyHeight + 'px' }"
|
||
>
|
||
<div ref="bodyContent" class="flow-root"><slot /></div>
|
||
</NScrollbar>
|
||
<template v-if="$slots.footer" #footer>
|
||
<div class="dialog-footer"><slot name="footer" /></div>
|
||
</template>
|
||
</NModal>
|
||
</template>
|
||
|
||
<style>
|
||
/* 本组件专属布局与 Naive 内部结构覆盖。 */
|
||
@reference "../../styles/styles.css";
|
||
|
||
.app-dialog {
|
||
--app-field: var(--app-control);
|
||
--app-field-hover: var(--app-control-hover);
|
||
@apply max-w-[calc(100vw-32px)] max-h-[calc(100dvh-32px)] overflow-hidden;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
|
||
.app-dialog .n-card-header {
|
||
@apply shrink-0 items-start;
|
||
}
|
||
/* 正文自然撑开,超过剩余视口高度后由 NScrollbar 内部滚动。 */
|
||
.app-dialog .n-card-content {
|
||
@apply min-h-0 overflow-hidden;
|
||
display: block;
|
||
}
|
||
|
||
.dialog-body-scroll.n-scrollbar {
|
||
@apply min-h-0;
|
||
}
|
||
|
||
.app-dialog .n-card-header__close {
|
||
@apply grid place-items-center w-10 h-10 -m-2 rounded-none bg-transparent;
|
||
}
|
||
|
||
.app-dialog .n-card-header__close .n-base-icon {
|
||
@apply p-2 box-content bg-(--app-subtle);
|
||
}
|
||
|
||
.app-dialog .n-card-header__close:hover {
|
||
@apply bg-transparent;
|
||
}
|
||
|
||
.app-dialog .n-card-header__close:hover .n-base-icon {
|
||
@apply bg-(--app-control-hover);
|
||
}
|
||
|
||
.app-dialog .n-card__footer {
|
||
@apply shrink-0 bg-(--app-surface);
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-end;
|
||
}
|
||
|
||
.app-dialog .dialog-footer {
|
||
@apply flex justify-end gap-2;
|
||
align-items: center;
|
||
padding-top: 0;
|
||
}
|
||
|
||
/* 标题与底栏保留一致的边距,正文按内容和可用视口确定高度。 */
|
||
.app-dialog.n-card > .n-card-header {
|
||
padding-top: var(--n-padding-left);
|
||
}
|
||
|
||
.app-dialog.n-card > .n-card-content {
|
||
padding-top: 0;
|
||
padding-bottom: 0;
|
||
}
|
||
|
||
/* 没有按钮底栏时,正文保留与左右内边距一致的底部留白。 */
|
||
.app-dialog.n-card:not(:has(> .n-card__footer)) > .n-card-content {
|
||
padding-bottom: var(--n-padding-left);
|
||
}
|
||
|
||
.app-dialog.n-card > .n-card__footer {
|
||
padding-top: var(--n-padding-left);
|
||
padding-bottom: var(--n-padding-left);
|
||
}
|
||
</style>
|