54
loading...
This website collects cookies to deliver better user experience
npx create-react-app animated-sidebar
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
tailwind.config.jd
replace the content with this-module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
index.css
add this code block-@tailwind base;
@tailwind components;
@tailwind utilities;
Sidebar.js
in the src
folder. Now create a functional component-const Sidebar = () => {
return (
<div>
</div>
)
}
export default Sidebar
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;
<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>
const [showSidebar, setShowSidebar] = useState(false);
<>
{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>
</>
<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"
}`}
showSidebar
variable is true then it will add the translate-x-0
otherwise translate-x-full
. Our sidebar now works 🎉ease-in-out duration-300
<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>