Skip to content

useFocusWithin onBlurWithin not triggering in situation where focused component is unmounted #2015

Closed

Description

@agusterodin

🐛 Bug Report

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.

💻 Code Sample

import { useFocusWithin } from '@react-aria/interactions'

export default function Searchbox({ searchInputRef }) {
  const [textboxText, setTextboxText] = useState('')

  let { focusWithinProps } = useFocusWithin({
    onFocusWithin: e => console.log('FOCUS WITHIN'),
    onBlurWithin: e => console.log('FOCUS BLUR')
  })

  return (
    <div className="relative w-min" {...focusWithinProps}>
      <input className="text-gray-900" value={textboxText} onChange={event => setTextboxText(event.target.value)} />
      {textboxText !== '' && (
        <button className="absolute top-0 right-0 flex h-full text-gray-500 px-2" onClick={event => setTextboxText('')}>
          X
        </button>
      )}
    </div>
  )
}

🌍 Your Environment

react-aria/interactions 3.4.0, Google Chrome 91.0.4472.101 (arm64), MacOS 11.4

🧢 Your Company/Team

Personal project / education

Activity

  1. LFDanLu commented on Jun 14, 2021

    @LFDanLu
    Member

    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 focusout event instead of React's onBlur which would handle the child unmounting case.

  2. added
    bug
    on Jun 14, 2021
  3. moved this from ✏️ To Groom to 📋 Waiting for Sprint in RSP Component Milestoneson Apr 5, 2023
  4. VapidLinus commented on Oct 30, 2023

    @VapidLinus

    @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...

    function useFocusBugWorkaround() {
        const ref = useRef<HTMLSpanElement>(null)
    
        const updateFocus = useCallback(() => {
            ref.current?.focus()
            ref.current?.blur()
        }, [])
    
        const focusFixElement = <span ref={ref} tabIndex={-1} aria-hidden="true" />
    
        return { updateFocus, focusFixElement }
    }

    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.

  5. snowystinger commented on Mar 3, 2025

    @snowystinger
    Member

    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

  6. moved this from 📋 Waiting for Sprint to ✅ Done in RSP Component Milestoneson Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions