mirror of
https://github.com/rommapp/romm.git
synced 2026-12-06 15:54:45 +01:00
feat: implement shared-element transitions for GameCard and GameDetails
This commit is contained in:
4 files changed
+121
-5
No files matched your search
@@ -18,9 +18,11 @@
|
||||
// * Label below the card, 11.5px, truncated
|
||||
// * Optional `hero` variant: 300×169 (16:9) + larger multi-line label
|
||||
import { computed, ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import type { SimpleRom } from "@/stores/roms";
|
||||
import GameActionBtn from "@/v2/components/GameActions/GameActionBtn.vue";
|
||||
import { useBackgroundArt } from "@/v2/composables/useBackgroundArt";
|
||||
import { useViewTransition } from "@/v2/composables/useViewTransition";
|
||||
import RIcon from "@/v2/lib/primitives/RIcon/RIcon.vue";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
@@ -85,6 +87,28 @@ const ratingLabel = computed(() => {
|
||||
const r = props.rom.rom_user?.rating;
|
||||
return r && r > 0 ? r.toString() : null;
|
||||
});
|
||||
|
||||
// Shared-element morph: when the user clicks through to GameDetails, tag
|
||||
// the card art so the browser pairs it with the destination cover and
|
||||
// animates between them. Modifier keys / middle-click fall through to
|
||||
// the regular router-link behaviour so opening in a new tab still works.
|
||||
const router = useRouter();
|
||||
const artEl = ref<HTMLElement | null>(null);
|
||||
const { morphTransition } = useViewTransition();
|
||||
|
||||
function onCardClick(e: MouseEvent) {
|
||||
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) {
|
||||
return;
|
||||
}
|
||||
if (!artEl.value) return;
|
||||
e.preventDefault();
|
||||
morphTransition(
|
||||
{ el: artEl.value, name: `rom-cover-${props.rom.id}` },
|
||||
async () => {
|
||||
await router.push(href.value);
|
||||
},
|
||||
);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -94,10 +118,15 @@ const ratingLabel = computed(() => {
|
||||
:class="{ 'r-gc--hero': hero, 'r-gc--focused': focused }"
|
||||
:aria-label="title"
|
||||
:data-rom-id="rom.id"
|
||||
@click="onCardClick"
|
||||
@mouseenter="onHighlight"
|
||||
@focus="onHighlight"
|
||||
>
|
||||
<div class="r-gc__art" :class="{ 'r-v2-shimmer': !imgLoaded && !imgError }">
|
||||
<div
|
||||
ref="artEl"
|
||||
class="r-gc__art"
|
||||
:class="{ 'r-v2-shimmer': !imgLoaded && !imgError }"
|
||||
>
|
||||
<img
|
||||
v-if="(coverUrl || fallbackUrl) && !(imgError && !fallbackUrl)"
|
||||
:src="
|
||||
|
||||
@@ -2,19 +2,31 @@
|
||||
// CoverColumn — fixed-width left column with the ROM cover (or a placeholder
|
||||
// if none resolves). Sized via --r-cover-w and top-aligned so tall right-
|
||||
// column content doesn't stretch the image.
|
||||
//
|
||||
// `romId` opts the cover into the shared-element morph from GameCard via
|
||||
// `view-transition-name: rom-cover-{id}`. Source side (GameCard) tags the
|
||||
// matching element imperatively at click time — see useViewTransition.
|
||||
import { computed } from "vue";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
src: string | null | undefined;
|
||||
alt: string;
|
||||
romId?: number | string;
|
||||
}>();
|
||||
|
||||
const morphStyle = computed(() =>
|
||||
props.romId != null
|
||||
? { viewTransitionName: `rom-cover-${props.romId}` }
|
||||
: undefined,
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="r-v2-det-cover">
|
||||
<img v-if="src" :src="src" :alt="alt" />
|
||||
<div v-else class="r-v2-det-cover__placeholder">
|
||||
<img v-if="src" :src="src" :alt="alt" :style="morphStyle" />
|
||||
<div v-else class="r-v2-det-cover__placeholder" :style="morphStyle">
|
||||
{{ alt }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// useViewTransition — tiny wrapper around the browser View Transitions API
|
||||
// for shared-element morphs (e.g. game card cover → GameDetails cover).
|
||||
//
|
||||
// Usage at the source side (the element you click):
|
||||
//
|
||||
// const { morphTransition } = useViewTransition();
|
||||
// function onClick(e: MouseEvent) {
|
||||
// e.preventDefault();
|
||||
// if (!coverEl.value) return router.push(href);
|
||||
// morphTransition(
|
||||
// { el: coverEl.value, name: `rom-cover-${rom.id}` },
|
||||
// () => router.push(href),
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// At the destination side, just paint a static `view-transition-name` on
|
||||
// the matching element — the browser pairs it with the source by name.
|
||||
// One element per name on screen at a time; we tag the source imperatively
|
||||
// so two visible cards with the same ROM never collide.
|
||||
//
|
||||
// Graceful degradation:
|
||||
// * `prefers-reduced-motion: reduce` → no transition, just navigate.
|
||||
// * Browser without `document.startViewTransition` → just navigate.
|
||||
|
||||
export interface MorphSource {
|
||||
el: HTMLElement;
|
||||
name: string;
|
||||
}
|
||||
|
||||
function isSupported(): boolean {
|
||||
return (
|
||||
typeof document !== "undefined" &&
|
||||
typeof document.startViewTransition === "function"
|
||||
);
|
||||
}
|
||||
|
||||
function prefersReducedMotion(): boolean {
|
||||
return (
|
||||
typeof window !== "undefined" &&
|
||||
window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
||||
);
|
||||
}
|
||||
|
||||
export function useViewTransition() {
|
||||
function morphTransition(
|
||||
source: MorphSource,
|
||||
navigate: () => void | Promise<void>,
|
||||
): void {
|
||||
if (!isSupported() || prefersReducedMotion()) {
|
||||
void navigate();
|
||||
return;
|
||||
}
|
||||
|
||||
// Tag the source element so the browser captures it as a separate
|
||||
// transition layer. The destination element carries the same name in
|
||||
// its template, so the snapshots get paired automatically.
|
||||
source.el.style.viewTransitionName = source.name;
|
||||
|
||||
const transition = document.startViewTransition(async () => {
|
||||
await navigate();
|
||||
});
|
||||
|
||||
// Clean up the inline style after the transition finishes — the
|
||||
// source element usually unmounts during navigate(), but if a route
|
||||
// keeps it alive (kept-alive view, error mid-nav, …) we don't want a
|
||||
// dangling view-transition-name on the page.
|
||||
transition.finished.finally(() => {
|
||||
if (source.el.isConnected) {
|
||||
source.el.style.viewTransitionName = "";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return { morphTransition, isSupported };
|
||||
}
|
||||
@@ -217,7 +217,7 @@ const tabs = computed(() => [
|
||||
<template>
|
||||
<section v-if="currentRom" class="r-v2-det">
|
||||
<div class="r-v2-det__body">
|
||||
<CoverColumn :src="resolvedCover" :alt="title" />
|
||||
<CoverColumn :src="resolvedCover" :alt="title" :rom-id="currentRom.id" />
|
||||
|
||||
<div class="r-v2-det__info">
|
||||
<GameHeader
|
||||
|
||||
Reference in new issue
Block a user