How To Add Page Transitions To NextJS 14 (including Exit Animations)

  Рет қаралды 29,424

Tom Is Loading

Tom Is Loading

Күн бұрын

Animated UI components for React, Tailwind CSS, Framer Motion & More ✨ www.hover.dev
Today we're going to check out how to create custom, flexible page transitions with the NextJS 14 app router. We'll do this without any fancy animation framework tricks. We will write our code in a way that it can be reused and built upon such that each of your pages can selectively animate however you need them to (including not at all!).
📚 Project Links
Code: github.com/TomIsLoading/next-...
🔗 My Links
TikTok: / tomisloading
Instagram: / tomisloading
GitHub: github.com/TomIsLoading
Twitter: / tomisloading
Discord & more: linktr.ee/tomisloading
Portfolio templates: tomisloading.gumroad.com/
Website: www.hover.dev
Timeline:
0:00 - Introduction
0:23 - Project overview
2:09 - Creating a custom link component
4:01 - Preventing default link behavior
6:42 - Adding our animation

Пікірлер: 92
@youssefkhaled2113
@youssefkhaled2113 29 күн бұрын
i guess this should work with any react application not just next js amazing work
@thearmbandit7645
@thearmbandit7645 29 күн бұрын
This is absolutely an amazing guide with an upload that is PERFECTLY timed. I've spent the last 2 days banging my head against the wall with these page transitions and now I can finally move forward and focus on working with framer motion on smaller things You are a true hero.
@ritikjangir718
@ritikjangir718 2 күн бұрын
This is what I was looking for. This is golden.
@aymenbachiri-yh2hd
@aymenbachiri-yh2hd 3 күн бұрын
Thank you so much Tom, please keep posting videos like this
@k0wal454
@k0wal454 24 күн бұрын
it is nice to support people like u. keep up the good work
@user-nb7sk4go9e
@user-nb7sk4go9e Ай бұрын
Bro literally upload video as soon as I needed it. Subscribed :)
@tomisloading
@tomisloading Ай бұрын
Haha thank you!! 😁
@JacquesOP
@JacquesOP 28 күн бұрын
Amazing work !
@MortezaPorabdolahi
@MortezaPorabdolahi 3 күн бұрын
Amazing stuff, Thanks ♥
@BooksWeCanRead
@BooksWeCanRead Ай бұрын
That was awesome! So well explained! 🙌👏👏
@tomisloading
@tomisloading Ай бұрын
Thank you! 😁
@StormKittenMeow
@StormKittenMeow Ай бұрын
I was thinking about this and you just uploaded
@tomisloading
@tomisloading Ай бұрын
Love when that happens 😁
@SanthoshKumar-dev
@SanthoshKumar-dev Ай бұрын
me too i just think like 5 hours before... man ru really witch or something... KUDOS TO THE GOOD WORK !!!!!! PLEASE DONT STOP POSTING VIDEOS !!!!!!! i will sponsor u if u want (i am broke)
@bozzhik
@bozzhik 29 күн бұрын
yooo dog. i was waiting for this. thx
@Deus-lo-Vuilt
@Deus-lo-Vuilt 28 күн бұрын
Wow , Thanks for video
@temitopedavid9508
@temitopedavid9508 29 күн бұрын
thank you tom
@vaggelis_best
@vaggelis_best Ай бұрын
I my god this is probably the best solution I have seen in tackling this problem. Well, it might also be the only complete one I have seen! Thank you very much
@tomisloading
@tomisloading Ай бұрын
Happy to help!! 😁
@KillerPhantom123
@KillerPhantom123 28 күн бұрын
This is gold!
@reubence_
@reubence_ 28 күн бұрын
Did something similar using the web transitions api - but was disappointed after I realised it doesn’t work on safari yet. This seems like a better approach. Heck it’s so easy I’m almost dumbfounded at myself for not figuring it out on my own.
@richardlepeuriancross5193
@richardlepeuriancross5193 27 күн бұрын
Eyyy. This cheered me up as was almost exactly the approach i took. Rather than remove the class via TransitionLink, consider an effect hook (useEfrect) which reacts to the pathname changing (usePathname). That was you ensure the page has loaded before you show it.
@tomisloading
@tomisloading 27 күн бұрын
Great idea!
@alexg7282
@alexg7282 28 күн бұрын
Thanks !
@ProLactationNation
@ProLactationNation 21 күн бұрын
Man.. If i would have the authority, I would give you a medal!
@tretiakdev
@tretiakdev 22 күн бұрын
Cool ❤
@caczus
@caczus 14 күн бұрын
You should try the next-view-transitions library from shuding
@xphstos_
@xphstos_ 25 күн бұрын
Great idea! Thanks! Quick question, since we're preventing the default event and using the router hook to navigate the user is there a reason we used specificaly the Link component? Couldn't that work also with a normal ancor element or a button or even a simple div?
@RobbPage
@RobbPage Ай бұрын
animate presence has always given me issues. will give this method a try. thanks!
@tomisloading
@tomisloading Ай бұрын
Same here!! Happy to help 🙂
@neociber24
@neociber24 28 күн бұрын
Great tutorial. A correction "use client" just means the element may be interactive on the client, in NextJS all elements are rendered on the server.
@VitorVelosoSA
@VitorVelosoSA 12 күн бұрын
Correct. And I believe this one of the most confusing things among the community in NextJS.
@tomascaetano4270
@tomascaetano4270 7 күн бұрын
This is done.
@M1a2n3o43
@M1a2n3o43 Ай бұрын
Could you add some of these popular transition animations to your website? Great video!
@tomisloading
@tomisloading Ай бұрын
Potentially, yea! A bit tough to do though given different routers will require different solutions
@ayannagorimsn
@ayannagorimsn 29 күн бұрын
@@tomisloading Please make one of the animation free(I would love to buy it but I cant afford it). And others paid if you want
@nothing-tj9eh
@nothing-tj9eh Ай бұрын
thankss for the tutorail can you give us small example how to set it up with framer motion cause i couldn't get it to work with app router
@tomisloading
@tomisloading Ай бұрын
I’ve personally just stopped using Framer Motion for page transitions with the app router, at least with animate presence. If you wanted to use something like useAnimate, you could add a ref to the body element then pass it down through context or something I suppose 😁
@KarnakOvy8
@KarnakOvy8 Ай бұрын
you can also use the template.tsx component, which is like a layout, but re-render every time
@RegalWK
@RegalWK 24 күн бұрын
Does this new TransitionLink component support prefetching? I assume yes since its native Link component with use client directive
@sahilverma_dev
@sahilverma_dev Ай бұрын
Finally
@tomisloading
@tomisloading Ай бұрын
😎
@TutoDS2014
@TutoDS2014 29 күн бұрын
Do you know a good tutorial to do animations on solid (using solid start)?
@moodyhamoudi
@moodyhamoudi Ай бұрын
Really smart solution. Question though, would the fixed sleep times cause issues for slow connections?
@tomisloading
@tomisloading Ай бұрын
It could! Another solution could be to have your links handle only exit animations, then have each individual page handle their own entrance animations
@moodyhamoudi
@moodyhamoudi Ай бұрын
@@tomisloading ur cooking bro
@MauricioAndrian
@MauricioAndrian 29 күн бұрын
What about the new native page transitions animations? Do they work?
@ikirbz
@ikirbz 29 күн бұрын
2 things "Auto Rename Tag" plugin "trigger suggest" keyboard shortcut (usually ctrl + space)
@alishakhatri6919
@alishakhatri6919 22 күн бұрын
hey, can you make a video about Opposite Scroll Content ?
@abdrahmanoladimeji4698
@abdrahmanoladimeji4698 10 күн бұрын
Hi, pls if I want to use this to create a kind of preloader, different from default Next Loading file whereby it'll override everything and get displayed first, how do I go about it?
@inderjotsingh5868
@inderjotsingh5868 5 күн бұрын
i have seen people doing page transitions using template.ts file in next , do we have any performance implications using that ?
@chopsueey
@chopsueey 29 күн бұрын
awesome! but couldn't you just use a button element instead of the nextjs link-component, because you are essentially using a programmatical approach for page-navigation (useRouter)? or is next/link still beneficial or necessary?
@ayannagorimsn
@ayannagorimsn 29 күн бұрын
I dont think it is needed We can use a button or a tag as well I did it with button only
@chopsueey
@chopsueey 29 күн бұрын
@@ayannagorimsn ah, good to know, thx the docs say that the Link-component provide prefetching for the route in nextjs, so probably better to use it
@MauricioAndrian
@MauricioAndrian 29 күн бұрын
Using buttons for page navigation breaks a11y. Try Ctrl/Cmd + Left Click in both and you'll see.
@ibnurasikh
@ibnurasikh Ай бұрын
I was also thinking about this, but there's a potential issue. The method might exhibit strange behavior if the content doesn't prefetch. This could cause a noticeable pause after the animation finishes, as the new page content has to be fetched and rendered, right?
@tomisloading
@tomisloading Ай бұрын
True, that was the idea with additional wait after redirecting to a new page. If you DID see this becoming an issue, I would just have each page define their own entrance animations so that they only occur once the page actually loads.
@ibnurasikh
@ibnurasikh 29 күн бұрын
​@@tomisloading Hmm, I think you should prefetch before the exit animation starts. In your code, fetching seems to happen after the animation ends, i.e., after `sleep(500)` finishes. Is there a reason you’re not implementing prefetch first using `router.prefetch(url, as, options)`?
@jou3883
@jou3883 23 күн бұрын
Hi Tom! I implemented this in my current nextjs project. Somehow if I call the sleep function before removing the 'page-transition' class, it doesnt navigate to the page I clicked on immediately (which is what its supposed to do since we setTimeout to 500ms), but the previous page is still being displayed by like 2s before the page that is clicked on shows. But when I removed the sleep function after router pushes to clicked href, it worked. could this be a nextjs bug? or I'm doing something wrong? This only happens when I refresh the page and quickly navigates to other pages.
@amansarma417
@amansarma417 29 күн бұрын
Would this approach work if the user clicks back button after navigating to the page made by custom link component?
@MauricioAndrian
@MauricioAndrian 29 күн бұрын
No, this only run 'onClick'ing the component. Anything else, won't work.
@amansarma417
@amansarma417 28 күн бұрын
@@MauricioAndrian I've implemented my own solution using a custom hook which runs on every page navigation via useEffect and ads a layer of transition.
@aliventurous
@aliventurous 21 күн бұрын
@@amansarma417please share
@Smartercow
@Smartercow 14 күн бұрын
Instead of the sleep function there's this: *import { setTimeout } from "node:timers/promises";* - It does the same and it's async
@habibthephantom7040
@habibthephantom7040 4 күн бұрын
But this is client component not server one
@felipefregginrules
@felipefregginrules Ай бұрын
This obvi works on the pages router too right?
@tomisloading
@tomisloading Ай бұрын
It does!
@DouglasSouza-te9bt
@DouglasSouza-te9bt 28 күн бұрын
Theme pls
@TrungNguyen-fi5ls
@TrungNguyen-fi5ls 19 күн бұрын
I thought we couldn't manipulate DOM ??? 8:23
@visheshjhadi1930
@visheshjhadi1930 24 күн бұрын
There are few issues with this approach 1. SEO as all the links will be on client side then server side 2. The back and forward button will not trigger the transition 3. If you go to network tab and slow the throttling then you will notice that the transition starts and end on the same page. If anyone have the solution to this please let me know
@user-fr9we8vq7x
@user-fr9we8vq7x 29 күн бұрын
This can really hurt the seo, as you are removing all of the links from server side rendering. I would do some king of a nested client component inside a server component, so that the link is actually rendered on the server, and then the client adds transition logic on top of it. Great tutorial!
@MauricioAndrian
@MauricioAndrian 29 күн бұрын
You are already breaking SEO (adding "use client") if you try to style the links based on current pathname which is required in 90% of all designs.
@PrinzEugen39
@PrinzEugen39 19 күн бұрын
SEO this SEO that
@nandanchoudhary9798
@nandanchoudhary9798 28 күн бұрын
to explain the idea it's good But, No real world application uses such approach. it's not necessary that new page would load within 500ms
@null_spacex
@null_spacex 15 күн бұрын
There's a difference between applications and websites.
@nicolasmayorga8288
@nicolasmayorga8288 28 күн бұрын
I mean, it's good but not recommended cause in this way you're not getting pre-fetching and caching data in each link.
@zlatkoiliev8927
@zlatkoiliev8927 29 күн бұрын
Aaand this is really bad practice, because we loose all good features of the next link, like pre-fetching, and caching. Only because we want to have cool transition animations…
@richardlepeuriancross5193
@richardlepeuriancross5193 27 күн бұрын
Use the next link inside Transition link?
@youhabbo01
@youhabbo01 27 күн бұрын
@@richardlepeuriancross5193 e.preventDefault ?
@fathinfadhil
@fathinfadhil 26 күн бұрын
he literally pass down the props to next's Link component, it'll work fineee
@waleedbukhari8988
@waleedbukhari8988 11 күн бұрын
​@@richardlepeuriancross5193at some point he is right, link works fine as a server component while transition link is a custom client component, you do loose preloading and some next features i guess. Use it wisely. Even if it is a nextlink. But it has a client wrapper on it.
@sebastiancasal8589
@sebastiancasal8589 6 күн бұрын
But he is using next link, he is just adding a momentary modification of the pre rendered dom, the main component still are pre rendered in the server. It shouldn’t impact in the performance of the site
@uditnagar7905
@uditnagar7905 27 күн бұрын
ezsnippet copied today's insta reel from here 🤣🤣🤣
@ashishkumawat6110
@ashishkumawat6110 17 күн бұрын
That's just vanilla JS stuff
@null_spacex
@null_spacex 15 күн бұрын
But it's the way to do it in Next App Router right now..
@znkdev
@znkdev 29 күн бұрын
pretty dope! but way too much work.
My Top 5 Techniques for Web Animation
9:58
Olivier Larose
Рет қаралды 63 М.
Fetching Data Doesn't Get Better Than This
6:58
Josh tried coding
Рет қаралды 102 М.
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 10 МЛН
Useful gadget for styling hair 🤩💖 #gadgets #hairstyle
00:20
FLIP FLOP Hacks
Рет қаралды 10 МЛН
10 Tailwind Classes I Wish I Knew Earlier
13:31
Web Dev Simplified
Рет қаралды 173 М.
I can’t believe they built this in React
16:08
Theo - t3․gg
Рет қаралды 114 М.
The Easy Way to Design Top Tier Websites
11:54
Sajid
Рет қаралды 213 М.
I DONT USE NEXT JS
54:01
ThePrimeTime
Рет қаралды 341 М.
TailwindCSS Is Ugly. Here's How To Deal With It.
10:32
Tom Is Loading
Рет қаралды 7 М.
GSAP vs Framer Motion for React
10:24
Olivier Larose
Рет қаралды 17 М.
5 MORE TailwindCSS Tips I Wish I Learned Earlier
5:32
Tom Is Loading
Рет қаралды 12 М.
The Right Way To Build REST APIs
10:07
Awesome
Рет қаралды 83 М.
#samsung #retrophone #nostalgia #x100
0:14
mobijunk
Рет қаралды 13 МЛН
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 11 МЛН
Tag him😳💕 #miniphone #iphone #samsung #smartphone #fy
0:11
Pockify™
Рет қаралды 3,1 МЛН