-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The use of `FocusScope` started to fail on the CI machine. It also had the mentioned caveats. That's now replaced with a custom hook.
- Loading branch information
1 parent
f62faf9
commit eda3709
Showing
5 changed files
with
113 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { RefObject, useEffect } from "react"; | ||
|
||
/** | ||
* Executes a callback function when focus is lost from the container element. i.e. when the currently focused element | ||
* was within the container and: | ||
* - The focus is going to an element outside the container, or | ||
* - The focus is about to get lost (i.e. go to `document.body`). | ||
* | ||
* Note: react-aria's `useFocusWithin` (and it's `onBlurWithin` option) can't be used, since (at least currently) it | ||
* doesn't cover the scenario where the focus is about to get lost due to the focused element getting unmounted. | ||
*/ | ||
export function useOnFocusLost( | ||
onFocusLost: (args: { | ||
focusLosingElement: HTMLElement | null; | ||
focusReceivingElement: Element | null; | ||
}) => void, | ||
containerRef: RefObject<HTMLElement> | ||
): void { | ||
useEffect(() => { | ||
const handleBodyFocus = (e: FocusEvent) => { | ||
if ( | ||
e.target instanceof HTMLElement && | ||
containerRef.current?.contains(e.target) && | ||
(!e.relatedTarget || e.relatedTarget instanceof HTMLElement) && | ||
!containerRef.current?.contains(e.relatedTarget) | ||
) { | ||
onFocusLost({ | ||
focusLosingElement: e.target, | ||
focusReceivingElement: e.relatedTarget, | ||
}); | ||
} | ||
}; | ||
containerRef.current?.addEventListener("focusout", handleBodyFocus); | ||
return () => { | ||
containerRef.current?.removeEventListener("focusout", handleBodyFocus); | ||
}; | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters