修复:动态收缩弹窗正文并保持底部操作栏可见

This commit is contained in:
GouJ committed 2026-09-27 15:04:12 +08:00
1 parent c67b73a6c0
commit cd00236563
1 file changed
+13 -9
+13 -9
View File
@@ -6,22 +6,25 @@ import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
defineProps<{ title: string; description: string; busy?: boolean; wide?: boolean }>()
const open = defineModel<boolean>('open', { default: false })
const bodyAnchor = ref<HTMLElement | null>(null)
const bodyMaxHeight = ref(0)
const bodyHeight = ref(0)
let layoutObserver: ResizeObserver | undefined
/** 按视口与实际标题、底栏尺寸限制正文,短内容保持自然高度。 */
/** 正文使用内容高度与剩余视口高度的较小值;为 NScrollbar 提供明确滚动高度。 */
function updateBodyHeight() {
const card = bodyAnchor.value?.closest('.app-dialog')
if (!card) return
const scrollContent = bodyAnchor.value?.parentElement
if (!card || !scrollContent) 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
bodyMaxHeight.value = Math.max(
48,
const available = Math.max(
0,
window.innerHeight - 32 - (header?.offsetHeight ?? 0) - (footer?.offsetHeight ?? 0)
- (contentStyle ? parseFloat(contentStyle.paddingTop) + parseFloat(contentStyle.paddingBottom) : 0)
- 2
)
bodyHeight.value = Math.min(scrollContent.scrollHeight, available)
}
watch(open, async shown => {
layoutObserver?.disconnect()
@@ -29,12 +32,14 @@ watch(open, async shown => {
await nextTick()
updateBodyHeight()
const card = bodyAnchor.value?.closest('.app-dialog')
if (!card) return
const scrollContent = bodyAnchor.value?.parentElement
if (!card || !scrollContent) 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(scrollContent)
}, { flush: 'post' })
onMounted(() => window.addEventListener('resize', updateBodyHeight))
onBeforeUnmount(() => {
@@ -57,7 +62,7 @@ onBeforeUnmount(() => {
role="dialog"
:aria-label="title"
>
<NScrollbar class="dialog-body-scroll" :style="{ maxHeight: bodyMaxHeight + 'px' }">
<NScrollbar class="dialog-body-scroll" :style="{ height: bodyHeight + 'px' }">
<span ref="bodyAnchor" class="dialog-body-anchor" aria-hidden="true" />
<p v-if="description" class="text-sm leading-6 text-muted">{{ description }}</p>
<slot />
@@ -84,7 +89,7 @@ onBeforeUnmount(() => {
@apply shrink-0;
}
/* 正文使用实际测得的剩余视口高度,短内容保持自然高度。 */
/* 正文容器可收缩,滚动高度由内容与剩余视口共同确定。 */
.app-dialog .n-card__content {
@apply min-h-0 overflow-hidden;
display: block;
@@ -92,7 +97,6 @@ onBeforeUnmount(() => {
.dialog-body-scroll.n-scrollbar {
@apply min-h-0;
height: auto;
}
/* 仅隐藏 NScrollbar 的视觉滚动条,不影响鼠标滚轮、触控板与内容拖动滚动。 */