fix: page-transition layout in app-shell

This commit is contained in:
Timi
2026-11-10 17:27:28 +08:00
parent a70a665883
commit 05a6cbc387
14 changed files with 1356 additions and 95 deletions
+1
View File
@@ -1,4 +1,5 @@
/AGENTS.md
/.workbuddy
# 文档站
/docs/.vitepress/cache
-7
View File
@@ -36,15 +36,9 @@ declare module 'vue' {
Space: typeof import('./src/components/space/index.vue')['default']
TBadge: typeof import('tdesign-mobile-vue')['Badge']
TButton: typeof import('tdesign-mobile-vue')['Button']
TCheckbox: typeof import('tdesign-mobile-vue')['Checkbox']
TColorPicker: typeof import('tdesign-mobile-vue')['ColorPicker']
TEmpty: typeof import('tdesign-mobile-vue')['Empty']
TForm: typeof import('tdesign-mobile-vue')['Form']
TFormItem: typeof import('tdesign-mobile-vue')['FormItem']
TIcon: typeof import('tdesign-mobile-vue')['Icon']
TImage: typeof import('tdesign-mobile-vue')['Image']
TImageViewer: typeof import('tdesign-mobile-vue')['ImageViewer']
TInput: typeof import('tdesign-mobile-vue')['Input']
TiptapCodeBlockButton: typeof import('./src/components/tiptap-editor/TiptapCodeBlockButton.vue')['default']
TiptapEditor: typeof import('./src/components/tiptap-editor/index.vue')['default']
TiptapIframeButton: typeof import('./src/components/tiptap-editor/TiptapIframeButton.vue')['default']
@@ -55,7 +49,6 @@ declare module 'vue' {
TiptapView: typeof import('./src/components/tiptap-view/index.vue')['default']
TLoading: typeof import('tdesign-mobile-vue')['Loading']
TPicker: typeof import('tdesign-mobile-vue')['Picker']
TPopover: typeof import('tdesign-mobile-vue')['Popover']
TPopup: typeof import('tdesign-mobile-vue')['Popup']
TTabBar: typeof import('tdesign-mobile-vue')['TabBar']
TTabBarItem: typeof import('tdesign-mobile-vue')['TabBarItem']
+6 -8
View File
@@ -13,19 +13,16 @@ import { timiWebSsrStub } from "./plugins/ssr-stub";
const docsDir = resolve(__dirname, "..");
/** 仓库根目录,组件源码位于它的 `src` 下。 */
const repoRoot = resolve(docsDir, "..");
/** timi-web 仓库目录,docs 通过 link 依赖它。 */
const timiWebRoot = resolve(repoRoot, "../timi-web");
/**
* 源码引用别名。
*
* demo 直接引用 `src` 源码而非构建产物,改组件即可热更新文档。
* 组件源码自身的传递依赖(tiptap、prismjs 等)由 timi-web 的 node_modules 解析,
* 组件源码自身的传递依赖(tiptap、prismjs 等)由 timi-web 包自身解析,
* docs 无需重复声明。
*/
const alias = {
"timi-tdesign-mobile": resolve(repoRoot, "src/index.ts"),
"timi-web/editor": resolve(timiWebRoot, "src/editor/index.ts")
"timi-tdesign-mobile": resolve(repoRoot, "src/index.ts")
};
/** 组件文档侧边栏,按 components.ts 的分类与顺序生成。 */
@@ -104,7 +101,8 @@ export default defineConfig({
vite: {
resolve: {
alias
alias,
dedupe: ["vue", "vue-router"]
},
plugins: [
@@ -123,8 +121,8 @@ export default defineConfig({
port: 3004,
host: true,
fs: {
// demo 需要读取 docs 之外的 src 与 timi-web 源码。
allow: [repoRoot, timiWebRoot]
// demo 需要读取 docs 之外的组件源码。
allow: [repoRoot]
}
}
}
@@ -120,7 +120,7 @@ onUnmounted(() => {
// 手机视口:移动组件在桌面宽度下会被拉伸,这里统一收到移动端宽度
.viewport {
width: 100%;
max-width: var(--tui-demo-viewport, 26rem);
max-width: var(--tui-demo-viewport, 32rem);
padding: 1rem;
overflow: hidden;
border: 1px solid var(--vp-c-divider);
@@ -204,4 +204,25 @@ onUnmounted(() => {
}
}
}
// VitePress 会在 reduced-motion 环境把所有 transition-duration 设为 0。
// 文档预览中的 PageTransition 用于验证组件转场,因此在预览范围内恢复原定时长。
@media (prefers-reduced-motion: reduce) {
.tui-demo {
:deep(.page-transition .push-left-enter-active) {
transition-duration: 520ms, 200ms !important;
}
:deep(.page-transition .push-left-leave-active),
:deep(.page-transition .push-right-enter-active),
:deep(.page-transition .push-right-leave-active) {
transition-duration: 520ms, 300ms !important;
}
:deep(.page-transition .fade-enter-active),
:deep(.page-transition .fade-leave-active) {
transition-duration: 200ms !important;
}
}
}
</style>
+1 -2
View File
@@ -16,7 +16,7 @@
## 基础用法
:::preview 三种形态 || 切换手机、竖屏平板与横屏平板,观察一级导航在底部与左侧之间切换
:::preview 三种形态与页面过渡 || 切换形态并点击一级导航,观察 rail 占位和内容页滑动范围
demo-preview=../demos/app-shell/basic.vue
@@ -142,7 +142,6 @@ const tabList = [
| `--tui-rail-width` | 左侧导航栏宽度,由 `rail-width` 写入 |
| `--tui-tabbar-height` | 底部标签栏高度,由 `tab-bar-height` 写入 |
| `--tui-fixed-left` | 内容区左边界;无导航栏时为 `0px` |
| `--tui-transition-inset-left` | 转场期间让出的导航区宽度,供 PageTransition 读取 |
外观都有默认值,按需覆盖:
+28 -12
View File
@@ -24,22 +24,38 @@
## 在带导航的外壳里使用
转场期间页面会脱离文档流铺满视口,这对全屏页面没问题,但放进 `AppShell` 这类带左侧导航的外壳时,整页滑入会盖住导航栏。
转场页面相对 `PageTransition` 容器定位,尺寸始终跟随容器。放进 `AppShell` 时,容器只占导航栏以外的内容区,rail 保持原位,页面只在内容区内滑动。
组件为此读取两个变量作为转场定位基准,`AppShell` 会自动写入:
因此 `PageTransition` 本身必须有明确的宽高;自定义外壳应把它放在内容区域中:
- `--tui-transition-inset-left`
- `--tui-transition-inset-top`
自定义外壳时同样赋值即可,不需要改组件:
```less
.my-shell {
--tui-transition-inset-left: 96px;
}
```vue
<template>
<div class="shell">
<nav>主导航</nav>
<div class="content">
<page-transition />
</div>
</div>
</template>
```
两个变量都没定义时退化为 `inset: 0`,与全屏行为一致。
```less
.shell {
height: 100vh;
display: flex;
}
nav {
width: 6rem;
flex: none;
}
.content {
flex: 1;
min-width: 0;
height: 100%;
}
```
## 缓存页面
+44
View File
@@ -0,0 +1,44 @@
<template>
<app-shell
v-model="active"
:items="tabList"
:mode="mode"
logo-text="糕"
@change="onTabChange"
>
<page-transition transition="push-left" />
</app-shell>
</template>
<script lang="ts" setup>
import { inject, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
AppShell,
PageTransition,
type AppShellItem,
type AppShellMode
} from "timi-tdesign-mobile";
import { appShellDemoModeKey } from "./context";
const mode = inject(appShellDemoModeKey, ref<AppShellMode>("wide"));
const route = useRoute();
const router = useRouter();
const tabList: AppShellItem[] = [
{ value: "/customers", icon: "usergroup", text: "客户", badge: 12 },
{ value: "/product", icon: "layers", text: "产品" },
{ value: "/store", icon: "shop-3", text: "门店" },
{ value: "/management", icon: "setting-1", text: "管理" }
];
const active = ref(route.path);
watch(() => route.path, path => {
active.value = path;
});
async function onTabChange(value: string | number): Promise<void> {
await router.push(String(value));
}
</script>
+62 -33
View File
@@ -12,30 +12,18 @@
<span v-text="item.text" />
</button>
</div>
<div class="frame">
<app-shell
v-model="active"
:items="tabList"
:mode="mode"
logo-text="糕"
>
<div class="page">
<header class="navbar">
<span v-text="activeText" />
</header>
<main class="content">
<p class="mode" v-text="`当前形态:${mode}`" />
<p class="desc" v-text="descText" />
</main>
</div>
</app-shell>
</div>
<div ref="frameRef" class="frame" />
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
import { AppShell, type AppShellMode } from "timi-tdesign-mobile";
import { createApp, defineComponent, h, onBeforeUnmount, onMounted, ref } from "vue";
import type { App, Component } from "vue";
import { createMemoryHistory, createRouter } from "vue-router";
import TDesignMobile from "tdesign-mobile-vue";
import type { AppShellMode } from "timi-tdesign-mobile";
import AppShellDemo from "./AppShellDemo.vue";
import { appShellDemoModeKey } from "./context";
const modeList: Array<{ value: AppShellMode; text: string }> = [
{ value: "phone", text: "手机" },
@@ -43,21 +31,62 @@ const modeList: Array<{ value: AppShellMode; text: string }> = [
{ value: "wide", text: "横屏平板" }
];
const tabList = [
{ value: "/customers", icon: "usergroup", text: "客户", badge: 12 },
{ value: "/product", icon: "layers", text: "产品" },
{ value: "/store", icon: "shop-3", text: "门店" },
{ value: "/management", icon: "setting-1", text: "管理" }
const mode = ref<AppShellMode>("wide");
const frameRef = ref<HTMLElement>();
let demoApp: App | undefined;
function createDemoPage(name: string, title: string, description: string): Component {
return defineComponent({
name,
setup() {
return () => h("div", { class: "page" }, [
h("header", { class: "navbar" }, title),
h("main", { class: "content" }, [
h("p", { class: "mode" }, `当前形态:${mode.value}`),
h("p", { class: "desc" }, description)
])
]);
}
});
}
const routeList = [
{ path: "/customers", name: "DemoCustomersPage", title: "客户", description: "客户页面通过真实路由切换。" },
{ path: "/product", name: "DemoProductPage", title: "产品", description: "左侧导航保持固定,内容页在可用区域内滑动。" },
{ path: "/store", name: "DemoStorePage", title: "门店", description: "窄屏时导航回到底部,页面过渡仍受内容容器约束。" },
{ path: "/management", name: "DemoManagementPage", title: "管理", description: "切换上方形态后,可观察不同宽度下的页面过渡范围。" }
];
const mode = ref<AppShellMode>("wide");
const active = ref("/customers");
const router = createRouter({
history: createMemoryHistory(),
routes: routeList.map(item => ({
path: item.path,
name: item.name,
component: createDemoPage(item.name, item.title, item.description)
}))
});
const activeText = computed(() => tabList.find(item => item.value === active.value)?.text || "");
async function mountDemo(): Promise<void> {
await router.push("/customers");
if (!frameRef.value) {
return;
}
const descText = computed(() => (mode.value === "wide"
? "横屏平板使用左侧导航栏,一级导航始终可见。"
: "手机与竖屏平板保留底部标签栏,把纵向空间留给内容。"));
demoApp = createApp(AppShellDemo);
demoApp.use(TDesignMobile);
demoApp.use(router);
demoApp.provide(appShellDemoModeKey, mode);
demoApp.mount(frameRef.value);
}
onMounted(() => {
void mountDemo();
});
onBeforeUnmount(() => {
demoApp?.unmount();
demoApp = undefined;
});
</script>
<style scoped lang="less">
@@ -85,7 +114,7 @@ const descText = computed(() => (mode.value === "wide"
}
.frame {
height: 340px;
height: 100%;
overflow: hidden;
border-radius: .75rem;
border: 1px solid var(--td-component-border);
@@ -95,7 +124,7 @@ const descText = computed(() => (mode.value === "wide"
}
}
.page {
:deep(.page) {
height: 100%;
display: flex;
flex-direction: column;
+4
View File
@@ -0,0 +1,4 @@
import type { InjectionKey, Ref } from "vue";
import type { AppShellMode } from "timi-tdesign-mobile";
export const appShellDemoModeKey: InjectionKey<Ref<AppShellMode>> = Symbol("AppShellDemoMode");
+3 -2
View File
@@ -11,8 +11,9 @@
},
"dependencies": {
"tdesign-mobile-vue": "^1.17.1",
"timi-web": "link:E:/WebProject/timi-web",
"vue": "^3.5.26"
"timi-web": "^1.0.2",
"vue": "^3.5.26",
"vue-router": "^4.5.0"
},
"devDependencies": {
"@types/node": "^25.0.8",
+1181 -22
View File
File diff suppressed because it is too large Load Diff
-3
View File
@@ -22,9 +22,6 @@
"paths": {
"timi-tdesign-mobile": [
"../src/index.ts"
],
"timi-web/editor": [
"../../timi-web/src/editor/index.ts"
]
}
},
+1 -2
View File
@@ -138,8 +138,7 @@ const tabBarHeightValue = computed(() => toSize(props.tabBarHeight, "52px"));
const shellStyle = computed<StyleValue>(() => ({
"--tui-rail-width": railWidthValue.value,
"--tui-tabbar-height": tabBarHeightValue.value,
"--tui-fixed-left": showRail.value ? railWidthValue.value : "0px",
"--tui-transition-inset-left": showRail.value ? railWidthValue.value : "0px"
"--tui-fixed-left": showRail.value ? railWidthValue.value : "0px"
}));
// ---------- 导航 ----------
+3 -3
View File
@@ -248,11 +248,11 @@ onUnmounted(() => {
:deep(.push-left-leave-active),
:deep(.push-right-enter-active),
:deep(.push-right-leave-active) {
// 转场期间页面脱离文档流铺满视口,用 inset 让出应用外壳的导航区域
inset: var(--tui-transition-inset-top, 0) 0 0 var(--tui-transition-inset-left, 0);
// 转场页面仍以 PageTransition 容器为定位基准,保留外壳内容区的尺寸。
inset: var(--tui-transition-inset-top, 0) 0 0;
width: auto;
height: auto;
position: fixed;
position: absolute;
transition: transform @duration @easing, opacity .2s linear;
backface-visibility: hidden;
}