Skip to content

Commit

Permalink
fix tabbable for radios (#2357)
Browse files Browse the repository at this point in the history
* fix tabbable for radios

* prettier

* add note about focus trapping

* prettier
  • Loading branch information
KonnorRogers authored Feb 3, 2025
1 parent 6761fdc commit b0399ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti

- Improved performance of `<sl-select>` when using a large number of options [#2318]
- Updated the Japanese translation [#2329]
- Fixed a bug with radios in `<sl-dialog>` focus trapping.

## 2.19.1

Expand Down
16 changes: 13 additions & 3 deletions src/internal/tabbable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,19 @@ function isTabbable(el: HTMLElement) {
return false;
}

// Radios without a checked attribute are not tabbable
if (tag === 'input' && el.getAttribute('type') === 'radio' && !el.hasAttribute('checked')) {
return false;
if (tag === 'input' && el.getAttribute('type') === 'radio') {
const rootNode = el.getRootNode() as HTMLElement;

const findRadios = `input[type='radio'][name="${el.getAttribute('name')}"]`;
const firstChecked = rootNode.querySelector(`${findRadios}:checked`);

if (firstChecked) {
return firstChecked === el;
}

const firstRadio = rootNode.querySelector(findRadios);

return firstRadio === el;
}

if (!isVisible(el)) {
Expand Down

0 comments on commit b0399ca

Please sign in to comment.