GithubHelp home page GithubHelp logo

cjimmy / next-spring-transition Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 326 KB

Next.js + react-spring page transitions demo

Home Page: https://next-spring-transition.vercel.app

JavaScript 86.78% CSS 13.22%
nextjs react-spring transition-animation animations-demostration

next-spring-transition's Introduction

Next.js + react-spring

This is a minimal demo Next.js app of using react-spring, an animation library, for page transitions.

GIF of what the demo page looks like

๐Ÿ‘‰ See the demo: next-spring-transition.vercel.app

Animations

Two types of page transition animations are coded in.

  1. Scale effect, global โ€“ This effect scales everything from 1.1x -> 1.0x, effectively looking like it's moving down in the z-axis. It is a global effect that happens at the start of every new page load, and is coded in pages/_app.js. I've seen other repos tackle this problem, but many were made before hooks were popularized. This method is a low-touch way to add animations to route changes without having to add code to each page.
  2. Intro and outro โ€“ The main problem this repo aims to show is how to choreograph intro and outro animations. For simplicity, the outro animations are simply the intro animations but in reverse (and it looks fine). You can look at index.js, about.js or blog/[postId].js as examples. (They are all nearly identical, to keep the example simple.)

The key trick (for #2)

The key trick to initiating an outro animation is a combination of

  • Using react-spring's useChain hook to easily sequence animations with timing.
  • Within in each animation (e.g. useSpring, useTrail), using the reverse param to essentially run the animation backwards.
  • Delaying the route change by toggling a state var and when the last animation finishes, then using router.push() to the new location. This is made possible by attaching a handler on the onRest param of react-spring animations. Unfortunately, this also requies bypassing the Next-provided <Link> and setting a couple of states, but this can easily be abstracted into a <CustomLink> of sorts.

Run it locally

First, to run it. Download this repo, navigate to the repo.

yarn
# or `npm install`

yarn dev
# or `npm run dev`

Open http://localhost:3000 with your browser to see the result.

next-spring-transition's People

Contributors

cjimmy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.