page.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <script setup lang="ts">
  2. import type { StyleValue } from 'vue';
  3. import type { PageProps } from './types';
  4. import { computed, nextTick, onMounted, ref, useTemplateRef } from 'vue';
  5. import { CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT } from '@vben-core/shared/constants';
  6. import { cn } from '@vben-core/shared/utils';
  7. defineOptions({
  8. name: 'Page',
  9. });
  10. const { autoContentHeight = false, heightOffset = 0 } =
  11. defineProps<PageProps>();
  12. const headerHeight = ref(0);
  13. const footerHeight = ref(0);
  14. const shouldAutoHeight = ref(false);
  15. const headerRef = useTemplateRef<HTMLDivElement>('headerRef');
  16. const footerRef = useTemplateRef<HTMLDivElement>('footerRef');
  17. const contentStyle = computed<StyleValue>(() => {
  18. if (autoContentHeight) {
  19. return {
  20. height: `calc(var(${CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT}) - ${headerHeight.value}px - ${typeof heightOffset === 'number' ? `${heightOffset}px` : heightOffset})`,
  21. overflowY: shouldAutoHeight.value ? 'auto' : 'unset',
  22. };
  23. }
  24. return {};
  25. });
  26. async function calcContentHeight() {
  27. if (!autoContentHeight) {
  28. return;
  29. }
  30. await nextTick();
  31. headerHeight.value = headerRef.value?.offsetHeight || 0;
  32. footerHeight.value = footerRef.value?.offsetHeight || 0;
  33. setTimeout(() => {
  34. shouldAutoHeight.value = true;
  35. }, 30);
  36. }
  37. onMounted(() => {
  38. calcContentHeight();
  39. });
  40. </script>
  41. <template>
  42. <div class="relative">
  43. <div
  44. v-if="
  45. description ||
  46. $slots.description ||
  47. title ||
  48. $slots.title ||
  49. $slots.extra
  50. "
  51. ref="headerRef"
  52. :class="
  53. cn(
  54. 'bg-card border-border relative flex items-end border-b px-6 py-4',
  55. headerClass,
  56. )
  57. "
  58. >
  59. <div class="flex-auto">
  60. <slot name="title">
  61. <div v-if="title" class="mb-2 flex text-lg font-semibold">
  62. {{ title }}
  63. </div>
  64. </slot>
  65. <slot name="description">
  66. <p v-if="description" class="text-muted-foreground">
  67. {{ description }}
  68. </p>
  69. </slot>
  70. </div>
  71. <div v-if="$slots.extra">
  72. <slot name="extra"></slot>
  73. </div>
  74. </div>
  75. <div :class="cn('h-full p-4', contentClass)" :style="contentStyle">
  76. <slot></slot>
  77. </div>
  78. <div
  79. v-if="$slots.footer"
  80. ref="footerRef"
  81. :class="
  82. cn(
  83. 'bg-card align-center absolute bottom-0 left-0 right-0 flex px-6 py-4',
  84. footerClass,
  85. )
  86. "
  87. >
  88. <slot name="footer"></slot>
  89. </div>
  90. </div>
  91. </template>