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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 31 additions & 46 deletions packages/mui-base/src/FocusTrap/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,35 @@ function FocusTrap(props: FocusTrapProps): JSX.Element {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open]);

const contain = React.useCallback(
(nativeEvent: FocusEvent | null) => {
React.useEffect(() => {
// We might render an empty child.
if (!open || !rootRef.current) {
return;
}

const doc = ownerDocument(rootRef.current);

const loopFocus = (nativeEvent: KeyboardEvent) => {
lastKeydown.current = nativeEvent;

if (disableEnforceFocus || !isEnabled() || nativeEvent.key !== 'Tab') {
return;
}

// Make sure the next tab starts from the right place.
// doc.activeElement refers to the origin.
if (doc.activeElement === rootRef.current && nativeEvent.shiftKey) {
// We need to ignore the next contain as
// it will try to move the focus back to the rootRef element.
ignoreNextEnforceFocus.current = true;
if (sentinelEnd.current) {
sentinelEnd.current.focus();
}
}
};

const contain = () => {
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)


// Cleanup functions are executed lazily in React 17.
// Contain can be called between the component being unmounted and its cleanup function being run.
Expand Down Expand Up @@ -243,10 +268,7 @@ function FocusTrap(props: FocusTrapProps): JSX.Element {
}

// if the focus event is not coming from inside the children's react tree, reset the refs
if (
(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.

reactFocusEventTarget.current = null;
} else if (reactFocusEventTarget.current !== null) {
return;
Expand Down Expand Up @@ -285,35 +307,6 @@ function FocusTrap(props: FocusTrapProps): JSX.Element {
} else {
rootElement.focus();
}
},
[disableEnforceFocus, isEnabled, getTabbable],
);

React.useEffect(() => {
// We might render an empty child.
if (!open || !rootRef.current) {
return;
}

const doc = ownerDocument(rootRef.current);

const loopFocus = (nativeEvent: KeyboardEvent) => {
lastKeydown.current = nativeEvent;

if (disableEnforceFocus || !isEnabled() || nativeEvent.key !== 'Tab') {
return;
}

// Make sure the next tab starts from the right place.
// doc.activeElement refers to the origin.
if (doc.activeElement === rootRef.current && nativeEvent.shiftKey) {
// We need to ignore the next contain as
// it will try to move the focus back to the rootRef element.
ignoreNextEnforceFocus.current = true;
if (sentinelEnd.current) {
sentinelEnd.current.focus();
}
}
};

doc.addEventListener('focusin', contain);
Expand All @@ -327,7 +320,7 @@ function FocusTrap(props: FocusTrapProps): JSX.Element {
// https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule.
const interval = setInterval(() => {
if (doc.activeElement && doc.activeElement.tagName === 'BODY') {
contain(null);
contain();
}
}, 50);

Expand All @@ -337,15 +330,7 @@ function FocusTrap(props: FocusTrapProps): JSX.Element {
doc.removeEventListener('focusin', contain);
doc.removeEventListener('keydown', loopFocus, true);
};
}, [
disableAutoFocus,
disableEnforceFocus,
disableRestoreFocus,
isEnabled,
open,
getTabbable,
contain,
]);
}, [disableAutoFocus, disableEnforceFocus, disableRestoreFocus, isEnabled, open, getTabbable]);

const onFocus = (event: FocusEvent) => {
if (nodeToRestore.current === null) {
Expand Down