Bootstrap 5 Dropdown: Window.getComputedStyle: Argument 1 is not an object.
Created by: chilled-capybara
Prerequisites
-
I have searched for duplicate or closed issues -
I have validated any HTML to avoid common problems -
I have read the contributing guidelines
Describe the issue
Despite the dropdown-menu
class and the data-bs-toggle="dropdown"
property, unlike Bootstrap 4, there is currently no flexibility in the order in which the dropdown menu and trigger button should be used within the dropdown
wrapper.
From the docs;
Wrap the dropdown’s toggle (your button or link) and the dropdown menu within .dropdown, or another element that declares position: relative;.
I've added two tests cases, one for button-then-menu and one menu-then-button;
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown"
aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
</ul>
</div>
<div class="dropdown">
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
</ul>
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown"
aria-expanded="false">
Dropdown button
</button>
</div>
If the button is the second element, it will throw;
Uncaught TypeError: Window.getComputedStyle: Argument 1 is not an object.
Whilst there is a way to avoid the error (by changing the order), I'm not sure whether there should be the adaptability that was present in Bootstrap 4?
Reduced test cases
Working example (button then dropdown); https://codepen.io/chilled-capybara/pen/rNzOjrJ
Example with error (dropdown then button); https://codepen.io/chilled-capybara/pen/ExvVZep
What operating system(s) are you seeing the problem on?
Linux
What browser(s) are you seeing the problem on?
Firefox
What version of Bootstrap are you using?
v5.1.3