So I have this sidebar which have routes to different pages of the app. On hover I'm styling the li
with a right border and the icon with the same color. Now I have been trying to add active class to the currently active path with the same styling as the li (right border and icon color). But I'm unable to do so.
I have already tried the solutions from this post but nothing seems to be working. I tried with NavLink
from react-router-dom
and added activeClassName
but it doesn't work.
This is the current state of the code:
const Sidebar = () => {
return (
<div className={style.sidebarContainer} >
<li className={style.navItem} >
<Link to='/' activeClassName="active" >
<PersonIcon />
</Link>
</li>
<li className={style.navItem}>
<Link to='/example1' activeClassName="active" >
<SettingsIcon />
</Link>
</li>
<li className={style.navItem}>
<Link to='/example2' activeClassName="active" >
<LightModeIcon />
</Link>
</li>
</div>
)
}
This is the stylesheet:
.sidebarContainer{
background: black;
}
.sidebarContainer a{
text-decoration: none;
color: gray;
width: 100%;
}
.navItem {
width: 100%;
padding: 1rem 0;
cursor: pointer;
border-right: 4px solid transparent;
transition: all 0.3s ease 0s !important;
list-style: none;
padding-left: 1.3rem;
display: flex;
align-items: center;
}
.navItem:hover {
border-right: 4px solid red;
}
.navItem:hover svg {
color: red;
}
.navItem svg {
font-size: 1.6rem !important;
transition: all 0.3s ease 0s !important;
}
Here's how it looks: