Skip to content

useFocus ​

browser
test coverage
Last changed: 24 days ago

Hook that allows you to focus on a specific element

typescript
import { useFocus } from '@siberiacancode/reactuse';

Usage ​

typescript
const { focus, blur, focused } = useFocus(ref);
// or
const { ref, focus, blur, focused } = useFocus();

Demo ​

Api ​

Parameters

NameTypeDefaultNote
targetHookTarget-The target element to focus
options.initialValue?booleanfalseThe initial focus state of the target
options.onFocus?(event: FocusEvent) => void-The callback function to be invoked on focus
options.onBlur?(event: FocusEvent) => void-The callback function to be invoked on blur

Parameters

NameTypeDefaultNote
options.initialValue?booleanfalseThe initial focus state of the target
options.onFocus?(event: FocusEvent) => void-The callback function to be invoked on focus
options.onBlur?(event: FocusEvent) => void-The callback function to be invoked on blur

Source ​

Source • Demo

Contributors ​

Avatardebabin

Released under the MIT License.