fix: Update <FocusTrap> to handle single focusable child element#306
Merged
maximo-macchi-cb merged 4 commits intomasterfrom Jan 22, 2026
Merged
Conversation
Collaborator
✅ Heimdall Review Status
✅
|
| Code Owner | Status | Calculation | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ui-systems-eng-team |
✅
1/1
|
Denominator calculation
|
hcopp
reviewed
Jan 21, 2026
| (activeElementIsMenuItemOrOption || secondElementIsMenuItemOrOption)) | ||
| ) { | ||
| secondElement.focus(); | ||
| secondElement?.focus(); |
Contributor
There was a problem hiding this comment.
Would it be feasible to add tests for FocusTrap for when there are 0, 1, or 2+ children?
Contributor
Author
There was a problem hiding this comment.
Good idea! I've rewritten the test file to test <FocusTrap> more directly rather than using <Dropdown> (which is now deprecated).
I didn't include tests for 0 children. Handling that edge case requires making more changes to the <FocusTrap> component. While not a huge lift, I don't think it's an edge case we should be considering. The purpose of the component is to trap focus on focusable elements. The note I added to the docs in this PR calls this out as well. I could add a warning when in a dev environment if that's helpful.
hcopp
previously approved these changes
Jan 22, 2026
8ad6b95 to
725ea64
Compare
hcopp
approved these changes
Jan 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed? Why?
This PR adds a fallback check if there's only one focusable element in a
<FocusTrap>. If there's no second focusable element, the<FocusTrap>continues its focus logic and does not error out.A note has been added to the docs to warn customers that at least one child element should be focusable. This is done because adding fallback checks for the first focusable element adds a bit more complexity. It's an edge case we shouldn't have to consider because a
<FocusTrap>should always have at least one focusable element.Root cause (required for bugfixes)
There's a constant called
secondElementused to handle focus logic for our dropdown components. This constant assumes there are at least two focusable children elements based on how it's queried:const secondElement = focusableElements[1]. When there's only one focusable child element,secondElementisundefinedand so any reference to an attribute ofsecondElementsuch asconst secondElementIsMenuItemOrOption = secondElement?.role === 'menuitem' || secondElement?.role === 'option'errors out.UI changesTesting
How has it been tested?
Testing instructions
Illustrations/Icons Checklist
Required if this PR changes files under
packages/illustrations/**orpackages/icons/**Change management
type=routine
risk=low
impact=sev5
automerge=false