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

focusManager.md

63 lines1.4 KB

1---
2id: FocusManager
3title: FocusManager
4---
5​
6The `FocusManager` manages the focus state within TanStack 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```tsx
21import { focusManager } from '@tanstack/react-query'
22​
23focusManager.setEventListener((handleFocus) => {
24 // Listen to visibilitychange
25 if (typeof window !== 'undefined' && window.addEventListener) {
26 window.addEventListener('visibilitychange', handleFocus, false)
27 }
28​
29 return () => {
30 // Be sure to unsubscribe if a new handler is set
31 window.removeEventListener('visibilitychange', handleFocus)
32 }
33})
34```
35​
36## `focusManager.setFocused`
37​
38`setFocused` can be used to manually set the focus state. Set `undefined` to fallback to the default focus check.
39​
40```tsx
41import { focusManager } from '@tanstack/react-query'
42​
43// Set focused
44focusManager.setFocused(true)
45​
46// Set unfocused
47focusManager.setFocused(false)
48​
49// Fallback to the default focus check
50focusManager.setFocused(undefined)
51```
52​
53**Options**
54​
55- `focused: boolean | undefined`
56​
57## `focusManager.isFocused`
58​
59`isFocused` can be used to get the current focus state.
60​
61```tsx
62const isFocused = focusManager.isFocused()
63```