feat(master-detail): add cover sub-mode and providePane

- MasterDetail / MasterDetailPage: add `sub-mode` ("shift" | "cover", default
  "shift", so existing callers are unaffected). `cover` leaves the master pane
  untouched and puts the sub pane over the detail column: the case where the
  list must stay visible while drilling into another module. The detail pane
  fades out in place instead of sliding into the master column, so it stays
  mounted and keeps its scroll position and internal state.
- use-master-detail: export `providePane(context)` and return `close` from
  useMasterDetailPane. A page can now hand a derived pane context to its whole
  subtree, which is how a host rewrites sub view ids (prefixing them with the
  owning record). The alternative - a wrapper component - breaks the
  `.detail > .tui-page-layout` selectors in page-layout.less and sends the
  in-pane navbar back to `position: fixed`.
- docs: cover vs shift, and how to prefix sub view ids without a wrapper.
This commit is contained in:
Timi
2026-11-18 15:08:44 +08:00
parent a50f134b25
commit bdaee75b81
8 changed files with 200 additions and 35 deletions
+1 -1
View File
@@ -80,7 +80,7 @@ export {
};
export { createLayoutContext, provideLayout, useLayout } from "./app-shell";
export { useExpandToMaster, useMasterDetail, useMasterDetailPane, usePane } from "../composables/use-master-detail";
export { useExpandToMaster, useMasterDetail, useMasterDetailPane, usePane, providePane } from "../composables/use-master-detail";
export type { ExpandToMasterOptions, PaneContext, PaneDirection, UseMasterDetailOptions, UseMasterDetailPaneOptions, UseMasterDetailPaneReturn, UseMasterDetailReturn } from "../composables/use-master-detail";
export type {
+3 -1
View File
@@ -6,6 +6,7 @@
:top-inset="topInset"
:detail-empty="!selected"
:sub="sub"
:sub-mode="subMode"
>
<template #master>
<div class="tui-master-wrap">
@@ -54,7 +55,8 @@ const props = withDefaults(defineProps<MasterDetailPageProps>(), {
direction: "forward",
masterWidth: 380,
topInset: "",
sub: false
sub: false,
subMode: "shift"
});
defineSlots<{
+9 -2
View File
@@ -19,9 +19,16 @@ export interface MasterDetailPageProps {
/**
* 是否进入第三层:主栏换成详情内容,右栏显示 `#sub` 槽。
*
* 三层依次是:列表 → 详情 → 子页。进入时列表整列滑出左侧、详情退到主栏,
* 返回时反向还原成「列表 + 详情」。只有分栏形态下有效,
* 三层依次是:列表 → 详情 → 子页。只有分栏形态下有效,
* 窄屏请让子页走整页路由(`usePane().openSub()` 会返回 false)。
*/
sub?: boolean;
/**
* 第三层的呈现方式,默认 `shift`。
*
* - `shift`:列表整列滑出、详情退到主栏,适合同一条记录继续往下钻;
* - `cover`:主栏不动、子页盖住详情栏,适合从详情跳进另一个模块的页面,
* 列表要一直留在眼前(详情原地保活,返回时滚动位置和状态都在)。
*/
subMode?: "shift" | "cover";
}
+33 -2
View File
@@ -4,7 +4,8 @@
:class="{
split: isSplit,
'is-collapsed': isSplit && detailEmpty,
'has-sub': isSplit && showSub
'has-sub': isSplit && showSub,
'is-sub-cover': isSplit && showSub && subMode === 'cover'
}"
:style="rootStyle"
>
@@ -44,7 +45,8 @@ const props = withDefaults(defineProps<MasterDetailProps>(), {
emptyText: "",
topInset: "",
detailEmpty: false,
sub: false
sub: false,
subMode: "shift"
});
defineSlots<{
@@ -231,6 +233,35 @@ const rootStyle = computed<StyleValue>(() => ({
right: 0;
}
}
/**
* 第三层的 cover 形态:右栏换人,主栏不动。
*
* shift 是"整条轨道左移",用户已经不看列表了;cover 针对的相反——
* 从详情跳进别的模块,列表本身要一直留在眼前。
* 详情不参与位移,只原地淡出(保活,返回时滚动位置和内部状态都还在),
* 子页像换班一样滑进右栏。
*/
&.has-sub.is-sub-cover {
.master {
left: 0;
right: calc(100% - var(--tui-master-width));
border-right-color: var(--td-component-border, rgba(0, 0, 0, .08));
}
.detail {
left: var(--tui-master-width);
right: 0;
opacity: 0;
pointer-events: none;
}
.sub {
left: var(--tui-master-width);
right: 0;
}
}
}
}
</style>
+11
View File
@@ -31,4 +31,15 @@ export interface MasterDetailProps {
* 只有分栏形态下有效,窄屏请让子页走整页路由。
*/
sub?: boolean;
/**
* 第三层的呈现方式,默认 `shift`。
*
* - `shift`:整条轨道左移,列表滑出、详情退到主栏。适合同一条记录继续往下钻,
* 用户已经不需要再看列表了。
* - `cover`:主栏不动,子页直接盖住详情栏(详情原地保活,返回时滚动位置和内部状态都在)。
* 适合从详情跳进**另一个模块**的页面——列表本身是用户要一直看的东西。
*
* 两种方式共用同一套 `sub` / `subView` 状态,切换只影响布局,不影响层级和历史的处理。
*/
subMode?: "shift" | "cover";
}
+58 -29
View File
@@ -86,6 +86,24 @@ export function usePane(): PaneContext | null {
return inject(PANE_KEY, null);
}
/**
* 把一套栏内上下文提供给后代。
*
* 用在"页面要改写子页标识"的场合:列表页在最外层 provide 一套派生上下文,
* 右栏里的页面调 `openSub()` 时标识先经过它,不必在中间塞一层容器组件。
*
* 不要用容器组件代替它:栏内页面的骨架样式(导航栏退化成栏内头部、内容区铺满)
* 挂在 `.detail > .tui-page-layout` 这类直系选择器上,多包一层就全落空。
*
* 注意 provide 只在当前组件的后代里生效,且会遮蔽上层的同一套上下文,
* 所以派生时要把 `close` / `openSub` 转发给宿主,别自己重写一套。
*
* @param context 派生出来的上下文,通常转发 `usePane()` 拿到的宿主上下文
*/
export function providePane(context: PaneContext): void {
provide(PANE_KEY, context);
}
/**
* 主从布局的选中态管理。
*
@@ -99,7 +117,7 @@ export function useMasterDetail(options: UseMasterDetailOptions = {}): UseMaster
const isWide = layout.isWide;
// 右栏里的页面通过 usePane().close() 把控制权交回来
provide(PANE_KEY, {
providePane({
close: (saved = false) => options.onClose?.(saved),
openSub: view => options.onOpenSub?.(view) ?? false
});
@@ -226,6 +244,13 @@ export interface UseMasterDetailPaneReturn {
openSub: (view: string) => boolean;
/** 关闭子页面,回到详情层。 */
closeSub: () => void;
/**
* 关闭右栏最上面一层:有子页先关子页,其次关编辑,最后关详情。
*
* 和 `usePane().close()` 是同一套逻辑,给"页面要派生一套栏内上下文"时转发用
* (见 `providePane`)。
*/
close: (saved?: boolean) => void;
}
/**
@@ -313,35 +338,38 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
? options.edit(id)
: options.detail(id)),
onOpenSub: requestSub,
onClose: saved => {
if (popPane(saved)) {
return;
}
// 降级:没有可回退的层级,只改 query
pendingNav.value = "replace";
paneDirection.value = "back";
// 有子页先关子页,回到详情
if (subView.value) {
subView.value = "";
return;
}
// 取消/保存编辑:回到详情,保存过就让详情重新加载
if (editing.value) {
editing.value = false;
if (saved) {
detailVersion.value++;
}
return;
}
// 关闭详情:右滑出,回到空栏
selectedId.value = "";
}
onClose: saved => closePane(saved)
});
/** 关闭右栏最上面一层;页面派生上下文时原样转发这一个。 */
function closePane(saved = false): void {
if (popPane(saved)) {
return;
}
// 降级:没有可回退的层级,只改 query
pendingNav.value = "replace";
paneDirection.value = "back";
// 有子页先关子页,回到详情
if (subView.value) {
subView.value = "";
return;
}
// 取消/保存编辑:回到详情,保存过就让详情重新加载
if (editing.value) {
editing.value = false;
if (saved) {
detailVersion.value++;
}
return;
}
// 关闭详情:右滑出,回到空栏
selectedId.value = "";
}
function openDetail(id: string): void {
// 正在编辑时直接点列表里另一项:栈顶是编辑层,就地改成新客户的详情的话,
// 下面那条旧客户详情会重新可达,返回时会掉回上一个客户。
@@ -494,7 +522,8 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
openDetail,
openEdit,
openSub: requestSub,
closeSub
closeSub,
close: closePane
};
}