Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FocusTrap] Refactor & cleanup #38878

Merged
merged 2 commits into from
Sep 11, 2023
Merged

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Sep 9, 2023

Things done in the PR:

  • moved the contain function back in the useEffect
  • remove the usage of the nativeEvent - the reason for adding this event handler was to check if the focus was invoked with different target based on [TrapFocus] Fix portal support #21610 (comment), but there can never be a difference between the focusin's event.target element and the document.activeElement - and this check is already captured in the second part of the condition (I was testing the scenario that was shared in that reproduction and it still works as expected, and all tests pass too), so everything should be fine.

@mnajdova mnajdova added component: FocusTrap The React component. enhancement This is not a bug, nor a new feature labels Sep 9, 2023
@mui-bot
Copy link

mui-bot commented Sep 9, 2023

Netlify deploy preview

https://deploy-preview-38878--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 39a6f69

const rootElement = rootRef.current;
const doc = ownerDocument(rootRef.current);
Copy link
Member Author

Choose a reason for hiding this comment

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

It's already defined in the upper scope (the useEffect)

(nativeEvent && reactFocusEventTarget.current !== nativeEvent.target) ||
doc.activeElement !== reactFocusEventTarget.current
) {
if (doc.activeElement !== reactFocusEventTarget.current) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The second part of the condition should always capture the first part too. I can't find scenario where the focusin's event.target will not be the document.activeElement

Copy link
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 a need for it either. As far as I now, doc.activeElement moves synchronously, so nativeEvent.target === document.activeElement at all time. Can't see a need for it from #21610.

@mnajdova mnajdova marked this pull request as ready for review September 9, 2023 15:48
(nativeEvent && reactFocusEventTarget.current !== nativeEvent.target) ||
doc.activeElement !== reactFocusEventTarget.current
) {
if (doc.activeElement !== reactFocusEventTarget.current) {
Copy link
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 a need for it either. As far as I now, doc.activeElement moves synchronously, so nativeEvent.target === document.activeElement at all time. Can't see a need for it from #21610.

@mnajdova mnajdova merged commit 1bbb086 into mui:master Sep 11, 2023
5 checks passed
xcode-it pushed a commit to xcode-it/material-ui that referenced this pull request Sep 11, 2023
@oliviertassinari
Copy link
Member

Nice, thanks for the cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: FocusTrap The React component. enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants