Repository navigation
Conversation
commit: |
Bundle size
PerformanceNo regressions in 36 benchmarks · ran 3m 53s · details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Signed-off-by: Jenna Smith <175330+jjenzz@users.noreply.github.com>
|
Thanks for this! The triggerless menu test added in #5645 has been intermittently failing the React 18 browser CI on my PR #5793, and Codex traced it to one queued focus request canceling another. I tried this fix against the failing test and it passed consistently, so it looks like this PR fixes that failure too. @michaldudak Regardless of the flaky tests, this feels like the more correct behaviour for a shared utility. We can't really know how consumers might sequence focus programmatically, but it does increase the bundle size, so I'm not sure how to weigh it up. Wdyt? |
|
Yeah, this looks like a good solution. The bundle size increase isn't that big, so I'm fine with it. 🔴 Modal focus-guard redirect overrides a newer hover focus in ContextMenu Location: if (modal) {
// enqueueFocus returns a rAF-cancel function we don't need here.
void enqueueFocus(getTabbableContent()[0]);Both modal guards queue a redirect with no shouldFocus check. Before this PR, any later enqueueFocus call cancelled that frame through the module-level rafId. That included the synchronous item focus useListNavigation makes on hover. Now cancellation only applies when both calls target the same element, so a stale redirect to item A survives a newer focus on item B. This also contradicts the PR description's claim that "final focus is unchanged in every reachable call-site ordering". Failure scenario: I checked this with a throwaway jsdom test using the real ContextMenu and faked frames: Item A is focused and highlighted. The test fails on the PR head (final focus A) and passes with the base Fix: Only redirect while focus is still on the guard. Use the same shape for the before guard, targeting if (modal) {
const guard = event.currentTarget;
void enqueueFocus(getTabbableContent()[0], {
shouldFocus: () => activeElement(ownerDocument(guard)) === guard,
});Other notes: This PR fixes the cross-cancellation, but opening focus still lands on the next frame. Tests that press the next key on the focused element should keep waiting for real DOM focus, not just data-highlighted. |
Focus guard redirects are queued for the next frame. Now that queued focus is only cancelled per element, a newer focus on another element (e.g. a hover highlight in ContextMenu) no longer cancels the redirect, so it could pull focus back. Only redirect while focus is still on the guard.
|
Thanks for catching this, @michaldudak — and for the clear repro. You're right: the modal focus guards queued their redirect unconditionally and relied on the old module-level cancel to get dropped when something else took focus. With per-element cancellation that no longer happened, so a stale redirect could pull focus back from a newer hover focus. Fixed in 4a070a3: both guards now only redirect if focus is still on the guard when the frame runs ( I've also corrected the "final focus is unchanged in every reachable call-site ordering" line in the description — that claim was wrong. Re: opening focus still landing on the next frame — agreed, the new tests wait for real DOM focus rather than just |
| return button; | ||
| } | ||
|
|
||
| it('focuses the element on the next frame', () => { |
There was a problem hiding this comment.
Would be nice to wrap all of that inside a describe('enqueueFocus() {
|
@michaldudak how relevant are these? PR reviewNothing here blocks merge. I traced every caller of Tests (1)1. 🟡 Only the after-guard redirect is coveredLocation: const afterGuard = menu.nextElementSibling as HTMLElement;The PR adds The test also locates the guard with Failure scenario: The before-guard's Fix: Add the mirror case. Focus Simplifications (1)1. ℹ️ The
|
|
Thanks! Addressed the nits:
I left the |
|
This pull request has been inactive for 30 days. Please remove the stale label or leave a comment to keep it open. Otherwise, it will be closed in 15 days. |
Fixes #5268
Summary
enqueueFocuskeeps a single module-level rAF handle and unconditionally cancels it on entry, so concurrent callers cancel each other's queued focus regardless of target. On keyboard-open of a roving-focus popup,FloatingFocusManager's initial-focus call cancels the option focus queued byuseListNavigation— the manager'sshouldFocusguard is designed to yield when focus has already moved inside the popup, but the module-level cancel kills the very frame that would move it, so the guard never gets the chance.Pending frames are now keyed per element (
WeakMap<FocusableElement, number>), so a caller only replaces its own target's pending focus. With that in place, the initial-focus guard observes the item focus and yields as designed. When the manager's initial-focus fallback resolves to a different element than the active item (any tabbable rendered before the items), the old code left focus permanently on that element and list navigation dead — the new hook-level test pins that case.useListNavigationretires its own previous queued request before queueing focus for another item, preserving the previous last-wins behavior for consecutive queued requests. Anulltarget is now a no-op instead of cancelling an unrelated caller's pending focus.The existing TODO in
useListNavigation.test.tsx("the animation frame callback fromenqueueFocussometimes kicking in after the click … causes flakeyness") describes the same mechanism.Behavior notes
shouldFocus— the modal focus guards inFloatingFocusManagerdo this, so a guard redirect yields to a newer focus that lands before the frame.AnimationFramescheduler was evaluated and deliberately left out: it changes timing under JSDOM's rAF stub and breaks four JSDOM-onlyFloatingFocusManagertests. [internal] Refactor: remove raw requestAnimationFrame #1901 also left this file's raw rAF in place.Tests
enqueueFocus.test.ts(new): the cancellation semantics unit-tested with faked frames — JSDOM's realcancelAnimationFramedoesn't cancel, so the suite runs onvi.useFakeTimers(). Two cases fail onmain(cross-element cancel,nullside effect); the rest pin the preserved behavior (same-element replacement, cancel-function guard,sync,shouldFocus), each verified to fail against a mutated implementation.useListNavigation.test.tsx: keyboard-open focuses the active item when the manager's initial focus targets another tabbable (fails onmain); consecutive queued requests skip intermediate items.ContextMenuRoot.test.tsx: a queued modal focus-guard redirect doesn't override a newer hover focus on another item (fails without the guard check).