useFocusWithin

Monitor whether the current focus is within a certain area, Same as css attribute :focus-within.

Examples

Default usage

isFocusWithin:false

Basic usage

Use useRef to set area that needs monitoring. The focus can be switched by click the outside with the mouse, or using keys such as `tab` on the keyboard.

Pass in DOM element

isFocusWithin:false

Pass in DOM element

Pass in a function that returns the DOM element.

API

const isFocusWithin = useFocusWithin(  target,  {   onFocus,   onBlur,   onChange  });
const isFocusWithin = useFocusWithin(  target,  {   onFocus,   onBlur,   onChange  });

Params

PropertyDescriptionTypeDefault
targetDOM element() => Element | Element-
optionsMore configOptions-

Options

PropertyDescriptionTypeDefault
onFocusCallback to be executed on focus(e: FocusEvent) => void-
onBlurCallback to be executed on blur(e: FocusEvent) => void-
onChangeCallback to be executed on focus change(isFocusWithin: boolean) => void-

Result

PropertyDescriptionType
isFocusWithinWhether the focus is in the current areaAccessor<boolean>