Skip to content

Commit

Permalink
Allow modal dialogs in useRovingTabIndex (#4013)
Browse files Browse the repository at this point in the history
* skip modal dialog elements in useRovingTabIndex

* changeset
  • Loading branch information
keithamus authored Dec 6, 2023
1 parent 4321d59 commit d26aeab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-tables-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Avoid error when using Modal Dialog within TreeView
11 changes: 11 additions & 0 deletions src/TreeView/useRovingTabIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export function useRovingTabIndex({
getNextFocusable: (direction, from, event) => {
if (!(from instanceof HTMLElement)) return

// Skip elements within a modal dialog
// This need to be in a try/catch to avoid errors in
// non-supported browsers
try {
if (from.closest('dialog:modal')) {
return
}
} catch {
// Don't return
}

return getNextFocusableElement(from, event) ?? from
},
focusInStrategy: () => {
Expand Down

0 comments on commit d26aeab

Please sign in to comment.