@vvedantb/native-ui 0.36.1 → 0.37.1

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/README.md CHANGED
@@ -715,6 +715,34 @@ A small menu anchored to its trigger, the native counterpart of the web `Dropdow
715
715
 
716
716
  Sizes come from the web menu classes: content at least 192 wide, `p-1.5`, radius 16, on the opaque overlay surface of `Dialog` and `Sheet` (`FloatingSurface` without blur); items `px-2.5 py-2` at radius 8, `text-sm` with a 16 icon, at least 44 high; radio items `pl-8` with a 16 check; the label `text-xs` semibold, uppercase, `tracking-widest`, muted; the separator a 1-high `separator` line. Items are `palette.overlayForeground` (`palette.danger` when `destructive`) and take the `default` fill while pressed. A tap outside, Android back and the VoiceOver escape gesture close the menu. Roles: `menu`, `menuitem`, `radio` (with `checked`), `header`. With larger text, items are `text-lg` and 56 high. No sub-menus, checkbox items, shortcuts or `align="center"`.
717
717
 
718
+ ## Shared element transitions (Unreleased)
719
+
720
+ A view with the same tag on two screens moves and resizes from one to the other (Reanimated 4.8 `SharedTransition`; needs `react-native-reanimated` 4.8 or later). On the root and on `@vvedantb/native-ui/primitives`. Use it between native-stack screens, such as a list row and its detail.
721
+
722
+ ```tsx
723
+ import { SharedTransitionView, SharedTransitionText } from "@vvedantb/native-ui";
724
+
725
+ // List screen
726
+ <SharedTransitionView tag={`card-${item.id}`} style={{ height: 72, borderRadius: 16 }}>
727
+ <SharedTransitionText tag={`title-${item.id}`}>{item.title}</SharedTransitionText>
728
+ </SharedTransitionView>
729
+
730
+ // Detail screen: the same tags
731
+ <SharedTransitionView tag={`card-${id}`} style={{ height: 240, borderRadius: 16 }}>
732
+ <SharedTransitionText tag={`title-${id}`}>{title}</SharedTransitionText>
733
+ </SharedTransitionView>
734
+ ```
735
+
736
+ | Export | Use |
737
+ | --- | --- |
738
+ | `SharedTransitionView`, `SharedTransitionText` | `tag` (required), `style`, `testID` and the other `View` or `Text` props. An `Animated.View` or `Animated.Text` with `sharedTransitionTag` and `sharedTransitionStyle` set. Use `style`, not `className`: NativeWind does not style Reanimated views. |
739
+ | `useSharedTransitionStyle()` | `DEFAULT_SHARED_TRANSITION`, or `undefined` with Reduce Motion on. For your own `Animated.*` view: pass `sharedTransitionTag={sharedTransitionTag(id)}` and `sharedTransitionStyle={style}` only when it is defined. |
740
+ | `sharedTransitionTag(id)` | The tag, prefixed `nui:`. Use it on both screens. |
741
+ | `DEFAULT_SHARED_TRANSITION` | `SharedTransition.springify(280).dampingRatio(1).reduceMotion(ReduceMotion.System).fadeContent({ fadeOut: { end: 0.55 }, fadeIn: { duration: 180 } })`. |
742
+ | `createSharedTransition({ duration, dampingRatio, fadeContent })` | A new builder with these changed. `fadeContent: false` leaves children as they are. It always applies `ReduceMotion.System`. |
743
+
744
+ The spring is critically damped: no bounce. `fadeContent` fades the children of the source view out by 55% of the move and the children of the target in over 180 ms, so text does not squash while the box resizes. The two views may have different children. With Reduce Motion on, the wrappers render a plain `View` or `Text` with no tag, and the screens change as without a shared transition. Tag only one view per id on each screen.
745
+
718
746
  ## BulkActionBar (Unreleased)
719
747
 
720
748
  The floating selection bar, as the web `BulkActionBar`. On `/primitives` and the root. Put it last in a full-size container: it sits at the bottom, centred.
@@ -113,12 +113,68 @@ export function DropdownMenu(t0) {
113
113
  export function isMeasuredRect(rect) {
114
114
  return rect.width > 0 && rect.height > 0;
115
115
  }
116
+ /** The wrapper shrink-wraps the trigger in a flex row (such as the PromptInput `leading` slot). */
117
+ const TRIGGER_HOST = {
118
+ alignSelf: "flex-start"
119
+ };
120
+
121
+ /** Frames to wait for layout to settle, when a view reads empty. */
122
+ const MEASURE_RETRIES = 2;
123
+ /** Last-resort trigger size when a press has a touch point but no `onLayout` size and no style size: a 44 point touch target. */
124
+ const FALLBACK_SIZE = {
125
+ width: 44,
126
+ height: 44
127
+ };
128
+
129
+ /** Numeric `width` and `height` of a plain style object, such as `{ width: 44, height: 44 }`; `undefined` for a function, array or missing size. */
130
+ export function sizeFromStyle(style) {
131
+ if (!style || typeof style !== "object" || Array.isArray(style)) return undefined;
132
+ if (!("width" in style) || !("height" in style)) return undefined;
133
+ const {
134
+ width,
135
+ height
136
+ } = style;
137
+ if (typeof width !== "number" || typeof height !== "number") return undefined;
138
+ return width > 0 && height > 0 ? {
139
+ width,
140
+ height
141
+ } : undefined;
142
+ }
143
+
144
+ /**
145
+ * The trigger's box from a press: the press point in window coordinates
146
+ * (`pageX`, `pageY`) less its point in the view (`locationX`, `locationY`) is
147
+ * the view's origin, and `size` is its laid-out size. `null` without a size or a usable touch point.
148
+ */
149
+ export function triggerRectFromPress(event, size) {
150
+ const point = event?.nativeEvent;
151
+ if (!size || !point) return null;
152
+ const {
153
+ pageX,
154
+ pageY,
155
+ locationX,
156
+ locationY
157
+ } = point;
158
+ if (typeof pageX !== "number" || !Number.isFinite(pageX) || typeof pageY !== "number" || !Number.isFinite(pageY) || typeof locationX !== "number" || !Number.isFinite(locationX) || typeof locationY !== "number" || !Number.isFinite(locationY)) {
159
+ return null;
160
+ }
161
+ const rect = {
162
+ x: pageX - locationX,
163
+ y: pageY - locationY,
164
+ width: size.width,
165
+ height: size.height
166
+ };
167
+ return isMeasuredRect(rect) ? rect : null;
168
+ }
169
+
116
170
  /**
117
171
  * The view's box in window coordinates, or `null` when it cannot be measured.
118
172
  * `measureInWindow` first; when it returns an empty box, `measure` (its
119
- * `pageX` and `pageY` are window coordinates too) as a second reading.
173
+ * `pageX` and `pageY` are window coordinates too) as a second reading. When
174
+ * both are empty, it tries again on the next frame, up to `retries` times: a
175
+ * view under a keyboard-lifted or avoiding parent may not be laid out yet.
120
176
  */
121
- export function measureAnchor(view, done) {
177
+ export function measureAnchor(view, done, retries = MEASURE_RETRIES) {
122
178
  view.measureInWindow((x, y, width, height) => {
123
179
  const rect = {
124
180
  x,
@@ -134,29 +190,38 @@ export function measureAnchor(view, done) {
134
190
  width: w,
135
191
  height: h
136
192
  };
137
- done(isMeasuredRect(second) ? second : null);
193
+ if (isMeasuredRect(second)) return done(second);
194
+ if (retries <= 0) return done(null);
195
+ requestAnimationFrame(() => measureAnchor(view, done, retries - 1));
138
196
  });
139
197
  });
140
198
  }
141
199
 
142
200
  /**
143
- * Opens the menu, or closes it when open. On press it measures itself
144
- * (`measureInWindow`) and the menu opens under it. Reports `expanded`. A
145
- * caller `ref` is kept: the menu's own handle never replaces it.
201
+ * Opens the menu, or closes it when open. On press it measures its wrapper
202
+ * (`measureInWindow`) and the menu opens under it. The wrapper is a
203
+ * `collapsable={false}` view, so Android keeps a native view to measure. On
204
+ * press, the box comes at once from the press point and a size (`onLayout`,
205
+ * then a numeric `style` width and height, then 44), and the menu opens; a live
206
+ * measure then refines it. When the press has no touch point and every measure
207
+ * reads empty, the last good box is used. Reports `expanded`. A caller `ref` is
208
+ * kept: it gets the wrapper, as the menu's own handle does.
146
209
  */
147
210
  export function DropdownMenuTrigger(t0) {
148
- const $ = _c(22);
211
+ const $ = _c(33);
149
212
  let accessibilityState;
150
213
  let asChild;
151
214
  let callerRef;
152
215
  let onPress;
153
216
  let props;
217
+ let style;
154
218
  if ($[0] !== t0) {
155
219
  ({
156
220
  asChild,
157
221
  onPress,
158
222
  accessibilityState,
159
223
  ref: callerRef,
224
+ style,
160
225
  ...props
161
226
  } = t0);
162
227
  $[0] = t0;
@@ -165,12 +230,14 @@ export function DropdownMenuTrigger(t0) {
165
230
  $[3] = callerRef;
166
231
  $[4] = onPress;
167
232
  $[5] = props;
233
+ $[6] = style;
168
234
  } else {
169
235
  accessibilityState = $[1];
170
236
  asChild = $[2];
171
237
  callerRef = $[3];
172
238
  onPress = $[4];
173
239
  props = $[5];
240
+ style = $[6];
174
241
  }
175
242
  const {
176
243
  open,
@@ -178,10 +245,21 @@ export function DropdownMenuTrigger(t0) {
178
245
  setTrigger
179
246
  } = useMenu("DropdownMenuTrigger");
180
247
  const ref = useRef(null);
181
- const Component = asChild ? Slot.Pressable : Pressable;
248
+ const layoutSizeRef = useRef(undefined);
249
+ const lastRectRef = useRef(undefined);
182
250
  let t1;
183
- if ($[6] !== callerRef) {
184
- t1 = view => {
251
+ if ($[7] !== style) {
252
+ t1 = sizeFromStyle(style);
253
+ $[7] = style;
254
+ $[8] = t1;
255
+ } else {
256
+ t1 = $[8];
257
+ }
258
+ const styleSize = t1;
259
+ const Component = asChild ? Slot.Pressable : Pressable;
260
+ let t2;
261
+ if ($[9] !== callerRef) {
262
+ t2 = view => {
185
263
  ref.current = view;
186
264
  if (typeof callerRef === "function") {
187
265
  callerRef(view);
@@ -191,69 +269,129 @@ export function DropdownMenuTrigger(t0) {
191
269
  }
192
270
  }
193
271
  };
194
- $[6] = callerRef;
195
- $[7] = t1;
272
+ $[9] = callerRef;
273
+ $[10] = t2;
196
274
  } else {
197
- t1 = $[7];
275
+ t2 = $[10];
198
276
  }
199
- let t2;
200
- if ($[8] !== accessibilityState || $[9] !== open) {
201
- t2 = {
277
+ let t3;
278
+ if ($[11] !== styleSize) {
279
+ t3 = styleSize ? {
280
+ ...TRIGGER_HOST,
281
+ ...styleSize
282
+ } : TRIGGER_HOST;
283
+ $[11] = styleSize;
284
+ $[12] = t3;
285
+ } else {
286
+ t3 = $[12];
287
+ }
288
+ let t4;
289
+ if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
290
+ t4 = event => {
291
+ const view_0 = ref.current;
292
+ if (!view_0) {
293
+ return;
294
+ }
295
+ const {
296
+ width,
297
+ height
298
+ } = event.nativeEvent.layout;
299
+ if (width > 0 && height > 0) {
300
+ layoutSizeRef.current = {
301
+ width,
302
+ height
303
+ };
304
+ }
305
+ measureAnchor(view_0, rect => {
306
+ if (rect) {
307
+ lastRectRef.current = rect;
308
+ }
309
+ });
310
+ };
311
+ $[13] = t4;
312
+ } else {
313
+ t4 = $[13];
314
+ }
315
+ let t5;
316
+ if ($[14] !== accessibilityState || $[15] !== open) {
317
+ t5 = {
202
318
  ...accessibilityState,
203
319
  expanded: open
204
320
  };
205
- $[8] = accessibilityState;
206
- $[9] = open;
207
- $[10] = t2;
321
+ $[14] = accessibilityState;
322
+ $[15] = open;
323
+ $[16] = t5;
208
324
  } else {
209
- t2 = $[10];
325
+ t5 = $[16];
210
326
  }
211
- let t3;
212
- if ($[11] !== onPress || $[12] !== open || $[13] !== setOpen || $[14] !== setTrigger) {
213
- t3 = event => {
214
- onPress?.(event);
327
+ let t6;
328
+ if ($[17] !== onPress || $[18] !== open || $[19] !== setOpen || $[20] !== setTrigger || $[21] !== styleSize) {
329
+ t6 = event_0 => {
330
+ onPress?.(event_0);
215
331
  if (open) {
216
332
  return setOpen(false);
217
333
  }
218
- const view_0 = ref.current;
219
- if (!view_0) {
220
- return setOpen(true);
334
+ const view_1 = ref.current;
335
+ const size = layoutSizeRef.current ?? styleSize ?? FALLBACK_SIZE;
336
+ const pressRect = triggerRectFromPress(event_0, size);
337
+ if (pressRect) {
338
+ setTrigger(pressRect);
221
339
  }
222
- measureAnchor(view_0, rect => {
223
- if (rect) {
224
- setTrigger(rect);
340
+ setOpen(true);
341
+ if (!view_1) {
342
+ return;
343
+ }
344
+ measureAnchor(view_1, rect_0 => {
345
+ if (rect_0) {
346
+ lastRectRef.current = rect_0;
347
+ return setTrigger(rect_0);
348
+ }
349
+ if (!pressRect && lastRectRef.current) {
350
+ setTrigger(lastRectRef.current);
225
351
  }
226
- setOpen(true);
227
352
  });
228
353
  };
229
- $[11] = onPress;
230
- $[12] = open;
231
- $[13] = setOpen;
232
- $[14] = setTrigger;
233
- $[15] = t3;
354
+ $[17] = onPress;
355
+ $[18] = open;
356
+ $[19] = setOpen;
357
+ $[20] = setTrigger;
358
+ $[21] = styleSize;
359
+ $[22] = t6;
234
360
  } else {
235
- t3 = $[15];
361
+ t6 = $[22];
236
362
  }
237
- let t4;
238
- if ($[16] !== Component || $[17] !== props || $[18] !== t1 || $[19] !== t2 || $[20] !== t3) {
239
- t4 = <Component ref={t1} accessibilityRole="button" {...props} accessibilityState={t2} onPress={t3} />;
240
- $[16] = Component;
241
- $[17] = props;
242
- $[18] = t1;
243
- $[19] = t2;
244
- $[20] = t3;
245
- $[21] = t4;
363
+ let t7;
364
+ if ($[23] !== Component || $[24] !== props || $[25] !== style || $[26] !== t5 || $[27] !== t6) {
365
+ t7 = <Component accessibilityRole="button" {...props} style={style} accessibilityState={t5} onPress={t6} />;
366
+ $[23] = Component;
367
+ $[24] = props;
368
+ $[25] = style;
369
+ $[26] = t5;
370
+ $[27] = t6;
371
+ $[28] = t7;
246
372
  } else {
247
- t4 = $[21];
373
+ t7 = $[28];
248
374
  }
249
- return t4;
375
+ let t8;
376
+ if ($[29] !== t2 || $[30] !== t3 || $[31] !== t7) {
377
+ t8 = <View ref={t2} collapsable={false} accessible={false} style={t3} onLayout={t4}>{t7}</View>;
378
+ $[29] = t2;
379
+ $[30] = t3;
380
+ $[31] = t7;
381
+ $[32] = t8;
382
+ } else {
383
+ t8 = $[32];
384
+ }
385
+ return t8;
250
386
  }
251
387
  /**
252
388
  * Where the menu goes: under the trigger, `sideOffset` away, its start edge on
253
389
  * the trigger's start (`"start"`) or its end edge on the trigger's end
254
390
  * (`"end"`), kept 8 inside the safe area. It flips above the trigger when its
255
- * content does not fit below and there is more room above. Without a trigger
256
- * box it sits at the top of the safe area.
391
+ * content does not fit below and there is more room above; it then sets `top`
392
+ * only, never `bottom`. Before the content
393
+ * is measured (0), it flips only when less than one item row fits below. Without
394
+ * a trigger box it sits at the top of the safe area.
257
395
  */
258
396
  export function dropdownMenuPosition({
259
397
  trigger,
@@ -282,10 +420,16 @@ export function dropdownMenuPosition({
282
420
  const above = trigger.y - sideOffset;
283
421
  const roomBelow = maxBottom - below;
284
422
  const roomAbove = above - minTop;
285
- if (contentHeight > roomBelow && roomAbove > roomBelow) {
423
+ // Not measured yet (0): flip only when almost no room is below, so a bottom
424
+ // trigger never gets a first layout with a tiny `maxHeight`.
425
+ const tooTight = contentHeight > 0 ? contentHeight > roomBelow : roomBelow < DROPDOWN_MENU.itemHeight;
426
+ if (tooTight && roomAbove > roomBelow) {
427
+ // `top` only: `bottom` is ignored inside `WindowLayerHost` on Android. The menu's
428
+ // bottom edge lands on `above`; before measure, a two-row estimate stands in.
429
+ const height = contentHeight > 0 ? contentHeight : Math.min(roomAbove, DROPDOWN_MENU.itemHeight * 2 + DROPDOWN_MENU.padding * 2);
286
430
  return {
287
431
  ...horizontal,
288
- bottom: window.height - above,
432
+ top: Math.max(minTop, above - height),
289
433
  maxHeight: roomAbove
290
434
  };
291
435
  }
@@ -306,7 +450,7 @@ export function dropdownMenuPosition({
306
450
  * 100 ms. With Reduce Motion on, it appears and leaves at once.
307
451
  */
308
452
  export function DropdownMenuContent(t0) {
309
- const $ = _c(76);
453
+ const $ = _c(80);
310
454
  let children;
311
455
  let className;
312
456
  let props;
@@ -497,142 +641,154 @@ export function DropdownMenuContent(t0) {
497
641
  t11 = $[35];
498
642
  }
499
643
  const t12 = open ? "auto" : "none";
500
- const t13 = window.width - insets.left - insets.right - 2 * DROPDOWN_MENU.edge;
501
- const t14 = contentHeight === null ? 0 : 1;
502
- let t15;
503
- if ($[36] !== position || $[37] !== t13 || $[38] !== t14) {
504
- t15 = {
644
+ const t13 = position.top ?? null;
645
+ const t14 = position.bottom ?? null;
646
+ const t15 = position.left ?? null;
647
+ const t16 = position.right ?? null;
648
+ const t17 = window.width - insets.left - insets.right - 2 * DROPDOWN_MENU.edge;
649
+ const t18 = contentHeight === null ? 0 : 1;
650
+ let t19;
651
+ if ($[36] !== position.maxHeight || $[37] !== t13 || $[38] !== t14 || $[39] !== t15 || $[40] !== t16 || $[41] !== t17 || $[42] !== t18) {
652
+ t19 = {
505
653
  position: "absolute",
506
- ...position,
654
+ top: t13,
655
+ bottom: t14,
656
+ left: t15,
657
+ right: t16,
658
+ maxHeight: position.maxHeight,
507
659
  minWidth: DROPDOWN_MENU.minWidth,
508
- maxWidth: t13,
509
- opacity: t14
660
+ maxWidth: t17,
661
+ opacity: t18
510
662
  };
511
- $[36] = position;
663
+ $[36] = position.maxHeight;
512
664
  $[37] = t13;
513
665
  $[38] = t14;
514
666
  $[39] = t15;
667
+ $[40] = t16;
668
+ $[41] = t17;
669
+ $[42] = t18;
670
+ $[43] = t19;
515
671
  } else {
516
- t15 = $[39];
672
+ t19 = $[43];
517
673
  }
518
- let t16;
519
- if ($[40] !== transformOrigin) {
520
- t16 = {
674
+ let t20;
675
+ if ($[44] !== transformOrigin) {
676
+ t20 = {
521
677
  transformOrigin
522
678
  };
523
- $[40] = transformOrigin;
524
- $[41] = t16;
679
+ $[44] = transformOrigin;
680
+ $[45] = t20;
525
681
  } else {
526
- t16 = $[41];
682
+ t20 = $[45];
527
683
  }
528
- let t17;
529
- if ($[42] !== panelStyle || $[43] !== t16) {
530
- t17 = [t16, panelStyle];
531
- $[42] = panelStyle;
532
- $[43] = t16;
533
- $[44] = t17;
684
+ let t21;
685
+ if ($[46] !== panelStyle || $[47] !== t20) {
686
+ t21 = [t20, panelStyle];
687
+ $[46] = panelStyle;
688
+ $[47] = t20;
689
+ $[48] = t21;
534
690
  } else {
535
- t17 = $[44];
691
+ t21 = $[48];
536
692
  }
537
- let t18;
538
- if ($[45] !== position.maxHeight) {
539
- t18 = {
693
+ let t22;
694
+ if ($[49] !== position.maxHeight) {
695
+ t22 = {
540
696
  maxHeight: position.maxHeight
541
697
  };
542
- $[45] = position.maxHeight;
543
- $[46] = t18;
698
+ $[49] = position.maxHeight;
699
+ $[50] = t22;
544
700
  } else {
545
- t18 = $[46];
701
+ t22 = $[50];
546
702
  }
547
- let t19;
548
- let t20;
549
- if ($[47] === Symbol.for("react.memo_cache_sentinel")) {
550
- t19 = (_width, height) => setContentHeight(height);
551
- t20 = {
703
+ let t23;
704
+ let t24;
705
+ if ($[51] === Symbol.for("react.memo_cache_sentinel")) {
706
+ t23 = (_width, height) => setContentHeight(height);
707
+ t24 = {
552
708
  padding: DROPDOWN_MENU.padding
553
709
  };
554
- $[47] = t19;
555
- $[48] = t20;
710
+ $[51] = t23;
711
+ $[52] = t24;
556
712
  } else {
557
- t19 = $[47];
558
- t20 = $[48];
713
+ t23 = $[51];
714
+ t24 = $[52];
559
715
  }
560
- let t21;
561
- if ($[49] !== className) {
562
- t21 = cn(className);
563
- $[49] = className;
564
- $[50] = t21;
716
+ let t25;
717
+ if ($[53] !== className) {
718
+ t25 = cn(className);
719
+ $[53] = className;
720
+ $[54] = t25;
565
721
  } else {
566
- t21 = $[50];
722
+ t25 = $[54];
567
723
  }
568
- let t22;
569
- if ($[51] !== children || $[52] !== props || $[53] !== style || $[54] !== t21) {
570
- t22 = <ScrollView bounces={false} showsVerticalScrollIndicator={false} onContentSizeChange={t19} contentContainerStyle={t20}><View className={t21} style={style} {...props}>{children}</View></ScrollView>;
571
- $[51] = children;
572
- $[52] = props;
573
- $[53] = style;
574
- $[54] = t21;
575
- $[55] = t22;
724
+ let t26;
725
+ if ($[55] !== children || $[56] !== props || $[57] !== style || $[58] !== t25) {
726
+ t26 = <ScrollView bounces={false} showsVerticalScrollIndicator={false} onContentSizeChange={t23} contentContainerStyle={t24}><View className={t25} style={style} {...props}>{children}</View></ScrollView>;
727
+ $[55] = children;
728
+ $[56] = props;
729
+ $[57] = style;
730
+ $[58] = t25;
731
+ $[59] = t26;
576
732
  } else {
577
- t22 = $[55];
733
+ t26 = $[59];
578
734
  }
579
- let t23;
580
- if ($[56] !== t18 || $[57] !== t22) {
581
- t23 = <FloatingSurface blur={false} radius={DROPDOWN_MENU.radius} style={t18}>{t22}</FloatingSurface>;
582
- $[56] = t18;
583
- $[57] = t22;
584
- $[58] = t23;
735
+ let t27;
736
+ if ($[60] !== t22 || $[61] !== t26) {
737
+ t27 = <FloatingSurface blur={false} radius={DROPDOWN_MENU.radius} style={t22}>{t26}</FloatingSurface>;
738
+ $[60] = t22;
739
+ $[61] = t26;
740
+ $[62] = t27;
585
741
  } else {
586
- t23 = $[58];
742
+ t27 = $[62];
587
743
  }
588
- let t24;
589
- if ($[59] !== t17 || $[60] !== t23) {
590
- t24 = <Animated.View style={t17}>{t23}</Animated.View>;
591
- $[59] = t17;
592
- $[60] = t23;
593
- $[61] = t24;
744
+ let t28;
745
+ if ($[63] !== t21 || $[64] !== t27) {
746
+ t28 = <Animated.View style={t21}>{t27}</Animated.View>;
747
+ $[63] = t21;
748
+ $[64] = t27;
749
+ $[65] = t28;
594
750
  } else {
595
- t24 = $[61];
751
+ t28 = $[65];
596
752
  }
597
- let t25;
598
- if ($[62] !== close || $[63] !== t12 || $[64] !== t15 || $[65] !== t24) {
599
- t25 = <View accessibilityRole="menu" accessibilityViewIsModal={true} onAccessibilityEscape={close} pointerEvents={t12} style={t15}>{t24}</View>;
600
- $[62] = close;
601
- $[63] = t12;
602
- $[64] = t15;
603
- $[65] = t24;
604
- $[66] = t25;
753
+ let t29;
754
+ if ($[66] !== close || $[67] !== t12 || $[68] !== t19 || $[69] !== t28) {
755
+ t29 = <View accessibilityRole="menu" accessibilityViewIsModal={true} onAccessibilityEscape={close} pointerEvents={t12} style={t19}>{t28}</View>;
756
+ $[66] = close;
757
+ $[67] = t12;
758
+ $[68] = t19;
759
+ $[69] = t28;
760
+ $[70] = t29;
605
761
  } else {
606
- t25 = $[66];
762
+ t29 = $[70];
607
763
  }
608
- let t26;
609
- if ($[67] !== t11 || $[68] !== t25) {
610
- t26 = <View style={t10} pointerEvents="box-none">{t11}{t25}</View>;
611
- $[67] = t11;
612
- $[68] = t25;
613
- $[69] = t26;
764
+ let t30;
765
+ if ($[71] !== t11 || $[72] !== t29) {
766
+ t30 = <View style={t10} pointerEvents="box-none">{t11}{t29}</View>;
767
+ $[71] = t11;
768
+ $[72] = t29;
769
+ $[73] = t30;
614
770
  } else {
615
- t26 = $[69];
771
+ t30 = $[73];
616
772
  }
617
- let t27;
618
- if ($[70] !== menu || $[71] !== t26) {
619
- t27 = <MenuContext.Provider value={menu}>{t26}</MenuContext.Provider>;
620
- $[70] = menu;
621
- $[71] = t26;
622
- $[72] = t27;
773
+ let t31;
774
+ if ($[74] !== menu || $[75] !== t30) {
775
+ t31 = <MenuContext.Provider value={menu}>{t30}</MenuContext.Provider>;
776
+ $[74] = menu;
777
+ $[75] = t30;
778
+ $[76] = t31;
623
779
  } else {
624
- t27 = $[72];
780
+ t31 = $[76];
625
781
  }
626
- let t28;
627
- if ($[73] !== close || $[74] !== t27) {
628
- t28 = <WindowLayer onRequestClose={close} fade={false}>{t27}</WindowLayer>;
629
- $[73] = close;
630
- $[74] = t27;
631
- $[75] = t28;
782
+ let t32;
783
+ if ($[77] !== close || $[78] !== t31) {
784
+ t32 = <WindowLayer onRequestClose={close} fade={false}>{t31}</WindowLayer>;
785
+ $[77] = close;
786
+ $[78] = t31;
787
+ $[79] = t32;
632
788
  } else {
633
- t28 = $[75];
789
+ t32 = $[79];
634
790
  }
635
- return t28;
791
+ return t32;
636
792
  }
637
793
 
638
794
  /** Item type and icon size: the web `text-sm` with a 16 icon in 48 rows, or `text-lg` with a 22 icon and 56 rows with larger text. */
@@ -0,0 +1,194 @@
1
+ import { c as _c } from "react/compiler-runtime";
2
+ import { Text, View } from "react-native";
3
+ import Animated, { ReduceMotion, SharedTransition } from "react-native-reanimated";
4
+ import { useReduceMotion } from "./useReduceMotion";
5
+
6
+ // Shared element transitions (Reanimated 4.8): a view with the same
7
+ // `sharedTransitionTag` on two screens moves and resizes from one to the other
8
+ // as the screens change. Docs:
9
+ // https://docs.swmansion.com/react-native-reanimated/docs/shared-element-transitions/shared-transition/
10
+ // https://docs.swmansion.com/react-native-reanimated/docs/shared-element-transitions/children-of-shared-views/
11
+
12
+ /** Spring length for a list-to-detail move, in ms. */
13
+ export const SHARED_TRANSITION_DURATION = 280;
14
+
15
+ /**
16
+ * Critically damped (1): the shared view settles with no bounce, as a
17
+ * list-to-detail move should. Also easier on a low-end Android GPU.
18
+ */
19
+ export const SHARED_TRANSITION_DAMPING_RATIO = 1;
20
+
21
+ /**
22
+ * Children of the shared views: the source's children are gone by 55% of the
23
+ * move, and the target's children fade in over 180 ms once it lands. This hides
24
+ * the squash of text and icons while the box resizes.
25
+ */
26
+ export const DEFAULT_FADE_CONTENT = {
27
+ fadeOut: {
28
+ end: 0.55
29
+ },
30
+ fadeIn: {
31
+ duration: 180
32
+ }
33
+ };
34
+ /**
35
+ * A new `SharedTransition` builder with the kit defaults and `overrides`. It
36
+ * always applies `ReduceMotion.System`, so the system setting skips the move.
37
+ * Builders are mutable: this returns a fresh one on every call.
38
+ */
39
+ export function createSharedTransition(overrides = {}) {
40
+ const {
41
+ duration = SHARED_TRANSITION_DURATION,
42
+ dampingRatio = SHARED_TRANSITION_DAMPING_RATIO,
43
+ fadeContent = DEFAULT_FADE_CONTENT
44
+ } = overrides;
45
+ const builder = SharedTransition.springify(duration).dampingRatio(dampingRatio).reduceMotion(ReduceMotion.System);
46
+ return fadeContent === false ? builder : builder.fadeContent(fadeContent);
47
+ }
48
+
49
+ /** The default builder, for `sharedTransitionStyle`. Do not chain on it: use `createSharedTransition`. */
50
+ export const DEFAULT_SHARED_TRANSITION = createSharedTransition();
51
+
52
+ /**
53
+ * A tag for `sharedTransitionTag`, prefixed so it cannot clash with a tag from
54
+ * another library. Give both screens the same `id`.
55
+ */
56
+ export function sharedTransitionTag(id) {
57
+ return `nui:${id}`;
58
+ }
59
+
60
+ /**
61
+ * The default shared transition, or `undefined` with Reduce Motion on. Spread
62
+ * the result with the tag only when it is defined:
63
+ * `<Animated.View sharedTransitionTag={style ? tag : undefined} sharedTransitionStyle={style} />`.
64
+ */
65
+ export function useSharedTransitionStyle() {
66
+ return useReduceMotion() ? undefined : DEFAULT_SHARED_TRANSITION;
67
+ }
68
+ /**
69
+ * An `Animated.View` that takes part in a shared element transition with the
70
+ * `SharedTransitionView` that has the same `tag` on the other screen. With
71
+ * Reduce Motion on it is a plain `View`: the screens change with no shared move.
72
+ * NativeWind does not style an `Animated.View`, so use `style`.
73
+ */
74
+ export function SharedTransitionView(t0) {
75
+ const $ = _c(17);
76
+ let children;
77
+ let rest;
78
+ let style;
79
+ let tag;
80
+ if ($[0] !== t0) {
81
+ ({
82
+ tag,
83
+ style,
84
+ children,
85
+ ...rest
86
+ } = t0);
87
+ $[0] = t0;
88
+ $[1] = children;
89
+ $[2] = rest;
90
+ $[3] = style;
91
+ $[4] = tag;
92
+ } else {
93
+ children = $[1];
94
+ rest = $[2];
95
+ style = $[3];
96
+ tag = $[4];
97
+ }
98
+ const transition = useSharedTransitionStyle();
99
+ if (!transition) {
100
+ let t1;
101
+ if ($[5] !== children || $[6] !== rest || $[7] !== style) {
102
+ t1 = <View style={style} {...rest}>{children}</View>;
103
+ $[5] = children;
104
+ $[6] = rest;
105
+ $[7] = style;
106
+ $[8] = t1;
107
+ } else {
108
+ t1 = $[8];
109
+ }
110
+ return t1;
111
+ }
112
+ let t1;
113
+ if ($[9] !== tag) {
114
+ t1 = sharedTransitionTag(tag);
115
+ $[9] = tag;
116
+ $[10] = t1;
117
+ } else {
118
+ t1 = $[10];
119
+ }
120
+ let t2;
121
+ if ($[11] !== children || $[12] !== rest || $[13] !== style || $[14] !== t1 || $[15] !== transition) {
122
+ t2 = <Animated.View sharedTransitionTag={t1} sharedTransitionStyle={transition} style={style} {...rest}>{children}</Animated.View>;
123
+ $[11] = children;
124
+ $[12] = rest;
125
+ $[13] = style;
126
+ $[14] = t1;
127
+ $[15] = transition;
128
+ $[16] = t2;
129
+ } else {
130
+ t2 = $[16];
131
+ }
132
+ return t2;
133
+ }
134
+ /** `SharedTransitionView` for text: an `Animated.Text`, or a plain `Text` with Reduce Motion on. */
135
+ export function SharedTransitionText(t0) {
136
+ const $ = _c(17);
137
+ let children;
138
+ let rest;
139
+ let style;
140
+ let tag;
141
+ if ($[0] !== t0) {
142
+ ({
143
+ tag,
144
+ style,
145
+ children,
146
+ ...rest
147
+ } = t0);
148
+ $[0] = t0;
149
+ $[1] = children;
150
+ $[2] = rest;
151
+ $[3] = style;
152
+ $[4] = tag;
153
+ } else {
154
+ children = $[1];
155
+ rest = $[2];
156
+ style = $[3];
157
+ tag = $[4];
158
+ }
159
+ const transition = useSharedTransitionStyle();
160
+ if (!transition) {
161
+ let t1;
162
+ if ($[5] !== children || $[6] !== rest || $[7] !== style) {
163
+ t1 = <Text style={style} {...rest}>{children}</Text>;
164
+ $[5] = children;
165
+ $[6] = rest;
166
+ $[7] = style;
167
+ $[8] = t1;
168
+ } else {
169
+ t1 = $[8];
170
+ }
171
+ return t1;
172
+ }
173
+ let t1;
174
+ if ($[9] !== tag) {
175
+ t1 = sharedTransitionTag(tag);
176
+ $[9] = tag;
177
+ $[10] = t1;
178
+ } else {
179
+ t1 = $[10];
180
+ }
181
+ let t2;
182
+ if ($[11] !== children || $[12] !== rest || $[13] !== style || $[14] !== t1 || $[15] !== transition) {
183
+ t2 = <Animated.Text sharedTransitionTag={t1} sharedTransitionStyle={transition} style={style} {...rest}>{children}</Animated.Text>;
184
+ $[11] = children;
185
+ $[12] = rest;
186
+ $[13] = style;
187
+ $[14] = t1;
188
+ $[15] = transition;
189
+ $[16] = t2;
190
+ } else {
191
+ t2 = $[16];
192
+ }
193
+ return t2;
194
+ }
@@ -61,5 +61,7 @@ export { fetchGitHubUpdate, isNewerVersion, newerGitHubRelease } from "../update
61
61
  export { Sheet, SheetTrigger, SheetClose, SheetContent, SheetHandle, SheetHeader, SheetTitle, SheetDescription, SheetBody, SheetFooter } from "../Sheet";
62
62
  export { Toaster } from "../Toast";
63
63
  export { toast } from "../toastStore";
64
+ // Shared element transitions (Reanimated 4.8): the same `tag` on two screens moves the view between them.
65
+ export { DEFAULT_SHARED_TRANSITION, DEFAULT_FADE_CONTENT, createSharedTransition, sharedTransitionTag, useSharedTransitionStyle, SharedTransitionView, SharedTransitionText } from "../SharedTransition";
64
66
  // Overlays in the app's window (the hold menu, the image viewer): wrap the navigator once.
65
67
  export { WindowLayerHost } from "../primitives/window-layer";
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export { fontClass, fontFamily } from "./typography";
6
6
  export { FLOATING_INSET, FLOATING_TAB_BAR_HEIGHT, FLOATING_TAB_BAR_HEIGHT_LARGE, FLOATING_RADIUS, FLOATING_ROW_RADIUS, FLOATING_PILL_RADIUS, floatingShadow, floatingHairline, floatingFill, floatingSolidFill, tabBarReserve } from "./floating";
7
7
  export { impactHaptic, notificationHaptic, selectionHaptic } from "./haptics";
8
8
  export { useReduceTransparency } from "./useReduceTransparency";
9
+ export { DEFAULT_SHARED_TRANSITION, DEFAULT_FADE_CONTENT, createSharedTransition, sharedTransitionTag, useSharedTransitionStyle, SharedTransitionView, SharedTransitionText } from "./SharedTransition";
9
10
  export { FloatingSurface, GlassSheetBackground } from "./FloatingSurface";
10
11
  export { LiquidGlass, GlassBlurProvider, GlassBlurTarget } from "./LiquidGlass";
11
12
  export { AmbientBackground } from "./AmbientBackground";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.36.1",
3
+ "version": "0.37.1",
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",
@@ -328,12 +328,12 @@
328
328
  "react-native": "0.88.1",
329
329
  "react-native-gesture-handler": "~3.3.0",
330
330
  "react-native-keyboard-controller": "1.22.7",
331
- "react-native-reanimated": "4.7.1",
331
+ "react-native-reanimated": "4.8.0",
332
332
  "react-native-safe-area-context": "~5.11.1",
333
333
  "react-native-screens": "~4.30.1",
334
334
  "react-native-svg": "15.16.0",
335
335
  "react-native-webview": "14.0.1",
336
- "react-native-worklets": "0.13.0",
336
+ "react-native-worklets": "0.14.0",
337
337
  "typescript": "7.0.2",
338
338
  "vitest": "^3.2.0",
339
339
  "@vvedantb/server": "0.17.1"
@@ -18,6 +18,7 @@ import {
18
18
  View,
19
19
  useWindowDimensions,
20
20
  type GestureResponderEvent,
21
+ type LayoutChangeEvent,
21
22
  type PressableProps,
22
23
  type ViewProps,
23
24
  } from "react-native";
@@ -123,58 +124,140 @@ export function isMeasuredRect(rect: DropdownMenuRect): boolean {
123
124
 
124
125
  type Measurable = Pick<ComponentRef<typeof View>, "measureInWindow" | "measure">;
125
126
 
127
+ /** The wrapper shrink-wraps the trigger in a flex row (such as the PromptInput `leading` slot). */
128
+ const TRIGGER_HOST = { alignSelf: "flex-start" } as const;
129
+
130
+ /** Frames to wait for layout to settle, when a view reads empty. */
131
+ const MEASURE_RETRIES = 2;
132
+
133
+ type LayoutSize = { width: number; height: number };
134
+
135
+ /** Last-resort trigger size when a press has a touch point but no `onLayout` size and no style size: a 44 point touch target. */
136
+ const FALLBACK_SIZE: LayoutSize = { width: 44, height: 44 };
137
+
138
+ /** Numeric `width` and `height` of a plain style object, such as `{ width: 44, height: 44 }`; `undefined` for a function, array or missing size. */
139
+ export function sizeFromStyle(style: PressableProps["style"]): LayoutSize | undefined {
140
+ if (!style || typeof style !== "object" || Array.isArray(style)) return undefined;
141
+ if (!("width" in style) || !("height" in style)) return undefined;
142
+ const { width, height } = style;
143
+ if (typeof width !== "number" || typeof height !== "number") return undefined;
144
+ return width > 0 && height > 0 ? { width, height } : undefined;
145
+ }
146
+
147
+ /**
148
+ * The trigger's box from a press: the press point in window coordinates
149
+ * (`pageX`, `pageY`) less its point in the view (`locationX`, `locationY`) is
150
+ * the view's origin, and `size` is its laid-out size. `null` without a size or a usable touch point.
151
+ */
152
+ export function triggerRectFromPress(
153
+ event: { nativeEvent?: Partial<Record<"pageX" | "pageY" | "locationX" | "locationY", number>> } | null | undefined,
154
+ size: LayoutSize | undefined,
155
+ ): DropdownMenuRect | null {
156
+ const point = event?.nativeEvent;
157
+ if (!size || !point) return null;
158
+ const { pageX, pageY, locationX, locationY } = point;
159
+ if (
160
+ typeof pageX !== "number" || !Number.isFinite(pageX) ||
161
+ typeof pageY !== "number" || !Number.isFinite(pageY) ||
162
+ typeof locationX !== "number" || !Number.isFinite(locationX) ||
163
+ typeof locationY !== "number" || !Number.isFinite(locationY)
164
+ ) {
165
+ return null;
166
+ }
167
+ const rect = { x: pageX - locationX, y: pageY - locationY, width: size.width, height: size.height };
168
+ return isMeasuredRect(rect) ? rect : null;
169
+ }
170
+
126
171
  /**
127
172
  * The view's box in window coordinates, or `null` when it cannot be measured.
128
173
  * `measureInWindow` first; when it returns an empty box, `measure` (its
129
- * `pageX` and `pageY` are window coordinates too) as a second reading.
174
+ * `pageX` and `pageY` are window coordinates too) as a second reading. When
175
+ * both are empty, it tries again on the next frame, up to `retries` times: a
176
+ * view under a keyboard-lifted or avoiding parent may not be laid out yet.
130
177
  */
131
- export function measureAnchor(view: Measurable, done: (rect: DropdownMenuRect | null) => void) {
178
+ export function measureAnchor(view: Measurable, done: (rect: DropdownMenuRect | null) => void, retries = MEASURE_RETRIES) {
132
179
  view.measureInWindow((x, y, width, height) => {
133
180
  const rect = { x, y, width, height };
134
181
  if (isMeasuredRect(rect)) return done(rect);
135
182
  view.measure((_x, _y, w, h, pageX, pageY) => {
136
183
  const second = { x: pageX, y: pageY, width: w, height: h };
137
- done(isMeasuredRect(second) ? second : null);
184
+ if (isMeasuredRect(second)) return done(second);
185
+ if (retries <= 0) return done(null);
186
+ requestAnimationFrame(() => measureAnchor(view, done, retries - 1));
138
187
  });
139
188
  });
140
189
  }
141
190
 
142
191
  /**
143
- * Opens the menu, or closes it when open. On press it measures itself
144
- * (`measureInWindow`) and the menu opens under it. Reports `expanded`. A
145
- * caller `ref` is kept: the menu's own handle never replaces it.
192
+ * Opens the menu, or closes it when open. On press it measures its wrapper
193
+ * (`measureInWindow`) and the menu opens under it. The wrapper is a
194
+ * `collapsable={false}` view, so Android keeps a native view to measure. On
195
+ * press, the box comes at once from the press point and a size (`onLayout`,
196
+ * then a numeric `style` width and height, then 44), and the menu opens; a live
197
+ * measure then refines it. When the press has no touch point and every measure
198
+ * reads empty, the last good box is used. Reports `expanded`. A caller `ref` is
199
+ * kept: it gets the wrapper, as the menu's own handle does.
146
200
  */
147
201
  export function DropdownMenuTrigger({
148
202
  asChild,
149
203
  onPress,
150
204
  accessibilityState,
151
205
  ref: callerRef,
206
+ style,
152
207
  ...props
153
208
  }: DropdownMenuTriggerProps) {
154
209
  const { open, setOpen, setTrigger } = useMenu("DropdownMenuTrigger");
155
210
  const ref = useRef<ComponentRef<typeof View> | null>(null);
211
+ // This trigger's last `onLayout` size and last good measured box, kept for when a live reading is empty.
212
+ const layoutSizeRef = useRef<LayoutSize | undefined>(undefined);
213
+ const lastRectRef = useRef<DropdownMenuRect | undefined>(undefined);
214
+ const styleSize = sizeFromStyle(style);
156
215
  const Component = asChild ? Slot.Pressable : Pressable;
157
216
  return (
158
- <Component
217
+ <View
159
218
  ref={(view: ComponentRef<typeof View> | null) => {
160
219
  ref.current = view;
161
220
  if (typeof callerRef === "function") callerRef(view);
162
221
  else if (callerRef) callerRef.current = view;
163
222
  }}
164
- accessibilityRole="button"
165
- {...props}
166
- accessibilityState={{ ...accessibilityState, expanded: open }}
167
- onPress={(event: GestureResponderEvent) => {
168
- onPress?.(event);
169
- if (open) return setOpen(false);
223
+ collapsable={false}
224
+ accessible={false}
225
+ style={styleSize ? { ...TRIGGER_HOST, ...styleSize } : TRIGGER_HOST}
226
+ onLayout={(event: LayoutChangeEvent) => {
170
227
  const view = ref.current;
171
- if (!view) return setOpen(true);
228
+ if (!view) return;
229
+ const { width, height } = event.nativeEvent.layout;
230
+ if (width > 0 && height > 0) layoutSizeRef.current = { width, height };
172
231
  measureAnchor(view, (rect) => {
173
- if (rect) setTrigger(rect);
174
- setOpen(true);
232
+ if (rect) lastRectRef.current = rect;
175
233
  });
176
234
  }}
177
- />
235
+ >
236
+ <Component
237
+ accessibilityRole="button"
238
+ {...props}
239
+ style={style}
240
+ accessibilityState={{ ...accessibilityState, expanded: open }}
241
+ onPress={(event: GestureResponderEvent) => {
242
+ onPress?.(event);
243
+ if (open) return setOpen(false);
244
+ const view = ref.current;
245
+ const size = layoutSizeRef.current ?? styleSize ?? FALLBACK_SIZE;
246
+ const pressRect = triggerRectFromPress(event, size);
247
+ // Anchor and open at once from the press, then refine from a live measure.
248
+ if (pressRect) setTrigger(pressRect);
249
+ setOpen(true);
250
+ if (!view) return;
251
+ measureAnchor(view, (rect) => {
252
+ if (rect) {
253
+ lastRectRef.current = rect;
254
+ return setTrigger(rect);
255
+ }
256
+ if (!pressRect && lastRectRef.current) setTrigger(lastRectRef.current);
257
+ });
258
+ }}
259
+ />
260
+ </View>
178
261
  );
179
262
  }
180
263
 
@@ -192,8 +275,10 @@ export type DropdownMenuPosition = {
192
275
  * Where the menu goes: under the trigger, `sideOffset` away, its start edge on
193
276
  * the trigger's start (`"start"`) or its end edge on the trigger's end
194
277
  * (`"end"`), kept 8 inside the safe area. It flips above the trigger when its
195
- * content does not fit below and there is more room above. Without a trigger
196
- * box it sits at the top of the safe area.
278
+ * content does not fit below and there is more room above; it then sets `top`
279
+ * only, never `bottom`. Before the content
280
+ * is measured (0), it flips only when less than one item row fits below. Without
281
+ * a trigger box it sits at the top of the safe area.
197
282
  */
198
283
  export function dropdownMenuPosition({
199
284
  trigger,
@@ -224,8 +309,14 @@ export function dropdownMenuPosition({
224
309
  const above = trigger.y - sideOffset;
225
310
  const roomBelow = maxBottom - below;
226
311
  const roomAbove = above - minTop;
227
- if (contentHeight > roomBelow && roomAbove > roomBelow) {
228
- return { ...horizontal, bottom: window.height - above, maxHeight: roomAbove };
312
+ // Not measured yet (0): flip only when almost no room is below, so a bottom
313
+ // trigger never gets a first layout with a tiny `maxHeight`.
314
+ const tooTight = contentHeight > 0 ? contentHeight > roomBelow : roomBelow < DROPDOWN_MENU.itemHeight;
315
+ if (tooTight && roomAbove > roomBelow) {
316
+ // `top` only: `bottom` is ignored inside `WindowLayerHost` on Android. The menu's
317
+ // bottom edge lands on `above`; before measure, a two-row estimate stands in.
318
+ const height = contentHeight > 0 ? contentHeight : Math.min(roomAbove, DROPDOWN_MENU.itemHeight * 2 + DROPDOWN_MENU.padding * 2);
319
+ return { ...horizontal, top: Math.max(minTop, above - height), maxHeight: roomAbove };
229
320
  }
230
321
  return { ...horizontal, top: below, maxHeight: roomBelow };
231
322
  }
@@ -338,7 +429,13 @@ export function DropdownMenuContent({
338
429
  pointerEvents={open ? "auto" : "none"}
339
430
  style={{
340
431
  position: "absolute",
341
- ...position,
432
+ // Every edge is set, `null` when unused, so React Native clears the one
433
+ // a flip leaves behind instead of keeping a stale `top`.
434
+ top: position.top ?? null,
435
+ bottom: position.bottom ?? null,
436
+ left: position.left ?? null,
437
+ right: position.right ?? null,
438
+ maxHeight: position.maxHeight,
342
439
  minWidth: DROPDOWN_MENU.minWidth,
343
440
  maxWidth: window.width - insets.left - insets.right - 2 * DROPDOWN_MENU.edge,
344
441
  opacity: contentHeight === null ? 0 : 1,
@@ -0,0 +1,120 @@
1
+ import type { ReactNode } from "react";
2
+ import { Text, View, type StyleProp, type TextProps, type TextStyle, type ViewProps, type ViewStyle } from "react-native";
3
+ import Animated, { ReduceMotion, SharedTransition, type FadeContentConfig } from "react-native-reanimated";
4
+ import { useReduceMotion } from "./useReduceMotion";
5
+
6
+ // Shared element transitions (Reanimated 4.8): a view with the same
7
+ // `sharedTransitionTag` on two screens moves and resizes from one to the other
8
+ // as the screens change. Docs:
9
+ // https://docs.swmansion.com/react-native-reanimated/docs/shared-element-transitions/shared-transition/
10
+ // https://docs.swmansion.com/react-native-reanimated/docs/shared-element-transitions/children-of-shared-views/
11
+
12
+ /** Spring length for a list-to-detail move, in ms. */
13
+ export const SHARED_TRANSITION_DURATION = 280;
14
+
15
+ /**
16
+ * Critically damped (1): the shared view settles with no bounce, as a
17
+ * list-to-detail move should. Also easier on a low-end Android GPU.
18
+ */
19
+ export const SHARED_TRANSITION_DAMPING_RATIO = 1;
20
+
21
+ /**
22
+ * Children of the shared views: the source's children are gone by 55% of the
23
+ * move, and the target's children fade in over 180 ms once it lands. This hides
24
+ * the squash of text and icons while the box resizes.
25
+ */
26
+ export const DEFAULT_FADE_CONTENT: FadeContentConfig = {
27
+ fadeOut: { end: 0.55 },
28
+ fadeIn: { duration: 180 },
29
+ };
30
+
31
+ export type SharedTransitionOverrides = {
32
+ /** Spring length in ms. Default `280`. */
33
+ duration?: number;
34
+ /** Default `1` (no bounce). */
35
+ dampingRatio?: number;
36
+ /** Default `DEFAULT_FADE_CONTENT`. Pass `false` to leave children as they are (no `fadeContent`). */
37
+ fadeContent?: FadeContentConfig | false;
38
+ };
39
+
40
+ /**
41
+ * A new `SharedTransition` builder with the kit defaults and `overrides`. It
42
+ * always applies `ReduceMotion.System`, so the system setting skips the move.
43
+ * Builders are mutable: this returns a fresh one on every call.
44
+ */
45
+ export function createSharedTransition(overrides: SharedTransitionOverrides = {}): SharedTransition {
46
+ const { duration = SHARED_TRANSITION_DURATION, dampingRatio = SHARED_TRANSITION_DAMPING_RATIO, fadeContent = DEFAULT_FADE_CONTENT } = overrides;
47
+ const builder = SharedTransition.springify(duration).dampingRatio(dampingRatio).reduceMotion(ReduceMotion.System);
48
+ return fadeContent === false ? builder : builder.fadeContent(fadeContent);
49
+ }
50
+
51
+ /** The default builder, for `sharedTransitionStyle`. Do not chain on it: use `createSharedTransition`. */
52
+ export const DEFAULT_SHARED_TRANSITION: SharedTransition = createSharedTransition();
53
+
54
+ /**
55
+ * A tag for `sharedTransitionTag`, prefixed so it cannot clash with a tag from
56
+ * another library. Give both screens the same `id`.
57
+ */
58
+ export function sharedTransitionTag(id: string): string {
59
+ return `nui:${id}`;
60
+ }
61
+
62
+ /**
63
+ * The default shared transition, or `undefined` with Reduce Motion on. Spread
64
+ * the result with the tag only when it is defined:
65
+ * `<Animated.View sharedTransitionTag={style ? tag : undefined} sharedTransitionStyle={style} />`.
66
+ */
67
+ export function useSharedTransitionStyle(): SharedTransition | undefined {
68
+ return useReduceMotion() ? undefined : DEFAULT_SHARED_TRANSITION;
69
+ }
70
+
71
+ export type SharedTransitionViewProps = Omit<ViewProps, "style"> & {
72
+ /** The id both screens share. Namespaced with `sharedTransitionTag`. */
73
+ tag: string;
74
+ style?: StyleProp<ViewStyle>;
75
+ children?: ReactNode;
76
+ };
77
+
78
+ /**
79
+ * An `Animated.View` that takes part in a shared element transition with the
80
+ * `SharedTransitionView` that has the same `tag` on the other screen. With
81
+ * Reduce Motion on it is a plain `View`: the screens change with no shared move.
82
+ * NativeWind does not style an `Animated.View`, so use `style`.
83
+ */
84
+ export function SharedTransitionView({ tag, style, children, ...rest }: SharedTransitionViewProps) {
85
+ const transition = useSharedTransitionStyle();
86
+ if (!transition) {
87
+ return (
88
+ <View style={style} {...rest}>
89
+ {children}
90
+ </View>
91
+ );
92
+ }
93
+ return (
94
+ <Animated.View sharedTransitionTag={sharedTransitionTag(tag)} sharedTransitionStyle={transition} style={style} {...rest}>
95
+ {children}
96
+ </Animated.View>
97
+ );
98
+ }
99
+
100
+ export type SharedTransitionTextProps = Omit<TextProps, "style"> & {
101
+ tag: string;
102
+ style?: StyleProp<TextStyle>;
103
+ };
104
+
105
+ /** `SharedTransitionView` for text: an `Animated.Text`, or a plain `Text` with Reduce Motion on. */
106
+ export function SharedTransitionText({ tag, style, children, ...rest }: SharedTransitionTextProps) {
107
+ const transition = useSharedTransitionStyle();
108
+ if (!transition) {
109
+ return (
110
+ <Text style={style} {...rest}>
111
+ {children}
112
+ </Text>
113
+ );
114
+ }
115
+ return (
116
+ <Animated.Text sharedTransitionTag={sharedTransitionTag(tag)} sharedTransitionStyle={transition} style={style} {...rest}>
117
+ {children}
118
+ </Animated.Text>
119
+ );
120
+ }
@@ -175,5 +175,17 @@ export { Toaster } from "../Toast";
175
175
  export { toast } from "../toastStore";
176
176
  export type { ToastOptions, ToastVariant } from "../toastStore";
177
177
 
178
+ // Shared element transitions (Reanimated 4.8): the same `tag` on two screens moves the view between them.
179
+ export {
180
+ DEFAULT_SHARED_TRANSITION,
181
+ DEFAULT_FADE_CONTENT,
182
+ createSharedTransition,
183
+ sharedTransitionTag,
184
+ useSharedTransitionStyle,
185
+ SharedTransitionView,
186
+ SharedTransitionText,
187
+ } from "../SharedTransition";
188
+ export type { SharedTransitionOverrides, SharedTransitionViewProps, SharedTransitionTextProps } from "../SharedTransition";
189
+
178
190
  // Overlays in the app's window (the hold menu, the image viewer): wrap the navigator once.
179
191
  export { WindowLayerHost } from "../primitives/window-layer";
package/src/index.ts CHANGED
@@ -29,6 +29,20 @@ export {
29
29
  } from "./floating";
30
30
  export { impactHaptic, notificationHaptic, selectionHaptic } from "./haptics";
31
31
  export { useReduceTransparency } from "./useReduceTransparency";
32
+ export {
33
+ DEFAULT_SHARED_TRANSITION,
34
+ DEFAULT_FADE_CONTENT,
35
+ createSharedTransition,
36
+ sharedTransitionTag,
37
+ useSharedTransitionStyle,
38
+ SharedTransitionView,
39
+ SharedTransitionText,
40
+ } from "./SharedTransition";
41
+ export type {
42
+ SharedTransitionOverrides,
43
+ SharedTransitionViewProps,
44
+ SharedTransitionTextProps,
45
+ } from "./SharedTransition";
32
46
  export { FloatingSurface, GlassSheetBackground } from "./FloatingSurface";
33
47
  export {
34
48
  LiquidGlass,