twbs / bootstrap

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.
https://getbootstrap.com
MIT License
170.25k stars 78.78k forks source link

Dropdown opens and re-closes with one click in Firefox when emulating mobile device #38414

Open ryanwawr opened 1 year ago

ryanwawr commented 1 year ago

Prerequisites

Describe the issue

When clicking on a dropdown menu in quick succession, clicks after the first seem to trigger both a show and hide event, so the menu ends up in the same state (either shown or hidden) as when I clicked. I've provided a minimal CodePen example below and included a GIF of the behavior exhibiting.

This happens in Firefox when emulating a mobile device. I used Galaxy S20 Android 11. It does not occur when using Firefox at a normal resolution with no mobile emulation enabled. It does not occur in Chrome with or without emulation.

In this GIF, I click a few times slowly to demonstrate the menu opening and closing as expected. I then click somewhat quickly, and the menu frequently opens and re-closes or closes and re-opens in response to a single click. JTapG4k

Reduced test cases

Minimal reproduction example. A menu with a single dropdown element. https://codepen.io/ryanwawr/pen/gOBOYWd?editors=1111

What operating system(s) are you seeing the problem on?

Windows

What browser(s) are you seeing the problem on?

Firefox

What version of Bootstrap are you using?

5.2.3 in CodePen. Also exhibits in a project I'm developing that uses 5.1.0.

mootezaloui commented 1 year ago

Hello, @ryanwawr When simulating a mobile device, I think that Firefox has a recognized problem with this I already had this issue. to avoid it I suggest including the "data-toggle" property and setting it to "dropdown" on the dropdown button element. Adding the
"data-flip" property and setting it to "false" will also stop the menu from flipping to the button's other side. The following CSS rule may be added to the dropdown menu element as an alternative remedy: .dropdown-menu { touch-action: pan-y; } This will make menu touch scrolling possible, which could stop the problem from happening. To determine if the problem has been fixed, you may also try upgrading Firefox or Bootstrap to their most recent versions. sometimes the browser and Bootstrap's latest versions can fix this error.

ryanwawr commented 1 year ago

@mootezaloui I updated the CodePen with your recommended fixes, (other than updating Bootstrap, as it doesn't appear that 5.3 is out of alpha yet), and it didn't seem to resolve the issue. I'm not sure why setting data-flip or touch-action would resolve anything, as the button isn't flipping to start with, and I'm not doing anything other than clicking a few times to reproduce the issue.

mootezaloui commented 1 year ago

@ryanwawr One thing that might be worth trying is to add the "data-boundary" property to the dropdown button element and set it to "viewport". This will constrain the dropdown menu to the bounds of the viewport, which could help prevent it from getting stuck in an open or closed state.

If that doesn't work, you could also try using a different approach to handling dropdown menus, such as using a custom JavaScript implementation instead of relying on Bootstrap's built-in functionality. <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton"> Dropdown Button </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <a class="dropdown-item" href="#">Option 1</a> <a class="dropdown-item" href="#">Option 2</a> <a class="dropdown-item" href="#">Option 3</a> </div> </div>

<script> const dropdownButton = document.getElementById('dropdownMenuButton'); const dropdownMenu = dropdownButton.nextElementSibling;

dropdownButton.addEventListener('click', function() { if (dropdownMenu.classList.contains('show')) { dropdownMenu.classList.remove('show'); } else { dropdownMenu.classList.add('show'); } }); </script>