fix: keep dialog body scrollable and footer visible
This commit is contained in:
1 parent
ebc66dd704
commit
ca70e4d888
1 file changed
+46
-9
@@ -1,9 +1,46 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { NModal, NScrollbar } from 'naive-ui'
|
import { NModal, NScrollbar } from 'naive-ui'
|
||||||
|
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
||||||
|
|
||||||
/** Naive UI 管理焦点锁定、Esc、遮罩与主题;长弹窗只滚动内部正文。 */
|
/** Naive UI 管理焦点锁定、Esc、遮罩与主题;长弹窗只滚动内部正文。 */
|
||||||
defineProps<{ title: string; description: string; busy?: boolean; wide?: boolean }>()
|
defineProps<{ title: string; description: string; busy?: boolean; wide?: boolean }>()
|
||||||
const open = defineModel<boolean>('open', { default: false })
|
const open = defineModel<boolean>('open', { default: false })
|
||||||
|
const bodyAnchor = ref<HTMLElement | null>(null)
|
||||||
|
const bodyMaxHeight = ref(0)
|
||||||
|
let layoutObserver: ResizeObserver | undefined
|
||||||
|
|
||||||
|
/** 按视口与实际标题、底栏尺寸限制正文,短内容保持自然高度。 */
|
||||||
|
function updateBodyHeight() {
|
||||||
|
const card = bodyAnchor.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
|
||||||
|
bodyMaxHeight.value = Math.max(
|
||||||
|
48,
|
||||||
|
window.innerHeight - 32 - (header?.offsetHeight ?? 0) - (footer?.offsetHeight ?? 0)
|
||||||
|
- (contentStyle ? parseFloat(contentStyle.paddingTop) + parseFloat(contentStyle.paddingBottom) : 0)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
watch(open, async shown => {
|
||||||
|
layoutObserver?.disconnect()
|
||||||
|
if (!shown) return
|
||||||
|
await nextTick()
|
||||||
|
updateBodyHeight()
|
||||||
|
const card = bodyAnchor.value?.closest('.app-dialog')
|
||||||
|
if (!card) 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)
|
||||||
|
}, { flush: 'post' })
|
||||||
|
onMounted(() => window.addEventListener('resize', updateBodyHeight))
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
layoutObserver?.disconnect()
|
||||||
|
window.removeEventListener('resize', updateBodyHeight)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -20,7 +57,8 @@ const open = defineModel<boolean>('open', { default: false })
|
|||||||
role="dialog"
|
role="dialog"
|
||||||
:aria-label="title"
|
:aria-label="title"
|
||||||
>
|
>
|
||||||
<NScrollbar class="dialog-body-scroll">
|
<NScrollbar class="dialog-body-scroll" :style="{ maxHeight: bodyMaxHeight + 'px' }">
|
||||||
|
<span ref="bodyAnchor" class="dialog-body-anchor" aria-hidden="true" />
|
||||||
<p class="text-sm leading-6 text-muted">{{ description }}</p>
|
<p class="text-sm leading-6 text-muted">{{ description }}</p>
|
||||||
<slot />
|
<slot />
|
||||||
</NScrollbar>
|
</NScrollbar>
|
||||||
@@ -46,21 +84,20 @@ const open = defineModel<boolean>('open', { default: false })
|
|||||||
@apply shrink-0;
|
@apply shrink-0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/* 正文使用实际测得的剩余视口高度,短内容保持自然高度。 */
|
||||||
* 正文必须形成完整的 flex 高度链:
|
|
||||||
* Card content 占据 header/footer 之外的剩余空间,
|
|
||||||
* NScrollbar 再占满 content,这样内部 container 才能正确计算可滚动高度。
|
|
||||||
*/
|
|
||||||
.app-dialog .n-card__content {
|
.app-dialog .n-card__content {
|
||||||
@apply min-h-0 flex-1 overflow-hidden;
|
@apply min-h-0 overflow-hidden;
|
||||||
display: flex;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.dialog-body-scroll.n-scrollbar {
|
.dialog-body-scroll.n-scrollbar {
|
||||||
@apply min-h-0 flex-1;
|
@apply min-h-0;
|
||||||
|
height: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 仅隐藏 NScrollbar 的视觉滚动条,不影响鼠标滚轮、触控板与内容拖动滚动。 */
|
/* 仅隐藏 NScrollbar 的视觉滚动条,不影响鼠标滚轮、触控板与内容拖动滚动。 */
|
||||||
|
.dialog-body-anchor { display: block; height: 0; }
|
||||||
|
|
||||||
.dialog-body-scroll.n-scrollbar .n-scrollbar-rail {
|
.dialog-body-scroll.n-scrollbar .n-scrollbar-rail {
|
||||||
opacity: 0 !important;
|
opacity: 0 !important;
|
||||||
pointer-events: none !important;
|
pointer-events: none !important;
|
||||||
|
|||||||
Reference in new issue
Block a user