@vvedantb/native-ui 0.28.0 → 0.30.0

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.
@@ -1,7 +1,7 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import * as Slot from "@rn-primitives/slot";
3
3
  import { createContext, Fragment, useContext, useEffect, useState } from "react";
4
- import { Modal, Pressable, StyleSheet } from "react-native";
4
+ import { Pressable, StyleSheet } from "react-native";
5
5
  import Animated, { runOnJS, useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated";
6
6
  import { useSafeAreaInsets } from "react-native-safe-area-context";
7
7
  import { palette } from "../palette";
@@ -11,6 +11,7 @@ import { useReduceMotion } from "../useReduceMotion";
11
11
  import { useScheme } from "../useScheme";
12
12
  import { SheetDragProvider, SheetGestureContext, slideSheetIn, slideSheetOut, useSheetBackdropStyle, useSheetDragState } from "./sheet-drag";
13
13
  import { useControllableState } from "./use-controllable-state";
14
+ import { WindowLayer } from "./window-layer";
14
15
  const OverlayContext = createContext(null);
15
16
  export function useOverlay(part) {
16
17
  const context = useContext(OverlayContext);
@@ -150,25 +151,26 @@ export function OverlayClose(props) {
150
151
  }
151
152
 
152
153
  /**
153
- * RN `Modal` with a tinted backdrop, safe-area padding and keyboard avoidance
154
+ * A `WindowLayer` (in the app's window under `WindowLayerHost`, so a chat
155
+ * under it keeps its place; else a `Modal`) with a tinted backdrop, safe-area padding and keyboard avoidance
154
156
  * (`KeyboardAvoidingView`: frame by frame with `KeyboardControllerProvider`).
155
157
  * `placement="center"` centres the child (dialogs) inside the insets on every
156
158
  * side: the backdrop fades while the panel scales from 0.96 and fades in (200
157
- * ms on open, 150 ms on close), and the `Modal` stays up until it is out of
159
+ * ms on open, 150 ms on close), and the layer stays up until it is out of
158
160
  * sight. `"bottom"` pins it to the bottom
159
161
  * edge (sheets), which pad their own bottom inset: the sheet slides up on
160
162
  * open and back down on close, however it closes, and may be dragged down to
161
163
  * close (`SheetDragView`); the backdrop fades with the sheet's position. The
162
- * `Modal` stays up until the sheet is out of sight. The backdrop closes the
164
+ * layer stays up until the sheet is out of sight. The backdrop closes the
163
165
  * overlay only when `dismissible`; Android back and the VoiceOver escape
164
166
  * gesture always close it, as Escape does on the web. With Reduce Motion on,
165
167
  * overlays appear and leave in place. With the Gesture Handler parts
166
168
  * (`GestureHandlerProvider`), the content sits in its own root view: on
167
169
  * Android a `Modal` is a separate root, and without one there every gesture
168
- * in it silently does nothing.
170
+ * in it silently does nothing (the fallback `Modal`).
169
171
  */
170
172
  export function OverlayModal(t0) {
171
- const $ = _c(20);
173
+ const $ = _c(22);
172
174
  const {
173
175
  part,
174
176
  placement,
@@ -176,10 +178,11 @@ export function OverlayModal(t0) {
176
178
  onShow,
177
179
  children
178
180
  } = t0;
181
+ const overlay = useOverlay(part);
179
182
  const {
180
183
  open,
181
184
  setOpen
182
- } = useOverlay(part);
185
+ } = overlay;
183
186
  const reduceMotion = useReduceMotion();
184
187
  const slides = placement === "bottom" && !reduceMotion;
185
188
  const scales = placement === "center" && !reduceMotion;
@@ -200,6 +203,9 @@ export function OverlayModal(t0) {
200
203
  }
201
204
  const close = t1;
202
205
  const ModalRoot = useContext(SheetGestureContext)?.ModalRoot ?? Fragment;
206
+ if (!shown) {
207
+ return null;
208
+ }
203
209
  let t2;
204
210
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
205
211
  t2 = () => setShown(false);
@@ -232,20 +238,28 @@ export function OverlayModal(t0) {
232
238
  t4 = $[14];
233
239
  }
234
240
  let t5;
235
- if ($[15] !== close || $[16] !== onShow || $[17] !== shown || $[18] !== t4) {
236
- t5 = <Modal visible={shown} transparent={true} animationType="none" statusBarTranslucent={true} onRequestClose={close} onShow={onShow}>{t4}</Modal>;
237
- $[15] = close;
238
- $[16] = onShow;
239
- $[17] = shown;
240
- $[18] = t4;
241
- $[19] = t5;
241
+ if ($[15] !== overlay || $[16] !== t4) {
242
+ t5 = <OverlayContext.Provider value={overlay}>{t4}</OverlayContext.Provider>;
243
+ $[15] = overlay;
244
+ $[16] = t4;
245
+ $[17] = t5;
246
+ } else {
247
+ t5 = $[17];
248
+ }
249
+ let t6;
250
+ if ($[18] !== close || $[19] !== onShow || $[20] !== t5) {
251
+ t6 = <WindowLayer onRequestClose={close} onShow={onShow} fade={false}>{t5}</WindowLayer>;
252
+ $[18] = close;
253
+ $[19] = onShow;
254
+ $[20] = t5;
255
+ $[21] = t6;
242
256
  } else {
243
- t5 = $[19];
257
+ t6 = $[21];
244
258
  }
245
- return t5;
259
+ return t6;
246
260
  }
247
261
 
248
- /** The `Modal`'s content: mounted with each opening, so a sheet starts below the screen (or at rest). */
262
+ /** The layer's content: mounted with each opening, so a sheet starts below the screen (or at rest). */
249
263
  function OverlayModalBody(t0) {
250
264
  const $ = _c(48);
251
265
  const {
@@ -29,7 +29,16 @@ const upsert = layer => {
29
29
  layers = at === -1 ? [...layers, layer] : layers.map((item, index) => index === at ? layer : item);
30
30
  emit();
31
31
  };
32
+
33
+ // Layers that have called `onShow` once, as a Modal does once per opening.
34
+ const announced = new Set();
35
+ const showOnce = layer => {
36
+ if (announced.has(layer.id)) return;
37
+ announced.add(layer.id);
38
+ layer.onShow?.();
39
+ };
32
40
  const remove = id => {
41
+ announced.delete(id);
33
42
  if (!layers.some(item => item.id === id)) return;
34
43
  layers = layers.filter(item => item.id !== id);
35
44
  emit();
@@ -99,7 +108,7 @@ export function WindowLayerHost(t0) {
99
108
  if ($[6] !== shown) {
100
109
  let t7;
101
110
  if ($[8] === Symbol.for("react.memo_cache_sentinel")) {
102
- t7 = layer => <Animated.View key={layer.id} entering={enter} exiting={exit} style={StyleSheet.absoluteFill} pointerEvents="box-none">{layer.node}</Animated.View>;
111
+ t7 = layer => <Animated.View key={layer.id} entering={layer.fade ? enter : undefined} exiting={layer.fade ? exit : undefined} onLayout={() => showOnce(layer)} style={StyleSheet.absoluteFill} pointerEvents="box-none">{layer.node}</Animated.View>;
103
112
  $[8] = t7;
104
113
  } else {
105
114
  t7 = $[8];
@@ -127,7 +136,8 @@ export function WindowLayerHost(t0) {
127
136
  * `WindowLayerHost` when the app has one, else a transparent `Modal` (with
128
137
  * `onDismiss` there). Render it only while the overlay shows. Android back
129
138
  * calls `onRequestClose`. Its children keep the providers above the host, not
130
- * those between the host and this element.
139
+ * those between the host and this element: an overlay with its own context
140
+ * (a Dialog's open state, a menu's) provides it again inside.
131
141
  */
132
142
  function _temp2() {
133
143
  return EMPTY;
@@ -136,68 +146,76 @@ function _temp() {
136
146
  return layers;
137
147
  }
138
148
  export function WindowLayer(t0) {
139
- const $ = _c(14);
149
+ const $ = _c(17);
140
150
  const {
141
151
  onRequestClose,
142
152
  onDismiss,
153
+ onShow,
154
+ fade: t1,
143
155
  children
144
156
  } = t0;
157
+ const fade = t1 === undefined ? true : t1;
145
158
  const id = useId();
146
159
  const reduceMotion = useReduceMotion();
147
160
  const hosted = useSyncExternalStore(subscribe, _temp3, _temp4);
148
- let t1;
149
161
  let t2;
150
- if ($[0] !== children || $[1] !== hosted || $[2] !== id || $[3] !== onRequestClose) {
151
- t1 = () => {
162
+ let t3;
163
+ if ($[0] !== children || $[1] !== fade || $[2] !== hosted || $[3] !== id || $[4] !== onRequestClose || $[5] !== onShow) {
164
+ t2 = () => {
152
165
  if (hosted) {
153
166
  upsert({
154
167
  id,
155
168
  node: children,
156
- onRequestClose
169
+ onRequestClose,
170
+ onShow,
171
+ fade
157
172
  });
158
173
  }
159
174
  };
160
- t2 = [hosted, id, children, onRequestClose];
175
+ t3 = [hosted, id, children, onRequestClose, onShow, fade];
161
176
  $[0] = children;
162
- $[1] = hosted;
163
- $[2] = id;
164
- $[3] = onRequestClose;
165
- $[4] = t1;
166
- $[5] = t2;
177
+ $[1] = fade;
178
+ $[2] = hosted;
179
+ $[3] = id;
180
+ $[4] = onRequestClose;
181
+ $[5] = onShow;
182
+ $[6] = t2;
183
+ $[7] = t3;
167
184
  } else {
168
- t1 = $[4];
169
- t2 = $[5];
185
+ t2 = $[6];
186
+ t3 = $[7];
170
187
  }
171
- useLayoutEffect(t1, t2);
172
- let t3;
188
+ useLayoutEffect(t2, t3);
173
189
  let t4;
174
- if ($[6] !== id) {
175
- t3 = () => () => remove(id);
176
- t4 = [id];
177
- $[6] = id;
178
- $[7] = t3;
179
- $[8] = t4;
190
+ let t5;
191
+ if ($[8] !== id) {
192
+ t4 = () => () => remove(id);
193
+ t5 = [id];
194
+ $[8] = id;
195
+ $[9] = t4;
196
+ $[10] = t5;
180
197
  } else {
181
- t3 = $[7];
182
- t4 = $[8];
198
+ t4 = $[9];
199
+ t5 = $[10];
183
200
  }
184
- useLayoutEffect(t3, t4);
201
+ useLayoutEffect(t4, t5);
185
202
  if (hosted) {
186
203
  return null;
187
204
  }
188
- const t5 = reduceMotion ? "none" : "fade";
189
- let t6;
190
- if ($[9] !== children || $[10] !== onDismiss || $[11] !== onRequestClose || $[12] !== t5) {
191
- t6 = <Modal visible={true} transparent={true} statusBarTranslucent={true} animationType={t5} onRequestClose={onRequestClose} onDismiss={onDismiss}>{children}</Modal>;
192
- $[9] = children;
193
- $[10] = onDismiss;
194
- $[11] = onRequestClose;
195
- $[12] = t5;
196
- $[13] = t6;
205
+ const t6 = fade && !reduceMotion ? "fade" : "none";
206
+ let t7;
207
+ if ($[11] !== children || $[12] !== onDismiss || $[13] !== onRequestClose || $[14] !== onShow || $[15] !== t6) {
208
+ t7 = <Modal visible={true} transparent={true} statusBarTranslucent={true} animationType={t6} onRequestClose={onRequestClose} onDismiss={onDismiss} onShow={onShow}>{children}</Modal>;
209
+ $[11] = children;
210
+ $[12] = onDismiss;
211
+ $[13] = onRequestClose;
212
+ $[14] = onShow;
213
+ $[15] = t6;
214
+ $[16] = t7;
197
215
  } else {
198
- t6 = $[13];
216
+ t7 = $[16];
199
217
  }
200
- return t6;
218
+ return t7;
201
219
  }
202
220
  function _temp4() {
203
221
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
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",
@@ -1,7 +1,6 @@
1
1
  import * as Slot from "@rn-primitives/slot";
2
2
  import { createContext, useContext, useEffect, useRef, useState, type ComponentType, type ReactNode } from "react";
3
3
  import {
4
- Modal,
5
4
  Pressable,
6
5
  ScrollView,
7
6
  StyleSheet,
@@ -26,6 +25,7 @@ import { tokens } from "./tokens";
26
25
  import { useReduceMotion } from "./useReduceMotion";
27
26
  import { useScheme } from "./useScheme";
28
27
  import { useControllableState } from "./primitives/use-controllable-state";
28
+ import { WindowLayer } from "./primitives/window-layer";
29
29
 
30
30
  /**
31
31
  * Sizes from the web menu classes (`_menu-classes.ts`): content `min-w-[12rem]
@@ -193,7 +193,8 @@ export type DropdownMenuContentProps = ViewProps & {
193
193
  };
194
194
 
195
195
  /**
196
- * The menu in a transparent RN `Modal`: the opaque overlay surface of
196
+ * The menu in a `WindowLayer` (in the app's window under `WindowLayerHost`, so
197
+ * a chat under it keeps its place; else a transparent `Modal`): the opaque overlay surface of
197
198
  * `Dialog` and `Sheet` (`FloatingSurface` without blur, with the floating
198
199
  * shadow) at radius 16, at least 192 wide, `p-1.5`, scrolling when taller than
199
200
  * the room. A tap outside, Android back and the VoiceOver escape gesture close
@@ -209,7 +210,8 @@ export function DropdownMenuContent({
209
210
  children,
210
211
  ...props
211
212
  }: DropdownMenuContentProps) {
212
- const { open, setOpen, trigger } = useMenu("DropdownMenuContent");
213
+ const menu = useMenu("DropdownMenuContent");
214
+ const { open, setOpen, trigger } = menu;
213
215
  const window = useWindowDimensions();
214
216
  const insets = useSafeAreaInsets();
215
217
  const reduceMotion = useReduceMotion();
@@ -265,16 +267,14 @@ export function DropdownMenuContent({
265
267
  // The scale grows from the corner nearest the trigger: its edge on the
266
268
  // trigger's (`align`), and the top, or the bottom when the menu flipped above.
267
269
  const transformOrigin = `${align === "end" ? 100 : 0}% ${position.bottom === undefined ? 0 : 100}%`;
270
+ if (!shown) return null;
268
271
  return (
269
- <Modal
270
- visible={shown}
271
- transparent
272
- animationType="none"
273
- statusBarTranslucent
274
- onRequestClose={close}
275
- >
272
+ // The panel runs its own motion, so no fade; the items need the menu's
273
+ // context, which a layer loses, so it is provided again.
274
+ <WindowLayer onRequestClose={close} fade={false}>
275
+ <MenuContext.Provider value={menu}>
276
276
  {/* A full-size host, as `OverlayModal` has: without it the backdrop and the
277
- menu are bare Modal children with no box to size or hit-test against,
277
+ menu are bare layer children with no box to size or hit-test against,
278
278
  and the menu can stack under tab bars and glass layers. */}
279
279
  <View style={{ flex: 1 }} pointerEvents="box-none">
280
280
  <Pressable
@@ -314,7 +314,8 @@ export function DropdownMenuContent({
314
314
  </Animated.View>
315
315
  </View>
316
316
  </View>
317
- </Modal>
317
+ </MenuContext.Provider>
318
+ </WindowLayer>
318
319
  );
319
320
  }
320
321
 
@@ -9,10 +9,13 @@ import type { NativeScrollEvent } from "react-native";
9
9
  * the `FlatList` of `ConversationList`. `scrollToEnd` goes to the newest
10
10
  * message, which is offset 0 in an inverted list. `scrollTo` goes to an
11
11
  * offset; only the `ScrollView` has it, so call it as `scrollTo?.(...)`.
12
+ * `scrollToIndex` brings one row of the list's `data` into view (a find in
13
+ * the thread); only the `FlatList` has it. `viewPosition` 0.5 centres it.
12
14
  */
13
15
  export type ConversationScroller = {
14
16
  scrollToEnd: (options?: { animated?: boolean }) => void;
15
17
  scrollTo?: (options: { y: number; animated?: boolean }) => void;
18
+ scrollToIndex?: (options: { index: number; animated?: boolean; viewPosition?: number }) => void;
16
19
  };
17
20
 
18
21
  export type ConversationContextValue = {
@@ -255,6 +255,9 @@ export function ConversationList<T>({
255
255
  // when inverted); `keyboardMoving` is true while the keyboard scrolls the
256
256
  // list, which is not the reader leaving the bottom.
257
257
  const size = useRef({ content: 0, view: 0, inset: 0, keyboardMoving: false });
258
+ // The list, and the row `scrollToIndex` last asked for, for its retry when that row was not drawn.
259
+ const listRef = useRef<FlatList<T> | null>(null);
260
+ const indexTarget = useRef<{ index: number; animated?: boolean; viewPosition?: number; tries: number } | null>(null);
258
261
  // Not inverted: hidden until the first jump to the end (see above). An
259
262
  // empty list (a spinner, an empty state) shows at once.
260
263
  const hasRows = (props.data?.length ?? 0) > 0;
@@ -328,9 +331,27 @@ export function ConversationList<T>({
328
331
  )
329
332
  : props.renderScrollComponent
330
333
  }
334
+ // A row the list has not drawn yet (it draws only those near the view):
335
+ // go to its estimated offset, which draws it, then to the row itself.
336
+ onScrollToIndexFailed={
337
+ props.onScrollToIndexFailed ??
338
+ ((info) => {
339
+ const list = listRef.current;
340
+ const target = indexTarget.current;
341
+ if (!list || !target || target.index !== info.index || target.tries >= 3) return;
342
+ target.tries += 1;
343
+ list.scrollToOffset({ offset: info.averageItemLength * info.index, animated: false });
344
+ requestAnimationFrame(() => list.scrollToIndex(target));
345
+ })
346
+ }
331
347
  ref={(list: FlatList<T> | null) => {
348
+ listRef.current = list;
332
349
  conversation.scrollRef.current = list && {
333
350
  scrollToEnd: (options) => list.scrollToOffset({ offset: end(), animated: options?.animated }),
351
+ scrollToIndex: (options) => {
352
+ indexTarget.current = { ...options, tries: 0 };
353
+ list.scrollToIndex(options);
354
+ },
334
355
  };
335
356
  }}
336
357
  />