);
};</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>
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!
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:
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:
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: