|
| 1 | +<script lang="ts" setup> |
| 2 | +import type { |
| 3 | + SectionLayoutCardArea, |
| 4 | + SectionLayoutProps, |
| 5 | + SectionLayoutSlots, |
| 6 | +} from './interface' |
| 7 | +import { toArray, useElementSize } from '@vueuse/core' |
| 8 | +import { computed, getCurrentInstance, shallowRef, watch } from 'vue' |
| 9 | +import { loadCSS } from '@/internal/load-css' |
| 10 | +import css from './index.css?raw' |
| 11 | +
|
| 12 | +defineOptions({ |
| 13 | + name: 'SectionLayout', |
| 14 | + __cKitStaticCSS: css, |
| 15 | +}) |
| 16 | +
|
| 17 | +const { |
| 18 | + card, |
| 19 | + height = '100%', |
| 20 | + onMainContentResize, |
| 21 | +} = defineProps<SectionLayoutProps>() |
| 22 | +
|
| 23 | +defineSlots<SectionLayoutSlots>() |
| 24 | +
|
| 25 | +loadCSS() |
| 26 | +
|
| 27 | +export interface SectionLayoutConfig { |
| 28 | + cardClass?: any |
| 29 | +} |
| 30 | +
|
| 31 | +const { config } = getCurrentInstance()!.proxy!.$options as { |
| 32 | + config?: SectionLayoutConfig |
| 33 | +} |
| 34 | +
|
| 35 | +const ns = 'kit-section-layout' |
| 36 | +
|
| 37 | +const cardAreas = computed(() => { |
| 38 | + const set = new Set(toArray(card)) |
| 39 | + return { |
| 40 | + has(area: SectionLayoutCardArea) { |
| 41 | + return card === true || set.has(area) |
| 42 | + }, |
| 43 | + } |
| 44 | +}) |
| 45 | +
|
| 46 | +const mainContentRef = shallowRef<HTMLDivElement>() |
| 47 | +const mainContentSize = useElementSize(mainContentRef) |
| 48 | +watch([mainContentSize.width, mainContentSize.height], () => { |
| 49 | + onMainContentResize && |
| 50 | + toArray(onMainContentResize).forEach((fn) => |
| 51 | + fn({ |
| 52 | + width: mainContentSize.width.value, |
| 53 | + height: mainContentSize.height.value, |
| 54 | + }), |
| 55 | + ) |
| 56 | +}) |
| 57 | +
|
| 58 | +defineExpose({ |
| 59 | + mainContentRef, |
| 60 | + mainContentSize: { |
| 61 | + width: mainContentSize.width, |
| 62 | + height: mainContentSize.height, |
| 63 | + }, |
| 64 | +}) |
| 65 | +</script> |
| 66 | + |
| 67 | +<template> |
| 68 | + <div |
| 69 | + :class="ns" |
| 70 | + :style="{ height }" |
| 71 | + > |
| 72 | + <div |
| 73 | + :style="topStyle" |
| 74 | + :class="[ |
| 75 | + `${ns}__top`, |
| 76 | + cardAreas.has('top') && ['is-card', config?.cardClass], |
| 77 | + topClass, |
| 78 | + ]" |
| 79 | + > |
| 80 | + <slot name="top" /> |
| 81 | + </div> |
| 82 | + |
| 83 | + <div |
| 84 | + :style="mainStyle" |
| 85 | + :class="[ |
| 86 | + `${ns}__main`, |
| 87 | + cardAreas.has('main') && ['is-card', config?.cardClass], |
| 88 | + mainClass, |
| 89 | + ]" |
| 90 | + > |
| 91 | + <slot name="main"> |
| 92 | + <div |
| 93 | + :style="mainHeadStyle" |
| 94 | + :class="[`${ns}__main-head`, mainHeadClass]" |
| 95 | + > |
| 96 | + <slot name="main-head" /> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div |
| 100 | + ref="mainContentRef" |
| 101 | + :style="mainContentStyle" |
| 102 | + :class="[`${ns}__main-content`, mainContentClass]" |
| 103 | + > |
| 104 | + <component |
| 105 | + :is="$slots['main-content'] || $slots.default" |
| 106 | + v-if="$slots['main-content'] || $slots.default" |
| 107 | + :width="mainContentSize.width.value" |
| 108 | + :height="mainContentSize.height.value" |
| 109 | + /> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div |
| 113 | + :style="mainFootStyle" |
| 114 | + :class="[`${ns}__main-foot`, mainFootClass]" |
| 115 | + > |
| 116 | + <slot name="main-foot" /> |
| 117 | + </div> |
| 118 | + </slot> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div |
| 122 | + :style="bottomStyle" |
| 123 | + :class="[ |
| 124 | + `${ns}__bottom`, |
| 125 | + cardAreas.has('bottom') && ['is-card', config?.cardClass], |
| 126 | + bottomClass, |
| 127 | + ]" |
| 128 | + > |
| 129 | + <slot name="bottom" /> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | +</template> |
0 commit comments