feat: master-detail third pane
- MasterDetail / MasterDetailPage: add `sub` prop and `#sub` slot. The three columns share one container and are placed by left/right, so the detail pane moves between columns without remounting and keeps its scroll position. - useMasterDetailPane: add subView / openSub / closeSub, keep the sub view in query and browser history alongside detail and view. - page-layout: sub pane reuses the in-pane navbar rules; the list navbar slides out on the third level instead of being hidden. - docs: demos for the third pane, the layered navbars and PaneTransition; restore transition durations in demo previews under prefers-reduced-motion, where VitePress zeroes them. Fix the missing `split` ref in the master-detail demo and the incorrect note about the back arrow.
This commit is contained in:
@@ -16,9 +16,20 @@ export interface PaneContext {
|
||||
/**
|
||||
* 关闭当前栏内容,回到上一层。
|
||||
*
|
||||
* 分层时关最上面一层:有子页先关子页,其次关编辑,最后关详情。
|
||||
*
|
||||
* @param saved 是否有数据变更,上层可据此决定要不要刷新
|
||||
*/
|
||||
close: (saved?: boolean) => void;
|
||||
/**
|
||||
* 在当前栏之上再压一层子页面(分栏内的第三层)。
|
||||
*
|
||||
* 只有分栏形态下接得住。返回 false 表示没接住——窄屏、没有选中项,
|
||||
* 或者宿主页面没有声明子页槽,调用方应当改走整页路由。
|
||||
*
|
||||
* @param view 子页标识,由列表页解析,例如 `recycle-rule:698f`
|
||||
*/
|
||||
openSub: (view: string) => boolean;
|
||||
}
|
||||
|
||||
/** useMasterDetail 的配置。 */
|
||||
@@ -43,6 +54,12 @@ export interface UseMasterDetailOptions {
|
||||
* 不需要自己判断该跳哪条路由。
|
||||
*/
|
||||
onClose?: (saved?: boolean) => void;
|
||||
/**
|
||||
* 右栏内容请求压入一个子页面。
|
||||
*
|
||||
* 返回 false 表示宿主管不了这个子页,调用方应改走整页跳转。
|
||||
*/
|
||||
onOpenSub?: (view: string) => boolean;
|
||||
}
|
||||
|
||||
/** useMasterDetail 的返回值。 */
|
||||
@@ -83,7 +100,8 @@ export function useMasterDetail(options: UseMasterDetailOptions = {}): UseMaster
|
||||
|
||||
// 右栏里的页面通过 usePane().close() 把控制权交回来
|
||||
provide(PANE_KEY, {
|
||||
close: (saved = false) => options.onClose?.(saved)
|
||||
close: (saved = false) => options.onClose?.(saved),
|
||||
openSub: view => options.onOpenSub?.(view) ?? false
|
||||
});
|
||||
|
||||
function open(id: string): void {
|
||||
@@ -164,13 +182,15 @@ export interface UseMasterDetailPaneOptions {
|
||||
queryKey?: string;
|
||||
/** 编辑态写回 query 的字段名,默认 view。 */
|
||||
viewQueryKey?: string;
|
||||
/** 子页标识写回 query 的字段名,默认 sub。 */
|
||||
subQueryKey?: string;
|
||||
/** 是否把选中项和编辑态同步到 query,默认 true。 */
|
||||
syncQuery?: boolean;
|
||||
/**
|
||||
* 是否让栏内层级进入浏览器历史,默认 true。
|
||||
*
|
||||
* 开启后「选中一项」和「进入编辑」各推一条 history,手势返回会逐级回退
|
||||
* (编辑 → 详情 → 空栏 → 离开页面),前进手势也能还原右栏。
|
||||
* 开启后「选中一项」「进入编辑」「打开子页」各推一条 history,手势返回会逐级回退
|
||||
* (子页 → 详情 → 空栏 → 离开页面),前进手势也能还原右栏。
|
||||
* 关掉则退回旧行为:层级只改 query,手势返回直接离开列表页。
|
||||
*/
|
||||
historyLevels?: boolean;
|
||||
@@ -182,6 +202,12 @@ export interface UseMasterDetailPaneReturn {
|
||||
selectedId: ReturnType<typeof ref<string>>;
|
||||
/** 右栏是否处于编辑视图。 */
|
||||
editing: ReturnType<typeof ref<boolean>>;
|
||||
/**
|
||||
* 当前栏内子页标识,空字符串表示没有子页。
|
||||
*
|
||||
* 非空时页面处于第三层:列表滑出,详情在主栏,右栏渲染 `#sub` 槽。
|
||||
*/
|
||||
subView: ReturnType<typeof ref<string>>;
|
||||
/** 详情重建计数,保存后自增可让详情重新加载。 */
|
||||
detailVersion: ReturnType<typeof ref<number>>;
|
||||
/** 右栏切换方向,直接传给 PaneTransition。 */
|
||||
@@ -192,18 +218,30 @@ export interface UseMasterDetailPaneReturn {
|
||||
openDetail: (id: string) => void;
|
||||
/** 右栏切到编辑;没有配置 edit 路由时无效。 */
|
||||
openEdit: () => void;
|
||||
/**
|
||||
* 在右栏压入一层子页面;返回 false 表示当前不是分栏,调用方应改走整页路由。
|
||||
*
|
||||
* 调用方通常是详情页:`if (!pane.openSub("recycle-rule:" + id)) router.push(...)`。
|
||||
*/
|
||||
openSub: (view: string) => boolean;
|
||||
/** 关闭子页面,回到详情层。 */
|
||||
closeSub: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主从分栏的完整状态机:选中项、编辑态、切换方向、关闭逻辑、转屏、query 同步。
|
||||
* 主从分栏的完整状态机:选中项、编辑态、子页、切换方向、关闭逻辑、转屏、query 同步。
|
||||
*
|
||||
* 列表页只需要 `openDetail(item.id)` 和 `openEdit()`,模板里把 `editing` /
|
||||
* `selectedId` / `paneDirection` 接到 `PaneTransition` 上即可,不用自己写分支。
|
||||
* 列表页只需要 `openDetail(item.id)`、`openEdit()` 和 `subView`,
|
||||
* 模板里把 `editing` / `selectedId` / `subView` / `paneDirection` 接到
|
||||
* `MasterDetailPage` 上即可,不用自己写分支。
|
||||
*
|
||||
* 编辑视图是可选的:不传 `edit` 时 `openEdit` 什么都不做,`editing` 恒为 false。
|
||||
*
|
||||
* 栏内层级默认会进浏览器历史(见 `historyLevels`):选中一项和进入编辑各推一条,
|
||||
* 所以浏览器返回手势、物理返回键会先在栏内逐级回退(编辑 → 详情 → 空栏),
|
||||
* 子页是第三层:详情页通过 `usePane().openSub(view)` 压入,列表页用 `#sub` 槽渲染。
|
||||
* 此时布局从「列表 + 详情」变成「详情 + 子页」,返回再还原。
|
||||
*
|
||||
* 栏内层级默认会进浏览器历史(见 `historyLevels`):选中一项、进入编辑、打开子页各推一条,
|
||||
* 所以浏览器返回手势、物理返回键会先在栏内逐级回退(子页/编辑 → 详情 → 空栏),
|
||||
* 空栏时再返回才离开列表页。返回手势触发的是 query 变化,
|
||||
* `PageTransition` 按 path 和 depth 判定,不会把它当成翻页。
|
||||
*/
|
||||
@@ -213,8 +251,10 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
|
||||
const queryKey = options.queryKey ?? "detail";
|
||||
const viewKey = options.viewQueryKey ?? "view";
|
||||
const subKey = options.subQueryKey ?? "sub";
|
||||
|
||||
const editing = ref(options.initialEditing ?? String(route.query[viewKey] || "") === "edit");
|
||||
const subView = ref(String(route.query[subKey] || ""));
|
||||
const detailVersion = ref(0);
|
||||
const paneDirection = ref<PaneDirection>("forward");
|
||||
|
||||
@@ -228,9 +268,13 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
/** 从编辑态直接换一项时,先退回详情层,再由反向同步换成这个 id。 */
|
||||
const pendingSwapId = ref("");
|
||||
|
||||
/** 栏内层级:有选中项记 1 级,再进编辑记 2 级,正好对应三级返回。 */
|
||||
function levelOf(id: string | undefined, isEditing: boolean): number {
|
||||
return (id ? 1 : 0) + (isEditing && id ? 1 : 0);
|
||||
/**
|
||||
* 栏内层级:有选中项记 1 级,子页或编辑再各加 1 级。
|
||||
*
|
||||
* 子页和编辑都从详情发起,互斥,所以最多两级叠加。
|
||||
*/
|
||||
function levelOf(id: string | undefined, isEditing: boolean, view: string): number {
|
||||
return id ? 1 + (view || isEditing ? 1 : 0) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,13 +292,27 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
return true;
|
||||
}
|
||||
|
||||
function requestSub(view: string): boolean {
|
||||
// 窄屏没有右栏可叠;没有选中项也没有"上一层"可以退
|
||||
if (!view || !isWide.value || !selectedId.value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pendingNav.value = "push";
|
||||
paneDirection.value = "forward";
|
||||
subView.value = view;
|
||||
return true;
|
||||
}
|
||||
|
||||
const { selectedId, isWide, open } = useMasterDetail({
|
||||
route: options.detail,
|
||||
initial: options.initial ?? String(route.query[queryKey] || ""),
|
||||
// 宽变窄时按右栏当前视图决定落到哪个整页:正在编辑就进编辑页
|
||||
// 宽变窄时按右栏当前视图决定落到哪个整页:正在编辑就进编辑页。
|
||||
// 子页在这条路径上会被丢掉——它没有对应的整页路由,转屏后退回详情是能接受的最差情况。
|
||||
followViewport: id => (editing.value && options.edit
|
||||
? options.edit(id)
|
||||
: options.detail(id)),
|
||||
onOpenSub: requestSub,
|
||||
onClose: saved => {
|
||||
if (popPane(saved)) {
|
||||
return;
|
||||
@@ -264,6 +322,12 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
pendingNav.value = "replace";
|
||||
paneDirection.value = "back";
|
||||
|
||||
// 有子页先关子页,回到详情
|
||||
if (subView.value) {
|
||||
subView.value = "";
|
||||
return;
|
||||
}
|
||||
|
||||
// 取消/保存编辑:回到详情,保存过就让详情重新加载
|
||||
if (editing.value) {
|
||||
editing.value = false;
|
||||
@@ -294,6 +358,8 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
// 从空到有才算开新层级;换一项不堆历史,否则连点 N 项要按 N 次返回
|
||||
pendingNav.value = selectedId.value ? "replace" : "push";
|
||||
editing.value = false;
|
||||
// 换订单时子页属于上一单,跟着一起收掉
|
||||
subView.value = "";
|
||||
open(id);
|
||||
}
|
||||
|
||||
@@ -305,23 +371,43 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
// 详情 → 编辑是加深一层,手势返回应该先退回详情
|
||||
pendingNav.value = "push";
|
||||
paneDirection.value = "forward";
|
||||
subView.value = "";
|
||||
editing.value = true;
|
||||
}
|
||||
|
||||
// 选中项和编辑态一起写回 query:刷新、分享链接、转屏都靠它恢复。
|
||||
// 合并成一次导航,避免两个 watcher 互相覆盖。
|
||||
function closeSub(): void {
|
||||
if (!subView.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 子页滑出右侧,栏宽同时收缩
|
||||
paneDirection.value = "back";
|
||||
|
||||
if (popPane()) {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingNav.value = "replace";
|
||||
subView.value = "";
|
||||
}
|
||||
|
||||
// 选中项、编辑态、子页一起写回 query:刷新、分享链接、转屏都靠它恢复。
|
||||
// 合并成一次导航,避免多个 watcher 互相覆盖。
|
||||
if (options.syncQuery !== false) {
|
||||
watch([selectedId, editing], ([id, isEditing]) => {
|
||||
watch([selectedId, editing, subView], ([id, isEditing, view]) => {
|
||||
// 窄屏走的是整页详情路由,列表页不该再改 query 把它顶掉
|
||||
if (!isWide.value || route.path !== options.path) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextDetail = id || "";
|
||||
const nextView = isEditing && id ? "edit" : "";
|
||||
// 子页比编辑深一层,两者互斥:子页在时不再记编辑态
|
||||
const nextSub = id ? view : "";
|
||||
const nextView = isEditing && id && !nextSub ? "edit" : "";
|
||||
|
||||
if (String(route.query[queryKey] || "") === nextDetail
|
||||
&& String(route.query[viewKey] || "") === nextView) {
|
||||
&& String(route.query[viewKey] || "") === nextView
|
||||
&& String(route.query[subKey] || "") === nextSub) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -331,13 +417,14 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
// paneLevel 跟着 history 条目走:pop 时浏览器自动还原它,前进手势也能对上。
|
||||
// replace 只继承不抬升,取 min 是为了:直接带 ?detail=x 链接进来时这一条
|
||||
// 底下没有列表层,标成栏内层级的话再返回就会真的退出站点。
|
||||
const nextLevel = levelOf(id, isEditing);
|
||||
const nextLevel = levelOf(id, isEditing, nextSub);
|
||||
void router[nav]({
|
||||
path: options.path,
|
||||
query: {
|
||||
...route.query,
|
||||
[queryKey]: nextDetail || undefined,
|
||||
[viewKey]: nextView || undefined
|
||||
[viewKey]: nextView || undefined,
|
||||
[subKey]: nextSub || undefined
|
||||
},
|
||||
state: { paneLevel: nav === "push" ? nextLevel : Math.min(readPaneLevel(), nextLevel) }
|
||||
});
|
||||
@@ -351,15 +438,16 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
() => [
|
||||
route.path,
|
||||
String(route.query[queryKey] || ""),
|
||||
String(route.query[viewKey] || "")
|
||||
String(route.query[viewKey] || ""),
|
||||
String(route.query[subKey] || "")
|
||||
] as const,
|
||||
([path, nextId, nextView]) => {
|
||||
([path, nextId, nextView, nextSub]) => {
|
||||
if (!isWide.value || path !== options.path) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextEditing = nextView === "edit" && !!nextId;
|
||||
if (nextId === selectedId.value && nextEditing === editing.value) {
|
||||
const nextEditing = nextView === "edit" && !!nextId && !nextSub;
|
||||
if (nextId === selectedId.value && nextEditing === editing.value && nextSub === subView.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -370,12 +458,13 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
paneDirection.value = "back";
|
||||
pendingNav.value = "replace";
|
||||
editing.value = false;
|
||||
subView.value = "";
|
||||
selectedId.value = swapId;
|
||||
return;
|
||||
}
|
||||
|
||||
const nextLevel = levelOf(nextId, nextEditing);
|
||||
const previousLevel = levelOf(selectedId.value, editing.value);
|
||||
const nextLevel = levelOf(nextId, nextEditing, nextSub);
|
||||
const previousLevel = levelOf(selectedId.value, editing.value, subView.value);
|
||||
paneDirection.value = nextLevel < previousLevel
|
||||
? "back"
|
||||
: nextLevel > previousLevel
|
||||
@@ -384,6 +473,7 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
|
||||
selectedId.value = nextId;
|
||||
editing.value = nextEditing;
|
||||
subView.value = nextSub;
|
||||
|
||||
// 从保存过的编辑退回详情,让详情重新加载
|
||||
if (nextLevel < previousLevel && pendingSaved.value) {
|
||||
@@ -394,7 +484,18 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
);
|
||||
}
|
||||
|
||||
return { selectedId, editing, detailVersion, paneDirection, isWide, openDetail, openEdit };
|
||||
return {
|
||||
selectedId,
|
||||
editing,
|
||||
subView,
|
||||
detailVersion,
|
||||
paneDirection,
|
||||
isWide,
|
||||
openDetail,
|
||||
openEdit,
|
||||
openSub: requestSub,
|
||||
closeSub
|
||||
};
|
||||
}
|
||||
|
||||
/** useExpandToMaster 的配置。 */
|
||||
|
||||
Reference in New Issue
Block a user