Skip to content

Commit 8ee87be

Browse files
committed
feat: don't use private reanimated API
1 parent 1441ae3 commit 8ee87be

7 files changed

Lines changed: 56 additions & 80 deletions

File tree

src/animated.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Reanimated, { useSharedValue } from "react-native-reanimated";
99

1010
import { KeyboardControllerView } from "./bindings";
1111
import { KeyboardContext } from "./context";
12-
import { focusedInputEventsMap, keyboardEventsMap } from "./event-mappings";
1312
import { useAnimatedValue, useEventHandlerRegistration } from "./internal";
1413
import { KeyboardController } from "./module";
1514
import {
@@ -130,14 +129,10 @@ export const KeyboardProvider = (props: KeyboardProviderProps) => {
130129
const progressSV = useSharedValue(0);
131130
const heightSV = useSharedValue(0);
132131
const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
133-
const setKeyboardHandlers = useEventHandlerRegistration<KeyboardHandler>(
134-
keyboardEventsMap,
135-
viewTagRef,
136-
);
137-
const setInputHandlers = useEventHandlerRegistration<FocusedInputHandler>(
138-
focusedInputEventsMap,
139-
viewTagRef,
140-
);
132+
const setKeyboardHandlers =
133+
useEventHandlerRegistration<KeyboardHandler>(viewTagRef);
134+
const setInputHandlers =
135+
useEventHandlerRegistration<FocusedInputHandler>(viewTagRef);
141136
// memo
142137
const context = useMemo<KeyboardAnimationContext>(
143138
() => ({

src/event-handler.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/event-handler.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/event-handler.web.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/event-mappings.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/hooks/index.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useLayoutEffect } from "react";
2+
import { useEvent } from "react-native-reanimated";
23

34
import { AndroidSoftInputModes } from "../constants";
45
import { useKeyboardContext } from "../context";
@@ -111,8 +112,36 @@ export function useGenericKeyboardHandler(
111112
) {
112113
const context = useKeyboardContext();
113114

115+
const eventHandler = useEvent(
116+
(event) => {
117+
"worklet";
118+
119+
if (event.eventName.endsWith("onKeyboardMoveStart")) {
120+
handler.onStart?.(event);
121+
}
122+
123+
if (event.eventName.endsWith("onKeyboardMove")) {
124+
handler.onMove?.(event);
125+
}
126+
127+
if (event.eventName.endsWith("onKeyboardMoveEnd")) {
128+
handler.onEnd?.(event);
129+
}
130+
131+
if (event.eventName.endsWith("onKeyboardMoveInteractive")) {
132+
handler.onInteractive?.(event);
133+
}
134+
},
135+
[
136+
"onKeyboardMoveStart",
137+
"onKeyboardMove",
138+
"onKeyboardMoveEnd",
139+
"onKeyboardMoveInteractive",
140+
],
141+
);
142+
114143
useLayoutEffect(() => {
115-
const cleanup = context.setKeyboardHandlers(handler);
144+
const cleanup = context.setKeyboardHandlers(eventHandler);
116145

117146
return () => cleanup();
118147
}, deps);
@@ -228,8 +257,23 @@ export function useFocusedInputHandler(
228257
) {
229258
const context = useKeyboardContext();
230259

260+
const eventHandler = useEvent(
261+
(event) => {
262+
"worklet";
263+
264+
if (event.eventName.endsWith("onFocusedInputTextChanged")) {
265+
handler.onChangeText?.(event);
266+
}
267+
268+
if (event.eventName.endsWith("onFocusedInputSelectionChanged")) {
269+
handler.onSelectionChange?.(event);
270+
}
271+
},
272+
["onFocusedInputTextChanged", "onFocusedInputSelectionChanged"],
273+
);
274+
231275
useLayoutEffect(() => {
232-
const cleanup = context.setInputHandlers(handler);
276+
const cleanup = context.setInputHandlers(eventHandler);
233277

234278
return () => cleanup();
235279
}, deps);

src/internal.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useRef } from "react";
22
import { Animated } from "react-native";
33

4-
import { registerEventHandler, unregisterEventHandler } from "./event-handler";
54
import { findNodeHandle } from "./utils/findNodeHandle";
65

76
type EventHandler = (event: never) => void;
@@ -23,12 +22,8 @@ type ComponentOrHandle = Parameters<typeof findNodeHandle>[0];
2322
*/
2423
export function useEventHandlerRegistration<
2524
H extends Partial<Record<string, EventHandler>>,
26-
>(
27-
map: Map<keyof H, string>,
28-
viewTagRef: React.MutableRefObject<ComponentOrHandle>,
29-
) {
25+
>(viewTagRef: React.MutableRefObject<ComponentOrHandle>) {
3026
const onRegisterHandler = (handler: H) => {
31-
const ids: (number | null)[] = [];
3227
const attachWorkletHandlers = () => {
3328
const viewTag = findNodeHandle(viewTagRef.current);
3429

@@ -38,26 +33,8 @@ export function useEventHandlerRegistration<
3833
);
3934
}
4035

41-
ids.push(
42-
...Object.keys(handler).map((handlerName) => {
43-
const eventName = map.get(handlerName as keyof H);
44-
const functionToCall = handler[handlerName as keyof H];
45-
46-
if (eventName && viewTag) {
47-
return registerEventHandler(
48-
(event: Parameters<NonNullable<H[keyof H]>>[0]) => {
49-
"worklet";
50-
51-
functionToCall?.(event);
52-
},
53-
eventName,
54-
viewTag,
55-
);
56-
}
57-
58-
return null;
59-
}),
60-
);
36+
// TODO: must be a compat layer? Property `workletEventHandler` from `ref` may be missing. Should be on useEvent layer?
37+
handler.workletEventHandler.registerForEvents(viewTag);
6138
};
6239

6340
if (viewTagRef.current) {
@@ -68,7 +45,9 @@ export function useEventHandlerRegistration<
6845
}
6946

7047
return () => {
71-
ids.forEach((id) => (id ? unregisterEventHandler(id) : null));
48+
const viewTag = findNodeHandle(viewTagRef.current);
49+
50+
handler.workletEventHandler.unregisterFromEvents(viewTag);
7251
};
7352
};
7453

0 commit comments

Comments
 (0)