Appearance
useFocusWithin
Reactive utility to track if an element or one of its descendants has focus. It is meant to match the behavior of the :focus-within CSS pseudo-class. A common use case would be on a form element to see if any of its inputs currently have focus.
Demo
Basic Usage
tsx
import { useFocusWithin } from '@reause/core'
import { useRef } from 'react'
const target = useRef<HTMLFormElement>(null)
const { focused } = useFocusWithin(target)
// `focused` is true while the form or any input inside it has focusType Declarations
ts
export interface UseFocusWithinReturn {
focused: boolean;
}
export type ElementTarget<T extends TargetElement = TargetElement> = RefObject<T | null>
export type ElementTargetOrArray<T extends TargetElement = TargetElement> = ElementTarget<T> | ElementTarget<T>[]
export function useFocusWithin(target: ElementTarget, options?: ConfigurableWindow): UseFocusWithinReturn