Skip to content

Commit d600372

Browse files
authored
Fix Select + Dialog focus issue (#1608)
1 parent 19ed733 commit d600372

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/components/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [UNRELEASED]
99

10+
### Fixed
11+
12+
- `Select` inside a `Dialog` losing focus after clicking an option
13+
1014
### Changed
1115

1216
- `InputFiltersChipEditor` can use CheckboxGroup or RadioGroup.

packages/components/src/utils/useFocusTrap.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,20 @@ export function useFocusTrap(
7474
clickOutsideDeactivates: true,
7575
escapeDeactivates: false,
7676
fallbackFocus: element,
77+
initialFocus:
78+
autoFocusElement || surfaceElement
79+
? () => {
80+
// Check first if focus is already within the element
81+
// otherwise if focus trap is disabled then re-enabled
82+
// (e.g. when a Select closes inside a Dialog)
83+
// focus will move unnecessarily back to the surface / autoFocus element
84+
if (element.contains(document.activeElement)) {
85+
return document.activeElement as HTMLElement
86+
}
87+
return autoFocusElement || surfaceElement
88+
}
89+
: undefined,
7790
onDeactivate: () => setOff(),
78-
...(autoFocusElement || surfaceElement
79-
? { initialFocus: autoFocusElement || surfaceElement }
80-
: {}),
8191
})
8292
}
8393
disableFocusTrap && disableFocusTrap()

0 commit comments

Comments
 (0)