diff --git a/patches/react-native-keyboard-controller@1.22.7.patch b/patches/react-native-keyboard-controller@1.22.7.patch new file mode 100644 index 000000000..9e16d8492 --- /dev/null +++ b/patches/react-native-keyboard-controller@1.22.7.patch @@ -0,0 +1,142 @@ +diff --git a/src/components/KeyboardAvoidingView/hooks.ts b/src/components/KeyboardAvoidingView/hooks.ts +index 64bfa65439..0862b6ca6b 100644 +--- a/src/components/KeyboardAvoidingView/hooks.ts ++++ b/src/components/KeyboardAvoidingView/hooks.ts +@@ -1,6 +1,6 @@ + import { useLayoutEffect } from "react"; + import { Platform } from "react-native"; +-import { useSharedValue } from "react-native-reanimated"; ++import { runOnUI, useSharedValue } from "react-native-reanimated"; + + import { useKeyboardContext } from "../../context"; + import { useKeyboardHandler } from "../../hooks"; +@@ -14,16 +14,28 @@ export const useKeyboardAnimation = () => { + const height = useSharedValue(0); + const progress = useSharedValue(0); + const isClosed = useSharedValue(true); ++ const hasReceivedEvent = useSharedValue(false); + + useLayoutEffect(() => { +- const initialHeight = -reanimated.height.value; +- const initialProgress = reanimated.progress.value; +- +- // eslint-disable-next-line react-compiler/react-compiler +- heightWhenOpened.value = initialHeight; +- height.value = initialHeight; +- progress.value = initialProgress; +- isClosed.value = initialProgress === 0; ++ // writes from JS are applied on the UI thread asynchronously, so they may ++ // overwrite values set by keyboard events that arrived in between (i. e. when ++ // input has `autoFocus`) - set initial values only if no event arrived yet ++ runOnUI(() => { ++ "worklet"; ++ ++ if (hasReceivedEvent.value) { ++ return; ++ } ++ ++ const initialHeight = -reanimated.height.value; ++ const initialProgress = reanimated.progress.value; ++ ++ // eslint-disable-next-line react-compiler/react-compiler ++ heightWhenOpened.value = initialHeight; ++ height.value = initialHeight; ++ progress.value = initialProgress; ++ isClosed.value = initialProgress === 0; ++ })(); + }, []); + + useKeyboardHandler( +@@ -31,6 +43,8 @@ export const useKeyboardAnimation = () => { + onStart: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; ++ + if (e.height > 0) { + isClosed.value = false; + heightWhenOpened.value = e.height; +@@ -39,20 +53,28 @@ export const useKeyboardAnimation = () => { + onMove: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; + progress.value = e.progress; + height.value = e.height; + }, + onInteractive: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; + progress.value = e.progress; + height.value = e.height; + }, + onEnd: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; + isClosed.value = e.height === 0; + ++ // `onStart` may have been dispatched before this handler was attached ++ if (e.height > 0 && e.progress === 1) { ++ heightWhenOpened.value = e.height; ++ } ++ + height.value = e.height; + progress.value = e.progress; + }, +@@ -67,10 +89,18 @@ export const useTranslateAnimation = () => { + + const padding = useSharedValue(0); + const translate = useSharedValue(0); ++ const hasReceivedEvent = useSharedValue(false); + + useLayoutEffect(() => { +- // eslint-disable-next-line react-compiler/react-compiler +- padding.value = reanimated.progress.value; ++ // see `useKeyboardAnimation` for an explanation ++ runOnUI(() => { ++ "worklet"; ++ ++ if (!hasReceivedEvent.value) { ++ // eslint-disable-next-line react-compiler/react-compiler ++ padding.value = reanimated.progress.value; ++ } ++ })(); + }, []); + + useKeyboardHandler( +@@ -78,6 +108,8 @@ export const useTranslateAnimation = () => { + onStart: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; ++ + if (e.height === 0) { + padding.value = 0; + } +@@ -88,6 +120,8 @@ export const useTranslateAnimation = () => { + onMove: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; ++ + if (OS !== "ios") { + translate.value = e.progress; + } +@@ -95,6 +129,7 @@ export const useTranslateAnimation = () => { + onInteractive: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; + padding.value = 0; + + translate.value = e.progress; +@@ -102,6 +137,7 @@ export const useTranslateAnimation = () => { + onEnd: (e) => { + "worklet"; + ++ hasReceivedEvent.value = true; + padding.value = e.progress; + + if (OS !== "ios") { diff --git a/patches/react-native-keyboard-controller@1.22.7.patch.md b/patches/react-native-keyboard-controller@1.22.7.patch.md new file mode 100644 index 000000000..ab7ecb460 --- /dev/null +++ b/patches/react-native-keyboard-controller@1.22.7.patch.md @@ -0,0 +1,84 @@ +# react-native-keyboard-controller@1.22.7.patch + +## Stop the KeyboardAvoidingView initial state from clobbering keyboard events + +Upstream PR: https://github.com/kirillzyusko/react-native-keyboard-controller/pull/1685 +(this patch is the same change, minus its unit test). Remove this patch once a +release includes it. + +Fixes APP-3472: on Android, the composer's footer (media buttons, language, +character counter) intermittently stays behind the keyboard after the composer +opens. + +### Cause + +`useKeyboardAnimation` (`src/components/KeyboardAvoidingView/hooks.ts`) seeds +its shared values in a `useLayoutEffect` on the JS thread: + +```ts +heightWhenOpened.value = -reanimated.height.value +height.value = ... +progress.value = initialProgress +isClosed.value = initialProgress === 0 +``` + +In Reanimated 4, a `.value =` write from the JS thread is +`scheduleOnUI(() => (mutable.value = newValue))`. It's applied whenever the UI +thread gets to the queued job. The composer's text input has `autoFocus`, so the +keyboard starts opening as the view mounts, and its `onStart`/`onEnd` worklets +run on the UI thread too. When the UI thread is busy (the composer slide-in +plus the keyboard animation), the queued writes can land after those events +and reset the state to "closed, height 0": + +1. Writes land between `onStart` and `onEnd`: `onEnd` restores `isClosed` and + `progress`, but `heightWhenOpened` stays 0, so the relative keyboard height + is 0 and the view doesn't avoid the keyboard. This is the bug we see. +2. Writes land after `onEnd`: `isClosed` is back to `true` with the keyboard + open. With `behavior="height"` the style worklet then returns `{}`, and + Reanimated keeps the last applied height, so the open looks fine but the + view stays shrunk after the keyboard is dismissed (until it opens again). + +### Fix + +- Seed inside `runOnUI`, reading the provider's values on the UI thread, and + skip the seed if this hook has already handled a keyboard event + (`hasReceivedEvent`). This covers both orderings. +- Also refresh `heightWhenOpened` from an `onEnd` where the keyboard ended fully + open (`height > 0 && progress === 1`), in case `onStart` fired before the + handler was registered. +- Same guard for the `padding` seed in `useTranslateAnimation` (only affects + `behavior="translate-with-padding"`, which we don't use). + +### Validation + +Pixel 8 emulator (API 34), Reanimated 4.7.1, composer opens checked via the +accessibility tree: footer above the keyboard on open, back at the bottom after +Back, above the keyboard again after refocusing. + +Deterministic orderings (seed deferred to run right after the first +`onStart`/`onEnd`), 10 opens each: + +| `hooks.ts` | ordering | open | dismiss | refocus | +| ----------------------- | -------- | ----- | ------- | ------- | +| stock | 1 | 0/10 | 10/10 | 10/10 | +| stock | 2 | 10/10 | 0/10 | 10/10 | +| `onEnd` repair only[^1] | 1 | 10/10 | 10/10 | 10/10 | +| `onEnd` repair only[^1] | 2 | 10/10 | 0/10 | 10/10 | +| this patch | 1 | 10/10 | 10/10 | 10/10 | +| this patch | 2 | 10/10 | 10/10 | 10/10 | + +[^1]: The approach in https://github.com/bluesky-social/social-app/pull/12029. + +Natural timing (no forced delays, 40 opens each): stock 39/40 passed, with the +one failure being the one open where the seed landed after `onStart`. This +patch passed 40/40, including 3 opens where the seed landed after `onStart` and +was skipped. The natural rate depends heavily on load, so the deterministic +table is the real evidence. + +Not tested on device: iOS (the simulator wasn't available) and +`translate-with-padding`. Both are covered by the upstream unit test. + +Only `src/` is patched. Metro resolves the package's `react-native` entry +(`src/index`); Jest would use `lib/commonjs`, but no tests exercise this hook. +The login, signup and phone input screens use the same `KeyboardAvoidingView` +but don't auto-focus on mount. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8bfab5ebe..371decd7d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -220,6 +220,7 @@ patchedDependencies: expo@58.0.14: 0208b099bee03d1d30dddfe51b3a1598db2180fa85ae9a9c032a296cb0cb006f metro-runtime@0.87.1: 4a386cd6f62275a31620c3b0cb489d818359125e5c94f6144933de2dea7e4d98 metro@0.87.1: 15eec39e654a1ba09b4d6e582cda194f1331621f0f2b7e55db9a3a0364a9ebc3 + react-native-keyboard-controller@1.22.7: 0c820c84aaf84f847db8770a021188043505355371998f479b594f50d2c66c11 react-native-pager-view@9.0.9: 903ed1dbd04010438082eab966831b05d50c650abd58610cc448d0acb5867c6f react-native-reanimated@4.7.1: 557541b187597797db03adf716a768717a574dbdff8b221683f4b3f7bd912185 react-native-screens@4.30.0: 18e03b9c4fbb68c76cd490df5afbafecb68381b8b49cb04459667b682711e840 @@ -676,7 +677,7 @@ importers: version: 3.3.0(react-native@0.88.0(patch_hash=dc5394dccd29a3cc7ec7079a8fae133938d92a99a5f7fdd73ed5ef94dd868a96)(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.3.0)(react@19.3.0)(supports-color@8.1.1))(react@19.3.0) react-native-keyboard-controller: specifier: 1.22.7 - version: 1.22.7(bcaf5112feb0eedd10ce80c2d4d8ef5a) + version: 1.22.7(patch_hash=0c820c84aaf84f847db8770a021188043505355371998f479b594f50d2c66c11)(bcaf5112feb0eedd10ce80c2d4d8ef5a) react-native-mmkv: specifier: ^3.3.3 version: 3.3.3(react-native@0.88.0(patch_hash=dc5394dccd29a3cc7ec7079a8fae133938d92a99a5f7fdd73ed5ef94dd868a96)(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.3.0)(react@19.3.0)(supports-color@8.1.1))(react@19.3.0) @@ -18648,7 +18649,7 @@ snapshots: react: 19.3.0 react-native: 0.88.0(patch_hash=dc5394dccd29a3cc7ec7079a8fae133938d92a99a5f7fdd73ed5ef94dd868a96)(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.3.0)(react@19.3.0)(supports-color@8.1.1) - react-native-keyboard-controller@1.22.7(bcaf5112feb0eedd10ce80c2d4d8ef5a): + react-native-keyboard-controller@1.22.7(patch_hash=0c820c84aaf84f847db8770a021188043505355371998f479b594f50d2c66c11)(bcaf5112feb0eedd10ce80c2d4d8ef5a): dependencies: react: 19.3.0 react-native: 0.88.0(patch_hash=dc5394dccd29a3cc7ec7079a8fae133938d92a99a5f7fdd73ed5ef94dd868a96)(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.3.0)(react@19.3.0)(supports-color@8.1.1) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 893d1291b..f86a9fe8c 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -47,6 +47,7 @@ patchedDependencies: expo@58.0.14: patches/expo@58.0.14.patch 'metro-runtime@0.87.1': patches/metro-runtime@0.87.1.patch 'metro@0.87.1': patches/metro@0.87.1.patch + react-native-keyboard-controller@1.22.7: patches/react-native-keyboard-controller@1.22.7.patch react-native-pager-view@9.0.9: patches/react-native-pager-view@9.0.9.patch react-native-reanimated@4.7.1: patches/react-native-reanimated@4.7.1.patch react-native-screens@4.30.0: patches/react-native-screens@4.30.0.patch