@vvedantb/native-ui 0.25.0 → 0.25.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai/conversation-context.js +27 -0
- package/dist/ai/conversation.jsx +62 -52
- package/dist/ai/message-context-menu.jsx +414 -325
- package/dist/ai/message.jsx +98 -79
- package/dist/primitives/keyboard-controller.jsx +32 -26
- package/package.json +1 -1
- package/src/ai/conversation-context.ts +56 -0
- package/src/ai/conversation.tsx +12 -42
- package/src/ai/message-context-menu.tsx +80 -33
- package/src/ai/message.tsx +18 -5
- package/src/primitives/keyboard-controller.tsx +3 -0
- package/src/primitives/keyboard-parts.tsx +3 -1
package/dist/ai/message.jsx
CHANGED
|
@@ -14,6 +14,7 @@ import { useScheme } from "../useScheme";
|
|
|
14
14
|
import { META_TEXT_STYLE, chatTextSize } from "./chat-text";
|
|
15
15
|
import { ChatIconButton } from "./icon-button";
|
|
16
16
|
import { MESSAGE_ACTIONS_TITLE, MessageActionSheet } from "./message-action-sheet";
|
|
17
|
+
import { useOptionalConversation } from "./conversation-context";
|
|
17
18
|
import { MessageContextMenu } from "./message-context-menu";
|
|
18
19
|
import { ChatTextColorContext, ResponseText } from "./response-text";
|
|
19
20
|
const MessageContext = createContext("assistant");
|
|
@@ -106,7 +107,7 @@ function MessageHighlight() {
|
|
|
106
107
|
* at the start, its parts full width.
|
|
107
108
|
*
|
|
108
109
|
* With `onLongPress` or `actions` the message is a `Pressable` (same layout,
|
|
109
|
-
* `className`, `style` and view props): a 350 ms hold
|
|
110
|
+
* `className`, `style` and view props): a 350 ms hold dims it a little,
|
|
110
111
|
* fires a medium haptic, runs `onLongPress` and opens the
|
|
111
112
|
* `MessageActionSheet`; TalkBack reaches it as the `longpress` action. It stays
|
|
112
113
|
* a non-accessible container so its buttons and links keep their own focus:
|
|
@@ -114,15 +115,16 @@ function MessageHighlight() {
|
|
|
114
115
|
*/
|
|
115
116
|
/** How long a hold takes, in ms: shorter than React Native's 500, as iMessage. */
|
|
116
117
|
const HOLD_DELAY_MS = 350;
|
|
117
|
-
/**
|
|
118
|
+
/**
|
|
119
|
+
* A held message dims a little. It does not shrink: the hold menu measures the
|
|
120
|
+
* message while it is held, and a scaled box would draw the lifted copy smaller
|
|
121
|
+
* (and could wrap its text differently).
|
|
122
|
+
*/
|
|
118
123
|
const HELD_STYLE = {
|
|
119
|
-
transform: [{
|
|
120
|
-
scale: 0.98
|
|
121
|
-
}],
|
|
122
124
|
opacity: 0.9
|
|
123
125
|
};
|
|
124
126
|
export function Message(t0) {
|
|
125
|
-
const $ = _c(
|
|
127
|
+
const $ = _c(57);
|
|
126
128
|
let actionSheetTitle;
|
|
127
129
|
let actions;
|
|
128
130
|
let bot;
|
|
@@ -181,13 +183,15 @@ export function Message(t0) {
|
|
|
181
183
|
const pressable = useRef(null);
|
|
182
184
|
const hasActions = actions != null && actions.length > 0;
|
|
183
185
|
const longPressable = onLongPress != null || hasActions || menu != null;
|
|
186
|
+
const conversation = useOptionalConversation();
|
|
184
187
|
let t2;
|
|
185
|
-
if ($[12] !==
|
|
188
|
+
if ($[12] !== conversation || $[13] !== hasActions || $[14] !== menu || $[15] !== onLongPress) {
|
|
186
189
|
t2 = () => {
|
|
187
190
|
impactHaptic("medium");
|
|
188
191
|
onLongPress?.();
|
|
189
192
|
if (menu) {
|
|
190
193
|
const built = menu();
|
|
194
|
+
conversation?.openOverlay();
|
|
191
195
|
pressable.current?.measureInWindow((x, y, width, height) => setHeld({
|
|
192
196
|
anchor: {
|
|
193
197
|
x,
|
|
@@ -203,119 +207,134 @@ export function Message(t0) {
|
|
|
203
207
|
}
|
|
204
208
|
}
|
|
205
209
|
};
|
|
206
|
-
$[12] =
|
|
207
|
-
$[13] =
|
|
208
|
-
$[14] =
|
|
209
|
-
$[15] =
|
|
210
|
+
$[12] = conversation;
|
|
211
|
+
$[13] = hasActions;
|
|
212
|
+
$[14] = menu;
|
|
213
|
+
$[15] = onLongPress;
|
|
214
|
+
$[16] = t2;
|
|
210
215
|
} else {
|
|
211
|
-
t2 = $[
|
|
216
|
+
t2 = $[16];
|
|
212
217
|
}
|
|
213
218
|
const longPress = t2;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
alignSelf: t3,
|
|
220
|
-
alignItems: t4
|
|
219
|
+
let t3;
|
|
220
|
+
if ($[17] !== conversation) {
|
|
221
|
+
t3 = () => {
|
|
222
|
+
setHeld(null);
|
|
223
|
+
conversation?.closeOverlay();
|
|
221
224
|
};
|
|
222
|
-
$[
|
|
223
|
-
$[
|
|
224
|
-
$[18] = t5;
|
|
225
|
+
$[17] = conversation;
|
|
226
|
+
$[18] = t3;
|
|
225
227
|
} else {
|
|
226
|
-
|
|
228
|
+
t3 = $[18];
|
|
227
229
|
}
|
|
230
|
+
const closeMenu = t3;
|
|
231
|
+
const t4 = user ? "flex-end" : "flex-start";
|
|
232
|
+
const t5 = user ? "flex-end" : "stretch";
|
|
228
233
|
let t6;
|
|
229
|
-
if ($[19] !==
|
|
230
|
-
t6 =
|
|
231
|
-
|
|
234
|
+
if ($[19] !== t4 || $[20] !== t5) {
|
|
235
|
+
t6 = {
|
|
236
|
+
alignSelf: t4,
|
|
237
|
+
alignItems: t5
|
|
238
|
+
};
|
|
239
|
+
$[19] = t4;
|
|
232
240
|
$[20] = t5;
|
|
233
241
|
$[21] = t6;
|
|
234
242
|
} else {
|
|
235
243
|
t6 = $[21];
|
|
236
244
|
}
|
|
237
|
-
const layout = t6;
|
|
238
245
|
let t7;
|
|
239
|
-
if ($[22] !==
|
|
240
|
-
t7 =
|
|
241
|
-
$[22] =
|
|
242
|
-
$[23] =
|
|
246
|
+
if ($[22] !== style || $[23] !== t6) {
|
|
247
|
+
t7 = [t6, style];
|
|
248
|
+
$[22] = style;
|
|
249
|
+
$[23] = t6;
|
|
250
|
+
$[24] = t7;
|
|
243
251
|
} else {
|
|
244
|
-
t7 = $[
|
|
252
|
+
t7 = $[24];
|
|
245
253
|
}
|
|
246
|
-
const
|
|
254
|
+
const layout = t7;
|
|
247
255
|
let t8;
|
|
248
|
-
if ($[
|
|
249
|
-
t8 =
|
|
250
|
-
$[
|
|
251
|
-
$[25] = user;
|
|
256
|
+
if ($[25] !== className) {
|
|
257
|
+
t8 = cn("w-full max-w-[95%] gap-2", className);
|
|
258
|
+
$[25] = className;
|
|
252
259
|
$[26] = t8;
|
|
253
260
|
} else {
|
|
254
261
|
t8 = $[26];
|
|
255
262
|
}
|
|
263
|
+
const merged = t8;
|
|
256
264
|
let t9;
|
|
257
|
-
if ($[27] !==
|
|
258
|
-
t9 =
|
|
259
|
-
$[27] =
|
|
260
|
-
$[28] =
|
|
265
|
+
if ($[27] !== bot || $[28] !== user) {
|
|
266
|
+
t9 = bot && !user ? <MessageBotLabel name={bot.name} avatar={bot.avatar} /> : null;
|
|
267
|
+
$[27] = bot;
|
|
268
|
+
$[28] = user;
|
|
269
|
+
$[29] = t9;
|
|
261
270
|
} else {
|
|
262
|
-
t9 = $[
|
|
271
|
+
t9 = $[29];
|
|
263
272
|
}
|
|
264
273
|
let t10;
|
|
265
|
-
if ($[
|
|
266
|
-
t10 =
|
|
267
|
-
$[
|
|
268
|
-
$[
|
|
269
|
-
$[31] = t9;
|
|
270
|
-
$[32] = t10;
|
|
274
|
+
if ($[30] !== highlighted) {
|
|
275
|
+
t10 = highlighted ? <MessageHighlight /> : null;
|
|
276
|
+
$[30] = highlighted;
|
|
277
|
+
$[31] = t10;
|
|
271
278
|
} else {
|
|
272
|
-
t10 = $[
|
|
279
|
+
t10 = $[31];
|
|
273
280
|
}
|
|
274
|
-
const parts = t10;
|
|
275
281
|
let t11;
|
|
276
|
-
if ($[
|
|
277
|
-
t11 =
|
|
282
|
+
if ($[32] !== children || $[33] !== t10 || $[34] !== t9) {
|
|
283
|
+
t11 = <>{t9}{children}{t10}</>;
|
|
284
|
+
$[32] = children;
|
|
285
|
+
$[33] = t10;
|
|
286
|
+
$[34] = t9;
|
|
287
|
+
$[35] = t11;
|
|
288
|
+
} else {
|
|
289
|
+
t11 = $[35];
|
|
290
|
+
}
|
|
291
|
+
const parts = t11;
|
|
292
|
+
let t12;
|
|
293
|
+
if ($[36] !== actionSheetTitle || $[37] !== actions || $[38] !== closeMenu || $[39] !== conversation?.settleOverlay || $[40] !== from || $[41] !== hasActions || $[42] !== held || $[43] !== layout || $[44] !== longPress || $[45] !== longPressable || $[46] !== menu || $[47] !== merged || $[48] !== parts || $[49] !== pressing || $[50] !== props || $[51] !== sheetOpen || $[52] !== user) {
|
|
294
|
+
t12 = longPressable ? <><Pressable ref={pressable} accessible={false} className={merged} style={[layout, pressing ? HELD_STYLE : null]} onPressIn={() => setPressing(true)} onPressOut={() => setPressing(false)} onLongPress={longPress} delayLongPress={HOLD_DELAY_MS} accessibilityActions={[{
|
|
278
295
|
name: "longpress",
|
|
279
296
|
label: actionSheetTitle ?? MESSAGE_ACTIONS_TITLE
|
|
280
297
|
}]} onAccessibilityAction={event => {
|
|
281
298
|
if (event.nativeEvent.actionName === "longpress") {
|
|
282
299
|
longPress();
|
|
283
300
|
}
|
|
284
|
-
}} {...props}>{parts}</Pressable>{menu ? <MessageContextMenu open={held !== null} onClose={
|
|
301
|
+
}} {...props}>{parts}</Pressable>{menu ? <MessageContextMenu open={held !== null} onClose={closeMenu} onDismissed={conversation?.settleOverlay} anchor={held?.anchor ?? null} align={user ? "end" : "start"} preview={<MessageContext.Provider value={from}><View className={merged} style={[layout, {
|
|
285
302
|
maxWidth: "100%",
|
|
286
303
|
width: "100%"
|
|
287
304
|
}]}>{parts}</View></MessageContext.Provider>} menu={held?.menu ?? {
|
|
288
305
|
actions: []
|
|
289
306
|
}} /> : hasActions ? <MessageActionSheet open={sheetOpen} onClose={() => setSheetOpen(false)} actions={actions} title={actionSheetTitle} /> : null}</> : <View className={merged} style={layout} {...props}>{parts}</View>;
|
|
290
|
-
$[
|
|
291
|
-
$[
|
|
292
|
-
$[
|
|
293
|
-
$[
|
|
294
|
-
$[
|
|
295
|
-
$[
|
|
296
|
-
$[
|
|
297
|
-
$[
|
|
298
|
-
$[
|
|
299
|
-
$[
|
|
300
|
-
$[
|
|
301
|
-
$[
|
|
302
|
-
$[
|
|
303
|
-
$[
|
|
304
|
-
$[
|
|
305
|
-
$[
|
|
306
|
-
|
|
307
|
-
|
|
307
|
+
$[36] = actionSheetTitle;
|
|
308
|
+
$[37] = actions;
|
|
309
|
+
$[38] = closeMenu;
|
|
310
|
+
$[39] = conversation?.settleOverlay;
|
|
311
|
+
$[40] = from;
|
|
312
|
+
$[41] = hasActions;
|
|
313
|
+
$[42] = held;
|
|
314
|
+
$[43] = layout;
|
|
315
|
+
$[44] = longPress;
|
|
316
|
+
$[45] = longPressable;
|
|
317
|
+
$[46] = menu;
|
|
318
|
+
$[47] = merged;
|
|
319
|
+
$[48] = parts;
|
|
320
|
+
$[49] = pressing;
|
|
321
|
+
$[50] = props;
|
|
322
|
+
$[51] = sheetOpen;
|
|
323
|
+
$[52] = user;
|
|
324
|
+
$[53] = t12;
|
|
325
|
+
} else {
|
|
326
|
+
t12 = $[53];
|
|
308
327
|
}
|
|
309
|
-
let
|
|
310
|
-
if ($[
|
|
311
|
-
|
|
312
|
-
$[
|
|
313
|
-
$[
|
|
314
|
-
$[
|
|
328
|
+
let t13;
|
|
329
|
+
if ($[54] !== from || $[55] !== t12) {
|
|
330
|
+
t13 = <MessageContext.Provider value={from}>{t12}</MessageContext.Provider>;
|
|
331
|
+
$[54] = from;
|
|
332
|
+
$[55] = t12;
|
|
333
|
+
$[56] = t13;
|
|
315
334
|
} else {
|
|
316
|
-
|
|
335
|
+
t13 = $[56];
|
|
317
336
|
}
|
|
318
|
-
return
|
|
337
|
+
return t13;
|
|
319
338
|
}
|
|
320
339
|
|
|
321
340
|
/**
|
|
@@ -76,16 +76,18 @@ function StickyView(t0) {
|
|
|
76
76
|
* end on either platform.
|
|
77
77
|
*/
|
|
78
78
|
function ChatScrollView(t0) {
|
|
79
|
-
const $ = _c(
|
|
79
|
+
const $ = _c(19);
|
|
80
80
|
let bottomOffset;
|
|
81
81
|
let onInsetChange;
|
|
82
82
|
let onKeyboardMove;
|
|
83
83
|
let props;
|
|
84
84
|
let t1;
|
|
85
|
+
let t2;
|
|
85
86
|
if ($[0] !== t0) {
|
|
86
87
|
({
|
|
87
88
|
bottomOffset,
|
|
88
89
|
inverted: t1,
|
|
90
|
+
freeze: t2,
|
|
89
91
|
onInsetChange,
|
|
90
92
|
onKeyboardMove,
|
|
91
93
|
...props
|
|
@@ -96,18 +98,21 @@ function ChatScrollView(t0) {
|
|
|
96
98
|
$[3] = onKeyboardMove;
|
|
97
99
|
$[4] = props;
|
|
98
100
|
$[5] = t1;
|
|
101
|
+
$[6] = t2;
|
|
99
102
|
} else {
|
|
100
103
|
bottomOffset = $[1];
|
|
101
104
|
onInsetChange = $[2];
|
|
102
105
|
onKeyboardMove = $[3];
|
|
103
106
|
props = $[4];
|
|
104
107
|
t1 = $[5];
|
|
108
|
+
t2 = $[6];
|
|
105
109
|
}
|
|
106
110
|
const inverted = t1 === undefined ? false : t1;
|
|
107
|
-
|
|
111
|
+
const freeze = t2 === undefined ? false : t2;
|
|
108
112
|
let t3;
|
|
109
|
-
|
|
110
|
-
|
|
113
|
+
let t4;
|
|
114
|
+
if ($[7] !== onKeyboardMove) {
|
|
115
|
+
t3 = {
|
|
111
116
|
onStart: () => {
|
|
112
117
|
"worklet";
|
|
113
118
|
|
|
@@ -119,36 +124,37 @@ function ChatScrollView(t0) {
|
|
|
119
124
|
runOnJS(onKeyboardMove)(false);
|
|
120
125
|
}
|
|
121
126
|
};
|
|
122
|
-
|
|
123
|
-
$[
|
|
124
|
-
$[7] = t2;
|
|
127
|
+
t4 = [onKeyboardMove];
|
|
128
|
+
$[7] = onKeyboardMove;
|
|
125
129
|
$[8] = t3;
|
|
130
|
+
$[9] = t4;
|
|
126
131
|
} else {
|
|
127
|
-
t2 = $[7];
|
|
128
132
|
t3 = $[8];
|
|
133
|
+
t4 = $[9];
|
|
129
134
|
}
|
|
130
|
-
useKeyboardHandler(
|
|
131
|
-
let
|
|
132
|
-
if ($[
|
|
133
|
-
|
|
134
|
-
$[
|
|
135
|
-
$[
|
|
136
|
-
$[
|
|
135
|
+
useKeyboardHandler(t3, t4);
|
|
136
|
+
let t5;
|
|
137
|
+
if ($[10] !== inverted || $[11] !== onInsetChange) {
|
|
138
|
+
t5 = insets => onInsetChange(inverted ? Platform.OS === "ios" ? insets.top : 0 : insets.bottom);
|
|
139
|
+
$[10] = inverted;
|
|
140
|
+
$[11] = onInsetChange;
|
|
141
|
+
$[12] = t5;
|
|
137
142
|
} else {
|
|
138
|
-
|
|
143
|
+
t5 = $[12];
|
|
139
144
|
}
|
|
140
|
-
let
|
|
141
|
-
if ($[
|
|
142
|
-
|
|
143
|
-
$[
|
|
144
|
-
$[
|
|
145
|
-
$[
|
|
146
|
-
$[
|
|
147
|
-
$[
|
|
145
|
+
let t6;
|
|
146
|
+
if ($[13] !== bottomOffset || $[14] !== freeze || $[15] !== inverted || $[16] !== props || $[17] !== t5) {
|
|
147
|
+
t6 = <KeyboardChatScrollView {...props} inverted={inverted} freeze={freeze} offset={bottomOffset} keyboardLiftBehavior="always" onContentInsetChange={t5} />;
|
|
148
|
+
$[13] = bottomOffset;
|
|
149
|
+
$[14] = freeze;
|
|
150
|
+
$[15] = inverted;
|
|
151
|
+
$[16] = props;
|
|
152
|
+
$[17] = t5;
|
|
153
|
+
$[18] = t6;
|
|
148
154
|
} else {
|
|
149
|
-
|
|
155
|
+
t6 = $[18];
|
|
150
156
|
}
|
|
151
|
-
return
|
|
157
|
+
return t6;
|
|
152
158
|
}
|
|
153
159
|
|
|
154
160
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvedantb/native-ui",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Shared React Native / Expo UI primitives. Floating surfaces, forms, overlays, toasts, settings chrome, tab bar, AI chat parts, the family app marks, a NativeWind preset, the Expo web root and Clerk + Convex auth screens.",
|
|
6
6
|
"type": "module",
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createContext, useContext, type RefObject } from "react";
|
|
2
|
+
import type { NativeScrollEvent } from "react-native";
|
|
3
|
+
|
|
4
|
+
// The `Conversation` context on its own, so a message can reach it without the
|
|
5
|
+
// thread's components (`message.tsx` sets `overlay`).
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What the thread scrolls with: the `ScrollView` of `ConversationContent` or
|
|
9
|
+
* the `FlatList` of `ConversationList`. `scrollToEnd` goes to the newest
|
|
10
|
+
* message, which is offset 0 in an inverted list. `scrollTo` goes to an
|
|
11
|
+
* offset; only the `ScrollView` has it, so call it as `scrollTo?.(...)`.
|
|
12
|
+
*/
|
|
13
|
+
export type ConversationScroller = {
|
|
14
|
+
scrollToEnd: (options?: { animated?: boolean }) => void;
|
|
15
|
+
scrollTo?: (options: { y: number; animated?: boolean }) => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type ConversationContextValue = {
|
|
19
|
+
isAtBottom: boolean;
|
|
20
|
+
scrollToBottom: () => void;
|
|
21
|
+
scrollRef: RefObject<ConversationScroller | null>;
|
|
22
|
+
/** `bottomInset` and `inverted`, as `isScrolledToBottom`. */
|
|
23
|
+
onScroll: (event: NativeScrollEvent, bottomInset?: number, inverted?: boolean) => void;
|
|
24
|
+
onUserScroll: () => void;
|
|
25
|
+
followContent: () => void;
|
|
26
|
+
/** The height of the `ConversationDock` under the thread, 0 without one. */
|
|
27
|
+
dockHeight: number;
|
|
28
|
+
setDockHeight: (height: number) => void;
|
|
29
|
+
/**
|
|
30
|
+
* A message's hold menu over the thread. Its window takes the focus on
|
|
31
|
+
* Android, as it opens and again as it closes, and the keyboard handler of
|
|
32
|
+
* `ConversationList` would scroll the thread to its end each time. The list
|
|
33
|
+
* ignores the keyboard from `openOverlay` until the close has settled:
|
|
34
|
+
* `closeOverlay` marks it `closing`, and `settleOverlay` ends that on the
|
|
35
|
+
* close's last keyboard event (Android), the menu's dismiss (iOS) or the
|
|
36
|
+
* reader's next drag. Android gives no dismiss callback for a `Modal`.
|
|
37
|
+
*/
|
|
38
|
+
overlay: "none" | "open" | "closing";
|
|
39
|
+
openOverlay: () => void;
|
|
40
|
+
closeOverlay: () => void;
|
|
41
|
+
settleOverlay: () => void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const ConversationContext = createContext<ConversationContextValue | null>(null);
|
|
45
|
+
|
|
46
|
+
/** `isAtBottom` and `scrollToBottom` of the enclosing `Conversation`, for a custom scroll button. */
|
|
47
|
+
export function useConversation() {
|
|
48
|
+
const context = useContext(ConversationContext);
|
|
49
|
+
if (!context) throw new Error("Conversation parts must be used within Conversation");
|
|
50
|
+
return context;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The enclosing `Conversation`, or null for a message drawn outside one. */
|
|
54
|
+
export function useOptionalConversation() {
|
|
55
|
+
return useContext(ConversationContext);
|
|
56
|
+
}
|
package/src/ai/conversation.tsx
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createContext,
|
|
3
|
-
isValidElement,
|
|
4
|
-
useContext,
|
|
5
|
-
useRef,
|
|
6
|
-
useState,
|
|
7
|
-
type ComponentType,
|
|
8
|
-
type ReactNode,
|
|
9
|
-
type RefObject,
|
|
10
|
-
} from "react";
|
|
1
|
+
import { isValidElement, useRef, useState, type ComponentType, type ReactNode } from "react";
|
|
11
2
|
import {
|
|
12
3
|
FlatList,
|
|
13
4
|
Platform,
|
|
@@ -38,6 +29,7 @@ import { tokens } from "../tokens";
|
|
|
38
29
|
import { useReduceMotion } from "../useReduceMotion";
|
|
39
30
|
import { useScheme } from "../useScheme";
|
|
40
31
|
import { chatTextSize } from "./chat-text";
|
|
32
|
+
import { ConversationContext, useConversation, type ConversationContextValue, type ConversationScroller } from "./conversation-context";
|
|
41
33
|
|
|
42
34
|
/** Within this many points of the end, the thread counts as at the bottom and keeps following new content. */
|
|
43
35
|
export const AT_BOTTOM_THRESHOLD = 70;
|
|
@@ -59,38 +51,7 @@ export function isScrolledToBottom(
|
|
|
59
51
|
return contentSize.height + bottomInset - layoutMeasurement.height - contentOffset.y <= AT_BOTTOM_THRESHOLD;
|
|
60
52
|
}
|
|
61
53
|
|
|
62
|
-
|
|
63
|
-
* What the thread scrolls with: the `ScrollView` of `ConversationContent` or
|
|
64
|
-
* the `FlatList` of `ConversationList`. `scrollToEnd` goes to the newest
|
|
65
|
-
* message, which is offset 0 in an inverted list. `scrollTo` goes to an
|
|
66
|
-
* offset; only the `ScrollView` has it, so call it as `scrollTo?.(...)`.
|
|
67
|
-
*/
|
|
68
|
-
export type ConversationScroller = {
|
|
69
|
-
scrollToEnd: (options?: { animated?: boolean }) => void;
|
|
70
|
-
scrollTo?: (options: { y: number; animated?: boolean }) => void;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
type ConversationContextValue = {
|
|
74
|
-
isAtBottom: boolean;
|
|
75
|
-
scrollToBottom: () => void;
|
|
76
|
-
scrollRef: RefObject<ConversationScroller | null>;
|
|
77
|
-
/** `bottomInset` and `inverted`, as `isScrolledToBottom`. */
|
|
78
|
-
onScroll: (event: NativeScrollEvent, bottomInset?: number, inverted?: boolean) => void;
|
|
79
|
-
onUserScroll: () => void;
|
|
80
|
-
followContent: () => void;
|
|
81
|
-
/** The height of the `ConversationDock` under the thread, 0 without one. */
|
|
82
|
-
dockHeight: number;
|
|
83
|
-
setDockHeight: (height: number) => void;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const ConversationContext = createContext<ConversationContextValue | null>(null);
|
|
87
|
-
|
|
88
|
-
/** `isAtBottom` and `scrollToBottom` of the enclosing `Conversation`, for a custom scroll button. */
|
|
89
|
-
export function useConversation() {
|
|
90
|
-
const context = useContext(ConversationContext);
|
|
91
|
-
if (!context) throw new Error("Conversation parts must be used within Conversation");
|
|
92
|
-
return context;
|
|
93
|
-
}
|
|
54
|
+
export { useConversation, useOptionalConversation, type ConversationScroller } from "./conversation-context";
|
|
94
55
|
|
|
95
56
|
export type ConversationProps = ViewProps & { className?: string };
|
|
96
57
|
|
|
@@ -108,6 +69,7 @@ export function Conversation({ className, ...props }: ConversationProps) {
|
|
|
108
69
|
const autoScrolling = useRef(false);
|
|
109
70
|
const [isAtBottom, setIsAtBottom] = useState(true);
|
|
110
71
|
const [dockHeight, setDockHeight] = useState(0);
|
|
72
|
+
const [overlay, setOverlay] = useState<ConversationContextValue["overlay"]>("none");
|
|
111
73
|
|
|
112
74
|
const update = (next: boolean) => {
|
|
113
75
|
atBottom.current = next;
|
|
@@ -137,6 +99,10 @@ export function Conversation({ className, ...props }: ConversationProps) {
|
|
|
137
99
|
},
|
|
138
100
|
dockHeight,
|
|
139
101
|
setDockHeight,
|
|
102
|
+
overlay,
|
|
103
|
+
openOverlay: () => setOverlay("open"),
|
|
104
|
+
closeOverlay: () => setOverlay("closing"),
|
|
105
|
+
settleOverlay: () => setOverlay((current) => (current === "closing" ? "none" : current)),
|
|
140
106
|
};
|
|
141
107
|
|
|
142
108
|
return (
|
|
@@ -201,6 +167,7 @@ function useConversationScroll<P extends ConversationScrollHandlers>(
|
|
|
201
167
|
},
|
|
202
168
|
onScrollBeginDrag: (event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
203
169
|
conversation.onUserScroll();
|
|
170
|
+
conversation.settleOverlay();
|
|
204
171
|
onScrollBeginDrag?.(event);
|
|
205
172
|
},
|
|
206
173
|
// The app's handler first, so a scroller that measures itself (`ConversationList`) has the new size to follow.
|
|
@@ -346,6 +313,7 @@ export function ConversationList<T>({
|
|
|
346
313
|
<keyboard.ChatScrollView
|
|
347
314
|
{...scrollProps}
|
|
348
315
|
inverted={inverted}
|
|
316
|
+
freeze={conversation.overlay !== "none"}
|
|
349
317
|
// The composer pads the bottom inset, which the keyboard covers: the content rises by the rest.
|
|
350
318
|
bottomOffset={insets.bottom}
|
|
351
319
|
onInsetChange={(inset) => {
|
|
@@ -353,6 +321,8 @@ export function ConversationList<T>({
|
|
|
353
321
|
}}
|
|
354
322
|
onKeyboardMove={(moving) => {
|
|
355
323
|
size.current.keyboardMoving = moving;
|
|
324
|
+
// The closing menu's window hands the focus back with a keyboard event: frozen until it ends.
|
|
325
|
+
if (!moving) conversation.settleOverlay();
|
|
356
326
|
}}
|
|
357
327
|
/>
|
|
358
328
|
)
|