I have a navbar on the side of my page, I got the code from a website. Currently it has a button at the top that when the user clicks on it it expands out. However i'd like for it to expand when the user hovers over it. And un-expands when the mouse leaves it. But, it is coded using react-bootstrap so I'm not sure how I can edit this. Any ideas?
NavBar:
<div className="App container">
<SideNav
onSelect={(selected) => {
// Add your code here
}}
>
<SideNav.Toggle />
<SideNav.Nav defaultSelected="home">
<NavItem eventKey="home">
<NavIcon>
<Link to="/"><img src={Dash}/></Link>
</NavIcon>
<NavText>
<Link to="/">Dashboard</Link>
</NavText>
</NavItem>
<NavItem eventKey="sites">
<NavIcon>
<Link to="/sites"><img src={Site} /></Link>
</NavIcon>
<NavText>
<Link to="/sites">Sites</Link>
</NavText>
</NavItem>
<NavItem eventKey="tours">
<NavIcon>
<Link to="/tours"><img src={Tour}/></Link>
</NavIcon>
<NavText>
<Link to="/tours">Tours</Link>
</NavText>
</NavItem>
<NavItem eventKey="media">
<NavIcon>
<Link to="/media"><img src={Media}/> </Link>
</NavIcon>
<NavText>
<Link to="/media">Media</Link>
</NavText>
</NavItem>
<NavItem eventKey="newSite">
<NavIcon>
<Link to="/newSite/details"><img src={NewSite} /></Link>
</NavIcon>
<NavText>
<Link to="/newSite/details">Add new Site</Link>
</NavText>
</NavItem>
<NavItem eventKey="profile">
<NavIcon>
<Link to="/profile"><img src={Profile} /></Link>
</NavIcon>
<NavText>
<Link to="/profile">Profile</Link>
</NavText>
</NavItem>
</SideNav.Nav>
</SideNav>
<Routes />
</div>
Personally, I think that this is a very bad, inaccessible UX design, but if you want to go ahead and do it, you'll need to implement an onMouseEnter handler, like so:
handleOnMouseEnter(path) {
const { history } = this.props;
console.log("handleOnMouseEnter: ", path);
history.push(path);
}
In order to access the history
object within props, you'll need to wrap your component in the withRouter
HOC, something like so:
const WrappedApp = withRouter(App);
const rootElement = document.getElementById("root");
ReactDOM.render(
<Router>
<WrappedApp />
</Router>,
rootElement
);
Then your NavItem would look something like this:
<NavItem
eventKey="sites"
onMouseEnter={() => this.handleOnMouseEnter("/sites")}
>
<NavText>
<Link to="/sites">Sites</Link>
</NavText>
</NavItem>
Please see this CodeSandbox for a working implementation.