avatarCodeFlow

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3551

Abstract

<span class="hljs-name">Tilt</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>${<span class="hljs-attr">shareTechMono.className</span>} <span class="hljs-attr">w-</span>[<span class="hljs-attr">425px</span>] <span class="hljs-attr">h-</span>[<span class="hljs-attr">270px</span>] <span class="hljs-attr">bg-gradient-to-tr</span> <span class="hljs-attr">rounded-2xl</span> <span class="hljs-attr">flex</span> <span class="hljs-attr">content-center</span> <span class="hljs-attr">items-center</span> <span class="hljs-attr">justify-center</span> <span class="hljs-attr">from-</span>[#<span class="hljs-attr">9C2CF3</span>] <span class="hljs-attr">to-</span>[#<span class="hljs-attr">3A49F9</span>]} ></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"p-8 w-full h-full"</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"relative w-full h-full"</span>></span> <span class="hljs-tag"><<span class="hljs-name">Image</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"absolute"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/mastercard.svg"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">{70}</span> <span class="hljs-attr">height</span>=<span class="hljs-string">{24}</span> /></span> <span class="hljs-tag"><<span class="hljs-name">Image</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"absolute right-0 bottom-0 top-0 my-auto"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/chip.svg"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">{60}</span> <span class="hljs-attr">height</span>=<span class="hljs-string">{30}</span> /></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex flex-col w-full h-full justify-end gap-4"</span>></span> <span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-2xl"</span>></span>4242 4242 4242 4242<span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-28"</span>></span> <span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-lg uppercase"</span>></span>John Doe<span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-lg uppercase"</span>></span>10/24<span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span>

Options

   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Tilt</span>&gt;</span></span>

); };</pre></div><p id="b0e9">Now there are a few things to unwrap here. First we’ll use the <code>use client</code> directive from Next.js because <code>react-tilt</code> won’t work otherwise. Next I’m importing a font of my choosing that somewhat resembles a credit card.</p><p id="eb59">And then we get to our actual component. As you can see it’s pretty simple, we just use the <code>Tilt</code> component from <code>react-tilt</code> and pass in some styling. I hardcoded the width and height, but feel free to adjust it, or the colours too.</p><p id="ae87">Inside the <code>Tilt</code> component I just added the icons for our app which are just a chip and a Mastercard logo. Below the the images in the <code>div</code> I just display the card details like card number, card holder name and expiration date.</p><h1 id="3d69">Finish up</h1><p id="efe1">Now that we have our component created, the only thing that’s left is to import it in our page and display it. Let’s go to our <code>pages/index.tsx</code> file and add the following snippet:</p><div id="3aee"><pre><span class="hljs-keyword">import</span> { <span class="hljs-title class_">CreditCard</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/CreditCard"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">Home</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> ( <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex min-h-screen flex-col items-center justify-between p-24"</span>></span> <span class="hljs-tag"><<span class="hljs-name">CreditCard</span> /></span> <span class="hljs-tag"></<span class="hljs-name">main</span>></span></span> ); }</pre></div><p id="22e7">This will import and display our credit card in the middle of the screen. Now all that’s left is for you to hover around and check out that animation! Pretty slick if I might say so!</p><p id="880a">And that brings the end of this short article, I hope it was useful and you enjoyed it! If you need, here is the <a href="https://github.com/unhingedmagikarp/credit-card">GitHub repo</a>.</p><h1 id="633b">In Plain English 🚀</h1><p id="b441"><i>Thank you for being a part of the <a href="https://plainenglish.io"><b>In Plain English</b></a> community! Before you go:</i></p><ul><li>Be sure to <b>clap</b> and <b>follow</b> the writer ️👏<b>️️</b></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/inplainenglish/">LinkedIn</a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a> | <a href="https://newsletter.plainenglish.io/">Newsletter</a></b></li><li>Visit our other platforms: <a href="https://stackademic.com/"><b>Stackademic</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a></b></li><li>More content at <a href="https://plainenglish.io"><b>PlainEnglish.io</b></a></li></ul></article></body>

Animated Credit Card with Next.js and Tailwind

Photo by Joshua Woroniecki on Unsplash

I remember a good few years ago I saw an app or website where a loyalty card was displayed and it tilted and animated as you moved your cursor around. I was still new in the tech scene at that point and I was blown away! I completely forgot about it only until recently and decided to recreate it for fun. So join me in creating this small but neat animation!

Github repo can be found here

Setup

I’ll start with a new Next.js app with Tailwind and Typescript. Once that’s installed, we only need to add one dependency with the following command:

yarn add react-tilt

React-tilt is a wrapper around the Tilt.js library that’ll do the heavy lifting for us. Next we’ll be using some SVG icons that you can find in the Github repo at the bottom of this article.

Credit Card component

Now we get to the main part, we’ll create a new folder called components. In our new folder, let’s create a file called CreditCard.tsx. This is the destination of our component where we’ll insert the following snippet:

"use client";

import Image from "next/image";
import { Tilt } from "react-tilt";
import { Share_Tech_Mono } from "next/font/google";

const shareTechMono = Share_Tech_Mono({
  weight: "400",
  subsets: ["latin"],
});

export const CreditCard = () => {
  return (
    <Tilt
      className={`${shareTechMono.className} w-[425px] h-[270px] bg-gradient-to-tr rounded-2xl flex content-center items-center justify-center from-[#9C2CF3] to-[#3A49F9]`}
    >
      <div className="p-8 w-full h-full">
        <div className="relative w-full h-full">
          <Image
            className="absolute"
            alt=""
            src="/mastercard.svg"
            width={70}
            height={24}
          />
          <Image
            className="absolute right-0 bottom-0 top-0 my-auto"
            alt=""
            src="/chip.svg"
            width={60}
            height={30}
          />
          <div className="flex flex-col w-full h-full justify-end gap-4">
            <p className="text-2xl">4242 4242 4242 4242</p>
            <div className="flex gap-28">
              <p className="text-lg uppercase">John Doe</p>
              <p className="text-lg uppercase">10/24</p>
            </div>
          </div>
        </div>
      </div>
    </Tilt>
  );
};

Now there are a few things to unwrap here. First we’ll use the use client directive from Next.js because react-tilt won’t work otherwise. Next I’m importing a font of my choosing that somewhat resembles a credit card.

And then we get to our actual component. As you can see it’s pretty simple, we just use the Tilt component from react-tilt and pass in some styling. I hardcoded the width and height, but feel free to adjust it, or the colours too.

Inside the Tilt component I just added the icons for our app which are just a chip and a Mastercard logo. Below the the images in the div I just display the card details like card number, card holder name and expiration date.

Finish up

Now that we have our component created, the only thing that’s left is to import it in our page and display it. Let’s go to our pages/index.tsx file and add the following snippet:

import { CreditCard } from "./components/CreditCard";

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      <CreditCard />
    </main>
  );
}

This will import and display our credit card in the middle of the screen. Now all that’s left is for you to hover around and check out that animation! Pretty slick if I might say so!

And that brings the end of this short article, I hope it was useful and you enjoyed it! If you need, here is the GitHub repo.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

React
Front End Development
JavaScript
Typescript
UX
Recommended from ReadMedium