2

How can I change the behaviour of a b-dropdown-item-button within a b-dropdown-component so that it does not close automatically when I click on it?

The dropdown is syntactically structured as follows:

<b-dropdown>
    <b-dropdown-item-button>
        <span>Mark as read</span>
    <b-dropdown-item-button>

    <b-dropdown-group>
// Messages are output here
    </b-dropdown-group>
</bdropdown>

Now I wonder how I can prevent the dropdown from closing when I click on the b-dropdown-item-button.

3 Answers 3

14

Placing @click.native.capture.stop directive on any subcomponent of <b-dropdown> will prevent it from closing the dropdown.

For example:

<b-dropdown>
  <b-dropdown-item-button @click.native.capture.stop>
    <span>Mark as read</span>
  <b-dropdown-item-button>

  <b-dropdown-group @click.native.capture.stop>
    // no click will exit the parent, therefore they won't close the dropdown
  </b-dropdown-group>
</bdropdown>

As @GiorgioTempesta mentioned in a comment below, in Vue3 the .native should be removed. See v-on.native modifier removed (breaking change).

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

1 Comment

In Vue 3 it works but you have to remove the .native modifier, so just add @click.capture.stop.
1
  • First: make a reference which you want to stay show (i.e, b-drowpdown)
  • then make an function onClick which will work on click of button
  • finally remain shown the dropdown by this.$refs.dropdown.show(true) which override the defaults
<template>
    <b-dropdown ref="dropdown">
        <b-dropdown-item-button @click="onClick">
            <span>Mark as read</span>
        <b-dropdown-item-button>

        <b-dropdown-group>
    // Messages are output here
        </b-dropdown-group>
    </b-dropdown>
</template>
<script>
  export default {
    methods: {
      onClick() {
        this.$refs.dropdown.show(true)
      }
    }
  }
</script>

2 Comments

Don't reopen the dropdown instead of preventing it from closing. If any functionality is bound on dropdown open event it will also run when you click inside it. It's not scale-able.
Similarly, sometimes it's needed to keep functionality same but changing of UI. It's only needed to hide or show, not relating to stop it's attitude.
0

Add a prevent modifier @click.prevent to the element which shouldn't close the dropdown on click:

<b-dropdown>
  <b-dropdown-text>
     <b-form-input @click.prevent></b-form-input>
  </b-dropdown-text>
</b-dropdown>

Docs:
https://v2.vuejs.org/v2/guide/events.html#Event-Modifiers

Comments

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.