question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom tabindex in modal does not work

See original GitHub issue

Describe the bug

Elements with tabindex > 0 in the modal are never activated.

Steps to reproduce the bug

  1. Create a modal with elements having tabindex > 0, ex:
<template>
    <b-modal title="Some title" id="edit-text-element-modal">
        <input tabindex=0 type="text" placeholder="Some input which can be tabbed">
        <template v-slot:modal-footer>
            <button tabindex="2">
                Cancel
            </button>
            <button tabindex="1">
                OK
            </button>
        </template>
    </b-modal>
</template>
  1. Display it somehow, ex.
    mounted() {
        this.$bvModal.show("edit-text-element-modal");
    }
  1. When modal is opened, try to navigate with TAB through all the tabable elements (here: input and two buttons)
  2. Elements with tabindex > 0 are never focused.

Expected behavior

Elements with tabindex > 0 are focused in the correct order, i.e. “OK” button, “Cancel” button, input field.

Versions

Libraries:

  • BootstrapVue: 2.14.0
  • Vue: 2.6.11

Environment:

  • Device: Lenovo Thinkpad
  • OS: Ubuntu 20.04.2 LTS
  • Browser: Firefox
  • Version: 85.0 x64

Additional context

In short, this behavior caused problems when I wanted to change tab order of the buttons (make OK button get focus first even if it’s later in the DOM).

After looking through the https://github.com/bootstrap-vue/bootstrap-vue/blob/dev/src/components/modal/modal.js, I think it’s caused by tabTraps with tabindex="0". TAB index starts at index 0 and cannot leave that “0 level”, because when it is about to do so, it’s reset to one of the tabTraps at stays at 0.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Vooducommented, Feb 5, 2021

Here’s how you can achieve it: https://jsfiddle.net/websiter/mfL167jd/

It’s setting tabindex to -1 for all elements you want to exclude, setting footer buttons tabindex=0. Note their order in DOM should be the order in which you want the tabindex to select them and I’m also using CSS flexbox order to change their display order. It should work cross browser (but I haven’t tested it outside Chrome).

I woudn’t recommend setting tabindex values above 0 as in production scenarios you’re likely to have other elements with tabindex=0 in page and you might get weird results tabbing through elements in page while modal is open.

That CSS order should solve my problem. Thank you.

1reaction
andrei-gheorghiucommented, Feb 5, 2021

I can’t seem to tab to the close button or input in your fiddle, is that intended?

Yep, it’s intended. I’ve set their tabidex values to -1 (a.k.a: skipped by tabbing - but programmatically focusable). If I leave them focusable the footer buttons become un-focusable. I’m not sure why at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tabindex="-1" not working on bootstrap modal - Stack Overflow
If I keep pressing the tab key, the focus is going to the background and able to focus on other links.
Read more >
JavaScript - Bootstrap
Multiple open modals not supported. Be sure not to open a modal while another is still visible. Showing more than one modal at...
Read more >
Tabbing problem with Modal Popup on Modal Popup - Telerik
When a modal RadWindow is shown all other focusable elements on the page get their tabIndex attributes set to -1 to achieve the...
Read more >
Using tabindex - web.dev
tabindex can be applied to any element - although it is not ... Perhaps you're working on some new Custom Elements that resemble...
Read more >
Keyboard-navigable JavaScript widgets - Accessibility | MDN
Warning: avoid using positive values for tabindex . Elements with a positive tabindex are put before the default interactive elements on the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found