Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4041

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T08:19:08+00:00 2024-11-26T08:19:08+00:00

Create an animated sidebar with TailwindCSS in React💫

  • 61k

Hey everyone, in many apps you need a sidebar/drawer which slides in if you click on a hamburger icon. In this tutorial, we are going to see how to build that 🌟.

Demo

Video

Setup

Creating a new react app-

npx create-react-app animated-sidebar 
Enter fullscreen mode Exit fullscreen mode

Setting up tailwindCSS

Installing Tailwind-

npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p 
Enter fullscreen mode Exit fullscreen mode

Configuring paths-

Inside tailwind.config.jd replace the content with this-

module.exports = {   content: ["./src/**/*.{js,jsx,ts,tsx}"],   theme: {     extend: {},   },   plugins: [], }; 
Enter fullscreen mode Exit fullscreen mode

Add tailwindCSS to CSS

In index.css add this code block-

@tailwind base; @tailwind components; @tailwind utilities; 
Enter fullscreen mode Exit fullscreen mode

Creating the sidebar

Making a new component

I am going to create a separate component for Sidebar, so create a file Sidebar.js in the src folder. Now create a functional component-

const Sidebar = () => {   return (     <div>      </div>   ) }  export default Sidebar 
Enter fullscreen mode Exit fullscreen mode

Rendering the Sidebar component

We also need to render the component so add this in App.js–

import Sidebar from "./Sidebar";  function App() {   return (     <div className="flex flex-col items-center justify-center min-h-screen py-2">       <Sidebar />     </div>   ); }  export default App; 
Enter fullscreen mode Exit fullscreen mode

This should show us an empty canvas now.

Making a basic sidebar

I am going to make a simple div with a text in it-

<div className="top-0 right-0 w-[35vw] bg-blue-600  p-10 pl-20 text-white fixed h-full ">   <h2 className="mt-20 text-4xl font-semibold text-white">I am a sidebar</h2> </div> 
Enter fullscreen mode Exit fullscreen mode

This will give us a simple, blue sidebar on the right side-

image.png

Handling open and closed states

Create a useState to store a boolean value that decides if we should or shouldn't show the sidebar-

const [showSidebar, setShowSidebar] = useState(false); 
Enter fullscreen mode Exit fullscreen mode

We also need to show buttons/icons to open and close the sidebar so I will wrap the whole thing in a fragment, add a button to close, and a hamburger icon to open –

<>   {showSidebar ? (     <button       className="flex text-4xl text-white items-center cursor-pointer fixed right-10 top-6 z-50"       onClick={() => setShowSidebar(!showSidebar)}     >       x     </button>   ) : (     <svg       onClick={() => setShowSidebar(!showSidebar)}       className="fixed  z-30 flex items-center cursor-pointer right-10 top-6"       fill="#2563EB"       viewBox="0 0 100 80"       width="40"       height="40"     >       <rect width="100" height="10"></rect>       <rect y="30" width="100" height="10"></rect>       <rect y="60" width="100" height="10"></rect>     </svg>   )}    <div className="top-0 right-0 w-[35vw] bg-blue-600  p-10 pl-20 text-white fixed h-full z-40">     <h3 className="mt-20 text-4xl font-semibold text-white">I am a sidebar</h3>   </div> </> 
Enter fullscreen mode Exit fullscreen mode

This will not make any difference right now but let's add some conditional classes to the main sidebar div.

<div   className={`top-0 right-0 w-[35vw] bg-blue-600  p-10 pl-20 text-white fixed h-full z-40 ${     showSidebar ? "translate-x-0 " : "translate-x-full"   }`} 
Enter fullscreen mode Exit fullscreen mode

If the showSidebar variable is true then it will add the translate-x-0 otherwise translate-x-full. Our sidebar now works 🎉

Video

But it isn't smooth so let us see how to make the animation smooth. Just add these two classes to the blue div-

ease-in-out duration-300 
Enter fullscreen mode Exit fullscreen mode

The div should look like this now-

<div   className={`top-0 right-0 w-[35vw] bg-blue-600  p-10 pl-20 text-white fixed h-full z-40  ease-in-out duration-300 ${     showSidebar ? "translate-x-0 " : "translate-x-full"   }`} >   <h3 className="mt-20 text-4xl font-semibold text-white">I am a sidebar</h3> </div> 
Enter fullscreen mode Exit fullscreen mode

Our sidebar animation looks very smooth and great! 🥳

Video

Hope you liked this tutorial and add nice animation to the sidebar in your project. Peace ✌️

Useful links

GitHub repo

Animate and Change Header Background on Scroll

Connect with me

animationreacttailwindcsswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.