Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessibility issue: sl-radio-group #2191

Open
akuu2510 opened this issue Sep 30, 2024 · 1 comment
Open

Accessibility issue: sl-radio-group #2191

akuu2510 opened this issue Sep 30, 2024 · 1 comment
Labels
bug Things that aren't working right in the library.

Comments

@akuu2510
Copy link

akuu2510 commented Sep 30, 2024

Describe the bug

This bug is in the radio group component where all the sl-radios are disabled and then when I am using tab key from keyboard, disabled radio with value = true gets focussed and after I press up and down arrow keys the radio with value = true is getting un selected.

Expected behaviour

Component should not get focussed when disabled

To Reproduce

Steps to reproduce the behavior:

  1. Go to radio group using sl-radios and then select one of them
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/cdn/shoelace.js"></script>
<sl-radio-group label="Select an option" name="a" value="1">
  <sl-radio value="1">Option 1</sl-radio>
  <sl-radio value="2">Option 2</sl-radio>
  <sl-radio value="3">Option 3</sl-radio>
</sl-radio-group>
  1. disable all the radio buttons that is all the sl-radios
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/cdn/shoelace.js"></script>
<sl-radio-group label="Select an option" name="a" value="1">
  <sl-radio value="1" disabled>Option 1</sl-radio>
  <sl-radio value="2" disabled>Option 2</sl-radio>
  <sl-radio value="3" disabled>Option 3</sl-radio>
</sl-radio-group>
  1. Then by using tab try to focus the component you will see it gets focused
  2. Then use up and down arrow key to move you will see that it will throw error and the selected button get disappeared

Demo

GMT20240930-061145_Recording_1728x1118.mp4

Screenshots

image

Browser / OS

  • OS: Mac
  • Browser: Chrome
@akuu2510 akuu2510 added the bug Things that aren't working right in the library. label Sep 30, 2024
@akuu2510
Copy link
Author

akuu2510 commented Sep 30, 2024

Probably this can be the fix
ADD THE BELOW LINE
if (this.getAllRadios().every(radio => radio.disabled)) return;

CODE

private handleKeyDown(event: KeyboardEvent) {
    if (this.getAllRadios().every(radio => radio.disabled)) return;

    if (!['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(event.key)) {
      return;
    }

    const radios = this.getAllRadios().filter(radio => !radio.disabled);
    const checkedRadio = radios.find(radio => radio.checked) ?? radios[0];
    const incr = event.key === ' ' ? 0 : ['ArrowUp', 'ArrowLeft'].includes(event.key) ? -1 : 1;
    const oldValue = this.value;
    let index = radios.indexOf(checkedRadio) + incr;

    if (index < 0) {
      index = radios.length - 1;
    }

    if (index > radios.length - 1) {
      index = 0;
    }

    this.getAllRadios().forEach(radio => {
      radio.checked = false;

      if (!this.hasButtonGroup) {
        radio.tabIndex = -1;
      }
    });

    this.value = radios[index].value;
    radios[index].checked = true;

    if (!this.hasButtonGroup) {
      radios[index].tabIndex = 0;
      radios[index].focus();
    } else {
      radios[index].shadowRoot!.querySelector('button')!.focus();
    }

    if (this.value !== oldValue) {
      this.emit('sl-change');
      this.emit('sl-input');
    }

    event.preventDefault();
  }

That is when all the sl-radios are disabled then the up and down key board navigation should not work so in code I am returning it if all the radios are disabled
@claviska can you please help me in this fix

@akuu2510 akuu2510 changed the title sl-radio-group Accessibility issue: sl-radio-group Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Things that aren't working right in the library.
Projects
None yet
Development

No branches or pull requests

1 participant