Files
short-drama-agent-front/src/components/ui/AppDialog.vue
T

298 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { NButton, NModal, NScrollbar, useThemeVars } from 'naive-ui'
import { CircleAlert, CircleCheck, CircleX } from '@lucide/vue'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import FieldLabel from './FieldLabel.vue'
import MobilePanelHandle from './MobilePanelHandle.vue'
import { useMobileViewport } from '../../composables/useMobileViewport'
import { usePanelScrollState } from '../../composables/usePanelScrollState'
/** 公共弹窗的标题、说明、操作状态和宽度。 */
withDefaults(
defineProps<{
/** 弹窗标题,同时作为可访问名称。 */
title: string
/** 可选的简短标题补充,尽量一句话;完整说明与操作影响应放入默认正文插槽。 */
description?: string
/** 操作流程说明放入信息图标,业务状态说明插槽仍直接显示。 */
descriptionAsInfo?: boolean
/** 展示语义状态;默认保持中性,警告、成功和错误仅在标题旁显示对应图标。 */
status?: 'default' | 'warning' | 'success' | 'error'
/** 提交期间锁定关闭操作。 */
busy?: boolean
/** 使用宽弹窗布局。 */
wide?: boolean
/** 指定弹窗宽度,单位为像素;实际宽度仍受视口限制。 */
width?: number
}>(),
{ description: '', busy: false, wide: false, status: 'default' }
)
/** 状态图标使用当前 Naive 主题颜色,随深浅主题切换,不改变弹窗和按钮配色。 */
const themeVars = useThemeVars()
/** 关闭动画结束后通知调用方,便于在焦点陷阱释放后定位正文。 */
const emit = defineEmits<{ closed: [] }>()
/** 弹窗显隐状态,关闭行为由 Naive 管理。 */
const open = defineModel<boolean>('open', { default: false })
/** 手机端使用底部面板,桌面继续采用原居中弹窗。 */
const mobile = useMobileViewport()
/** 正文离开顶部时才显示标题区阴影,底栏始终保持平面。 */
const { bodyScrolled, onBodyScroll } = usePanelScrollState(open)
/** 正文容器隔离子元素外边距,保留完整的自然内容高度。 */
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) -
(mobile.value ? 12 : 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: mobile ? '100vw' : `${width ?? (wide ? 1040 : 600)}px` }"
class="app-dialog"
:class="{
'app-dialog-title-only': !description && !$slots.description,
'app-mobile-panel': mobile,
'has-body-scroll': bodyScrolled
}"
role="dialog"
:aria-label="title"
@after-leave="emit('closed')"
>
<!-- 以下是固定标题与说明模块,无说明时标题与关闭按钮垂直居中对齐 -->
<template #header>
<div class="min-w-0 w-full">
<MobilePanelHandle v-if="mobile" :active="open" :disabled="busy" @dismiss="open = false" />
<div class="flex min-w-0 items-center gap-2 text-lg font-semibold">
<!-- 以下是可被读屏识别的状态提示,颜色之外保留不同图形 -->
<CircleAlert
v-if="status === 'warning'"
:size="20"
class="shrink-0"
role="img"
aria-label="警告"
:style="{ color: themeVars.warningColor }"
/>
<CircleCheck
v-else-if="status === 'success'"
:size="20"
class="shrink-0"
role="img"
aria-label="成功"
:style="{ color: themeVars.successColor }"
/>
<CircleX
v-else-if="status === 'error'"
:size="20"
class="shrink-0"
role="img"
aria-label="错误"
:style="{ color: themeVars.errorColor }"
/>
<span class="app-panel-title" :title="title">{{ title }}</span>
<FieldLabel
class="shrink-0"
v-if="descriptionAsInfo && description && !$slots.description"
label="操作"
:description="description"
icon-only
icon="info"
/>
</div>
<div
v-if="$slots.description || (description && !descriptionAsInfo)"
class="mt-[2px] text-sm leading-6 font-normal text-muted"
>
<slot name="description">{{ description }}</slot>
</div>
</div>
</template>
<!-- 以下是自然高度正文模块,仅超过剩余视口高度时滚动 -->
<NScrollbar
class="dialog-body-scroll"
@scroll="onBodyScroll"
:style="{ height: 'auto', maxHeight: maxBodyHeight === undefined ? undefined : maxBodyHeight + 'px' }"
>
<div ref="bodyContent" class="flow-root"><slot /></div>
</NScrollbar>
<!-- 以下是固定操作底栏,未自定义操作时仍保留关闭入口 -->
<template #footer>
<div class="dialog-footer">
<slot name="footer"><NButton type="default" :disabled="busy" @click="open = false">关闭</NButton></slot>
</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;
position: relative;
z-index: 1;
border-bottom: 0;
box-shadow: none;
}
.app-dialog .n-card-header__main {
min-width: 0;
}
.app-dialog.has-body-scroll .n-card-header {
box-shadow: 0 4px 12px -8px color-mix(in srgb, var(--app-shadow) 60%, transparent);
}
/* 无描述的标题栏采用同一条中心线,标题下方保持紧凑的正文间距。 */
.app-dialog.app-dialog-title-only .n-card-header {
align-items: center;
padding-bottom: 16px;
}
/* 正文自然撑开,超过剩余视口高度后由 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__footer {
@apply shrink-0 bg-(--app-surface);
display: flex;
align-items: center;
justify-content: flex-end;
position: relative;
z-index: 1;
border-top: 0;
box-shadow: none;
}
.app-dialog .dialog-footer {
@apply flex justify-end gap-2;
width: 100%;
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 > .n-card__footer {
padding-top: var(--n-padding-left);
padding-bottom: var(--n-padding-left);
}
/* 手机居中弹窗统一贴底,短内容保持自然高度,长内容只滚动正文。 */
@media (max-width: 639px) {
.app-dialog.app-mobile-panel.n-card {
position: fixed;
inset: auto 0 0;
margin: 0;
max-width: 100vw;
max-height: calc(100dvh - max(12px, env(safe-area-inset-top)));
border-radius: 0;
}
.app-dialog.app-mobile-panel.n-card > .n-card-header {
padding: 44px 16px 16px;
}
.app-dialog.app-mobile-panel .n-card-header__close {
align-self: flex-start;
}
.app-dialog.app-mobile-panel.n-card > .n-card-content {
padding-inline: 16px;
}
.app-dialog.app-mobile-panel.n-card > .n-card__footer {
padding: 16px 16px max(16px, env(safe-area-inset-bottom));
}
.app-dialog.app-mobile-panel:is(.fade-in-scale-up-transition-enter-from, .fade-in-scale-up-transition-leave-to) {
transform: translate3d(0, 100%, 0);
opacity: 1;
}
.app-dialog.app-mobile-panel:is(
.fade-in-scale-up-transition-enter-active,
.fade-in-scale-up-transition-leave-active
) {
transition: transform 220ms cubic-bezier(0.32, 0.72, 0, 1);
}
}
@media (prefers-reduced-motion: reduce) {
.app-dialog.app-mobile-panel:is(
.fade-in-scale-up-transition-enter-active,
.fade-in-scale-up-transition-leave-active
) {
transition-duration: 0.01ms;
}
}
</style>