Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

use-thread-settings-sheet-presentation.ts

152 lines5.3 KB

1import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
2import { KeyboardController } from "react-native-keyboard-controller";
3​
4import type { ComposerEditorHandle } from "../../components/ComposerEditor";
5​
6type PresentationPhase = "closed" | "opening" | "visible" | "restoring";
7​
8/** UIKit completion event from the native-stack patch; absent from upstream event maps. */
9export type NavigationWithFinishTransitioning = {
10 readonly addListener: (type: "finishTransitioning", callback: () => void) => () => void;
11};
12​
13const SHEET_DISMISSAL_KEYBOARD_OVERLAP_MS = 300;
14​
15/** Gesture dismissal completes before router pop; recent completion means keyboard restoration need not wait again. */
16const NATIVE_DISMISSAL_ECHO_WINDOW_MS = 150;
17​
18/** Blur/focus costs two keyboard animations; inputView overrides also replay show when removed. */
19export function useThreadSettingsSheetPresentation(input: {
20 readonly editorRef: RefObject<ComposerEditorHandle | null>;
21 readonly isEditorFocused: boolean;
22}) {
23 const [phase, setPhase] = useState<PresentationPhase>("closed");
24 const isActiveRef = useRef(false);
25 const isMountedRef = useRef(true);
26 const isEditorFocusedRef = useRef(input.isEditorFocused);
27 const openingIdRef = useRef(0);
28 const focusRestoreIdRef = useRef(0);
29 const restoreFocusAfterDismissRef = useRef(false);
30 const restorePendingRef = useRef(false);
31 const lastStackTransitionFinishedAtRef = useRef(0);
32 const dismissRestoreTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
33 const clearDismissRestoreTimer = useCallback(() => {
34 if (dismissRestoreTimerRef.current !== null) {
35 clearTimeout(dismissRestoreTimerRef.current);
36 dismissRestoreTimerRef.current = null;
37 }
38 }, []);
39​
40 useEffect(() => {
41 isEditorFocusedRef.current = input.isEditorFocused;
42 }, [input.isEditorFocused]);
43​
44 useEffect(() => {
45 isMountedRef.current = true;
46 return () => {
47 isMountedRef.current = false;
48 isActiveRef.current = false;
49 openingIdRef.current += 1;
50 focusRestoreIdRef.current += 1;
51 clearDismissRestoreTimer();
52 };
53 }, [clearDismissRestoreTimer]);
54​
55 const open = useCallback(() => {
56 if (isActiveRef.current) {
57 return;
58 }
59​
60 isActiveRef.current = true;
61 focusRestoreIdRef.current += 1;
62 clearDismissRestoreTimer();
63 restorePendingRef.current = false;
64 restoreFocusAfterDismissRef.current =
65 phase === "restoring" || input.isEditorFocused || KeyboardController.isVisible();
66 setPhase("opening");
67​
68 const openingId = openingIdRef.current + 1;
69 openingIdRef.current = openingId;
70​
71 // Dismiss the keyboard before blur, then present next frame so native transitions overlap.
72 void KeyboardController.dismiss({ animated: true });
73 input.editorRef.current?.blur();
74​
75 requestAnimationFrame(() => {
76 if (!isMountedRef.current || !isActiveRef.current || openingIdRef.current !== openingId) {
77 return;
78 }
79 setPhase("visible");
80 });
81 }, [clearDismissRestoreTimer, input.editorRef, input.isEditorFocused, phase]);
82​
83 const restoreEditorFocus = useCallback(() => {
84 const focusRestoreId = focusRestoreIdRef.current + 1;
85 focusRestoreIdRef.current = focusRestoreId;
86 let attemptsRemaining = 20;
87​
88 // Retry focus restoration if UIKit briefly refuses first responder at the dismissal boundary.
89 const restoreFocus = () => {
90 if (!isMountedRef.current || focusRestoreIdRef.current !== focusRestoreId) {
91 return;
92 }
93 if (isEditorFocusedRef.current || attemptsRemaining <= 0) {
94 setPhase("closed");
95 return;
96 }
97​
98 attemptsRemaining -= 1;
99 input.editorRef.current?.focus();
100 setTimeout(restoreFocus, 50);
101 };
102 requestAnimationFrame(restoreFocus);
103 }, [input.editorRef]);
104​
105 const runPendingDismissalRestore = useCallback(() => {
106 if (!restorePendingRef.current) {
107 return;
108 }
109 restorePendingRef.current = false;
110 clearDismissRestoreTimer();
111 if (!isMountedRef.current || isActiveRef.current) {
112 return;
113 }
114 restoreEditorFocus();
115 }, [clearDismissRestoreTimer, restoreEditorFocus]);
116​
117 const onDismissed = useCallback(() => {
118 isActiveRef.current = false;
119​
120 if (!restoreFocusAfterDismissRef.current) {
121 setPhase("closed");
122 return;
123 }
124 // Keep the card expanded during focus handoff; hardware keyboards cannot hide a collapse with keyboard travel.
125 setPhase("restoring");
126 restoreFocusAfterDismissRef.current = false;
127 restorePendingRef.current = true;
128 clearDismissRestoreTimer();
129 if (Date.now() - lastStackTransitionFinishedAtRef.current <= NATIVE_DISMISSAL_ECHO_WINDOW_MS) {
130 runPendingDismissalRestore();
131 return;
132 }
133 dismissRestoreTimerRef.current = setTimeout(() => {
134 dismissRestoreTimerRef.current = null;
135 runPendingDismissalRestore();
136 }, SHEET_DISMISSAL_KEYBOARD_OVERLAP_MS);
137 }, [clearDismissRestoreTimer, runPendingDismissalRestore]);
138​
139 const onStackTransitionsFinished = useCallback(() => {
140 lastStackTransitionFinishedAtRef.current = Date.now();
141 runPendingDismissalRestore();
142 }, [runPendingDismissalRestore]);
143​
144 return {
145 isActive: phase === "opening" || phase === "visible",
146 keepsComposerExpanded: phase !== "closed",
147 isVisible: phase === "visible",
148 open,
149 onDismissed,
150 onStackTransitionsFinished,
151 } as const;