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

FocusManager.md

209 lines4.4 KB

1---
2id: FocusManager
3title: FocusManager
4redirect_from:
5 - reference/focusManager
6 - framework/react/reference/focusManager
7---
8​
9<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
10​
11Defined in: [packages/query-core/src/focusManager.ts:14](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L14)
12​
13The `FocusManager` manages the focus state within TanStack Query.
14​
15It can be used to change the default event listeners or to manually change the focus state.
16​
17## Extends
18​
19- `Subscribable`\<`Listener`\>
20​
21## Methods
22​
23### hasListeners()
24​
25```ts
26hasListeners(): boolean;
27```
28​
29Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
30​
31Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
32​
33#### Returns
34​
35`boolean`
36​
37`true` if at least one listener is registered.
38​
39#### Inherited from
40​
41```ts
42Subscribable.hasListeners
43```
44​
45***
46​
47### isFocused()
48​
49```ts
50isFocused(): boolean;
51```
52​
53Defined in: [packages/query-core/src/focusManager.ts:130](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L130)
54​
55`isFocused` can be used to get the current focus state.
56​
57#### Returns
58​
59`boolean`
60​
61The focus state set with `setFocused`, or otherwise whether the document is visible.
62​
63***
64​
65### onFocus()
66​
67```ts
68onFocus(): void;
69```
70​
71Defined in: [packages/query-core/src/focusManager.ts:119](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L119)
72​
73`onFocus` notifies all subscribed listeners with the current focus state.
74​
75#### Returns
76​
77`void`
78​
79***
80​
81### setEventListener()
82​
83```ts
84setEventListener(setup: SetupFn): void;
85```
86​
87Defined in: [packages/query-core/src/focusManager.ts:78](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L78)
88​
89`setEventListener` can be used to set a custom event listener that will
90be used to determine the focus state. The provided `setup` function
91receives a `setFocused` callback: call it with a `boolean` to manually
92set the focus state, or with no arguments to re-evaluate the current
93focus state and notify subscribers.
94​
95#### Parameters
96​
97##### setup
98​
99`SetupFn`
100​
101Receives the `setFocused` callback, registers the event listener, and may return
102a cleanup function that is called when the listener is replaced or no longer needed.
103​
104#### Returns
105​
106`void`
107​
108#### Example
109​
110```ts
111import { focusManager } from '@tanstack/query-core'
112​
113focusManager.setEventListener((handleFocus) => {
114 const listener = () => handleFocus()
115 // Listen to visibilitychange
116 if (typeof window !== 'undefined' && window.addEventListener) {
117 window.addEventListener('visibilitychange', listener, false)
118 }
119​
120 return () => {
121 // Be sure to unsubscribe if a new handler is set
122 window.removeEventListener('visibilitychange', listener)
123 }
124})
125```
126​
127***
128​
129### setFocused()
130​
131```ts
132setFocused(focused?: boolean): void;
133```
134​
135Defined in: [packages/query-core/src/focusManager.ts:108](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L108)
136​
137`setFocused` can be used to manually set the focus state. Set `undefined`
138to fall back to the default focus check.
139​
140#### Parameters
141​
142##### focused?
143​
144`boolean`
145​
146The focus state, or `undefined` to use the default focus check.
147​
148#### Returns
149​
150`void`
151​
152#### Example
153​
154```ts
155import { focusManager } from '@tanstack/query-core'
156​
157// Set focused
158focusManager.setFocused(true)
159​
160// Set unfocused
161focusManager.setFocused(false)
162​
163// Fallback to the default focus check
164focusManager.setFocused(undefined)
165```
166​
167***
168​
169### subscribe()
170​
171```ts
172subscribe(listener: Listener): () => void;
173```
174​
175Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
176​
177Registers a listener to be called on every update this object notifies about. Returns a function
178that removes the listener again β€” call it to stop listening. The base class never drops a listener
179on its own, though some subclasses clear all of theirs in `destroy()`.
180​
181#### Parameters
182​
183##### listener
184​
185`Listener`
186​
187Called on each update, with whatever the subclass passes to its subscribers.
188​
189#### Returns
190​
191A function that removes the listener.
192​
193() => `void`
194​
195#### Example
196​
197```ts
198const unsubscribe = subscribable.subscribe(() => {
199 // react to the update
200})
201​
202unsubscribe()
203```
204​
205#### Inherited from
206​
207```ts
208Subscribable.subscribe
209```