panelui-native 0.113.0 → 0.114.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.
Files changed (44) hide show
  1. package/lib/module/components/bottom-sheet/index.js +11 -0
  2. package/lib/module/components/bottom-sheet/index.js.map +1 -1
  3. package/lib/module/components/camera/camera-model.js +142 -0
  4. package/lib/module/components/camera/camera-model.js.map +1 -0
  5. package/lib/module/components/camera/index.js +1547 -0
  6. package/lib/module/components/camera/index.js.map +1 -0
  7. package/lib/module/components/image-viewer/image-viewer-geometry.js +15 -0
  8. package/lib/module/components/image-viewer/image-viewer-geometry.js.map +1 -1
  9. package/lib/module/components/image-viewer/index.js +107 -38
  10. package/lib/module/components/image-viewer/index.js.map +1 -1
  11. package/lib/module/icons/index.js +20 -0
  12. package/lib/module/icons/index.js.map +1 -1
  13. package/lib/module/index.js +2 -1
  14. package/lib/module/index.js.map +1 -1
  15. package/lib/module/primitives/portal-store.js +79 -0
  16. package/lib/module/primitives/portal-store.js.map +1 -0
  17. package/lib/module/primitives/portal.js +21 -30
  18. package/lib/module/primitives/portal.js.map +1 -1
  19. package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
  20. package/lib/typescript/src/components/camera/camera-model.d.ts +79 -0
  21. package/lib/typescript/src/components/camera/camera-model.d.ts.map +1 -0
  22. package/lib/typescript/src/components/camera/index.d.ts +481 -0
  23. package/lib/typescript/src/components/camera/index.d.ts.map +1 -0
  24. package/lib/typescript/src/components/image-viewer/image-viewer-geometry.d.ts +9 -0
  25. package/lib/typescript/src/components/image-viewer/image-viewer-geometry.d.ts.map +1 -1
  26. package/lib/typescript/src/components/image-viewer/index.d.ts +10 -3
  27. package/lib/typescript/src/components/image-viewer/index.d.ts.map +1 -1
  28. package/lib/typescript/src/icons/index.d.ts +4 -0
  29. package/lib/typescript/src/icons/index.d.ts.map +1 -1
  30. package/lib/typescript/src/index.d.ts +2 -1
  31. package/lib/typescript/src/index.d.ts.map +1 -1
  32. package/lib/typescript/src/primitives/portal-store.d.ts +40 -0
  33. package/lib/typescript/src/primitives/portal-store.d.ts.map +1 -0
  34. package/lib/typescript/src/primitives/portal.d.ts.map +1 -1
  35. package/package.json +5 -1
  36. package/src/components/bottom-sheet/index.tsx +22 -1
  37. package/src/components/camera/camera-model.ts +140 -0
  38. package/src/components/camera/index.tsx +1881 -0
  39. package/src/components/image-viewer/image-viewer-geometry.ts +15 -0
  40. package/src/components/image-viewer/index.tsx +126 -43
  41. package/src/icons/index.tsx +8 -0
  42. package/src/index.ts +36 -0
  43. package/src/primitives/portal-store.ts +94 -0
  44. package/src/primitives/portal.tsx +21 -36
@@ -151,6 +151,21 @@ export function snapPage(
151
151
  return clamp(clamp(target, current - 1, current + 1), 0, count - 1);
152
152
  }
153
153
 
154
+ /** What a one-finger drag on a picture at its fitted size is for. */
155
+ export type DragIntent = 'undecided' | 'page' | 'dismiss';
156
+
157
+ /**
158
+ * Sideways across a gallery turns the page; anything else drags the picture
159
+ * away. A drag that has not moved yet is `undecided`: some gesture-handler
160
+ * versions report the activating event with no translation at all, and
161
+ * comparing zero with zero would take every swipe as a dismiss.
162
+ */
163
+ export function dragIntent(translationX: number, translationY: number, count: number): DragIntent {
164
+ 'worklet';
165
+ if (translationX === 0 && translationY === 0) return 'undecided';
166
+ return count > 1 && Math.abs(translationX) > Math.abs(translationY) ? 'page' : 'dismiss';
167
+ }
168
+
154
169
  /** How far a drag has to travel, counting where it was flung, to close the viewer. */
155
170
  export const DISMISS_DISTANCE = 110;
156
171
 
@@ -44,8 +44,8 @@
44
44
  * ## One gesture surface, decided at the start
45
45
  *
46
46
  * Pinching, panning a zoomed image, turning the page and dragging to dismiss all
47
- * begin as a one- or two-finger drag, and which one it is gets decided the
48
- * moment it starts: two fingers zoom, one finger on a zoomed image pans it, one
47
+ * begin as a one- or two-finger drag, and which one it is gets decided as
48
+ * soon as it moves: two fingers zoom, one finger on a zoomed image pans it, one
49
49
  * finger moving sideways across a gallery turns the page, and anything else
50
50
  * drags the image away. Deciding once keeps a drag from changing its mind
51
51
  * halfway, which is what makes a viewer feel loose.
@@ -105,6 +105,7 @@ import {
105
105
  clamp,
106
106
  coverSize,
107
107
  dragFade,
108
+ dragIntent,
108
109
  dragScale,
109
110
  fitWithin,
110
111
  focalTranslation,
@@ -155,10 +156,15 @@ const PHASE_OPENING = 0;
155
156
  const PHASE_OPEN = 1;
156
157
  const PHASE_CLOSING = 2;
157
158
 
159
+ /** Pins a subtree's `left` to the screen's left under a right-to-left layout. */
160
+ const PHYSICAL = { direction: 'ltr' } as const;
161
+
158
162
  const MODE_NONE = 0;
159
163
  const MODE_PAN = 1;
160
164
  const MODE_PAGE = 2;
161
165
  const MODE_DISMISS = 3;
166
+ /** The pan has started but not yet moved, so there is no direction to decide on. */
167
+ const MODE_UNDECIDED = 4;
162
168
 
163
169
  /* -------------------------------------------------------------------------- */
164
170
  /* Registry */
@@ -280,6 +286,13 @@ export interface ImageViewerProps {
280
286
  * screen while the picture is zoomed or dragged. `0` squares them.
281
287
  */
282
288
  cornerRadius?: number;
289
+ /**
290
+ * Which way the gallery runs. In `rtl` the next page sits to the left and a
291
+ * swipe to the right turns to it. Page numbers, `index` and the screen
292
+ * reader's next and previous stay in page order either way. To follow the
293
+ * reading direction, pass `useDirection()`.
294
+ */
295
+ dir?: 'ltr' | 'rtl';
283
296
  }
284
297
 
285
298
  function ImageViewerRoot({
@@ -298,6 +311,7 @@ function ImageViewerRoot({
298
311
  closeLabel = 'Close',
299
312
  inset = DEFAULT_INSET,
300
313
  cornerRadius = DEFAULT_CORNER_RADIUS,
314
+ dir = 'ltr',
301
315
  }: ImageViewerProps) {
302
316
  const storeRef = useRef<ViewerStore | null>(null);
303
317
  storeRef.current ??= new ViewerStore();
@@ -364,6 +378,7 @@ function ImageViewerRoot({
364
378
  closeLabel={closeLabel}
365
379
  inset={inset}
366
380
  cornerRadius={cornerRadius}
381
+ sign={dir === 'rtl' ? -1 : 1}
367
382
  />
368
383
  </ModalPortal>
369
384
  ) : null}
@@ -584,6 +599,8 @@ interface ViewerOverlayProps {
584
599
  closeLabel: string;
585
600
  inset: number;
586
601
  cornerRadius: number;
602
+ /** `-1` when the pages run right to left. */
603
+ sign: 1 | -1;
587
604
  }
588
605
 
589
606
  function ViewerOverlay({
@@ -602,6 +619,7 @@ function ViewerOverlay({
602
619
  closeLabel,
603
620
  inset,
604
621
  cornerRadius,
622
+ sign,
605
623
  }: ViewerOverlayProps) {
606
624
  const version = useSyncExternalStore(store.subscribe, store.getSnapshot);
607
625
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -662,7 +680,7 @@ function ViewerOverlay({
662
680
  const shrink = useSharedValue(1);
663
681
  const fade = useSharedValue(1);
664
682
 
665
- // The gallery.
683
+ // The gallery, in page order: `sign` mirrors it on the way to the screen.
666
684
  const pagerX = useSharedValue(-page * pageWidth);
667
685
  const pagerStart = useSharedValue(0);
668
686
  const activePage = useSharedValue(page);
@@ -730,6 +748,19 @@ function ViewerOverlay({
730
748
  [chrome, dragX, dragY, gestureMode, onExited, phase, reduced, scale, shrink, t, tx, ty]
731
749
  );
732
750
 
751
+ /**
752
+ * A drag or a tap outside closing the viewer. Measured again first, like a
753
+ * close from `open`: the measure taken when the page turned can predate the
754
+ * owner scrolling the new page's thumbnail into place.
755
+ */
756
+ const closeFromGesture = useCallback(
757
+ (velocityX: number, velocityY: number) => {
758
+ measureCurrent(() => runOnUI(startClose)(velocityX, velocityY));
759
+ onRequestClose();
760
+ },
761
+ [measureCurrent, onRequestClose, startClose]
762
+ );
763
+
733
764
  const settleOpen = useCallback(() => {
734
765
  phase.value = PHASE_OPENING;
735
766
  const done = (finished?: boolean) => {
@@ -880,6 +911,62 @@ function ViewerOverlay({
880
911
  settleZoom();
881
912
  });
882
913
 
914
+ const decide = (translationX: number, translationY: number) => {
915
+ 'worklet';
916
+ const intent = dragIntent(translationX, translationY, count);
917
+ if (intent === 'undecided') {
918
+ gestureMode.value = MODE_UNDECIDED;
919
+ return;
920
+ }
921
+ if (intent === 'page') {
922
+ gestureMode.value = MODE_PAGE;
923
+ cancelAnimation(pagerX);
924
+ pagerStart.value = pagerX.value;
925
+ return;
926
+ }
927
+ gestureMode.value = MODE_DISMISS;
928
+ chrome.value = withTiming(0, { duration: 120 });
929
+ if (haptics) runOnJS(selectionTick)();
930
+ };
931
+
932
+ /**
933
+ * Decides a pan that activated without moving, from the first event that
934
+ * has. Only while the viewer is still open and no pinch has started since:
935
+ * a close or a second finger arriving first leaves the drag doing nothing.
936
+ */
937
+ const resolve = (translationX: number, translationY: number) => {
938
+ 'worklet';
939
+ if (gestureMode.value !== MODE_UNDECIDED) return;
940
+ if (phase.value !== PHASE_OPEN || pinching.value) {
941
+ gestureMode.value = MODE_NONE;
942
+ return;
943
+ }
944
+ decide(translationX, translationY);
945
+ };
946
+
947
+ /** Moves whatever the drag was decided to move. */
948
+ const follow = (translationX: number, translationY: number) => {
949
+ 'worklet';
950
+ if (gestureMode.value === MODE_PAN) {
951
+ const boundX = panBound(fit.width, scale.value, W);
952
+ const boundY = panBound(fit.height, scale.value, H);
953
+ tx.value = rubberBandClamp(startX.value + translationX, -boundX, boundX, W);
954
+ ty.value = rubberBandClamp(startY.value + translationY, -boundY, boundY, H);
955
+ } else if (gestureMode.value === MODE_PAGE) {
956
+ pagerX.value = rubberBandClamp(
957
+ pagerStart.value + sign * translationX,
958
+ -(count - 1) * pageWidth,
959
+ 0,
960
+ W
961
+ );
962
+ } else if (gestureMode.value === MODE_DISMISS) {
963
+ dragX.value = translationX;
964
+ dragY.value = translationY;
965
+ shrink.value = dragScale(translationY, H);
966
+ fade.value = dragFade(translationY, H);
967
+ }
968
+ };
969
+
883
970
  const pan = Gesture.Pan()
884
971
  .maxPointers(1)
885
972
  .minDistance(8)
@@ -897,39 +984,22 @@ function ViewerOverlay({
897
984
  startY.value = ty.value;
898
985
  return;
899
986
  }
900
- if (count > 1 && Math.abs(event.translationX) > Math.abs(event.translationY)) {
901
- gestureMode.value = MODE_PAGE;
902
- cancelAnimation(pagerX);
903
- pagerStart.value = pagerX.value;
904
- return;
905
- }
906
- gestureMode.value = MODE_DISMISS;
907
- chrome.value = withTiming(0, { duration: 120 });
908
- if (haptics) runOnJS(selectionTick)();
987
+ decide(event.translationX, event.translationY);
909
988
  })
910
989
  .onUpdate((event) => {
911
990
  'worklet';
912
- if (gestureMode.value === MODE_PAN) {
913
- const boundX = panBound(fit.width, scale.value, W);
914
- const boundY = panBound(fit.height, scale.value, H);
915
- tx.value = rubberBandClamp(startX.value + event.translationX, -boundX, boundX, W);
916
- ty.value = rubberBandClamp(startY.value + event.translationY, -boundY, boundY, H);
917
- } else if (gestureMode.value === MODE_PAGE) {
918
- pagerX.value = rubberBandClamp(
919
- pagerStart.value + event.translationX,
920
- -(count - 1) * pageWidth,
921
- 0,
922
- W
923
- );
924
- } else if (gestureMode.value === MODE_DISMISS) {
925
- dragX.value = event.translationX;
926
- dragY.value = event.translationY;
927
- shrink.value = dragScale(event.translationY, H);
928
- fade.value = dragFade(event.translationY, H);
929
- }
991
+ // Activated without a translation: the first update that has moved decides.
992
+ resolve(event.translationX, event.translationY);
993
+ follow(event.translationX, event.translationY);
930
994
  })
931
995
  .onEnd((event, success) => {
932
996
  'worklet';
997
+ // A quick flick can end before any update has moved, so the end event
998
+ // may be the first to: decide on it and apply it before acting.
999
+ if (gestureMode.value === MODE_UNDECIDED) {
1000
+ resolve(event.translationX, event.translationY);
1001
+ follow(event.translationX, event.translationY);
1002
+ }
933
1003
  const current = gestureMode.value;
934
1004
  gestureMode.value = MODE_NONE;
935
1005
  if (current === MODE_PAN) {
@@ -946,19 +1016,19 @@ function ViewerOverlay({
946
1016
  ty.value = withDecay({ velocity: event.velocityY, clamp: [-boundY, boundY] });
947
1017
  }
948
1018
  } else if (current === MODE_PAGE) {
949
- const target = snapPage(pagerX.value, event.velocityX, activePage.value, count, pageWidth);
950
- pagerX.value = withSpring(-target * pageWidth, {
951
- ...SETTLE_SPRING,
952
- velocity: event.velocityX,
953
- });
1019
+ const velocity = sign * event.velocityX;
1020
+ const target = snapPage(pagerX.value, velocity, activePage.value, count, pageWidth);
1021
+ pagerX.value = withSpring(-target * pageWidth, { ...SETTLE_SPRING, velocity });
954
1022
  if (target !== activePage.value) {
955
1023
  activePage.value = target;
956
1024
  runOnJS(onIndexChange)(target);
957
1025
  }
958
1026
  } else if (current === MODE_DISMISS) {
959
1027
  if (success && shouldDismiss(event.translationY, event.velocityY)) {
960
- startClose(event.velocityX, event.velocityY);
961
- runOnJS(onRequestClose)();
1028
+ // Closing now, before the measure: no other gesture starts, and the
1029
+ // `open` effect leaves the flight home to `closeFromGesture`.
1030
+ phase.value = PHASE_CLOSING;
1031
+ runOnJS(closeFromGesture)(event.velocityX, event.velocityY);
962
1032
  return;
963
1033
  }
964
1034
  dragX.value = withSpring(0, SETTLE_SPRING);
@@ -1007,8 +1077,8 @@ function ViewerOverlay({
1007
1077
  if (!success || phase.value !== PHASE_OPEN) return;
1008
1078
  const onImage = hitsRect(event.x, event.y, fit, scale.value, tx.value, ty.value);
1009
1079
  if (!onImage) {
1010
- startClose(0, 0);
1011
- runOnJS(onRequestClose)();
1080
+ phase.value = PHASE_CLOSING;
1081
+ runOnJS(closeFromGesture)(0, 0);
1012
1082
  return;
1013
1083
  }
1014
1084
  chromeOn.value = !chromeOn.value;
@@ -1048,7 +1118,8 @@ function ViewerOverlay({
1048
1118
  scale,
1049
1119
  settleZoom,
1050
1120
  shrink,
1051
- startClose,
1121
+ closeFromGesture,
1122
+ sign,
1052
1123
  startScale,
1053
1124
  startX,
1054
1125
  startY,
@@ -1056,7 +1127,9 @@ function ViewerOverlay({
1056
1127
  ty,
1057
1128
  ]);
1058
1129
 
1059
- const pagerStyle = useAnimatedStyle(() => ({ transform: [{ translateX: pagerX.value }] }));
1130
+ const pagerStyle = useAnimatedStyle(() => ({
1131
+ transform: [{ translateX: sign * pagerX.value }],
1132
+ }));
1060
1133
 
1061
1134
  const chromeStyle = useAnimatedStyle(() => ({
1062
1135
  opacity: chrome.value * clamp(t.value, 0, 1) * fade.value,
@@ -1084,6 +1157,10 @@ function ViewerOverlay({
1084
1157
  return (
1085
1158
  <View
1086
1159
  style={StyleSheet.absoluteFill}
1160
+ // Mounted until the picture is back in the page, which is after `open`
1161
+ // has gone false. Taking touches for that flight swallowed the first tap
1162
+ // on the screen behind it.
1163
+ pointerEvents={open ? 'auto' : 'none'}
1087
1164
  accessibilityViewIsModal
1088
1165
  onAccessibilityEscape={onRequestClose}
1089
1166
  >
@@ -1111,13 +1188,19 @@ function ViewerOverlay({
1111
1188
  ]}
1112
1189
  onAccessibilityAction={onAccessibilityAction}
1113
1190
  >
1114
- <Animated.View pointerEvents="none" style={[StyleSheet.absoluteFill, pagerStyle]}>
1191
+ {/* Laid out left to right whatever the app's direction: the pages and
1192
+ the flight are placed in measured screen pixels, which a right-to-left
1193
+ layout would mirror. `sign` does the mirroring the gallery wants. */}
1194
+ <Animated.View
1195
+ pointerEvents="none"
1196
+ style={[StyleSheet.absoluteFill, PHYSICAL, pagerStyle]}
1197
+ >
1115
1198
  {visiblePages.map(({ entry, at }) => (
1116
1199
  <ViewerPage
1117
1200
  key={entry.id}
1118
1201
  item={entry}
1119
1202
  at={at}
1120
- left={at * pageWidth}
1203
+ left={sign * at * pageWidth}
1121
1204
  viewport={{ width: W, height: H }}
1122
1205
  box={box}
1123
1206
  cornerRadius={cornerRadius}
@@ -68,6 +68,10 @@ import HgBookmark01Icon from '@hugeicons/core-free-icons/Bookmark01Icon';
68
68
  import HgBubbleChatIcon from '@hugeicons/core-free-icons/BubbleChatIcon';
69
69
  import HgCalendar03Icon from '@hugeicons/core-free-icons/Calendar03Icon';
70
70
  import HgCamera01Icon from '@hugeicons/core-free-icons/Camera01Icon';
71
+ import HgCameraRotated01Icon from '@hugeicons/core-free-icons/CameraRotated01Icon';
72
+ import HgFlashIcon from '@hugeicons/core-free-icons/FlashIcon';
73
+ import HgFlashOffIcon from '@hugeicons/core-free-icons/FlashOffIcon';
74
+ import HgTimer02Icon from '@hugeicons/core-free-icons/Timer02Icon';
71
75
  import HgCancel01Icon from '@hugeicons/core-free-icons/Cancel01Icon';
72
76
  import HgCheckListIcon from '@hugeicons/core-free-icons/CheckListIcon';
73
77
  import HgCheckmarkCircle01Icon from '@hugeicons/core-free-icons/CheckmarkCircle01Icon';
@@ -360,6 +364,10 @@ export const ImageIcon = icon(HgImage01Icon, { size: 16, color: '#737373' });
360
364
  export const PaperclipIcon = icon(HgAttachment01Icon, { size: 16, color: '#737373' });
361
365
  export const CameraIcon = icon(HgCamera01Icon, { size: 16, color: '#737373' });
362
366
  export const VideoIcon = icon(HgVideo01Icon, { size: 16, color: '#737373' });
367
+ export const CameraSwitchIcon = icon(HgCameraRotated01Icon, { size: 16, color: '#737373' });
368
+ export const FlashIcon = icon(HgFlashIcon, { size: 16, color: '#737373' });
369
+ export const FlashOffIcon = icon(HgFlashOffIcon, { size: 16, color: '#737373' });
370
+ export const TimerIcon = icon(HgTimer02Icon, { size: 16, color: '#737373' });
363
371
  export const UploadIcon = icon(HgCloudUploadIcon, { size: 16, color: '#737373' });
364
372
  export const MicIcon = icon(HgMic01Icon, { size: 16, color: '#737373' });
365
373
  export const AudioLinesIcon = icon(HgAudioWave01Icon, { size: 16, color: '#737373' });
package/src/index.ts CHANGED
@@ -91,6 +91,38 @@ export {
91
91
  type AvatarCropperSource,
92
92
  type PickAvatarImageOptions,
93
93
  } from './components/avatar-cropper';
94
+ export {
95
+ Camera,
96
+ hasCamera,
97
+ useCamera,
98
+ type CameraProps,
99
+ type CameraPreviewProps,
100
+ type CameraHeaderProps,
101
+ type CameraFooterProps,
102
+ type CameraControlsProps,
103
+ type CameraButtonProps,
104
+ type CameraFlashProps,
105
+ type CameraTimerProps,
106
+ type CameraDurationProps,
107
+ type CameraZoomProps,
108
+ type CameraModesProps,
109
+ type CameraLibraryProps,
110
+ type CameraFlipProps,
111
+ type CameraShutterProps,
112
+ type CameraHandle,
113
+ type CameraState,
114
+ type CameraCapture,
115
+ type CameraPhoto,
116
+ type CameraVideo,
117
+ type CameraPictureOptions,
118
+ type CameraViewOptions,
119
+ type CameraViewHandle,
120
+ type CameraZoomStop,
121
+ type CameraFacing,
122
+ type CameraFlashMode,
123
+ type CameraMode,
124
+ type CameraTimerDelay,
125
+ } from './components/camera';
94
126
  export {
95
127
  Attachment,
96
128
  type AttachmentProps,
@@ -1577,7 +1609,10 @@ export {
1577
1609
  BoldIcon,
1578
1610
  CalendarIcon,
1579
1611
  CameraIcon,
1612
+ CameraSwitchIcon,
1580
1613
  CardIcon,
1614
+ FlashIcon,
1615
+ FlashOffIcon,
1581
1616
  HeadsetIcon,
1582
1617
  LifebuoyIcon,
1583
1618
  HelpCircleIcon,
@@ -1647,6 +1682,7 @@ export {
1647
1682
  SparklesIcon,
1648
1683
  StarIcon,
1649
1684
  SunIcon,
1685
+ TimerIcon,
1650
1686
  TrashIcon,
1651
1687
  UnlockIcon,
1652
1688
  UploadIcon,
@@ -0,0 +1,94 @@
1
+ import type { ReactNode } from 'react';
2
+
3
+ /** One portal's content, and whether it is on its way out. */
4
+ export interface PortalEntry {
5
+ node: ReactNode;
6
+ /**
7
+ * Set for the one frame between the owner going away and the content being
8
+ * removed. The host stops the entry taking touches while it is set.
9
+ */
10
+ leaving: boolean;
11
+ }
12
+
13
+ export type PortalMap = ReadonlyMap<string, PortalEntry>;
14
+
15
+ /** Runs `callback` on a later frame and returns a function that cancels it. */
16
+ export type PortalScheduler = (callback: () => void) => () => void;
17
+
18
+ const nextFrame: PortalScheduler = (callback) => {
19
+ if (typeof requestAnimationFrame === 'function') {
20
+ const id = requestAnimationFrame(callback);
21
+ return () => cancelAnimationFrame(id);
22
+ }
23
+ const id = setTimeout(callback, 16);
24
+ return () => clearTimeout(id);
25
+ };
26
+
27
+ /**
28
+ * What every portal has put on screen, keyed by portal.
29
+ *
30
+ * Removal takes two commits rather than one. An overlay's views usually carry
31
+ * an `exiting` animation, and Reanimated keeps a removed view on screen until
32
+ * that animation ends. The view keeps the props it had when it was removed but
33
+ * not its handlers, so a full-screen backdrop left fading out takes every touch
34
+ * and does nothing with it: a tap on the button that opened the overlay is
35
+ * simply lost. So `unmount` first marks the entry `leaving`, which the host
36
+ * turns into `pointerEvents="none"`, and removes it a frame later. Whatever
37
+ * stays behind to animate has already stopped taking touches.
38
+ */
39
+ export class PortalStore {
40
+ private portals = new Map<string, PortalEntry>();
41
+ private snapshot: PortalMap = this.portals;
42
+ private listeners = new Set<() => void>();
43
+ private pending = new Map<string, () => void>();
44
+ private schedule: PortalScheduler;
45
+
46
+ constructor(schedule: PortalScheduler = nextFrame) {
47
+ this.schedule = schedule;
48
+ }
49
+
50
+ mount = (key: string, node: ReactNode) => {
51
+ // A remount inside the leaving frame (StrictMode, or a quick reopen) keeps
52
+ // the entry instead of losing it to the removal already scheduled.
53
+ this.cancel(key);
54
+ this.portals.set(key, { node, leaving: false });
55
+ this.emit();
56
+ };
57
+
58
+ unmount = (key: string) => {
59
+ const entry = this.portals.get(key);
60
+ if (!entry || entry.leaving) return;
61
+ this.portals.set(key, { node: entry.node, leaving: true });
62
+ this.emit();
63
+ this.pending.set(
64
+ key,
65
+ this.schedule(() => {
66
+ this.pending.delete(key);
67
+ if (!this.portals.get(key)?.leaving) return;
68
+ this.portals.delete(key);
69
+ this.emit();
70
+ })
71
+ );
72
+ };
73
+
74
+ subscribe = (listener: () => void) => {
75
+ this.listeners.add(listener);
76
+ return () => {
77
+ this.listeners.delete(listener);
78
+ };
79
+ };
80
+
81
+ getSnapshot = (): PortalMap => this.snapshot;
82
+
83
+ private cancel(key: string) {
84
+ const cancel = this.pending.get(key);
85
+ if (!cancel) return;
86
+ cancel();
87
+ this.pending.delete(key);
88
+ }
89
+
90
+ private emit() {
91
+ this.snapshot = new Map(this.portals);
92
+ this.listeners.forEach((listener) => listener());
93
+ }
94
+ }
@@ -7,39 +7,11 @@ import {
7
7
  useSyncExternalStore,
8
8
  type ReactNode,
9
9
  } from 'react';
10
+ import { StyleSheet, View } from 'react-native';
10
11
  import { ModalIsolationStore } from './modal-isolation-store';
12
+ import { PortalStore } from './portal-store';
11
13
  import { captureFocusRestore, FocusRestorationStore } from './focus-restoration-store';
12
14
 
13
- type PortalMap = ReadonlyMap<string, ReactNode>;
14
-
15
- class PortalStore {
16
- private portals = new Map<string, ReactNode>();
17
- private snapshot: PortalMap = this.portals;
18
- private listeners = new Set<() => void>();
19
-
20
- mount = (key: string, node: ReactNode) => {
21
- this.portals.set(key, node);
22
- this.emit();
23
- };
24
-
25
- unmount = (key: string) => {
26
- this.portals.delete(key);
27
- this.emit();
28
- };
29
-
30
- subscribe = (listener: () => void) => {
31
- this.listeners.add(listener);
32
- return () => this.listeners.delete(listener);
33
- };
34
-
35
- getSnapshot = (): PortalMap => this.snapshot;
36
-
37
- private emit() {
38
- this.snapshot = new Map(this.portals);
39
- this.listeners.forEach((listener) => listener());
40
- }
41
- }
42
-
43
15
  const PortalContext = createContext<PortalStore | null>(null);
44
16
  const ModalIsolationContext = createContext<ModalIsolationStore | null>(null);
45
17
  const FocusRestorationContext = createContext<FocusRestorationStore | null>(null);
@@ -154,13 +126,26 @@ export function PortalHost() {
154
126
 
155
127
  return (
156
128
  <>
157
- {Array.from(portals.entries(), ([key, node]) => (
158
- <PortalFragment key={key}>{node}</PortalFragment>
129
+ {Array.from(portals.entries(), ([key, { node, leaving }]) => (
130
+ /*
131
+ * Every entry sits in a full-screen layer from its first render, so the
132
+ * frame it is marked `leaving` changes one prop rather than remounting
133
+ * the content. `box-none` passes through wherever the content has
134
+ * nothing, which is how an open overlay already behaved. `none` is the
135
+ * frame before removal: views Reanimated keeps on screen to finish an
136
+ * exit animation then sit under a layer that takes no touches, so a tap
137
+ * on the screen behind reaches it. `collapsable` keeps the layer from
138
+ * being flattened away, since the prop has to live on a real view.
139
+ */
140
+ <View
141
+ key={key}
142
+ collapsable={false}
143
+ pointerEvents={leaving ? 'none' : 'box-none'}
144
+ style={StyleSheet.absoluteFill}
145
+ >
146
+ {node}
147
+ </View>
159
148
  ))}
160
149
  </>
161
150
  );
162
151
  }
163
-
164
- function PortalFragment({ children }: { children: ReactNode }) {
165
- return <>{children}</>;
166
- }