Skip to content

Add new focus trap controller#1198

Draft
brentswisher wants to merge 5 commits intodevelopfrom
feat/update-focus-trap-library
Draft

Add new focus trap controller#1198
brentswisher wants to merge 5 commits intodevelopfrom
feat/update-focus-trap-library

Conversation

@brentswisher
Copy link
Contributor

This change: (check at least one)

  • Adds a new feature
  • Fixes a bug
  • Improves maintainability
  • Improves documentation
  • Is a release activity

Is this a breaking change? (check one)

  • Yes
  • No

Is the: (complete all)

  • Title of this pull request clear, concise, and indicative of the issue number it addresses, if any?
  • Test suite(s) passing?
  • Code coverage maximal?
  • Changeset added?

What does this change address?
Closes #777

How does this change work?
This replaces the previous focus-trap implementation, with a new focus trap controller that uses a more actively maintained library. It allows us to remove the previous implementation that relied on a forked package we are struggling to keep maintained.

Additional context

This replaces the previous focus-trap implementation.
This allows us to remove the previous implementation
that relied on a forked package we are struggling to keep
maintained.
@brentswisher brentswisher self-assigned this Feb 20, 2026
@changeset-bot
Copy link

changeset-bot bot commented Feb 20, 2026

⚠️ No Changeset found

Latest commit: cc1e380

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Feb 20, 2026

size-limit report 📦

Path Size
packages/pharos/lib/index.js 1.05 MB (+0.47% 🔺)

};
private _focusTrapController = new FocusTrapController(
this,
() => this.shadowRoot?.querySelector('.dropdown-menu__list') as HTMLElement | null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these all be replaced with assigned @query objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that't probably true, I can take a look at that. I also have gone back and forth if it made more sense to use a "standard" class name like focus-trap for the element we want the focus trap on, instead of targeting specific classes in specific components.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I looked into this, and there is a timing issue. The @query objects don't exist until after first render, so we can't pass them in when the FocusTrapController is initialized.

Since the FocusTrapController is already getting an instance of the element as the first argument, we could assume the class name in the controller, and move the selection there. However, if we ever needed a component with 2 separate areas that had focus traps, we wouldn't have a way to do that. It also makes the coupling (which still exists) a little less explicit.

I think I will update it to accept a selector as the second argument. That feels like a good middle ground. It is a little easier in the components then passing in a getContainer function, but keeps the interface flexible for needing to select more than .focus-trap

Open to thoughts though!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good, thanks for the leg work on it!

@brentswisher brentswisher force-pushed the feat/update-focus-trap-library branch from a386e05 to 9c44f6a Compare March 9, 2026 12:42
The new focus trap library focuses the first element by default when
the trap is activated. This is not ideal for dropdowns, which should
only focus the first element if the user has navigated to the
dropdown using the keyboard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update to actively supported focus-trap library

2 participants