add PageTransition

This commit is contained in:
Timi
2026-09-22 21:27:08 +08:00
parent 37cf38fe27
commit 45f77d3168
7 changed files with 470 additions and 1 deletions
+333
View File
@@ -0,0 +1,333 @@
<template>
<div
class="page-transition"
:class="{ 'page-transition-reduced-motion': props.respectReducedMotion }"
>
<RouterView v-slot="{ Component, route }">
<Transition
:name="transitionName"
:mode="props.transitionMode"
:appear="props.appear"
>
<KeepAlive :include="keepAlivePageNames" :max="props.keepAliveMax">
<component
:is="isKeepAlive(route) ? CachedPage : RoutePage"
:key="resolveRouteKey(route)"
:component="Component"
:background="getPageBackground(route)"
:class="props.pageClass"
:style="props.pageStyle"
/>
</KeepAlive>
</Transition>
</RouterView>
</div>
</template>
<script lang="ts" setup>
import { defineComponent, h, onUnmounted, ref, watch } from "vue";
import type { Component, PropType, StyleValue } from "vue";
import { RouterView, useRoute, useRouter } from "vue-router";
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from "vue-router";
import type {
PageTransitionProps,
PageTransitionRoute
} from "./type";
defineOptions({
name: "PageTransition"
});
interface RoutePageProps {
component: Component;
background?: string;
}
const routePageProps = {
component: {
type: null as unknown as PropType<Component>,
required: true
},
background: String
};
function createRoutePage(name: string) {
return defineComponent({
name,
inheritAttrs: false,
props: routePageProps,
setup(pageProps: RoutePageProps, { attrs }) {
return () => h(
"div",
{
...attrs,
class: ["page-transition-page", attrs.class],
style: [attrs.style as StyleValue, pageProps.background === undefined ? undefined : { background: pageProps.background }]
},
[h(pageProps.component)]
);
}
});
}
const RoutePage = createRoutePage("PageTransitionPage");
const CachedPage = createRoutePage("PageTransitionKeepAlivePage");
const keepAlivePageNames = ["PageTransitionKeepAlivePage"];
const props = defineProps({
transition: {
type: [String, Function, Boolean] as PropType<PageTransitionProps["transition"]>,
default: "auto"
},
transitionMode: String as PropType<PageTransitionProps["transitionMode"]>,
appear: Boolean,
respectReducedMotion: {
type: Boolean,
default: true
},
keepAlive: {
type: [Boolean, Function] as PropType<PageTransitionProps["keepAlive"]>,
default: false
},
keepAliveMax: Number,
getRouteDepth: Function as PropType<PageTransitionProps["getRouteDepth"]>,
routeDepthQueryKey: {
type: String,
default: "transitionDepth"
},
routeDepthMetaKey: {
type: String,
default: "depth"
},
routeDepthBaseMetaKey: {
type: String,
default: "baseDepth"
},
routeDepthDynamicMetaKey: {
type: String,
default: "dynamicDepth"
},
getRouteKey: Function as PropType<PageTransitionProps["getRouteKey"]>,
pageBackground: [String, Function] as PropType<PageTransitionProps["pageBackground"]>,
pageClass: [String, Array, Object] as PropType<PageTransitionProps["pageClass"]>,
pageStyle: [String, Array, Object] as PropType<PageTransitionProps["pageStyle"]>
});
const router = useRouter();
const route = useRoute();
const transitionName = ref(resolveTransitionName(route, route));
function resolveTransitionName(
to: PageTransitionRoute,
from: PageTransitionRoute
): string {
const transition = props.transition ?? "auto";
if (transition === false) {
return "";
}
if (typeof transition === "function") {
const name = transition(to, from);
return name === false ? "" : name;
}
if (transition !== "auto") {
return transition;
}
const toDepth = resolveRouteDepth(to);
const fromDepth = resolveRouteDepth(from);
if (toDepth === undefined || fromDepth === undefined || toDepth === fromDepth) {
return "";
}
return fromDepth < toDepth ? "push-left" : "push-right";
}
function resolveRouteDepth(sourceRoute: PageTransitionRoute): number | undefined {
if (props.getRouteDepth) {
return props.getRouteDepth(sourceRoute);
}
const queryDepth = parseDepth(sourceRoute.query[props.routeDepthQueryKey ?? "transitionDepth"]);
if (queryDepth !== undefined) {
return queryDepth;
}
const meta = sourceRoute.meta as Record<string, unknown>;
const depthKey = props.routeDepthMetaKey ?? "depth";
const depth = parseDepth(meta[depthKey]);
const dynamicKey = props.routeDepthDynamicMetaKey ?? "dynamicDepth";
if (meta[dynamicKey] === true) {
const baseDepth = parseDepth(meta[props.routeDepthBaseMetaKey ?? "baseDepth"]) ?? depth ?? 0;
const pathSegments = sourceRoute.path.split("/").filter(Boolean);
return baseDepth + Math.max(0, pathSegments.length - 1);
}
if (depth !== undefined) {
return depth;
}
return sourceRoute.matched.length;
}
function parseDepth(value: unknown): number | undefined {
if (Array.isArray(value)) {
return parseDepth(value[0]);
}
if (typeof value !== "string" && typeof value !== "number") {
return undefined;
}
if (typeof value === "string" && !value.trim()) {
return undefined;
}
const depth = typeof value === "number" ? value : Number(value.trim());
if (!Number.isInteger(depth) || depth < 0) {
return undefined;
}
return depth;
}
function isKeepAlive(sourceRoute: RouteLocationNormalizedLoaded): boolean {
const matcher = props.keepAlive;
if (typeof matcher === "function") {
return matcher(sourceRoute);
}
return matcher === true;
}
function resolveRouteKey(sourceRoute: RouteLocationNormalizedLoaded): PropertyKey {
const resolver = props.getRouteKey;
return resolver ? resolver(sourceRoute) : sourceRoute.path;
}
function getPageBackground(sourceRoute: RouteLocationNormalizedLoaded): string {
const background = props.pageBackground;
if (typeof background === "function") {
return background(sourceRoute) ?? "var(--td-bg-color-page, transparent)";
}
return background ?? "var(--td-bg-color-page, transparent)";
}
const unregisterGuard = router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized) => {
transitionName.value = resolveTransitionName(to, from);
});
watch(
() => props.transition,
() => {
transitionName.value = resolveTransitionName(route, route);
}
);
onUnmounted(() => {
unregisterGuard();
});
</script>
<style scoped lang="less">
@easing: cubic-bezier(.19, 1, .22, 1);
@duration: 520ms;
@brightness: .98;
@shadow-intensity: .15;
@page-offset: 25%;
.page-transition {
position: relative;
z-index: 0;
width: 100%;
height: 100%;
overflow-x: clip;
isolation: isolate;
:deep(.page-transition-page) {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: auto;
overflow-x: clip;
background: var(--td-bg-color-page, transparent);
}
:deep(.push-left-enter-active),
:deep(.push-left-leave-active),
:deep(.push-right-enter-active),
:deep(.push-right-leave-active) {
width: 100%;
height: 100%;
position: fixed;
transition: transform @duration @easing, opacity .2s linear;
backface-visibility: hidden;
}
:deep(.push-left-enter-from) {
box-shadow: -4px 0 16px rgba(0, 0, 0, @shadow-intensity);
transform: translate3d(100%, 0, 0);
}
:deep(.push-left-enter-to) {
transform: translate3d(0, 0, 0);
}
:deep(.push-left-leave-active) {
transition: all @duration @easing, filter .3s;
}
:deep(.push-left-leave-to) {
filter: brightness(@brightness);
transform: translate3d(-@page-offset, 0, 0);
}
:deep(.push-right-enter-active) {
z-index: 0;
transition: transform @duration @easing, opacity .3s;
}
:deep(.push-right-enter-from) {
transform: translate3d(-@page-offset, 0, 0);
}
:deep(.push-right-enter-to) {
transform: translate3d(0, 0, 0);
}
:deep(.push-right-leave-active) {
z-index: 1;
transition: transform @duration @easing, box-shadow .3s;
}
:deep(.push-right-leave-from) {
transform: translate3d(0, 0, 0);
}
:deep(.push-right-leave-to) {
box-shadow: 4px 0 16px rgba(0, 0, 0, @shadow-intensity);
transform: translate3d(105%, 0, 0);
}
:deep(.fade-enter-active),
:deep(.fade-leave-active) {
transition: opacity .2s ease;
}
:deep(.fade-enter-from),
:deep(.fade-leave-to) {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.page-transition.page-transition-reduced-motion {
:deep(.push-left-enter-active),
:deep(.push-left-leave-active),
:deep(.push-right-enter-active),
:deep(.push-right-leave-active),
:deep(.fade-enter-active),
:deep(.fade-leave-active) {
transition: none;
}
}
}
</style>