All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S6841.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

Positive tabIndex values can disrupt the natural tab order of the webpage. This can be confusing for screen reader users who rely on a logical tab order to navigate through the content. If the tab order doesn’t match the visual or logical order of elements, users may struggle to understand the page structure.

Therefore, it’s recommended to avoid using positive tabIndex values.

How to fix it

If you need to make an element focusable that isn’t by default (like a <div> or <span>), you can use tabIndex="0". This will add the element to the natural tab order based on its position in the HTML. Otherwise, either remove the tabIndex value or use tabIndex="-1" to remove the element from the tab order.

Code examples

Noncompliant code example

function MyDiv() {
    return (
        <div>
            <span tabIndex="5">foo</span> // Noncompliant
            <span tabIndex="3">bar</span> // Noncompliant
            <span tabIndex="1">baz</span> // Noncompliant
            <span tabIndex="2">qux</span> // Noncompliant
        </div>
    );
}

Compliant solution

function MyDiv() {
    return (
        <div>
            <span tabIndex="0">foo</span>
            <span tabIndex="-1">bar</span>
            <span tabIndex={0}>baz</span>
            <span>qux</span>
        </div>
    );
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy