enhance(frontend): 通常のRouterViewにTransitionを追加
This commit is contained in:
parent
a865a949b5
commit
abddd40c09
|
@ -69,6 +69,8 @@ const emit = defineEmits<{
|
||||||
(ev: 'update:tab', key: string);
|
(ev: 'update:tab', key: string);
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
const viewId = inject(DI.viewId);
|
||||||
|
const viewTransitionName = computed(() => `${viewId}---pageHeader`);
|
||||||
const injectedPageMetadata = inject(DI.pageMetadata);
|
const injectedPageMetadata = inject(DI.pageMetadata);
|
||||||
const pageMetadata = computed(() => props.overridePageMetadata ?? injectedPageMetadata.value);
|
const pageMetadata = computed(() => props.overridePageMetadata ?? injectedPageMetadata.value);
|
||||||
|
|
||||||
|
@ -140,6 +142,7 @@ onUnmounted(() => {
|
||||||
backdrop-filter: var(--MI-blur, blur(15px));
|
backdrop-filter: var(--MI-blur, blur(15px));
|
||||||
border-bottom: solid 0.5px var(--MI_THEME-divider);
|
border-bottom: solid 0.5px var(--MI_THEME-divider);
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
view-transition-name: v-bind(viewTransitionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
.upper,
|
.upper,
|
||||||
|
|
|
@ -4,7 +4,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="_pageContainer" style="height: 100%;">
|
<div ref="rootEl" class="_pageContainer" :class="$style.root">
|
||||||
<KeepAlive :max="prefer.s.numberOfPageCache">
|
<KeepAlive :max="prefer.s.numberOfPageCache">
|
||||||
<Suspense :timeout="0">
|
<Suspense :timeout="0">
|
||||||
<component :is="currentPageComponent" :key="key" v-bind="Object.fromEntries(currentPageProps)"/>
|
<component :is="currentPageComponent" :key="key" v-bind="Object.fromEntries(currentPageProps)"/>
|
||||||
|
@ -18,11 +18,13 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { inject, provide, ref, shallowRef } from 'vue';
|
import { inject, nextTick, onMounted, provide, ref, shallowRef, useTemplateRef } from 'vue';
|
||||||
import type { Router } from '@/router.js';
|
import type { Router } from '@/router.js';
|
||||||
import { prefer } from '@/preferences.js';
|
import { prefer } from '@/preferences.js';
|
||||||
import MkLoadingPage from '@/pages/_loading_.vue';
|
import MkLoadingPage from '@/pages/_loading_.vue';
|
||||||
import { DI } from '@/di.js';
|
import { DI } from '@/di.js';
|
||||||
|
import { randomId } from '@/utility/random-id.js';
|
||||||
|
import { deepEqual } from '@/utility/deep-equal.js';
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
router?: Router;
|
router?: Router;
|
||||||
|
@ -34,18 +36,76 @@ if (router == null) {
|
||||||
throw new Error('no router provided');
|
throw new Error('no router provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const viewId = randomId();
|
||||||
|
provide(DI.viewId, viewId);
|
||||||
|
|
||||||
const currentDepth = inject(DI.routerCurrentDepth, 0);
|
const currentDepth = inject(DI.routerCurrentDepth, 0);
|
||||||
provide(DI.routerCurrentDepth, currentDepth + 1);
|
provide(DI.routerCurrentDepth, currentDepth + 1);
|
||||||
|
|
||||||
|
const rootEl = useTemplateRef('rootEl');
|
||||||
|
onMounted(() => {
|
||||||
|
rootEl.value.style.viewTransitionName = viewId; // view-transition-nameにcss varが使えないっぽいため直接代入
|
||||||
|
});
|
||||||
|
|
||||||
|
// view-transition-newなどの<pt-name-selector>にはcss varが使えず、v-bindできないため直接スタイルを生成
|
||||||
|
const viewTransitionStylesTag = document.createElement('style');
|
||||||
|
viewTransitionStylesTag.textContent = `
|
||||||
|
@keyframes ${viewId}-old {
|
||||||
|
to { transform: scale(0.95); opacity: 0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes ${viewId}-new {
|
||||||
|
from { transform: scale(0.95); opacity: 0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
::view-transition-old(${viewId}) {
|
||||||
|
animation-duration: 0.2s;
|
||||||
|
animation-name: ${viewId}-old;
|
||||||
|
}
|
||||||
|
|
||||||
|
::view-transition-new(${viewId}) {
|
||||||
|
animation-duration: 0.2s;
|
||||||
|
animation-name: ${viewId}-new;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
window.document.head.appendChild(viewTransitionStylesTag);
|
||||||
|
|
||||||
const current = router.current!;
|
const current = router.current!;
|
||||||
const currentPageComponent = shallowRef('component' in current.route ? current.route.component : MkLoadingPage);
|
const currentPageComponent = shallowRef('component' in current.route ? current.route.component : MkLoadingPage);
|
||||||
const currentPageProps = ref(current.props);
|
const currentPageProps = ref(current.props);
|
||||||
|
let currentRoutePath = current.route.path;
|
||||||
const key = ref(router.getCurrentFullPath());
|
const key = ref(router.getCurrentFullPath());
|
||||||
|
|
||||||
router.useListener('change', ({ resolved }) => {
|
router.useListener('change', ({ resolved }) => {
|
||||||
if (resolved == null || 'redirect' in resolved.route) return;
|
if (resolved == null || 'redirect' in resolved.route) return;
|
||||||
currentPageComponent.value = resolved.route.component;
|
if (resolved.route.path === currentRoutePath && deepEqual(resolved.props, currentPageProps.value)) return;
|
||||||
currentPageProps.value = resolved.props;
|
|
||||||
key.value = router.getCurrentFullPath();
|
function _() {
|
||||||
|
currentPageComponent.value = resolved.route.component;
|
||||||
|
currentPageProps.value = resolved.props;
|
||||||
|
key.value = router.getCurrentFullPath();
|
||||||
|
currentRoutePath = resolved.route.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
|
if (prefer.s.animation && document.startViewTransition) {
|
||||||
|
document.startViewTransition(() => new Promise((res) => {
|
||||||
|
_();
|
||||||
|
nextTick(() => {
|
||||||
|
res();
|
||||||
|
//setTimeout(res, 100);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
_();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" module>
|
||||||
|
.root {
|
||||||
|
height: 100%;
|
||||||
|
background-color: var(--MI_THEME-bg);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
|
@ -11,4 +11,5 @@ export const DI = {
|
||||||
router: Symbol() as InjectionKey<Router>,
|
router: Symbol() as InjectionKey<Router>,
|
||||||
mock: Symbol() as InjectionKey<boolean>,
|
mock: Symbol() as InjectionKey<boolean>,
|
||||||
pageMetadata: Symbol() as InjectionKey<Ref<Record<string, any>>>,
|
pageMetadata: Symbol() as InjectionKey<Ref<Record<string, any>>>,
|
||||||
|
viewId: Symbol() as InjectionKey<string>,
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
const CHARS = 'abcdefghijklmnopqrstuvwxyz'; // CSSの<custom-ident>などで使われることもあるのでa-z以外使うな
|
||||||
|
|
||||||
|
export function randomId(length = 32, characters = CHARS) {
|
||||||
|
let result = '';
|
||||||
|
const charactersLength = characters.length;
|
||||||
|
for ( let i = 0; i < length; i++ ) {
|
||||||
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in New Issue