Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

focusManager.md

65 lines1.5 KB

1---
2id: FocusManager
3title: FocusManager
4---
5​
6The `FocusManager` manages the focus state within React Query.
7​
8It can be used to change the default event listeners or to manually change the focus state.
9​
10Its available methods are:
11​
12- [`setEventListener`](#focusmanagerseteventlistener)
13- [`setFocused`](#focusmanagersetfocused)
14- [`isFocused`](#focusmanagerisfocused)
15​
16## `focusManager.setEventListener`
17​
18`setEventListener` can be used to set a custom event listener:
19​
20```js
21import { focusManager } from 'react-query'
22​
23focusManager.setEventListener(handleFocus => {
24 // Listen to visibilitychange and focus
25 if (typeof window !== 'undefined' && window.addEventListener) {
26 window.addEventListener('visibilitychange', handleFocus, false)
27 window.addEventListener('focus', handleFocus, false)
28 }
29​
30 return () => {
31 // Be sure to unsubscribe if a new handler is set
32 window.removeEventListener('visibilitychange', handleFocus)
33 window.removeEventListener('focus', handleFocus)
34 }
35})
36```
37​
38## `focusManager.setFocused`
39​
40`setFocused` can be used to manually set the focus state. Set `undefined` to fallback to the default focus check.
41​
42```js
43import { focusManager } from 'react-query'
44​
45// Set focused
46focusManager.setFocused(true)
47​
48// Set unfocused
49focusManager.setFocused(false)
50​
51// Fallback to the default focus check
52focusManager.setFocused(undefined)
53```
54​
55**Options**
56​
57- `focused: boolean | undefined`
58​
59## `focusManager.isFocused`
60​
61`isFocused` can be used to get the current focus state.
62​
63```js
64const isFocused = focusManager.isFocused()
65```