Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions examples/react/view-transitions/src/routes/posts.route.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createFileRoute } from '@tanstack/react-router'
import { createFileRoute, useRouter } from '@tanstack/react-router'
import * as React from 'react'
import { Link, Outlet } from '@tanstack/react-router'
import { fetchPosts } from '../posts'
Expand All @@ -10,6 +10,7 @@ export const Route = createFileRoute('/posts')({

function PostsLayoutComponent() {
const posts = Route.useLoaderData()
const router = useRouter()

return (
<div className="p-2 flex gap-2 [view-transition-name:main-content]">
Expand All @@ -25,8 +26,30 @@ function PostsLayoutComponent() {
}}
className="block py-1 text-blue-600 hover:opacity-75"
activeProps={{ className: 'font-bold underline' }}
// see styles.css for 'warp' transition
viewTransition={{ types: ['warp'] }}
// see styles.css for 'warp' and 'warp-backwards' transition
viewTransition={{
types: ({ fromLocation, toLocation }) => {
const fromRoute = router
.matchRoutes(fromLocation?.pathname ?? '/')
.find((entry) => entry.routeId === '/posts/$postId')
const toRoute = router
.matchRoutes(toLocation?.pathname ?? '/')
.find((entry) => entry.routeId === '/posts/$postId')

const fromIndex = Number(fromRoute?.params.postId)
const toIndex = Number(toRoute?.params.postId)

if (
Number.isNaN(fromIndex) ||
Number.isNaN(toIndex) ||
fromIndex === toIndex
) {
return false // no transition
}

return fromIndex > toIndex ? ['warp-backwards'] : ['warp']
},
}}
>
<div>{post.title.substring(0, 20)}</div>
</Link>
Expand Down
36 changes: 36 additions & 0 deletions examples/react/view-transitions/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ html:active-view-transition-type(warp) {
}
}

html:active-view-transition-type(warp-backwards) {
&::view-transition-old(post) {
animation: 400ms ease-out both warp-out-backwards;
}

&::view-transition-new(post) {
animation: 400ms ease-out both warp-in-backwards;
}
}

@keyframes warp-out {
from {
opacity: 1;
Expand All @@ -112,3 +122,29 @@ html:active-view-transition-type(warp) {
transform: scale(1) rotate(0deg);
}
}

@keyframes warp-in-backwards {
from {
opacity: 0;
filter: blur(15px) brightness(1.8);
transform: scale(0.9) rotate(45deg);
}
to {
opacity: 1;
filter: blur(0) brightness(1);
transform: scale(1) rotate(0deg);
}
}

@keyframes warp-out-backwards {
from {
opacity: 1;
filter: blur(0) brightness(1);
transform: scale(1) rotate(0deg);
}
to {
opacity: 0;
filter: blur(15px) brightness(1.8);
transform: scale(1.1) rotate(-90deg);
}
}