6
<div>
  <b-dropdown id="dropdown-1" text="Dropdown Button" class="m-md-2">
  <b-dropdown-item>First Action</b-dropdown-item>
  <b-dropdown-item>Second Action</b-dropdown-item>
  <b-dropdown-item>Third Action</b-dropdown-item>
  <b-dropdown-divider></b-dropdown-divider>
  <b-dropdown-item active>Active action</b-dropdown-item>
  <b-dropdown-item disabled>Disabled action</b-dropdown-item>
  </b-dropdown>
</div>

The items should be shown when the dropdown button is hovered!

1

3 Answers 3

16

Not as straighforward as I thought but here is one example on how to convert this bootstrap-dropdown into a hoverable dropdown.

<template>
  <div @mouseover="onOver" @mouseleave="onLeave">
    <b-dropdown id="dropdown-1" text="Dropdown Button" ref="dropdown" class="m-md-2">
       <b-dropdown-item>First Action</b-dropdown-item>
       <b-dropdown-item>Second Action</b-dropdown-item>
       <b-dropdown-item>Third Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
  export default {
    methods: {
      onOver() {
        this.$refs.dropdown.visible = true;
      },
      onLeave() {
        this.$refs.dropdown.visible = false;
      }
    }
  }
</script>

The idea is to use v-on directives mouseover and mouseleave on a wrapper div (somehow the vue directive does not work on the b-vue component directly, but might be only me). Then use the event trigger to alter dropdown.visible state. Also in this example I make use of Vue's $refs to get hold of the dropdown within the script.

Working example https://codesandbox.io/s/2erqk


Multiple Hover-Dropdowns

If you are planning on having this behaviour on multiple dropdowns at once, I would go into the trouble of creating a component out of it - incorporating the b-dropdown into a new component.

<template>
  <div @mouseover="onOver" @mouseleave="onLeave">
    <b-dropdown id="dropdown-1" text="Dropdown Button" ref="dropdown" class="m-md-2">
      <slot></slot>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "b-dropdown-hover",
  methods: {
    onOver() {
      this.$refs.dropdown.visible = true;
    },
    onLeave() {
      this.$refs.dropdown.visible = false;
    }
  }
};
</script>

And then use it like this :

<template>
  <div>
    <b-dropdown-hover>
       <b-dropdown-item>First Action</b-dropdown-item>
       <b-dropdown-item>Second Action</b-dropdown-item>
       <b-dropdown-item>Third Action</b-dropdown-item>
    </b-dropdown-hover>
    <b-dropdown-hover>
       <b-dropdown-item>First Action</b-dropdown-item>
       <b-dropdown-item>Second Action</b-dropdown-item>
       <b-dropdown-item>Third Action</b-dropdown-item>
    </b-dropdown-hover>
  </div>
</template>
<script>

  import BDropdownHover from '@/components/BDropdownHover'

  export default {
    components : {
      BDropdownHover
    }
  }
</script>

But you also have to include all events and props that you need from b-dropdown into the new component. Here is a working example of that: https://codesandbox.io/s/romantic-elgamal-lol7h

Sign up to request clarification or add additional context in comments.

4 Comments

Glad I could help @Abdan , will you mark my Answer as the correct one ?
If you want to use these directives on the b-dropdown component directly, you can add native modifier at the end (e.g. @mouseover.native = "onOver")
How do I implement this if I have more than one b-dropdown? Can I check it like if(this.$refs === dropdown1)?
how can I make it so that the b-dropdown-hover tag supports the same options as b-dropdown like dropright and dropleft?
2

I made another solution for multiple hover-dropdowns based on the answer of @pascal-lamers.

If you don't want to create different components for this task, instead you would like to solve it in the same nav element, then you need to define multiple refs and define the certain ref for your onOver and onLeave listeners.

<template>
    <div @mouseover="onOver('dropdown1')" @mouseleave="onLeave('dropdown1')">
        <b-dropdown id="dropdown-1" text="Dropdown Button" ref="dropdown1" class="m-md-2">
            <b-dropdown-item>First Action</b-dropdown-item>
            <b-dropdown-item>Second Action</b-dropdown-item>
            <b-dropdown-item>Third Action</b-dropdown-item>
        </b-dropdown>
    </div>
    <div @mouseover="onOver('dropdown2')" @mouseleave="onLeave('dropdown2')">
        <b-dropdown id="dropdown-2" text="Dropdown Button" ref="dropdown2" class="m-md-2">
            <b-dropdown-item>Fourth Action</b-dropdown-item>
            <b-dropdown-item>Fifth Action</b-dropdown-item>
            <b-dropdown-item>Sixth Action</b-dropdown-item>
        </b-dropdown>
    </div>
</template>

<script>
export default {
    methods: {
    onOver(element) {
        this.$refs[element].visible = true;
    },
    onLeave(element) {
        this.$refs[element].visible = false;
    }
    }
}
</script>

Comments

1

You also can use v-b-hover in bootstrap-vue v2.5.0 https://bootstrap-vue.org/docs/directives/hover

Example from bootstrap-vue:

<template>
  <div v-b-hover="hoverHandler"> ... </div>
</template>

<script>
  export default {
    methods: {
      hoverHandler(isHovered) {
        if (isHovered) {
          // Do something
        } else {
          // Do something else
        }
      }
    }
  }
</script>

1 Comment

Please add further details to expand on your answer, such as working code or documentation citations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.