|
| 1 | +import { useEffect, useRef } from 'react'; |
| 2 | +import { Platform } from 'react-native'; |
| 3 | + |
| 4 | +import { useStableCallback } from './useStableCallback'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Number of frames we wait before invoking input focus sensitive work after the |
| 8 | + * overlay closes. |
| 9 | + */ |
| 10 | +const SETTLE_FRAMES = Platform.OS === 'android' ? 2 : 0; |
| 11 | + |
| 12 | +/** |
| 13 | + * Runs a callback after a fixed number of animation frames. |
| 14 | + * |
| 15 | + * We use RAFs here because the settling work we care about is tied to the next |
| 16 | + * rendered frames after the overlay close transition. |
| 17 | + * |
| 18 | + * @param callback - callback to run once the frame budget has elapsed |
| 19 | + * @param frames - number of frames to wait |
| 20 | + * @param rafIds - accumulator used for later cancellation/cleanup |
| 21 | + */ |
| 22 | +const scheduleAfterFrames = (callback: () => void, frames: number, rafIds: number[]) => { |
| 23 | + if (frames <= 0) { |
| 24 | + callback(); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const rafId = requestAnimationFrame(() => scheduleAfterFrames(callback, frames - 1, rafIds)); |
| 29 | + rafIds.push(rafId); |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Returns a stable callback that is safe to run after a `PortalWhileClosingView` |
| 34 | + * has settled back into its original tree. |
| 35 | + * |
| 36 | + * Some followup actions are sensitive to that handoff window. If they run |
| 37 | + * while a view is still being returned from a portal host to its in place host, |
| 38 | + * they can target a node that is about to be reattached. On Android, that is |
| 39 | + * especially noticeable with focus sensitive work, where the target can lose |
| 40 | + * focus again mid keyboard animation. |
| 41 | + * |
| 42 | + * Two frames are intentional here: |
| 43 | + * - frame 1 lets the portal retarget and React commit the component tree |
| 44 | + * - frame 2 lets the native view hierarchy settle in its final host |
| 45 | + * |
| 46 | + * iOS does not currently need this settle window for this flow. |
| 47 | + * |
| 48 | + * A good example is the message composer edit action: after closing the message |
| 49 | + * overlay, we wait for the portal handoff to settle before focusing the input |
| 50 | + * and opening the keyboard. Doing this prematurely will result in the keyboard |
| 51 | + * being immediately closed. |
| 52 | + * |
| 53 | + * Another good example would be having a button wrapped in a `PortalWhileClosingView`, |
| 54 | + * that possibly renders (or morphs into) something when pressed. Handling `onPress` |
| 55 | + * prematurely here may lead to the morphed button rendering into a completely different |
| 56 | + * part of the UI hierarchy, causing unknown behaviour. This hook prevents that from |
| 57 | + * happening. |
| 58 | + * |
| 59 | + * @param callback - callback we want to invoke once the portal handoff has settled |
| 60 | + * @returns A stable callback gated behind the portal settle window. |
| 61 | + */ |
| 62 | +export const usePortalSettledCallback = <T extends unknown[]>(callback: (...args: T) => void) => { |
| 63 | + const rafIdsRef = useRef<number[]>([]); |
| 64 | + const stableCallback = useStableCallback(callback); |
| 65 | + |
| 66 | + const clearScheduledFrames = useStableCallback(() => { |
| 67 | + rafIdsRef.current.forEach((rafId) => cancelAnimationFrame(rafId)); |
| 68 | + rafIdsRef.current = []; |
| 69 | + }); |
| 70 | + |
| 71 | + useEffect(() => clearScheduledFrames, [clearScheduledFrames]); |
| 72 | + |
| 73 | + return useStableCallback((...args: T) => { |
| 74 | + clearScheduledFrames(); |
| 75 | + scheduleAfterFrames(() => stableCallback(...args), SETTLE_FRAMES, rafIdsRef.current); |
| 76 | + }); |
| 77 | +}; |
0 commit comments