i am trying to create a navbar and a sidebar. i have a button in navbar and when i click on it a sidebar
appears and disappears. both are seperate components. i am doing this using state
.
but now instead of just appearing on screen i want the sidebar to animate(slide in from left and slide out to left)
but i am unable to figure out how. can someone help?
Home.js
import { useState } from "react";
import Navbar from "../navbar/navbar";
import Sidebar from "../sidebar/sidebar";
import "./style.css";
import { useSelector, useDispatch } from "react-redux";
function Home() {
const sidebarOpen = useSelector((state) => state.sidebarOpenState);
return (
<>
<Navbar />
<div className="home-box d-flex">
{sidebarOpen && (
<div className="p-2 flex-fill">
<Sidebar />
</div>
)}
</div>
</>
);
}
export default Home;
Sidebar.js
export default function Sidebar() {
return (<div className="divSidebar">
</div>)
}
navbar.js
import "./style.css";
import { useSelector, useDispatch } from "react-redux";
import { actions } from "../../store/indexStore";
import { Link } from "react-router-dom";
function Navbar() {
const sidebarOpen = useSelector((state) => state.sidebarOpenState);
const dispatch = useDispatch();
const handleToggleSidebar = (e) => {
e.preventDefault();
// console.log('CLICKED');
dispatch(actions.toggleSidebar());
};
return (
<div className="divNavbar">
<ul>
<li>
<button className="btn-navbar-burger" onClick={handleToggleSidebar}>
</button>
</li>
</ul>
</div>
);
}
export default Navbar;
let me know if u want anything else.
—edit
style.css(sidebar)
.divSidebar {
margin-top: 5px;
height: 550px;
width: 300px;
background-color: #6cb4ee;
}
2
Answers
for show
animation
you need to doinghide/show
Sidebar with changing cssstyle
or toggle anclass
:update :
for example you can add this css style for hide or show to element :
Like this goonerbox.com
Very easy do !
Styled components just attaches css to the div’s. I suggest you use styled components on your project !
Bets of luck
Daniel
https://codesandbox.io/s/cocky-snow-jezquu?file=/src/App.js