Skip to content

useFocus

Category
Export Size
478 B
Last Changed
5 days ago

Reactive utility to track or set the focus state of a DOM element

Demo

Basic Usage

tsx
import { 
useFocus
} from '@reause/core'
import {
useRef
} from 'react'
const
input
=
useRef
<HTMLInputElement>(null)
const [
isFocused
,
setFocused
] =
useFocus
(
input
)

State changes to reflect whether the target element is the focused element. Setting the reactive state from the outside with setFocused(true) / setFocused(false) will trigger focus and blur events for true and false values respectively.

Setting initial focus

To focus the element on its first render one can provide the initialValue option as true. This will trigger a focus event on the target element.

tsx
const [
isFocused
] =
useFocus
(input, {
initialValue
: true })

Change focus state

Changes of the isFocused state via setFocused will automatically trigger focus and blur events for true and false values respectively. You can utilize this behavior to focus the target element as a result of another action (e.g. when a button click as shown below).

tsx
import { 
useFocus
} from '@reause/core'
import {
useRef
} from 'react'
function
Component
() {
const
input
=
useRef
<HTMLInputElement>(null)
const [
isFocused
,
setFocused
] =
useFocus
(
input
)
return ( <
div
>
<
button
type="button" onClick={() =>
setFocused
(true)}>
Click me to
focus
input below
</button> <input ref={
input
} type="text" />
</div> ) }

Type Declarations

ts
export interface UseFocusOptions extends ConfigurableWindow {
    initialValue?: boolean;
    focusVisible?: boolean;
    preventScroll?: boolean;
}

export type UseFocusReturn = readonly [
    isFocused: boolean,
    setFocused: Dispatch<SetStateAction<boolean>>
]

export function useFocus(target: RefObject<HTMLElement | SVGElement | null | undefined>, options?: UseFocusOptions): UseFocusReturn

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.7 on
a82a7 - fix(core): resolve DOM ref targets after commit
v0.1.0 on
01ea4 - fix(core): resolve useFocus audit findings (#574)
d4e0a - fix(core): useFocus returns [isFocused, setFocused]
8fddc - chore!: remove all Vue-only Maybe* types and getter unions, adopt React Ref (#462)

Released under the MIT License. v0.1.8