Skip to content

useFocusWithin ​

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

Code demonstration ​

API ​

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

Params ​

PropertyDescriptionTypeDefault
targetDOM element or ref() => Element | Element | JSX.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 areaRef<boolean>

Source ​

Source · Document · Demo

Released under the MIT License.