Bootstrap Popover cannot interact touchstart with trigger focus

Viewed 8

I tried to create a button to popover a menu to allow user select different action. The button work great with mouse, but it cannot function with touchpad in notebook device.

When I use the popover without focus trigger, the modal button can interact with touchpad and display the modal dialog. But when I use focus trigger, it's look like dismiss event fired before the button click.

How should I handle the touchpad event in this case? Thanks!

Here is the code

const popoverTriggerList = [].slice.call(document.querySelectorAll('a.btn[data-bs-toggle=\'popover\'][data-bs-content-id]'));
popoverTriggerList.map((popoverTriggerEle) => {
    if (bootstrap.Popover.getInstance(popoverTriggerEle) !== null) {
        return null;
    }

    const contentId = popoverTriggerEle.attributes['data-bs-content-id'].value;

    bootstrap.Tooltip.Default.allowList.a.push(/^data-bs-[\w]+/);

    const contentEle = document.getElementById(contentId);
    if (contentEle) {
        return new bootstrap.Popover(popoverTriggerEle, {
            container: 'body',
            content: contentEle.innerHTML,
            html: true,
        });
    }

    return null;
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>

<div id="popover-content" class="d-none">
  Popover content with <strong>HTML</strong>.
</div>

<div id="Popover1" class="d-none">
    <a tabindex="0" class="btn w-100 text-start" role="button"><i class="bi bi-building"></i> Link 1</a><br />
    <a tabindex="0" class="btn w-100 text-start" role="button"><i class="bi bi-person-fill"></i> Link 2</a><br />
    <a class="btn w-100 text-start" role="button" data-bs-toggle="modal" data-bs-target="#Modal1"><i class="bi bi-pencil-square"></i> Modal1</a><br />
    <a class="btn w-100 text-start" role="button" data-bs-toggle="modal" data-bs-target="#Modal2"><i class="bi bi-archive-fill"></i> Modal 2</a>
</div>

<div id="Modal1" class="modal fade" data-bs-backgrop="static" tabindex="-1" aria-label="Modal 1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                Header
            </div>
            <div class="modal-body">
                Body
            </div>
            <div class="modal-footer">
                <button type="button" class="btn" data-bs-dismiss="modal"><i class="bi bi-x-lg"></i> Cancel</button>
            </div>
        </div>
    </div>
</div>

<div id="Modal2" class="modal fade" data-bs-backgrop="static" tabindex="-1" aria-label="Modal 1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                Header
            </div>
            <div class="modal-body">
                Body
            </div>
            <div class="modal-footer">
                <button type="button" class="btn" data-bs-dismiss="modal"><i class="bi bi-x-lg"></i> Cancel</button>
            </div>
        </div>
    </div>
</div>

<a class="btn btn-primary" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content-id="Popover1" tabindex="0" role="button">Popover without focus</a>
<a class="btn btn-danger" role="button" data-bs-toggle="popover" data-bs-content-id="Popover1" tabindex="0" data-bs-trigger="focus">Popover with focus</a>

0 Answers
Related