import { ref, computed, watch, onMounted } from 'vue'
|
import { useElementSize } from '@vueuse/core'
|
function useLayoutHeight(options = {}) {
|
const { extraSpacing = 15, updateCssVar = true, cssVarName = '--art-full-height' } = options
|
const headerRef = ref()
|
const contentHeaderRef = ref()
|
const { height: headerHeight } = useElementSize(headerRef)
|
const { height: contentHeaderHeight } = useElementSize(contentHeaderRef)
|
const containerMinHeight = computed(() => {
|
const totalHeight = headerHeight.value + contentHeaderHeight.value + extraSpacing
|
return `calc(100vh - ${totalHeight}px)`
|
})
|
if (updateCssVar) {
|
watch(
|
containerMinHeight,
|
(newHeight) => {
|
requestAnimationFrame(() => {
|
document.documentElement.style.setProperty(cssVarName, newHeight)
|
})
|
},
|
{ immediate: true }
|
)
|
}
|
return {
|
/** 容器最小高度(响应式) */
|
containerMinHeight,
|
/** 头部元素引用 */
|
headerRef,
|
/** 内容头部元素引用 */
|
contentHeaderRef,
|
/** 头部高度(响应式) */
|
headerHeight,
|
/** 内容头部高度(响应式) */
|
contentHeaderHeight
|
}
|
}
|
function useAutoLayoutHeight(headerIds = ['app-header', 'app-content-header'], options = {}) {
|
const { extraSpacing = 15, updateCssVar = true, cssVarName = '--art-full-height' } = options
|
const headerRef = ref()
|
const contentHeaderRef = ref()
|
const { height: headerHeight } = useElementSize(headerRef)
|
const { height: contentHeaderHeight } = useElementSize(contentHeaderRef)
|
const containerMinHeight = computed(() => {
|
const totalHeight = headerHeight.value + contentHeaderHeight.value + extraSpacing
|
return `calc(100vh - ${totalHeight}px)`
|
})
|
if (updateCssVar) {
|
watch(
|
containerMinHeight,
|
(newHeight) => {
|
requestAnimationFrame(() => {
|
document.documentElement.style.setProperty(cssVarName, newHeight)
|
})
|
},
|
{ immediate: true }
|
)
|
}
|
onMounted(() => {
|
if (typeof document !== 'undefined') {
|
requestAnimationFrame(() => {
|
const header = document.getElementById(headerIds[0])
|
const contentHeader = document.getElementById(headerIds[1])
|
if (header) {
|
headerRef.value = header
|
}
|
if (contentHeader) {
|
contentHeaderRef.value = contentHeader
|
}
|
})
|
}
|
})
|
return {
|
/** 容器最小高度(响应式) */
|
containerMinHeight,
|
/** 头部元素引用 */
|
headerRef,
|
/** 内容头部元素引用 */
|
contentHeaderRef,
|
/** 头部高度(响应式) */
|
headerHeight,
|
/** 内容头部高度(响应式) */
|
contentHeaderHeight
|
}
|
}
|
export { useAutoLayoutHeight, useLayoutHeight }
|