-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend useRouter to also use transitions #18
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Some additional thoughts after tinkering further with this.
|
feat: memoize transition router fix: remove unused imports
I thought I'd give more context on what this PR does by adding a clear example. Sometimes it's hard to see it in the code. The changes so far are two:
The follow snippet provides a simple example of using programatic navigation. import { useTransitionRouter } from 'next-view-transitions'
import { useState } from "react";
export default function Page() {
const [withCustomTransition, setWithCustomTransition] = useState(true)
const router = useTransitionRouter();
const routerNavigate = () => {
router.push('/path/to/page', {
onTransitionReady: withCustomTransition ? slideInOut: undefined,
});
}
return (
// Markup with an onWhatever event that calls `routerNavigate`
)
} In the example, regular function slideInOut() {
// Outgoing page slides out towards the left while fading out.
document.documentElement.animate(
[
{
opacity: 1,
transform: 'translate(0, 0)',
},
{
opacity: 0,
transform: 'translate(-100%, 0)',
},
],
{
duration: 500,
easing: 'ease-in-out',
fill: 'forwards',
pseudoElement: '::view-transition-old(root)',
}
);
// Incoming page slides from the right while fading in.
document.documentElement.animate(
[
{
opacity: 0,
transform: 'translate(100%, 0)',
},
{
opacity: 1,
transform: 'translate(0, 0)',
},
],
{
duration: 500,
easing: 'ease-in-out',
fill: 'forwards',
pseudoElement: '::view-transition-new(root)',
}
);
} That's pretty much it. This code is already in the repo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! This is a great PR.
Related to #17
This PR is a quick POC to check whether extending the Next.js
useRouter
hook to enable transitions would work.So far I've tried it only on the example demo and a very quick project I'm developing where I want to use programmatic transitions.
Would it be interesting to develop this further?
Some things I'm thinking:
Exporting a customuseRouter
, just likeLink
. Acceptable or confusing naming?push
andreplace
only. Anything else would make sense?Also madepush
andreplace
stable withuseCallback
, assuming the original Next.js methods are also stable, but maybe it's preferrable to memoize the returned "router" object.Link
supports custom key modifiers, which I assume are Next-specific.