How can I make react-bootstrap's Dropdown open on mouse hover?

21,347

Solution 1

export class Nav extends React.Component {

  constructor(props) {
    super(props)
    this.state = { isOpen: false }
  }

  handleOpen = () => {
    this.setState({ isOpen: true })
  }

  handleClose = () => {
     this.setState({ isOpen: false })
  }

  render() {
    return (
       <Nav>
        <NavDropdown
          onMouseEnter = { this.handleOpen }
          onMouseLeave = { this.handleClose }
          open={ this.state.isOpen }
          noCaret
          id="language-switcher-container"
        >
          <MenuItem>Only one Item</MenuItem>
        </NavDropdown>
      </Nav>
    )
  }
}

Hope this solves your issue.

Solution 2

A much cleaner pure CSS solution here:

.dropdown:hover .dropdown-menu { display: block; }

Solution 3

Having just run into this issue myself, I found out you need both the bootstrap CSS and a parameter for react-bootstrap. Add the following CSS:

.dropdown:hover>.dropdown-menu {
  display: block;
}

Then you need to add the parameter renderMenuOnMount={true} for the child elements to render on load:

<NavDropdown renderMenuOnMount={true}>
  <NavDropdown.Item href="#">Item #1</NavDropdown.Item>
</NavDropdown>

Solution 4

It's as simple as that :)

<NavDropdown onMouseEnter={(e) => document.getElementById("idofthiselement").click()} onMouseLeave={(e) => document.getElementById("idofthiselement").click()} title="Others" id="idofthiselement">
Share:
21,347

Related videos on Youtube

W.Chen
Author by

W.Chen

Updated on July 09, 2022

Comments

  • W.Chen
    W.Chen almost 2 years

    Ok the question is simple. I need to make the dropdown open when mouse hover rather than on click.

    Currently my code is as follow.

        <Nav>
          <NavDropdown
            onMouseEnter = {()=> isOpen=true}
            open={isOpen}
            noCaret
            id="language-switcher-container"
          >
            <MenuItem>Only one Item</MenuItem>
          </NavDropdown>
        </Nav>
    

    as you can see, I tried onMouseEnter but no effect. Can someone solve this issue? What attribute should I pass in to achive this.

    The API page is here react-bootstrap API page

    • Rei Dien
      Rei Dien about 7 years
      first if you need a component to re-render, you need to set a new state, or new props. you can achieve you goal by adding isOpen to state, and then call setState onMouseEnter
    • W.Chen
      W.Chen about 7 years
      Yes, I adopted the exact same code as you suggest!! thanks for your help!
  • W.Chen
    W.Chen about 7 years
    Thanks for your help, yes I'm using bootstrap thus do it this way might not be a good idea,
  • Dan
    Dan about 7 years
    Like I say im not familiar with react, but I am with bootstrap. If you pop that code in your own js file and make sure you have a .dropdown-menu etc so that it works, then i dont see why it wouldnt be a good idea?
  • W.Chen
    W.Chen about 7 years
    Thanks again, it works. I guess I need to change my mind from traditional frontend to state-controlled rendering technique.
  • Foxhoundn
    Foxhoundn about 6 years
    Using jQuery to manipulate the DOM defeats the whole purpose of using React - a component library.
  • rbtbar
    rbtbar almost 5 years
    It shouldn't be the accepted answer. Just try to move the mouse over the dropdown-menu element - the dropdown is closed again.
  • Gary Vernon Grubb
    Gary Vernon Grubb almost 5 years
    @rbtbar, are you using nested menus? I have tested without nesting menus and it works fine. "react-bootstrap": "^0.32.4"
  • HMR
    HMR over 4 years
    The prop open does not exist anymore, it has been renamed to show
  • Schalton
    Schalton over 4 years
    This doesn't work in newer versions, the dropdown isn't rendered until it's first click. You'd need to trigger the onclick before you could control via css
  • waldrus
    waldrus about 4 years
    This technique will open all dropdown menus on the page. How do you limit this to targeted one, specific, dropdown? Or to hover over multiple other dropdowns throughout the page?
  • Adesh Kumar
    Adesh Kumar over 3 years
    use the different state for different dropdowns