useFocusManager

    API

    import useFocusManager from '@restart/hooks/useFocusManager'
    • useFocusManager(opts: FocusManagerOptions) => FocusController

      useFocusManager provides a way to track and manage focus as it moves around a container element. An onChange is fired when focus enters or leaves the element, but not when it moves around inside the element, similar to pointerenter and pointerleave DOM events.

      const [focused, setFocusState] = useState(false)
      const { onBlur, onFocus } = useFocusManager({
      onChange: nextFocused => setFocusState(nextFocused)
      })
      return (
      <div tabIndex="-1" onFocus={onFocus} onBlur={onBlur}>
      {String(focused)}
      <input />
      <input />
      <button>A button</button>
      </div>

      Parameters

      • optsFocusManagerOptions
        • isDisabled() => boolean

          When true, the event handlers will not report focus changes

        • didHandle(focused: boolean, event: FocusEvent) => void
        • onChange(focused: boolean, event: FocusEvent) => void
        • willHandle(focused: boolean, event: FocusEvent) => boolean | void

      Return Value FocusController

      a memoized FocusController containing event handlers