@jsb188/react 1.1.11 → 1.1.13
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/LICENSE +21 -0
- package/libs/page-filters.helpers.ts +65 -0
- package/libs/page-filters.ts +223 -0
- package/libs/use-page-filters.ts +168 -0
- package/package.json +21 -6
- package/states/appContext.ts +21 -59
- package/states/index.ts +1 -1
- package/states/keyDown.ts +158 -50
- package/states/keyboardLayer.ts +58 -0
- package/states/modal.ts +169 -88
- package/states/popOver.ts +246 -154
- package/states/uploads.ts +0 -2
- package/types/PopOver.d.ts +188 -63
- package/utils/hooks.ts +109 -29
- package/utils/lazy.ts +183 -0
- package/states/reactiveEvents.ts +0 -56
- package/utils/schema.ts +0 -170
package/states/appContext.ts
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
fetchLightModeSetting,
|
|
4
|
-
isSystemDarkMode,
|
|
5
|
-
saveAuthTokenToPlatform,
|
|
6
|
-
} from '@jsb188/app/platform.ts';
|
|
7
|
-
import type {
|
|
8
|
-
AccountData,
|
|
9
|
-
AccountSettings,
|
|
10
|
-
AuthenticationData,
|
|
11
|
-
LightModeEnum,
|
|
12
|
-
} from '@jsb188/app/types/auth.d.ts';
|
|
1
|
+
import { clearAppCache, fetchLightModeSetting, isSystemDarkMode, saveAuthTokenToPlatform } from '@jsb188/app/platform.ts';
|
|
2
|
+
import type { AccountData, AccountSettings, AuthenticationData, LightModeEnum } from '@jsb188/app/types/auth.d.ts';
|
|
13
3
|
import { setAuthToken } from '@jsb188/app/utils/api.ts';
|
|
14
4
|
import { getDisplayName } from '@jsb188/app/utils/string.ts';
|
|
15
5
|
import { createContext, useContext } from 'react';
|
|
@@ -54,6 +44,7 @@ export type AppContextData = {
|
|
|
54
44
|
// Required for app
|
|
55
45
|
init: 0 | 1 | 2; // 0 = not initialized,; 1 = loading; 2 = initialized
|
|
56
46
|
authorization: string; // This is token
|
|
47
|
+
storageToken: string | null;
|
|
57
48
|
networkError: boolean;
|
|
58
49
|
|
|
59
50
|
// Authentication data
|
|
@@ -66,7 +57,6 @@ export type AppContextData = {
|
|
|
66
57
|
activated: boolean;
|
|
67
58
|
hasPassword: boolean;
|
|
68
59
|
webVersion: string | null;
|
|
69
|
-
alertUpdatesOnMount: boolean;
|
|
70
60
|
|
|
71
61
|
// Preview theme overrides active theme
|
|
72
62
|
systemLightMode: 'LIGHT' | 'DARK';
|
|
@@ -81,6 +71,7 @@ export type AppContextData = {
|
|
|
81
71
|
export const DEFAULT_APP_CONTEXT = {
|
|
82
72
|
init: 0,
|
|
83
73
|
authorization: '',
|
|
74
|
+
storageToken: null,
|
|
84
75
|
networkError: false,
|
|
85
76
|
|
|
86
77
|
account: null,
|
|
@@ -95,7 +86,6 @@ export const DEFAULT_APP_CONTEXT = {
|
|
|
95
86
|
activated: false,
|
|
96
87
|
hasPassword: false,
|
|
97
88
|
webVersion: null,
|
|
98
|
-
alertUpdatesOnMount: false,
|
|
99
89
|
|
|
100
90
|
systemLightMode: 'LIGHT',
|
|
101
91
|
previewLightMode: null,
|
|
@@ -107,20 +97,14 @@ export const DEFAULT_APP_CONTEXT = {
|
|
|
107
97
|
*/
|
|
108
98
|
|
|
109
99
|
function ensureLightModeSetting(lightMode?: LightModeEnum): LightModeEnum {
|
|
110
|
-
return lightMode && ['LIGHT', 'DARK', 'SYSTEM'].includes(lightMode || '')
|
|
111
|
-
? lightMode
|
|
112
|
-
: 'LIGHT';
|
|
100
|
+
return lightMode && ['LIGHT', 'DARK', 'SYSTEM'].includes(lightMode || '') ? lightMode : 'LIGHT';
|
|
113
101
|
}
|
|
114
102
|
|
|
115
103
|
/**
|
|
116
104
|
* App log in with Promise
|
|
117
105
|
*/
|
|
118
106
|
|
|
119
|
-
export function onAccountLogin(
|
|
120
|
-
auth: any,
|
|
121
|
-
dispatchApp: AppContextDispatch,
|
|
122
|
-
otherCB?: (auth: any) => void,
|
|
123
|
-
) {
|
|
107
|
+
export function onAccountLogin(auth: any, dispatchApp: AppContextDispatch, otherCB?: (auth: any) => void) {
|
|
124
108
|
if (auth && auth?.token && auth.activated && auth.account) {
|
|
125
109
|
console.log('.. 🥝 logged into account ..');
|
|
126
110
|
|
|
@@ -161,19 +145,13 @@ export function onGuestInit(error: any, dispatchApp: AppContextDispatch) {
|
|
|
161
145
|
* Get active/current or preview theme
|
|
162
146
|
*/
|
|
163
147
|
|
|
164
|
-
export function getTheme(
|
|
165
|
-
appState: AppContextData,
|
|
166
|
-
priority: 'PREVIEW' | 'ACCOUNT' | 'ACTIVE',
|
|
167
|
-
) {
|
|
148
|
+
export function getTheme(appState: AppContextData, priority: 'PREVIEW' | 'ACCOUNT' | 'ACTIVE') {
|
|
168
149
|
let lightMode = appState.previewLightMode || appState.settings.lightMode;
|
|
169
150
|
if (lightMode === 'SYSTEM') {
|
|
170
151
|
lightMode = appState.systemLightMode;
|
|
171
152
|
}
|
|
172
153
|
|
|
173
|
-
const theme =
|
|
174
|
-
(priority === 'PREVIEW' && appState.previewTheme) ||
|
|
175
|
-
appState.settings.theme ||
|
|
176
|
-
'';
|
|
154
|
+
const theme = (priority === 'PREVIEW' && appState.previewTheme) || appState.settings.theme || '';
|
|
177
155
|
|
|
178
156
|
return {
|
|
179
157
|
lightMode: lightMode || 'LIGHT',
|
|
@@ -203,17 +181,14 @@ export function getPreviewAppearance(appState: AppContextData) {
|
|
|
203
181
|
* Merge sign in mutation auth values to context
|
|
204
182
|
*/
|
|
205
183
|
|
|
206
|
-
export function mergeAuthToDefaultValues(
|
|
207
|
-
auth?: AuthenticationData | null,
|
|
208
|
-
authToken?: string | null,
|
|
209
|
-
) {
|
|
184
|
+
export function mergeAuthToDefaultValues(auth?: AuthenticationData | null, authToken?: string | null) {
|
|
210
185
|
if (auth?.token && auth?.account?.id) {
|
|
211
186
|
let systemLightMode = 'LIGHT';
|
|
212
187
|
if (isSystemDarkMode()) {
|
|
213
188
|
systemLightMode = 'DARK';
|
|
214
189
|
}
|
|
215
190
|
|
|
216
|
-
setAuthToken(auth.token);
|
|
191
|
+
setAuthToken(auth.token, auth.storageToken);
|
|
217
192
|
|
|
218
193
|
const authSettings = {
|
|
219
194
|
...DEFAULT_APP_CONTEXT.settings,
|
|
@@ -226,12 +201,12 @@ export function mergeAuthToDefaultValues(
|
|
|
226
201
|
...DEFAULT_APP_CONTEXT,
|
|
227
202
|
init: 1, // Always 1 because we're always using SSR for authentication
|
|
228
203
|
authorization: auth.token,
|
|
204
|
+
storageToken: auth.storageToken || null,
|
|
229
205
|
account: auth.account,
|
|
230
206
|
primaryOrganizationId: auth.primaryOrganizationId || null,
|
|
231
207
|
cacheAuthorizationEpoch: auth.cacheAuthorizationEpoch || null,
|
|
232
208
|
settings: authSettings,
|
|
233
209
|
|
|
234
|
-
alertUpdatesOnMount: auth.alertUpdatesOnMount,
|
|
235
210
|
hasPassword: auth.hasPassword,
|
|
236
211
|
webVersion: auth.webVersion,
|
|
237
212
|
|
|
@@ -267,7 +242,9 @@ export const appReducer = (state: AppContextData, action: AppContextAction) => {
|
|
|
267
242
|
};
|
|
268
243
|
}
|
|
269
244
|
case 'APP_LOGIN': {
|
|
270
|
-
|
|
245
|
+
const nextState = mergeAuthToDefaultValues(data);
|
|
246
|
+
// Inline sign-in keeps the mounted app's completed initialization.
|
|
247
|
+
return { ...nextState, init: state.init === 2 ? 2 : nextState.init };
|
|
271
248
|
}
|
|
272
249
|
case 'APP_REFRESH_AUTH': {
|
|
273
250
|
const authorization = data?.authorization || '';
|
|
@@ -275,10 +252,11 @@ export const appReducer = (state: AppContextData, action: AppContextAction) => {
|
|
|
275
252
|
return state;
|
|
276
253
|
}
|
|
277
254
|
|
|
278
|
-
setAuthToken(authorization);
|
|
255
|
+
setAuthToken(authorization, data.storageToken);
|
|
279
256
|
return {
|
|
280
257
|
...state,
|
|
281
258
|
authorization,
|
|
259
|
+
storageToken: data.storageToken || null,
|
|
282
260
|
};
|
|
283
261
|
}
|
|
284
262
|
case 'APP_GUEST': {
|
|
@@ -307,10 +285,7 @@ export const appReducer = (state: AppContextData, action: AppContextAction) => {
|
|
|
307
285
|
if (data.id === state.account?.id) {
|
|
308
286
|
return {
|
|
309
287
|
...state,
|
|
310
|
-
hasPassword:
|
|
311
|
-
typeof data.hasPassword === 'boolean'
|
|
312
|
-
? data.hasPassword
|
|
313
|
-
: state.hasPassword,
|
|
288
|
+
hasPassword: typeof data.hasPassword === 'boolean' ? data.hasPassword : state.hasPassword,
|
|
314
289
|
account: !data.email
|
|
315
290
|
? state.account
|
|
316
291
|
: {
|
|
@@ -370,9 +345,7 @@ export const appReducer = (state: AppContextData, action: AppContextAction) => {
|
|
|
370
345
|
}
|
|
371
346
|
case 'APP_LIGHT_MODE': {
|
|
372
347
|
if (!data.lightMode && !data.preview) {
|
|
373
|
-
console.warn(
|
|
374
|
-
'Developer error in setLightMode(): You cannot set active lightMode to null.',
|
|
375
|
-
);
|
|
348
|
+
console.warn('Developer error in setLightMode(): You cannot set active lightMode to null.');
|
|
376
349
|
return state;
|
|
377
350
|
} else if (data.preview) {
|
|
378
351
|
return {
|
|
@@ -411,34 +384,23 @@ export const AppContext = createContext({
|
|
|
411
384
|
|
|
412
385
|
export function useCurrentAccount() {
|
|
413
386
|
const {
|
|
414
|
-
appState: {
|
|
415
|
-
activated,
|
|
416
|
-
hasPassword,
|
|
417
|
-
webVersion,
|
|
418
|
-
alertUpdatesOnMount,
|
|
419
|
-
account,
|
|
420
|
-
settings,
|
|
421
|
-
primaryOrganizationId,
|
|
422
|
-
},
|
|
387
|
+
appState: { init, activated, hasPassword, webVersion, account, settings, primaryOrganizationId },
|
|
423
388
|
dispatchApp,
|
|
424
389
|
} = useContext(AppContext);
|
|
425
390
|
const hasName = !!account?.profile?.firstName || !!account?.profile?.lastName;
|
|
426
|
-
const { fullName, displayName } = getDisplayName(
|
|
427
|
-
account?.profile?.firstName,
|
|
428
|
-
account?.profile?.lastName,
|
|
429
|
-
);
|
|
391
|
+
const { fullName, displayName } = getDisplayName(account?.profile?.firstName, account?.profile?.lastName);
|
|
430
392
|
|
|
431
393
|
return {
|
|
432
394
|
// Dispatch
|
|
433
395
|
dispatchApp,
|
|
434
396
|
|
|
435
397
|
// App state
|
|
398
|
+
init,
|
|
436
399
|
account,
|
|
437
400
|
settings,
|
|
438
401
|
activated,
|
|
439
402
|
hasPassword,
|
|
440
403
|
webVersion,
|
|
441
|
-
alertUpdatesOnMount,
|
|
442
404
|
|
|
443
405
|
// Computed
|
|
444
406
|
isLoggedIn: !!account?.id,
|
package/states/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ 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 './reactiveEvents';
|
|
8
8
|
export * from './uploads';
|
package/states/keyDown.ts
CHANGED
|
@@ -1,71 +1,179 @@
|
|
|
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
|
-
* Types
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
type KeyDownObserverObj = {
|
|
9
|
-
metaKey: boolean;
|
|
10
|
-
pressed: null | string;
|
|
12
|
+
export interface KeyDownObserverObj {
|
|
11
13
|
alert: boolean;
|
|
14
|
+
eventId: number;
|
|
15
|
+
metaKey: boolean;
|
|
12
16
|
modal: boolean;
|
|
13
|
-
|
|
17
|
+
pressed: null | string;
|
|
18
|
+
priorities: string[];
|
|
19
|
+
shiftKey: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type SetKeyDown = (data: Partial<Omit<KeyDownObserverObj, 'eventId' | 'priorities'>>) => void;
|
|
23
|
+
type KeyboardScope = { scopeId: string; layer: KeyboardLayer };
|
|
14
24
|
|
|
15
|
-
|
|
16
|
-
*
|
|
25
|
+
/*
|
|
26
|
+
* Report a blocking screen layer to legacy native keyboard handlers.
|
|
17
27
|
*/
|
|
28
|
+
function hasModalBlocker(layer: KeyboardLayer, layers: { modal: boolean; pageOverlay: boolean }) {
|
|
29
|
+
return layer === 'page' ? layers.pageOverlay || layers.modal : layer === 'pageOverlay' && layers.modal;
|
|
30
|
+
}
|
|
18
31
|
|
|
19
|
-
const keyDownState = atom
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
const keyDownState = atom({ eventId: 0, generation: 0, metaKey: false, pressed: null as string | null, shiftKey: false });
|
|
33
|
+
// A persistent navigation surface can observe ownership without rerendering for every app keystroke.
|
|
34
|
+
const idleKeyDownState = atom(null);
|
|
35
|
+
const keyDownScopesState = atom<KeyboardScope[]>([]);
|
|
36
|
+
const keyDownPrioritiesState = atom((get) => {
|
|
37
|
+
const layer = get(activeKeyboardLayerState);
|
|
38
|
+
return get(keyDownScopesState)
|
|
39
|
+
.filter((scope) => scope.layer === layer)
|
|
40
|
+
.map((scope) => scope.scopeId);
|
|
24
41
|
});
|
|
25
42
|
|
|
26
|
-
|
|
27
|
-
*
|
|
43
|
+
/*
|
|
44
|
+
* Publish a key only for the current ownership generation; overlay changes invalidate older events.
|
|
28
45
|
*/
|
|
46
|
+
const publishKeyDownState = atom(null, (get, set, data: Parameters<SetKeyDown>[0]) => {
|
|
47
|
+
if (data.modal !== undefined) set(setKeyboardLayerOpenState, { layer: 'modal', open: data.modal });
|
|
48
|
+
if (data.alert !== undefined) set(setKeyboardLayerOpenState, { layer: 'popup', open: data.alert });
|
|
49
|
+
const previous = get(keyDownState);
|
|
50
|
+
set(keyDownState, {
|
|
51
|
+
eventId: typeof data.pressed === 'string' ? previous.eventId + 1 : previous.eventId,
|
|
52
|
+
generation: get(keyboardLayerState).generation,
|
|
53
|
+
metaKey: data.metaKey ?? previous.metaKey,
|
|
54
|
+
pressed: data.pressed === undefined ? null : data.pressed,
|
|
55
|
+
shiftKey: data.shiftKey ?? previous.shiftKey,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
29
58
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
setter((prev: KeyDownObserverObj) => {
|
|
43
|
-
return {
|
|
44
|
-
...prev,
|
|
45
|
-
...data,
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
}
|
|
59
|
+
/*
|
|
60
|
+
* Prioritize a component within its own layer and discard any pending key.
|
|
61
|
+
*/
|
|
62
|
+
const activateKeyDownScopeState = atom(null, (get, set, scope: KeyboardScope) => {
|
|
63
|
+
set(keyDownScopesState, [
|
|
64
|
+
scope,
|
|
65
|
+
...get(keyDownScopesState).filter((item) => item.scopeId !== scope.scopeId || item.layer !== scope.layer),
|
|
66
|
+
]);
|
|
67
|
+
if (scope.layer === get(activeKeyboardLayerState)) set(keyDownState, { ...get(keyDownState), pressed: null });
|
|
68
|
+
});
|
|
50
69
|
|
|
51
|
-
|
|
52
|
-
*
|
|
70
|
+
/*
|
|
71
|
+
* Release one component's priority without affecting scopes in other layers.
|
|
53
72
|
*/
|
|
73
|
+
const deactivateKeyDownScopeState = atom(null, (get, set, scope: KeyboardScope) => {
|
|
74
|
+
const previous = get(keyDownScopesState);
|
|
75
|
+
set(
|
|
76
|
+
keyDownScopesState,
|
|
77
|
+
previous.filter((item) => item.scopeId !== scope.scopeId || item.layer !== scope.layer),
|
|
78
|
+
);
|
|
79
|
+
if (
|
|
80
|
+
scope.layer === get(activeKeyboardLayerState) &&
|
|
81
|
+
previous.find((item) => item.layer === scope.layer)?.scopeId === scope.scopeId
|
|
82
|
+
) {
|
|
83
|
+
set(keyDownState, { ...get(keyDownState), pressed: null });
|
|
84
|
+
}
|
|
85
|
+
});
|
|
54
86
|
|
|
87
|
+
/*
|
|
88
|
+
* Return the app-shell dispatcher, which routes published keys to the active layer.
|
|
89
|
+
*/
|
|
55
90
|
export function useSetKeyDown() {
|
|
56
|
-
const
|
|
57
|
-
|
|
91
|
+
const store = useStore();
|
|
92
|
+
return useCallback(
|
|
93
|
+
(data: Parameters<SetKeyDown>[0], event?: Event) => {
|
|
94
|
+
const owner = event && store.get(keyboardEventOwnersState).get(event);
|
|
95
|
+
if (owner && owner.generation !== store.get(keyboardLayerState).generation) return;
|
|
96
|
+
store.set(publishKeyDownState, data);
|
|
97
|
+
},
|
|
98
|
+
[store],
|
|
99
|
+
);
|
|
100
|
+
}
|
|
58
101
|
|
|
59
|
-
|
|
102
|
+
/*
|
|
103
|
+
* Observe events only within the active layer and its highest-priority component scope.
|
|
104
|
+
* Blocker flags describe overlays above this component, allowing modal-local shortcuts to keep working.
|
|
105
|
+
*/
|
|
106
|
+
export function useKeyDown(scopeId?: string, listen = true) {
|
|
107
|
+
const layer = useContext(KeyboardLayerContext);
|
|
108
|
+
const activeLayer = useAtomValue(activeKeyboardLayerState);
|
|
109
|
+
const layers = useAtomValue(keyboardLayerState);
|
|
110
|
+
const value = useAtomValue(listen ? keyDownState : idleKeyDownState);
|
|
111
|
+
const priorities = useAtomValue(keyDownPrioritiesState);
|
|
112
|
+
const store = useStore();
|
|
113
|
+
const observedValue = useMemo<KeyDownObserverObj>(() => {
|
|
114
|
+
const canObserve =
|
|
115
|
+
!!value &&
|
|
116
|
+
layer === activeLayer &&
|
|
117
|
+
value.generation === layers.generation &&
|
|
118
|
+
(priorities.length ? priorities[0] === scopeId : !scopeId);
|
|
119
|
+
return {
|
|
120
|
+
alert: layers.popup && layer !== 'popup',
|
|
121
|
+
modal: hasModalBlocker(layer, layers),
|
|
122
|
+
eventId: canObserve ? value.eventId : 0,
|
|
123
|
+
metaKey: canObserve && value.metaKey,
|
|
124
|
+
pressed: canObserve ? value.pressed : null,
|
|
125
|
+
priorities,
|
|
126
|
+
shiftKey: canObserve && value.shiftKey,
|
|
127
|
+
};
|
|
128
|
+
}, [activeLayer, layer, layers, priorities, scopeId, value]);
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* Prevent suspended subscribers from publishing or acknowledging another layer's event.
|
|
132
|
+
*/
|
|
133
|
+
const setKeyDown = useCallback<SetKeyDown>(
|
|
134
|
+
(data) => {
|
|
135
|
+
if (store.get(activeKeyboardLayerState) === layer) store.set(publishKeyDownState, data);
|
|
136
|
+
},
|
|
137
|
+
[layer, store],
|
|
138
|
+
);
|
|
139
|
+
return [observedValue, setKeyDown] as const;
|
|
60
140
|
}
|
|
61
141
|
|
|
62
|
-
|
|
63
|
-
*
|
|
142
|
+
/*
|
|
143
|
+
* Read only the active layer's priority stack, excluding suspended background menus.
|
|
64
144
|
*/
|
|
145
|
+
export function useKeyDownPriorities() {
|
|
146
|
+
return useAtomValue(keyDownPrioritiesState);
|
|
147
|
+
}
|
|
65
148
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
149
|
+
/*
|
|
150
|
+
* Observe blockers without subscribing to each key press.
|
|
151
|
+
*/
|
|
152
|
+
export function useKeyDownBlockers() {
|
|
153
|
+
const layer = useContext(KeyboardLayerContext);
|
|
154
|
+
const layers = useAtomValue(keyboardLayerState);
|
|
155
|
+
const priorities = useAtomValue(keyDownPrioritiesState);
|
|
156
|
+
const alert = layers.popup && layer !== 'popup';
|
|
157
|
+
const modal = hasModalBlocker(layer, layers);
|
|
158
|
+
return useMemo<Pick<KeyDownObserverObj, 'alert' | 'modal' | 'priorities'>>(
|
|
159
|
+
() => ({ alert, modal, priorities }),
|
|
160
|
+
[alert, modal, priorities],
|
|
161
|
+
);
|
|
162
|
+
}
|
|
69
163
|
|
|
70
|
-
|
|
164
|
+
/*
|
|
165
|
+
* Claim component priority within the inherited page, modal, or popup layer.
|
|
166
|
+
*/
|
|
167
|
+
export function useKeyDownScope(scopeId: string, active: boolean) {
|
|
168
|
+
const layer = useContext(KeyboardLayerContext);
|
|
169
|
+
const activeLayer = useAtomValue(activeKeyboardLayerState);
|
|
170
|
+
const priorities = useAtomValue(keyDownPrioritiesState);
|
|
171
|
+
const activateScope = useSetAtom(activateKeyDownScopeState);
|
|
172
|
+
const deactivateScope = useSetAtom(deactivateKeyDownScopeState);
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
if (!active) return;
|
|
175
|
+
activateScope({ scopeId, layer });
|
|
176
|
+
return () => deactivateScope({ scopeId, layer });
|
|
177
|
+
}, [active, layer, scopeId]);
|
|
178
|
+
return active && layer === activeLayer && priorities[0] === scopeId;
|
|
71
179
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { atom, useAtomValue, useStore } from 'jotai';
|
|
2
|
+
import { createContext, useCallback, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
export type KeyboardLayer = 'page' | 'pageOverlay' | '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({ pageOverlay: false, modal: false, popup: false, generation: 0 });
|
|
9
|
+
export const pageOverlayKeyboardOpenState = atom((get) => get(keyboardLayerState).pageOverlay);
|
|
10
|
+
export const activeKeyboardLayerState = atom<KeyboardLayer>((get) => {
|
|
11
|
+
const state = get(keyboardLayerState);
|
|
12
|
+
return state.popup ? 'popup' : state.modal ? 'modal' : state.pageOverlay ? 'pageOverlay' : 'page';
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/*
|
|
16
|
+
* Change overlay ownership synchronously and invalidate events from the previous layer.
|
|
17
|
+
*/
|
|
18
|
+
export const setKeyboardLayerOpenState = atom(
|
|
19
|
+
null,
|
|
20
|
+
(get, set, p: { layer: Exclude<KeyboardLayer, 'page'>; open: boolean; reset?: boolean }) => {
|
|
21
|
+
const previous = get(keyboardLayerState);
|
|
22
|
+
if (previous[p.layer] === p.open && !p.reset) return;
|
|
23
|
+
set(keyboardLayerState, { ...previous, [p.layer]: p.open, generation: previous.generation + 1 });
|
|
24
|
+
},
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Read the topmost keyboard layer for overlay renderers outside the modal subtree.
|
|
29
|
+
*/
|
|
30
|
+
export function useActiveKeyboardLayer() {
|
|
31
|
+
return useAtomValue(activeKeyboardLayerState);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* Check native events against current ownership without relying on a React render.
|
|
36
|
+
* Pin each event to its original layer so closing an overlay cannot pass that same key to the page.
|
|
37
|
+
*/
|
|
38
|
+
export function useIsKeyboardLayerActive() {
|
|
39
|
+
const layer = useContext(KeyboardLayerContext);
|
|
40
|
+
const store = useStore();
|
|
41
|
+
return useCallback(
|
|
42
|
+
(event?: Event) => {
|
|
43
|
+
const activeLayer = store.get(activeKeyboardLayerState);
|
|
44
|
+
const generation = store.get(keyboardLayerState).generation;
|
|
45
|
+
if (event) {
|
|
46
|
+
const owners = store.get(keyboardEventOwnersState);
|
|
47
|
+
if (!owners.has(event)) owners.set(event, { layer: activeLayer, generation });
|
|
48
|
+
const owner = owners.get(event)!;
|
|
49
|
+
return owner.layer === layer && owner.generation === generation && activeLayer === layer;
|
|
50
|
+
}
|
|
51
|
+
return activeLayer === layer;
|
|
52
|
+
},
|
|
53
|
+
[layer, store],
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Keep native-event ownership isolated between Jotai providers.
|
|
58
|
+
export const keyboardEventOwnersState = atom(() => new WeakMap<Event, { layer: KeyboardLayer; generation: number }>());
|