I have a div that i want its width to switch between 260px and 85px as i click on a button (that will trigger an useState), but i tried a few different ways and none is giving me the result im looking for.
Heres my code (without trying to apply the usestate in the navs classname):
export function Menu(){
const [expanded, setExpanded]= useState(true);
return(
<aside className="h-screen">
<nav className= {'h-full flex flex-col bg-sidebar w-[260px]'}>
<div>
<button onClick={()=>setExpanded((curr)=> !curr)}>
<img src="/Menu.png" alt="menuLogo" className="absolute top-4 ml-[23px]"/>
</button>
</div>
</nav>
2
Answers
You have to make the
nav
Tailwind class dependent on theexpanded
state:You can make use Javascript template literals to achieve your goal.
Ideally, this is what you will have to do:
I also added a Tailwind’s transition utilities for a smooth transition both ways. Hope this helps 🙂