Skip to content
On this page

useFocusWithin

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

Code demonstration

API

const isFocusWithin = useFocusWithin(target, {
  onFocus,
  onBlur,
  onChange,
})
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

SourceDocsDemo