@jsb188/react 1.1.10 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/libs/README.md +75 -0
- package/libs/page-filters.helpers.ts +65 -0
- package/libs/page-filters.ts +223 -0
- package/libs/use-page-filters.ts +147 -0
- package/package.json +24 -20
- package/states/apiData.ts +18 -16
- package/states/appContext.ts +327 -312
- package/states/connected.ts +135 -11
- package/states/index.ts +2 -1
- package/states/keyDown.ts +148 -47
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +369 -268
- package/states/popOver.ts +374 -244
- package/states/reactiveEvents.ts +99 -0
- package/states/uploads.ts +39 -27
- package/types/PopOver.d.ts +552 -223
- package/utils/hooks.ts +387 -272
- package/utils/lazy.ts +183 -0
- package/states/toasts.ts +0 -30
- package/utils/schema.ts +0 -158
package/states/connected.ts
CHANGED
|
@@ -1,12 +1,88 @@
|
|
|
1
|
-
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'
|
|
1
|
+
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai';
|
|
2
|
+
import type { SetStateAction } from 'react';
|
|
3
|
+
|
|
4
|
+
export type RealtimeConnectionStatus =
|
|
5
|
+
| 'CONNECTED'
|
|
6
|
+
| 'DISCONNECTED'
|
|
7
|
+
| 'REAUTHENTICATING'
|
|
8
|
+
| 'REVOKED';
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* Preserve terminal revocation when a recoverable transport path disconnects.
|
|
12
|
+
*/
|
|
13
|
+
export function getRealtimeConnectionStatusAfterDisconnect(
|
|
14
|
+
status: RealtimeConnectionStatus,
|
|
15
|
+
): RealtimeConnectionStatus {
|
|
16
|
+
return status === 'REVOKED' ? status : 'DISCONNECTED';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* Enter optimistic reauthentication only from an authenticated-ready lifecycle.
|
|
21
|
+
*/
|
|
22
|
+
export function getRealtimeConnectionStatusAfterAuthReset(
|
|
23
|
+
status: RealtimeConnectionStatus,
|
|
24
|
+
): RealtimeConnectionStatus {
|
|
25
|
+
return status === 'CONNECTED' || status === 'REAUTHENTICATING'
|
|
26
|
+
? 'REAUTHENTICATING'
|
|
27
|
+
: status;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* Return whether the app should surface its reconnecting recovery message.
|
|
32
|
+
*/
|
|
33
|
+
export function shouldShowRealtimeReconnectingMessage(
|
|
34
|
+
status: RealtimeConnectionStatus,
|
|
35
|
+
) {
|
|
36
|
+
return status === 'DISCONNECTED';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* realtimeConnectionStatusState; Track transport and authenticated readiness.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const realtimeConnectionStatusState =
|
|
44
|
+
atom<RealtimeConnectionStatus>('DISCONNECTED');
|
|
2
45
|
|
|
3
46
|
/**
|
|
4
|
-
*
|
|
5
|
-
* Developer should use either WebSockets, or SSE to track this,
|
|
6
|
-
* or just keep the value set to {true} on app mount.
|
|
47
|
+
* connectedToServerState; Preserve the strict authenticated-ready boolean API.
|
|
7
48
|
*/
|
|
8
49
|
|
|
9
|
-
const connectedToServerState = atom(
|
|
50
|
+
const connectedToServerState = atom(
|
|
51
|
+
(get) => get(realtimeConnectionStatusState) === 'CONNECTED',
|
|
52
|
+
(get, set, nextValue: SetStateAction<boolean>) => {
|
|
53
|
+
const connected = get(realtimeConnectionStatusState) === 'CONNECTED';
|
|
54
|
+
const nextConnected =
|
|
55
|
+
typeof nextValue === 'function' ? nextValue(connected) : nextValue;
|
|
56
|
+
set(
|
|
57
|
+
realtimeConnectionStatusState,
|
|
58
|
+
nextConnected ? 'CONNECTED' : 'DISCONNECTED',
|
|
59
|
+
);
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* optimisticConnectedToServerState; Keep presentation connected during auth rotation.
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
const optimisticConnectedToServerState = atom((get) => {
|
|
68
|
+
const status = get(realtimeConnectionStatusState);
|
|
69
|
+
return status === 'CONNECTED' || status === 'REAUTHENTICATING';
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* realtimeReconnectingMessageState; Narrowly observe visible recovery status.
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
const realtimeReconnectingMessageState = atom((get) =>
|
|
77
|
+
shouldShowRealtimeReconnectingMessage(get(realtimeConnectionStatusState)),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* realtimeConnectionGenerationState; Monotonic identity for replacement
|
|
82
|
+
* realtime connections, including reconnects whose false state is batched.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
const realtimeConnectionGenerationState = atom(0);
|
|
10
86
|
|
|
11
87
|
/**
|
|
12
88
|
* screenIsFocused; whether or not app is open & focused
|
|
@@ -19,7 +95,7 @@ const screenIsFocusedState = atom(true);
|
|
|
19
95
|
*/
|
|
20
96
|
|
|
21
97
|
export function useSetConnectedToServer() {
|
|
22
|
-
|
|
98
|
+
return useSetAtom(connectedToServerState);
|
|
23
99
|
}
|
|
24
100
|
|
|
25
101
|
/**
|
|
@@ -27,7 +103,7 @@ export function useSetConnectedToServer() {
|
|
|
27
103
|
*/
|
|
28
104
|
|
|
29
105
|
export function useConnectedToServerValue() {
|
|
30
|
-
|
|
106
|
+
return useAtomValue(connectedToServerState);
|
|
31
107
|
}
|
|
32
108
|
|
|
33
109
|
/**
|
|
@@ -35,7 +111,55 @@ export function useConnectedToServerValue() {
|
|
|
35
111
|
*/
|
|
36
112
|
|
|
37
113
|
export function useConnectedToServer() {
|
|
38
|
-
|
|
114
|
+
return useAtom(connectedToServerState);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Export; get the complete realtime connection lifecycle status.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
export function useRealtimeConnectionStatusValue() {
|
|
122
|
+
return useAtomValue(realtimeConnectionStatusState);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Export; set the complete realtime connection lifecycle status.
|
|
127
|
+
*/
|
|
128
|
+
|
|
129
|
+
export function useSetRealtimeConnectionStatus() {
|
|
130
|
+
return useSetAtom(realtimeConnectionStatusState);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Export; get whether realtime should appear connected to the user.
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
export function useOptimisticConnectedToServerValue() {
|
|
138
|
+
return useAtomValue(optimisticConnectedToServerState);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Export; get whether the app should surface its reconnecting message.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
export function useShowRealtimeReconnectingMessageValue() {
|
|
146
|
+
return useAtomValue(realtimeReconnectingMessageState);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Export; advance realtimeConnectionGenerationState
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
export function useSetRealtimeConnectionGeneration() {
|
|
154
|
+
return useSetAtom(realtimeConnectionGenerationState);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Export; get realtimeConnectionGenerationState value
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
export function useRealtimeConnectionGenerationValue() {
|
|
162
|
+
return useAtomValue(realtimeConnectionGenerationState);
|
|
39
163
|
}
|
|
40
164
|
|
|
41
165
|
/**
|
|
@@ -43,7 +167,7 @@ export function useConnectedToServer() {
|
|
|
43
167
|
*/
|
|
44
168
|
|
|
45
169
|
export function useSetScreenIsFocused() {
|
|
46
|
-
|
|
170
|
+
return useSetAtom(screenIsFocusedState);
|
|
47
171
|
}
|
|
48
172
|
|
|
49
173
|
/**
|
|
@@ -51,7 +175,7 @@ export function useSetScreenIsFocused() {
|
|
|
51
175
|
*/
|
|
52
176
|
|
|
53
177
|
export function useScreenIsFocusedValue() {
|
|
54
|
-
|
|
178
|
+
return useAtomValue(screenIsFocusedState);
|
|
55
179
|
}
|
|
56
180
|
|
|
57
181
|
/**
|
|
@@ -59,5 +183,5 @@ export function useScreenIsFocusedValue() {
|
|
|
59
183
|
*/
|
|
60
184
|
|
|
61
185
|
export function useScreenIsFocused() {
|
|
62
|
-
|
|
186
|
+
return useAtom(screenIsFocusedState);
|
|
63
187
|
}
|
package/states/index.ts
CHANGED
|
@@ -2,7 +2,8 @@ export * from './apiData';
|
|
|
2
2
|
export * from './appContext';
|
|
3
3
|
export * from './connected';
|
|
4
4
|
export * from './keyDown';
|
|
5
|
+
export * from './keyboardLayer';
|
|
5
6
|
export * from './modal';
|
|
6
7
|
export * from './popOver';
|
|
7
|
-
export * from './
|
|
8
|
+
export * from './reactiveEvents';
|
|
8
9
|
export * from './uploads';
|
package/states/keyDown.ts
CHANGED
|
@@ -1,66 +1,167 @@
|
|
|
1
|
-
import { atom,
|
|
2
|
-
import { useCallback } from 'react';
|
|
1
|
+
import { atom, useAtomValue, useSetAtom, useStore } from 'jotai';
|
|
2
|
+
import { useCallback, useContext, useEffect, useMemo } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
activeKeyboardLayerState,
|
|
5
|
+
KeyboardLayerContext,
|
|
6
|
+
type KeyboardLayer,
|
|
7
|
+
keyboardLayerState,
|
|
8
|
+
keyboardEventOwnersState,
|
|
9
|
+
setKeyboardLayerOpenState,
|
|
10
|
+
} from './keyboardLayer';
|
|
3
11
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
12
|
+
export interface KeyDownObserverObj {
|
|
13
|
+
alert: boolean;
|
|
14
|
+
eventId: number;
|
|
15
|
+
metaKey: boolean;
|
|
16
|
+
modal: boolean;
|
|
17
|
+
pressed: null | string;
|
|
18
|
+
priorities: string[];
|
|
19
|
+
shiftKey: boolean;
|
|
20
|
+
}
|
|
7
21
|
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
pressed: null | string;
|
|
11
|
-
alert: boolean;
|
|
12
|
-
modal: boolean;
|
|
13
|
-
};
|
|
22
|
+
type SetKeyDown = (data: Partial<Omit<KeyDownObserverObj, 'eventId' | 'priorities'>>) => void;
|
|
23
|
+
type KeyboardScope = { scopeId: string; layer: KeyboardLayer };
|
|
14
24
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
const keyDownState = atom({ eventId: 0, generation: 0, metaKey: false, pressed: null as string | null, shiftKey: false });
|
|
26
|
+
const keyDownScopesState = atom<KeyboardScope[]>([]);
|
|
27
|
+
const keyDownPrioritiesState = atom((get) => {
|
|
28
|
+
const layer = get(activeKeyboardLayerState);
|
|
29
|
+
return get(keyDownScopesState)
|
|
30
|
+
.filter((scope) => scope.layer === layer)
|
|
31
|
+
.map((scope) => scope.scopeId);
|
|
32
|
+
});
|
|
18
33
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
34
|
+
/*
|
|
35
|
+
* Publish a key only for the current ownership generation; overlay changes invalidate older events.
|
|
36
|
+
*/
|
|
37
|
+
const publishKeyDownState = atom(null, (get, set, data: Parameters<SetKeyDown>[0]) => {
|
|
38
|
+
if (data.modal !== undefined) set(setKeyboardLayerOpenState, { layer: 'modal', open: data.modal });
|
|
39
|
+
if (data.alert !== undefined) set(setKeyboardLayerOpenState, { layer: 'popup', open: data.alert });
|
|
40
|
+
const previous = get(keyDownState);
|
|
41
|
+
set(keyDownState, {
|
|
42
|
+
eventId: typeof data.pressed === 'string' ? previous.eventId + 1 : previous.eventId,
|
|
43
|
+
generation: get(keyboardLayerState).generation,
|
|
44
|
+
metaKey: data.metaKey ?? previous.metaKey,
|
|
45
|
+
pressed: data.pressed === undefined ? null : data.pressed,
|
|
46
|
+
shiftKey: data.shiftKey ?? previous.shiftKey,
|
|
47
|
+
});
|
|
24
48
|
});
|
|
25
49
|
|
|
26
|
-
|
|
27
|
-
*
|
|
50
|
+
/*
|
|
51
|
+
* Prioritize a component within its own layer and discard any pending key.
|
|
28
52
|
*/
|
|
53
|
+
const activateKeyDownScopeState = atom(null, (get, set, scope: KeyboardScope) => {
|
|
54
|
+
set(keyDownScopesState, [
|
|
55
|
+
scope,
|
|
56
|
+
...get(keyDownScopesState).filter((item) => item.scopeId !== scope.scopeId || item.layer !== scope.layer),
|
|
57
|
+
]);
|
|
58
|
+
if (scope.layer === get(activeKeyboardLayerState)) set(keyDownState, { ...get(keyDownState), pressed: null });
|
|
59
|
+
});
|
|
29
60
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
61
|
+
/*
|
|
62
|
+
* Release one component's priority without affecting scopes in other layers.
|
|
63
|
+
*/
|
|
64
|
+
const deactivateKeyDownScopeState = atom(null, (get, set, scope: KeyboardScope) => {
|
|
65
|
+
const previous = get(keyDownScopesState);
|
|
66
|
+
set(
|
|
67
|
+
keyDownScopesState,
|
|
68
|
+
previous.filter((item) => item.scopeId !== scope.scopeId || item.layer !== scope.layer),
|
|
69
|
+
);
|
|
70
|
+
if (
|
|
71
|
+
scope.layer === get(activeKeyboardLayerState) &&
|
|
72
|
+
previous.find((item) => item.layer === scope.layer)?.scopeId === scope.scopeId
|
|
73
|
+
) {
|
|
74
|
+
set(keyDownState, { ...get(keyDownState), pressed: null });
|
|
75
|
+
}
|
|
76
|
+
});
|
|
35
77
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
78
|
+
/*
|
|
79
|
+
* Return the app-shell dispatcher, which routes published keys to the active layer.
|
|
80
|
+
*/
|
|
81
|
+
export function useSetKeyDown() {
|
|
82
|
+
const store = useStore();
|
|
83
|
+
return useCallback(
|
|
84
|
+
(data: Parameters<SetKeyDown>[0], event?: Event) => {
|
|
85
|
+
const owner = event && store.get(keyboardEventOwnersState).get(event);
|
|
86
|
+
if (owner && owner.generation !== store.get(keyboardLayerState).generation) return;
|
|
87
|
+
store.set(publishKeyDownState, data);
|
|
88
|
+
},
|
|
89
|
+
[store],
|
|
90
|
+
);
|
|
43
91
|
}
|
|
44
92
|
|
|
45
|
-
|
|
46
|
-
*
|
|
93
|
+
/*
|
|
94
|
+
* Observe events only within the active layer and its highest-priority component scope.
|
|
95
|
+
* Blocker flags describe overlays above this component, allowing modal-local shortcuts to keep working.
|
|
47
96
|
*/
|
|
97
|
+
export function useKeyDown(scopeId?: string) {
|
|
98
|
+
const layer = useContext(KeyboardLayerContext);
|
|
99
|
+
const activeLayer = useAtomValue(activeKeyboardLayerState);
|
|
100
|
+
const layers = useAtomValue(keyboardLayerState);
|
|
101
|
+
const value = useAtomValue(keyDownState);
|
|
102
|
+
const priorities = useAtomValue(keyDownPrioritiesState);
|
|
103
|
+
const store = useStore();
|
|
104
|
+
const observedValue = useMemo<KeyDownObserverObj>(() => {
|
|
105
|
+
const canObserve =
|
|
106
|
+
layer === activeLayer && value.generation === layers.generation && (priorities.length ? priorities[0] === scopeId : !scopeId);
|
|
107
|
+
return {
|
|
108
|
+
alert: layers.popup && layer !== 'popup',
|
|
109
|
+
modal: layers.modal && layer === 'page',
|
|
110
|
+
eventId: canObserve ? value.eventId : 0,
|
|
111
|
+
metaKey: canObserve && value.metaKey,
|
|
112
|
+
pressed: canObserve ? value.pressed : null,
|
|
113
|
+
priorities,
|
|
114
|
+
shiftKey: canObserve && value.shiftKey,
|
|
115
|
+
};
|
|
116
|
+
}, [activeLayer, layer, layers, priorities, scopeId, value]);
|
|
48
117
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
118
|
+
/*
|
|
119
|
+
* Prevent suspended subscribers from publishing or acknowledging another layer's event.
|
|
120
|
+
*/
|
|
121
|
+
const setKeyDown = useCallback<SetKeyDown>(
|
|
122
|
+
(data) => {
|
|
123
|
+
if (store.get(activeKeyboardLayerState) === layer) store.set(publishKeyDownState, data);
|
|
124
|
+
},
|
|
125
|
+
[layer, store],
|
|
126
|
+
);
|
|
127
|
+
return [observedValue, setKeyDown] as const;
|
|
54
128
|
}
|
|
55
129
|
|
|
56
|
-
|
|
57
|
-
*
|
|
130
|
+
/*
|
|
131
|
+
* Read only the active layer's priority stack, excluding suspended background menus.
|
|
58
132
|
*/
|
|
133
|
+
export function useKeyDownPriorities() {
|
|
134
|
+
return useAtomValue(keyDownPrioritiesState);
|
|
135
|
+
}
|
|
59
136
|
|
|
137
|
+
/*
|
|
138
|
+
* Observe blockers without subscribing to each key press.
|
|
139
|
+
*/
|
|
140
|
+
export function useKeyDownBlockers() {
|
|
141
|
+
const layer = useContext(KeyboardLayerContext);
|
|
142
|
+
const layers = useAtomValue(keyboardLayerState);
|
|
143
|
+
const priorities = useAtomValue(keyDownPrioritiesState);
|
|
144
|
+
const alert = layers.popup && layer !== 'popup';
|
|
145
|
+
const modal = layers.modal && layer === 'page';
|
|
146
|
+
return useMemo<Pick<KeyDownObserverObj, 'alert' | 'modal' | 'priorities'>>(
|
|
147
|
+
() => ({ alert, modal, priorities }),
|
|
148
|
+
[alert, modal, priorities],
|
|
149
|
+
);
|
|
150
|
+
}
|
|
60
151
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
152
|
+
/*
|
|
153
|
+
* Claim component priority within the inherited page, modal, or popup layer.
|
|
154
|
+
*/
|
|
155
|
+
export function useKeyDownScope(scopeId: string, active: boolean) {
|
|
156
|
+
const layer = useContext(KeyboardLayerContext);
|
|
157
|
+
const activeLayer = useAtomValue(activeKeyboardLayerState);
|
|
158
|
+
const priorities = useAtomValue(keyDownPrioritiesState);
|
|
159
|
+
const activateScope = useSetAtom(activateKeyDownScopeState);
|
|
160
|
+
const deactivateScope = useSetAtom(deactivateKeyDownScopeState);
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
if (!active) return;
|
|
163
|
+
activateScope({ scopeId, layer });
|
|
164
|
+
return () => deactivateScope({ scopeId, layer });
|
|
165
|
+
}, [active, layer, scopeId]);
|
|
166
|
+
return active && layer === activeLayer && priorities[0] === scopeId;
|
|
66
167
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { atom, useAtomValue, useStore } from 'jotai';
|
|
2
|
+
import { createContext, useCallback, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
export type KeyboardLayer = 'page' | 'modal' | 'popup';
|
|
5
|
+
|
|
6
|
+
// Modal renderers provide their layer; ordinary route components inherit page ownership.
|
|
7
|
+
export const KeyboardLayerContext = createContext<KeyboardLayer>('page');
|
|
8
|
+
export const keyboardLayerState = atom({ modal: false, popup: false, generation: 0 });
|
|
9
|
+
export const activeKeyboardLayerState = atom<KeyboardLayer>((get) => {
|
|
10
|
+
const state = get(keyboardLayerState);
|
|
11
|
+
return state.popup ? 'popup' : state.modal ? 'modal' : 'page';
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* Change overlay ownership synchronously and invalidate events from the previous layer.
|
|
16
|
+
*/
|
|
17
|
+
export const setKeyboardLayerOpenState = atom(
|
|
18
|
+
null,
|
|
19
|
+
(get, set, p: { layer: Exclude<KeyboardLayer, 'page'>; open: boolean; reset?: boolean }) => {
|
|
20
|
+
const previous = get(keyboardLayerState);
|
|
21
|
+
if (previous[p.layer] === p.open && !p.reset) return;
|
|
22
|
+
set(keyboardLayerState, { ...previous, [p.layer]: p.open, generation: previous.generation + 1 });
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
* Read the topmost keyboard layer for overlay renderers outside the modal subtree.
|
|
28
|
+
*/
|
|
29
|
+
export function useActiveKeyboardLayer() {
|
|
30
|
+
return useAtomValue(activeKeyboardLayerState);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
* Check native events against current ownership without relying on a React render.
|
|
35
|
+
* Pin each event to its original layer so closing an overlay cannot pass that same key to the page.
|
|
36
|
+
*/
|
|
37
|
+
export function useIsKeyboardLayerActive() {
|
|
38
|
+
const layer = useContext(KeyboardLayerContext);
|
|
39
|
+
const store = useStore();
|
|
40
|
+
return useCallback(
|
|
41
|
+
(event?: Event) => {
|
|
42
|
+
const activeLayer = store.get(activeKeyboardLayerState);
|
|
43
|
+
const generation = store.get(keyboardLayerState).generation;
|
|
44
|
+
if (event) {
|
|
45
|
+
const owners = store.get(keyboardEventOwnersState);
|
|
46
|
+
if (!owners.has(event)) owners.set(event, { layer: activeLayer, generation });
|
|
47
|
+
const owner = owners.get(event)!;
|
|
48
|
+
return owner.layer === layer && owner.generation === generation && activeLayer === layer;
|
|
49
|
+
}
|
|
50
|
+
return activeLayer === layer;
|
|
51
|
+
},
|
|
52
|
+
[layer, store],
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Keep native-event ownership isolated between Jotai providers.
|
|
57
|
+
export const keyboardEventOwnersState = atom(() => new WeakMap<Event, { layer: KeyboardLayer; generation: number }>());
|