Files
timi-tdesign-mobile/src/components/master-detail/index.vue
T

154 lines
4.1 KiB
Vue

<template>
<div
class="tui-master-detail"
:class="{ split: isSplit, 'is-collapsed': isSplit && detailEmpty }"
:style="rootStyle"
>
<section class="master">
<slot name="master" />
</section>
<section v-if="isSplit" class="detail">
<slot name="detail">
<div v-if="emptyText && !detailEmpty" class="empty">
<center-tips :content="emptyText" />
</div>
</slot>
</section>
</div>
</template>
<script setup lang="ts">
import { computed, type StyleValue } from "vue";
import { CenterTips } from "timi-web";
import { useLayout } from "../../composables/use-layout";
import type { MasterDetailProps } from "./type";
defineOptions({
name: "MasterDetail"
});
const props = withDefaults(defineProps<MasterDetailProps>(), {
masterWidth: 380,
split: "auto",
emptyText: "",
topInset: "",
detailEmpty: false
});
defineSlots<{
master?: () => unknown;
detail?: () => unknown;
}>();
// ---------- 分栏判定 ----------
const layout = useLayout({ tablet: true });
/** split 为 auto 时跟随 AppShell 的横屏状态,显式传入布尔值时以业务为准。 */
const isSplit = computed(() => (props.split === "auto" ? layout.isWide.value : props.split));
// ---------- 尺寸 ----------
const rootStyle = computed<StyleValue>(() => ({
"--tui-master-width": typeof props.masterWidth === "number"
? `${props.masterWidth}px`
: props.masterWidth,
...(props.topInset ? { "--tui-master-top-inset": props.topInset } : {})
}));
</script>
<style scoped lang="less">
@easing: var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
.tui-master-detail {
flex: 1;
height: 100%;
display: flex;
min-height: 0;
.master {
flex: 1;
min-width: 0;
position: relative;
overflow-y: auto;
padding-top: var(--tui-master-top-inset, 0px);
-webkit-overflow-scrolling: touch;
}
.empty {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
&.split {
.master {
flex: none;
width: var(--tui-master-width);
box-sizing: border-box;
// 宽度过渡期间列表会被压窄,内容可能横向溢出,裁掉避免压到详情栏上
overflow-x: hidden;
border-right: 1px solid var(--td-component-border, rgba(0, 0, 0, .08));
// 时长和曲线都来自 page-layout.less 的变量,列表页导航栏用同一组,
// 两边必须同拍,否则列表跳、导航滑。
transition: width var(--tui-split-duration, 320ms) @easing, border-color .2s linear;
}
.detail {
flex: 1;
min-width: 0;
position: relative;
overflow-x: hidden;
overflow-y: auto;
padding-top: var(--tui-master-top-inset, 0px);
background: var(--tui-master-detail-background, var(--td-bg-color-page));
-webkit-overflow-scrolling: touch;
// 展开方向不加 opacity 过渡:
// 宽度从 0 长出来本身就是"揭示",再叠一层淡入会让内容看着迟迟不出现。
// 收起方向的淡出定义在 .is-collapsed 里,只在收起时生效。
/**
* 详情栏里放的整页组件自动适配栏位:贴合高度、建立定位上下文。
* 导航栏不隐藏,而是由 page-layout.less 退化成栏内头部,
* 这样编辑等操作仍然可用。
*/
:deep(.tui-page-layout) {
height: 100%;
position: relative;
min-height: 0;
}
}
/**
* 未选中详情:主栏占满,右栏收起。
*
* 详情栏不设 display: none——那样宽度变化就没有过渡了。
* 保持 flex 分配、把宽度压到 0,配合 overflow-x: hidden 裁掉内容,
* 主栏宽度一过渡,右栏自然被"挤"出来。
*/
&.is-collapsed {
.master {
width: 100%;
border-right-color: transparent;
}
.detail {
// 收起方向:内容比宽度先淡出。
// 否则右栏被压到只剩几十像素时,文字会挤成竖排、按钮会卡在角上,
// 看起来像残影而不是"被推走"。
//
// transition 只写在这里:展开时 class 被移除,属性回到默认的瞬间切换,
// 内容第一帧就是可见的。
opacity: 0;
overflow-y: hidden;
pointer-events: none;
transition: opacity .12s ease-out;
}
}
}
}
</style>