Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue Component: Dropdown menu open inside div

I’m building a dropdown component for my project. But when i add the component is a responsive table the menu is open within the div.

Here is my code: https://codesandbox.io/s/fast-pond-pto57t?file=%2Fsrc%2FApp.vue

enter image description here

How can i make the dropdown-menu to append to the body and set a transform(x,y)

like image 888
CodeAgent Avatar asked Dec 04 '25 14:12

CodeAgent


2 Answers

Try with position: fixed !important;:

new Vue({
  el: "#demo",
  data() {
    return {
      showMenu: false,
    };
  },
})
.dropdown-menu {
  position: fixed !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<div id="demo">
  <div class="table-responsive">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>#</th>
          <th>Name</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Joe Band</td>
          <td>
            <div class="dropdown">
              <button class="dropdown-toggle" @click="showMenu = !showMenu">
                Action
              </button>
              <ul class="dropdown-menu" :class="{ show: showMenu }">
                <li><a>Item 1</a></li>
                <li><a>Item 2</a></li>
                <li><a>Item 3</a></li>
              </ul>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
like image 122
Nikola Pavicevic Avatar answered Dec 07 '25 04:12

Nikola Pavicevic


I find both existing answers valuable, but neither complete:

  • TripleN's answer doesn't position the dropdown correctly
  • Nikola's answer doesn't append .dropdown-menu to <body>, so it stops working as soon as one of the ancestors of .dropdown has any valid transform value other than none (spec ref). Here's an example.

In my estimation, the correct solution would be to do both:

  1. allow Popper (used internally by Bootstrap 5) to position the dropdown (the popper content) correctly,
  2. append the popper content to <body> while dropdown is open.

That's pretty much it!
This particular task used to be easy in prior versions of Bootstrap. I've spent hours going through Bootstrap 5's and Popper's documentation and examples, trying to find a way to do this simple task: specify a target container for popper content.

If someone knows how to achieve this in Bootstrap 5 without having to meddle with DOM ourselves, I'd be delighted to learn how.
Eventually I gave up and ended up moving the menu in DOM myself, after Popper positions it:

Working demo.
In more detail, this hybrid solution:

  • uses Nikola's idea to give the menu position: fixed, so Popper does the heavy lifting of positioning
  • uses TripleN's idea to append the menu to <body>, after Popper positioned it. Additionally, I'm moving the menu back to its original place in DOM once the dropdown is closed. It might not be necessary, but to me it makes a lot of sense. Common sense.

The solution seems to work in all cases, including inside 3d transformed ancestors. I've smoke tested it in both latest Vue 2 (2.7.10) and latest Vue 3 (3.2.40). Does the job.

Same exact logic in Composition.


Side note: perhaps I'm too demanding but, by comparison with prior versions, I find Bootstrap 5 difficult to work with and poorly documented, at least at this point in time.

This should have been a trivial task. Specify container: 'body' in Dropdown's config. Done!

I'm a bit disappointed.

like image 44
tao Avatar answered Dec 07 '25 04:12

tao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!