You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
useFocusWithin onBlurWithin not triggering in situation where focused component is unmounted #2015
onBlurWithin callback is not firing when I click the clear (X) button and focus changes to document body
🤔 Expected Behavior
When clear (X) button is clicked the textboxText value gets set to "" and as a result the clear button gets unmounted and focus is changed to document body. onBlurWithin callback should fire as a result.
😯 Current Behavior
onBlurWithin doesn't fire when clear (X) button is pressed and focus is changed to body.
Screen.Recording.2021-06-12.at.6.08.25.PM.mov
🔦 Context
Can't add a simple custom clear button to a textbox.
This is probably since React doesn't fire a blur event when a component is unmounted, perhaps we could use a MutationObserver to watch for any changes in the component's children (more specifically, if a child node was focused but then removed) and then either fire onBlurWithin manually ourselves or perhaps move focus back to the wrapper
@LFDanLu I'm having a problem where useFocusRing({ within: true }) keeps returning true for isFocused after the focused child component is unmounted. Focusing other elements doesn't remove the isFocused state. These are different hooks but I would assume these issues are related.
Do you know if there is any workaround for this issue? Or whether a real fix will be developed?
EDIT: This isn't an elegant workaround but works for me for now...
then you append focusFixElement as a child to the component that uses useFocusRing({ within: true }) and call updateFocus whenever you dismount another child element.
Far from great and it clears focus globally, but that's fine for me for now. If anyone has a better workaround, please tag me.
Pretty sure fix: Fix Toast hover and focus behavior has fixed this issue. Closing, we can reopen after release if it didn't.
A codesandbox to reproduce would be useful if we need to reopen it
Activity
LFDanLu commentedon Jun 14, 2021
This is probably since React doesn't fire a blur event when a component is unmounted, perhaps we could use a MutationObserver to watch for any changes in the component's children (more specifically, if a child node was focused but then removed) and then either fire onBlurWithin manually ourselves or perhaps move focus back to the wrapper
React issue for blur: react/react#12363
EDIT:
Alternatively, we could explore using the browser's
focusoutevent instead of React'sonBlurwhich would handle the child unmounting case.VapidLinus commentedon Oct 30, 2023
@LFDanLu I'm having a problem where
useFocusRing({ within: true })keeps returningtrueforisFocusedafter the focused child component is unmounted. Focusing other elements doesn't remove theisFocusedstate. These are different hooks but I would assume these issues are related.Do you know if there is any workaround for this issue? Or whether a real fix will be developed?
EDIT: This isn't an elegant workaround but works for me for now...
then you append
focusFixElementas a child to the component that usesuseFocusRing({ within: true })and callupdateFocuswhenever you dismount another child element.Far from great and it clears focus globally, but that's fine for me for now. If anyone has a better workaround, please tag me.
snowystinger commentedon Mar 3, 2025
Pretty sure fix: Fix Toast hover and focus behavior has fixed this issue. Closing, we can reopen after release if it didn't.
A codesandbox to reproduce would be useful if we need to reopen it