|
| 1 | +import { useSyncExternalStore } from "react"; |
1 | 2 | import { findNodeHandle, type ReactNativeElement } from "react-native"; |
2 | 3 | import type { ReactNativeShadowNode } from "./types"; |
3 | 4 | import { getFabricUIManager } from "./fabric"; |
4 | 5 |
|
5 | | -let focusedScreenShadowNode: ReactNativeShadowNode | null = null; |
6 | | -let appRootShadowNode: ReactNativeShadowNode | null = null; |
| 6 | +export type GrabSelectionOwnerKind = "root" | "screen"; |
7 | 7 |
|
8 | | -export const setFocusedScreenRef = (ref: ReactNativeElement) => { |
9 | | - // @ts-expect-error - findNodeHandle is not typed correctly |
10 | | - const nativeTag = findNodeHandle(ref); |
| 8 | +export type GrabSelectionOwner = { |
| 9 | + id: string; |
| 10 | + kind: GrabSelectionOwnerKind; |
| 11 | + shadowNode: ReactNativeShadowNode; |
| 12 | + registrationOrder: number; |
| 13 | +}; |
11 | 14 |
|
12 | | - if (!nativeTag) { |
13 | | - throw new Error("Failed to find native tag for focused screen"); |
| 15 | +type SelectionOwnersStoreSnapshot = { |
| 16 | + owners: Map<string, GrabSelectionOwner>; |
| 17 | + focusedScreenOwnerId: string | null; |
| 18 | +}; |
| 19 | + |
| 20 | +let ownerIdCounter = 0; |
| 21 | +let registrationOrder = 0; |
| 22 | +let focusedScreenOwnerId: string | null = null; |
| 23 | +const owners = new Map<string, GrabSelectionOwner>(); |
| 24 | +const listeners = new Set<() => void>(); |
| 25 | + |
| 26 | +const notify = () => { |
| 27 | + for (const listener of listeners) { |
| 28 | + listener(); |
14 | 29 | } |
| 30 | +}; |
15 | 31 |
|
16 | | - focusedScreenShadowNode = getFabricUIManager().findShadowNodeByTag_DEPRECATED(nativeTag); |
| 32 | +const subscribe = (listener: () => void) => { |
| 33 | + listeners.add(listener); |
| 34 | + return () => { |
| 35 | + listeners.delete(listener); |
| 36 | + }; |
17 | 37 | }; |
18 | 38 |
|
19 | | -export const setAppRootRef = (ref: ReactNativeElement) => { |
| 39 | +const getSnapshot = (): SelectionOwnersStoreSnapshot => ({ |
| 40 | + owners: new Map(owners), |
| 41 | + focusedScreenOwnerId, |
| 42 | +}); |
| 43 | + |
| 44 | +const getOwnerShadowNode = (ref: ReactNativeElement, errorMessage: string) => { |
20 | 45 | // @ts-expect-error - findNodeHandle is not typed correctly |
21 | 46 | const nativeTag = findNodeHandle(ref); |
22 | 47 |
|
23 | 48 | if (!nativeTag) { |
24 | | - throw new Error("Failed to find native tag for app root"); |
| 49 | + throw new Error(errorMessage); |
| 50 | + } |
| 51 | + |
| 52 | + return getFabricUIManager().findShadowNodeByTag_DEPRECATED(nativeTag); |
| 53 | +}; |
| 54 | + |
| 55 | +const getFallbackRootOwner = () => { |
| 56 | + const rootOwners = Array.from(owners.values()).filter((owner) => owner.kind === "root"); |
| 57 | + rootOwners.sort((left, right) => right.registrationOrder - left.registrationOrder); |
| 58 | + return rootOwners[0] ?? null; |
| 59 | +}; |
| 60 | + |
| 61 | +export const createGrabSelectionOwnerId = (kind: GrabSelectionOwnerKind) => { |
| 62 | + ownerIdCounter += 1; |
| 63 | + return `react-native-grab-${kind}-${ownerIdCounter}`; |
| 64 | +}; |
| 65 | + |
| 66 | +export const registerGrabSelectionOwner = ( |
| 67 | + id: string, |
| 68 | + kind: GrabSelectionOwnerKind, |
| 69 | + ref: ReactNativeElement, |
| 70 | +) => { |
| 71 | + const shadowNode = getOwnerShadowNode( |
| 72 | + ref, |
| 73 | + kind === "root" |
| 74 | + ? "Failed to find native tag for app root" |
| 75 | + : "Failed to find native tag for screen", |
| 76 | + ); |
| 77 | + |
| 78 | + registrationOrder += 1; |
| 79 | + owners.set(id, { |
| 80 | + id, |
| 81 | + kind, |
| 82 | + shadowNode, |
| 83 | + registrationOrder, |
| 84 | + }); |
| 85 | + notify(); |
| 86 | +}; |
| 87 | + |
| 88 | +export const unregisterGrabSelectionOwner = (id: string) => { |
| 89 | + const removedOwner = owners.get(id); |
| 90 | + if (!removedOwner) { |
| 91 | + return; |
25 | 92 | } |
26 | 93 |
|
27 | | - appRootShadowNode = getFabricUIManager().findShadowNodeByTag_DEPRECATED(nativeTag); |
| 94 | + owners.delete(id); |
| 95 | + |
| 96 | + if (focusedScreenOwnerId === id) { |
| 97 | + focusedScreenOwnerId = null; |
| 98 | + } |
| 99 | + |
| 100 | + notify(); |
| 101 | +}; |
| 102 | + |
| 103 | +export const setGrabSelectionOwnerFocused = (id: string, isFocused: boolean) => { |
| 104 | + const owner = owners.get(id); |
| 105 | + if (!owner || owner.kind !== "screen") { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (isFocused) { |
| 110 | + focusedScreenOwnerId = id; |
| 111 | + } else if (focusedScreenOwnerId === id) { |
| 112 | + focusedScreenOwnerId = null; |
| 113 | + } |
| 114 | + |
| 115 | + notify(); |
28 | 116 | }; |
29 | 117 |
|
30 | | -export const getAppRootShadowNode = (): ReactNativeShadowNode => { |
31 | | - if (!appRootShadowNode) { |
32 | | - throw new Error("You seem to forgot to wrap your app root with ReactNativeGrabRoot."); |
| 118 | +export const clearGrabSelectionOwnerFocus = (id: string) => { |
| 119 | + if (focusedScreenOwnerId !== id) { |
| 120 | + return; |
33 | 121 | } |
34 | 122 |
|
35 | | - return appRootShadowNode; |
| 123 | + focusedScreenOwnerId = null; |
| 124 | + notify(); |
| 125 | +}; |
| 126 | + |
| 127 | +export const getGrabSelectionOwner = (id: string): GrabSelectionOwner | null => { |
| 128 | + return owners.get(id) ?? null; |
36 | 129 | }; |
37 | | -export const getFocusedScreenShadowNode = () => { |
38 | | - if (!focusedScreenShadowNode) { |
39 | | - // No native screens, so there will be only the app root. |
40 | | - return getAppRootShadowNode(); |
| 130 | + |
| 131 | +export const getResolvedGrabSelectionOwner = (): GrabSelectionOwner | null => { |
| 132 | + if (focusedScreenOwnerId) { |
| 133 | + const focusedOwner = owners.get(focusedScreenOwnerId); |
| 134 | + if (focusedOwner) { |
| 135 | + return focusedOwner; |
| 136 | + } |
41 | 137 | } |
42 | 138 |
|
43 | | - return focusedScreenShadowNode; |
| 139 | + return getFallbackRootOwner(); |
| 140 | +}; |
| 141 | + |
| 142 | +export const getResolvedGrabSelectionOwnerId = (): string | null => { |
| 143 | + return getResolvedGrabSelectionOwner()?.id ?? null; |
| 144 | +}; |
| 145 | + |
| 146 | +export const useResolvedGrabSelectionOwnerId = () => { |
| 147 | + return useSyncExternalStore( |
| 148 | + subscribe, |
| 149 | + () => getResolvedGrabSelectionOwnerId(), |
| 150 | + () => null, |
| 151 | + ); |
| 152 | +}; |
| 153 | + |
| 154 | +export const useIsResolvedGrabSelectionOwner = (id: string) => { |
| 155 | + return useSyncExternalStore( |
| 156 | + subscribe, |
| 157 | + () => getResolvedGrabSelectionOwnerId() === id, |
| 158 | + () => false, |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +export const useSelectionOwnersStore = () => { |
| 163 | + return useSyncExternalStore(subscribe, getSnapshot, getSnapshot); |
44 | 164 | }; |
0 commit comments