@vvedantb/native-ui 0.37.0 → 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.
@@ -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. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.37.0",
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",
@@ -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,