Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useSharedValue } from "react-native-reanimated";

import { useKeyboardHandler } from "./hooks";

export const KeyboardState = {
UNKNOWN: 0,
OPENING: 1,
OPEN: 2,
CLOSING: 3,
CLOSED: 4,
};

/**
* A compatibility layer for migration from https://docs.swmansion.com/react-native-reanimated/docs/device/useAnimatedKeyboard.
*
* @returns An object containing `height` and `state` properties represented as `SharedValue<number>`.
* @example
* ```ts
* import { useAnimatedKeyboard, useAnimatedStyle } from 'react-native-keyboard-controller';
*
* export default function App() {
* const keyboard = useAnimatedKeyboard();
*
* const animatedStyles = useAnimatedStyle(() => ({
* transform: [{ translateY: -keyboard.height.value }],
* }));
* }
*/
export const useAnimatedKeyboard = () => {
const height = useSharedValue(0);
const state = useSharedValue(KeyboardState.UNKNOWN);

useKeyboardHandler(
{
onStart: (e) => {
"worklet";

state.set(e.height > 0 ? KeyboardState.OPENING : KeyboardState.CLOSING);
},
onMove: (e) => {
"worklet";

height.set(e.height);
},
onInteractive: (e) => {
"worklet";

height.set(e.height);
},
onEnd: (e) => {
"worklet";

state.set(e.height > 0 ? KeyboardState.OPEN : KeyboardState.CLOSED);
height.set(e.height);
},
},
Comment on lines +34 to +56
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to memoize this config object with useMemo?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tomekzaw You know this part better, so I'll do whatever you say. But let me explain it a little bit. Internally useKeyboardHandler uses useEvent/useHandler hooks from REA. And I tried to have the same API as useAnimatedScrollHandler with only one difference - we attach handler to the view that is located in KeyboardProvider (it kind of makes sense because you have a single keyboard for whole app, but in case with ScrollView you may have multiple of them even on a single screen).

From what I saw useAnimatedScrollHandler doesn't memoize the config with useMemo, right? Is it very expensive to create such object? (from plain react creating an object is a very cheap operation, but I'm not sure about "worklet" part, I think it also should be fast?)

Anyway, the reason why I don't use useMemo is because I was trying to have the same API as useAnimatedScrollHandler and this hook doesn't use useMemo 🙃

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked @tjzel about this (he's our expert on memoization and worklets)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think useMemo isn't necessary here, the gains would probably be marginal.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tjzel for a confirmation!

[],
);

return { height, state };
};
8 changes: 4 additions & 4 deletions src/hooks/useKeyboardState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from "react";
import { KeyboardEvents } from "../../bindings";
import { KeyboardController } from "../../module";

import type { KeyboardState } from "../../types";
import type { IKeyboardState } from "../../types";

const EVENTS = ["keyboardDidShow", "keyboardDidHide"] as const;

Expand All @@ -12,9 +12,9 @@ const getLatestState = () => ({
isVisible: KeyboardController.isVisible(),
});

type KeyboardStateSelector<T> = (state: KeyboardState) => T;
type KeyboardStateSelector<T> = (state: IKeyboardState) => T;

const defaultSelector: KeyboardStateSelector<KeyboardState> = (state) => state;
const defaultSelector: KeyboardStateSelector<IKeyboardState> = (state) => state;

/**
* React Hook that represents the current keyboard state on iOS and Android.
Expand All @@ -40,7 +40,7 @@ const defaultSelector: KeyboardStateSelector<KeyboardState> = (state) => state;
* }
* ```
*/
function useKeyboardState<T = KeyboardState>(
function useKeyboardState<T = IKeyboardState>(
selector: KeyboardStateSelector<T> = defaultSelector as KeyboardStateSelector<T>,
): T {
const [state, setState] = useState<T>(() => selector(getLatestState()));
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./hooks";
export * from "./constants";
export * from "./module";
export * from "./types";
export * from "./compat";

export {
KeyboardAvoidingView,
Expand Down
2 changes: 1 addition & 1 deletion src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type KeyboardEventData = {
/**
* An object that represent current keyboard state.
*/
export type KeyboardState = {
export type IKeyboardState = {
/** Whether the keyboard is currently visible. */
isVisible: boolean;
} & KeyboardEventData;
Expand Down