Skip to content

fix: #8686 prevent useGridCell from overriding child focus restoration loops#10228

Open
jsmitrah wants to merge 5 commits into
adobe:mainfrom
jsmitrah:bug-fix/8686/table-cell-incorrectly-returns-focus-to-button
Open

fix: #8686 prevent useGridCell from overriding child focus restoration loops#10228
jsmitrah wants to merge 5 commits into
adobe:mainfrom
jsmitrah:bug-fix/8686/table-cell-incorrectly-returns-focus-to-button

Conversation

@jsmitrah

@jsmitrah jsmitrah commented Jun 18, 2026

Copy link
Copy Markdown

Closes #8686

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

  • Tab into the table row and use the arrow keys to focus on the "Open Dialog" button.
  • Press Enter to open the modal dialog, then press Escape to close it.
  • Verify that focus returns exactly to the "Open Dialog" button instead of jumping to the "Action 1" button.

🧢 Your Project:

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the interest, if you run yarn format it should get rid of all the noisy whitespace changes that I'm guessing your IDE applied

@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch 5 times, most recently from 0a7cf3b to 714723c Compare June 18, 2026 15:09
@jsmitrah jsmitrah requested a review from snowystinger June 19, 2026 05:35
@jsmitrah

Copy link
Copy Markdown
Author

Thanks @snowystinger, all tests are passed

@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch from ccb8bbd to 4b28c4a Compare June 19, 2026 08:22
@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch from 71950d3 to 22b6e5f Compare June 24, 2026 10:54
return;
}

// If the cell itself is focused, wait a frame so that focus finishes propagatating

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems like an important comment. JSDOM and the act environment is not a reliable test against this as it's simulating real browsers.
Did you run this against real browsers? If I recall, document.activeElement can be updated at different times depending on the browser you're in as well.

@jsmitrah jsmitrah Jun 26, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, @snowystinger. To avoid the timing issues of the document.activeElement across different real browsers, I updated the approach to use e.relatedTarget.

By checking e.relatedTarget synchronously, we can instantly determine if focus is moving directly into a nested child element. This provides identical, reliable accuracy in both JSDOM and real browsers without needing to wait for a frame

I've pushed the update.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see any update with relatedTarget?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Apologies for the confusion, @snowystinger . I initially tried an implementation using e.relatedTarget, but it completely broke the React 16 test suite (test-16) on CircleCI due to synthetic event pooling limitations in older environments.

Because asynchronous timeouts/frames break the synchronous act() testing clock, I reverted to the synchronous approach using getActiveElement(). I have also restored the original comment regarding focus propagation to keep the documentation clear.

@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch from b06221e to adbba17 Compare June 26, 2026 14:56
@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch from 6ab9b4c to bce1556 Compare June 26, 2026 15:21
Comment on lines -260 to -265
// useSelectableItem only handles setting the focused key when
// the focused element is the gridcell itself. We also want to
// set the focused key when a child element receives focus.
// If focus is currently visible (e.g. the user is navigating with the keyboard),
// then skip this. We want to restore focus to the previously focused row/cell
// in that case since the table should act like a single tab stop.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why was this deleted?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

"My apologies, @snowystinger! That was completely accidental during a refactor pass in my editor. I have fully restored the original comment block.

return;
}

// If the cell itself is focused, wait a frame so that focus finishes propagatating

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see any update with relatedTarget?

@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch from 535198e to 244fa42 Compare July 2, 2026 13:32
@jsmitrah jsmitrah force-pushed the bug-fix/8686/table-cell-incorrectly-returns-focus-to-button branch from 244fa42 to 24667bf Compare July 2, 2026 13:39
@jsmitrah jsmitrah requested a review from snowystinger July 2, 2026 13:50
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.

Table cell incorrectly returns focus to button on modal close

2 participants