Logo

dev-resources.site

for different kinds of informations.

Watch Out For Broken Links, 404 Page With Framer Motion, TailwindCSS and NextJs

Published at
8/4/2024
Categories
framermotion
tailwindcss
javascript
nextjs
Author
akcanakci
Author
9 person written this
akcanakci
open
Watch Out For Broken Links, 404 Page With Framer Motion, TailwindCSS and NextJs

Trying to be different isn't easy. We get so accustomed to usual apps, layouts, and colors that it's hard to think of something else.

In any case, here is my take on a different 404 page design. The tools I use are always the same: React/Next.js for the page, Tailwind CSS for styling, and Framer Motion to make it move.

Would you like to skip to the end? You can preview it at my website with the full code ready to grab. Also, there are many more designs! Hope you like it.


First Things First

I assume you know what React/Next.js is and how to install the necessary tools and libraries. I will be writing for Next.js. Here is a brief walkthrough:

  1. Install NextJs or just React
  2. Add TailwindCSS
  3. And finally, add Framer Motion!

Let's Begin

Create a file under the /pages directory and name it 404.js. But you are not limited to 404, of course. You can read about other custom errors here and change the page name with the contents in them accordingly.

After creating the page we need a function.
**404.js**

export default function YouDiedErrorPage() {
 return(...)
}
Enter fullscreen mode Exit fullscreen mode

Name of the function doesn't matter, just name it anything you want.

As you can see in the cover image, I have a text at the center of the screen. So we need a screen and a text!

**404.js**

export default function YouDiedErrorPage() {
  return (
    // Div as the screen
    <div className="grid h-screen w-screen place-items-center overflow-hidden bg-black text-center font-serif">
      {/* A container for the text which is centered */}
      <div className="relative whitespace-nowrap">
        {/* And a text with an id of title */}
        <h1
          id="title"
          className="absolute inset-0 flex items-center justify-center text-5xl text-red-500/40 opacity-0 will-change-transform md:text-7xl"
        >
          YOU <br className="inline-flex md:hidden" />
          DIED
        </h1>
      </div>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Whats the point of having framer motion if we are going to have just a single text? You are right! Let's have more text with proper styling and responsive goodness.

**404.js**

export default function YouDiedErrorPage() {
  return (
    // Div as the screen
    <div className="grid h-screen w-screen place-items-center overflow-hidden bg-black text-center font-serif">
      {/* A container for the text */}
      <div className="relative whitespace-nowrap">
        {/* And a text with an id of title */}
        <h1
          id="title"
          className="absolute inset-0 flex items-center justify-center text-5xl text-red-500/40 opacity-0 will-change-transform md:text-7xl"
        >
          YOU <br className="inline-flex md:hidden" />
          DIED
        </h1>
        <a
          href="#you-died-go-home"
          id="respawnText"
          className="absolute inset-0 flex items-center justify-center text-2xl text-neutral-400 opacity-0 transition-colors duration-300 hover:text-red-500 md:text-3xl"
        >
          Click here to respawn 
        </a>
        <p
          id="reasonOfDeath"
          className="absolute inset-0 mt-6 flex items-center justify-center text-neutral-500/80 opacity-0 md:mt-8"
        >
          - 404: Death by broken link! -
        </p>
      </div>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Have you ever played a Dark Souls-like game? If you fail or die, the "Game Over" (or in our case, "You Died") text fades and scales in. After that, a "Load Game" or "Exit" button appears along with some statistics or other relevant information. This will be the same!

We will show the title and after that a link with "Click here to respawn ↻" text will appear with a "- 404: Death by broken link! -" subtitle.

As you can see we have id's for each element which we will use to make the animations. For that we need framer motion's useAnimate() hook which you can read about it here.

Let's Animate

Framer Motion’s useAnimate hook with async functions allows you to easily sequence animations in any way you want. All you need is a scope to tell Framer Motion where to look and an animate function to specify what to do. Check out the code below:

  const [scope, animate] = useAnimate();

  async function killThem() {
    await animate("#title", { scale: 2, opacity: 1 }, { duration: 3 });
    await animate("#title", { opacity: 0 }, { duration: 1 });
    await animate("#respawnText", { opacity: 1 }, { duration: 1 });
    await animate("#reasonOfDeath", { opacity: 1 }, { duration: 0.7 });
  }
Enter fullscreen mode Exit fullscreen mode

Everything here is pretty self-explanatory. Create an async function with a proper name, and by awaiting every single animation create a sequence. Pick an id and tell it what to do. Amazingly simple! Look at the final code now and you will see what it does. I also added some additional functions which will be useful for development.

**404.js**

import { useAnimate } from "framer-motion";
import { useEffect } from "react";

export default function YouDiedErrorPage() {
  const [scope, animate] = useAnimate();

  async function killHim() {
    await animate("#title", { scale: 2, opacity: 1 }, { duration: 3 });
    await animate("#title", { opacity: 0 }, { duration: 1 });
    await animate("#respawnText", { opacity: 1 }, { duration: 1 });
    await animate("#reasonOfDeath", { opacity: 1 }, { duration: 0.7 });
  }
  // With this we are starting the animation, you can also call the function in anywhere you like!
  useEffect(() => {
    killHim();
  }, []);

  // Function to refresh the page for development and demonstration purposes.
  function handleRespawnClick() {
    window.location.reload();
  }

  return (
    <div className="grid h-screen w-screen place-items-center overflow-hidden bg-black text-center font-serif">
      <div ref={scope} className="relative whitespace-nowrap">
        <h1
          id="title"
          className="absolute inset-0 flex items-center justify-center text-5xl text-red-500/40 opacity-0 will-change-transform md:text-7xl"
        >
          YOU <br className="inline-flex md:hidden" />
          DIED
        </h1>
        <a
          onClick={handleRespawnClick} // For development, remove later.
          href="#you-died-go-home"
          id="respawnText"
          className="absolute inset-0 flex items-center justify-center text-2xl text-neutral-400 opacity-0 transition-colors duration-300 hover:text-red-500 md:text-3xl"
        >
          Click here to respawn 
        </a>
        <p
          id="reasonOfDeath"
          className="absolute inset-0 mt-6 flex items-center justify-center text-neutral-500/80 opacity-0 md:mt-8"
        >
          - 404: Death by broken link! -
        </p>
      </div>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Here I have the scope/ref at the container div. It is always better to have container divs for the animations instead of the entire screen. Remember to change the anchor link to anywhere you want and don't forget to turn it to a next/link if you are using NextJs :)

For now, that's all there is. Just a concept with a nice and easy way of making animations with framer-motion. Preview it here, enjoy and have a good day!

framermotion Article's
30 articles in total
Favicon
Building a food simulation game with Next.js and generative design
Favicon
How to Create a Flipping Card Animation Using Framer Motion
Favicon
State-of-the-Art Web Design with Remix, Tailwind, and Framer Motion
Favicon
How I Created a Stunning Portfolio with Next.js, Tailwind CSS, and Framer Motion
Favicon
Creating the iMessage Card Stack Animation: The Interactive Layer
Favicon
Creating the iMessage Card Stack Animation: The Timeline Design
Favicon
How to build awesome website with stunning animation?
Favicon
Adding motion to 3D models with Framer Motion and Three.js
Favicon
Creating a Smooth Animated Menu with React and Framer Motion
Favicon
My-Portfolio
Favicon
Watch Out For Broken Links, 404 Page With Framer Motion, TailwindCSS and NextJs
Favicon
Could not find a declaration file for module framer-motion Error in Next.js 14
Favicon
How to add Animations and Transitions in React
Favicon
Creating a text writing animation in React using Framer-Motion
Favicon
Unveiling My Portfolio Website: A Fusion of Tech and Creativity 🚀
Favicon
React developer portfolio template use framer motion to animate components
Favicon
Building a mini casual game with Next.js
Favicon
Creating a Multi-Level Sidebar Animation with Tailwind and Framer Motion in React
Favicon
Easy Animated Tabs in ReactJS with Framer Motion
Favicon
A Tinder-like card game with Framer-Motion
Favicon
Elevating Web Design with Framer Motion: Bringing Your Website to Life
Favicon
Animating the Web: Route Transitions in Next.js with Framer Motion
Favicon
Enhancing Form Usability with Framer Motion: A Guide to Animated, Chunked Form Transitions
Favicon
How to Build a fully accessible Accordion with HeadlessUI, Framer Motion, and TailwindCSS
Favicon
Build Portfolio Theme in Tailwind CSS & Next.js
Favicon
How to create Scroll-Linked Animations with React and Framer Motion 💃🏻🕺🏻
Favicon
How to build 3 simple animation with framer motion
Favicon
How to create an awesome navigation menu using chakra-UI and framer-motion.
Favicon
Make a slide open envelope
Favicon
Animate everything with Framer Motion

Featured ones: