@vvedantb/native-ui 0.21.0-next-90c9e82619627a37726c0e2115cd6bbacfd21bcb → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
- import { useState } from "react";
2
+ import { useContext, useState } from "react";
3
3
  import { FlatList, Image, Modal, Pressable, Text, View, useWindowDimensions } from "react-native";
4
4
  import { useSafeAreaInsets } from "react-native-safe-area-context";
5
5
  import { IconExternalLink, IconX } from "@tabler/icons-react-native";
@@ -9,6 +9,7 @@ import { withAlpha } from "../palette";
9
9
  import { tokens } from "../tokens";
10
10
  import { useReduceMotion } from "../useReduceMotion";
11
11
  import { useScheme } from "../useScheme";
12
+ import { ZoomImageContext } from "./zoomImage";
12
13
  import { imageAspect, imageGridTiles, imageViewerPage, MEDIA } from "./imageGrid";
13
14
  /**
14
15
  * The web `ImageGalleryPreview`: one image at its own shape (as wide as the
@@ -338,8 +339,11 @@ function _temp(tile) {
338
339
  * The web `ImageLightbox` on a phone: the images full screen on black, one
339
340
  * page each, swiped sideways (TalkBack and VoiceOver step with the adjustable
340
341
  * actions). Close (X, or Android back) and "Open in browser" sit at the top,
341
- * "2 / 5" at the bottom when there is more than one. No pinch zoom. It fades
342
- * in, or appears at once with Reduce Motion.
342
+ * "2 / 5" at the bottom when there is more than one. Under
343
+ * `GestureHandlerProvider` each image zooms: pinch (up to 5x, about the
344
+ * fingers), double tap (2.5x on the tapped point, again to fit), and a drag
345
+ * pans it while zoomed instead of turning the page. It fades in, or appears
346
+ * at once with Reduce Motion.
343
347
  */
344
348
  export function ImageViewer(t0) {
345
349
  const $ = _c(10);
@@ -357,7 +361,7 @@ export function ImageViewer(t0) {
357
361
  const t1 = reduceMotion ? "none" : "fade";
358
362
  let t2;
359
363
  if ($[0] !== images || $[1] !== index || $[2] !== onClose || $[3] !== onIndexChange || $[4] !== onOpenImage) {
360
- t2 = <ViewerPages images={images} index={index} onIndexChange={onIndexChange} onClose={onClose} onOpenImage={onOpenImage} />;
364
+ t2 = <ViewerRoot><ViewerPages images={images} index={index} onIndexChange={onIndexChange} onClose={onClose} onOpenImage={onOpenImage} /></ViewerRoot>;
361
365
  $[0] = images;
362
366
  $[1] = index;
363
367
  $[2] = onClose;
@@ -379,8 +383,27 @@ export function ImageViewer(t0) {
379
383
  }
380
384
  return t3;
381
385
  }
386
+
387
+ /** The zoom parts' gesture root, when there are zoom parts. */
388
+ function ViewerRoot(t0) {
389
+ const $ = _c(3);
390
+ const {
391
+ children
392
+ } = t0;
393
+ const zoom = useContext(ZoomImageContext);
394
+ let t1;
395
+ if ($[0] !== children || $[1] !== zoom) {
396
+ t1 = zoom ? <zoom.Root>{children}</zoom.Root> : children;
397
+ $[0] = children;
398
+ $[1] = zoom;
399
+ $[2] = t1;
400
+ } else {
401
+ t1 = $[2];
402
+ }
403
+ return t1;
404
+ }
382
405
  function ViewerPages(t0) {
383
- const $ = _c(48);
406
+ const $ = _c(50);
384
407
  const {
385
408
  images,
386
409
  index,
@@ -392,6 +415,8 @@ function ViewerPages(t0) {
392
415
  width,
393
416
  height
394
417
  } = useWindowDimensions();
418
+ const zoom = useContext(ZoomImageContext);
419
+ const [zoomed, setZoomed] = useState(false);
395
420
  const insets = useSafeAreaInsets();
396
421
  const count = images.length;
397
422
  const current = images[index];
@@ -405,35 +430,36 @@ function ViewerPages(t0) {
405
430
  } else {
406
431
  t1 = $[0];
407
432
  }
408
- let t2;
433
+ const t2 = !zoomed;
434
+ let t3;
409
435
  if ($[1] !== width) {
410
- t2 = (_, item) => ({
436
+ t3 = (_, item) => ({
411
437
  length: width,
412
438
  offset: width * item,
413
439
  index: item
414
440
  });
415
441
  $[1] = width;
416
- $[2] = t2;
442
+ $[2] = t3;
417
443
  } else {
418
- t2 = $[2];
444
+ t3 = $[2];
419
445
  }
420
- let t3;
446
+ let t4;
421
447
  if ($[3] !== count || $[4] !== onIndexChange || $[5] !== width) {
422
- t3 = event => onIndexChange(imageViewerPage(event.nativeEvent.contentOffset.x, width, count));
448
+ t4 = event => onIndexChange(imageViewerPage(event.nativeEvent.contentOffset.x, width, count));
423
449
  $[3] = count;
424
450
  $[4] = onIndexChange;
425
451
  $[5] = width;
426
- $[6] = t3;
452
+ $[6] = t4;
427
453
  } else {
428
- t3 = $[6];
454
+ t4 = $[6];
429
455
  }
430
- let t4;
431
- if ($[7] !== height || $[8] !== width) {
432
- t4 = t5 => {
456
+ let t5;
457
+ if ($[7] !== height || $[8] !== width || $[9] !== zoom) {
458
+ t5 = t6 => {
433
459
  const {
434
460
  item: item_1
435
- } = t5;
436
- return <View style={{
461
+ } = t6;
462
+ return zoom ? <zoom.Image image={item_1} width={width} height={height} onZoomChange={setZoomed} /> : <View style={{
437
463
  width,
438
464
  height
439
465
  }} className="items-center justify-center"><Image source={{
@@ -445,110 +471,112 @@ function ViewerPages(t0) {
445
471
  };
446
472
  $[7] = height;
447
473
  $[8] = width;
448
- $[9] = t4;
474
+ $[9] = zoom;
475
+ $[10] = t5;
449
476
  } else {
450
- t4 = $[9];
477
+ t5 = $[10];
451
478
  }
452
- const t5 = current?.alt ?? "Image";
453
- let t6;
454
- if ($[10] !== count || $[11] !== index) {
455
- t6 = count > 1 ? {
479
+ const t6 = current?.alt ?? "Image";
480
+ let t7;
481
+ if ($[11] !== count || $[12] !== index) {
482
+ t7 = count > 1 ? {
456
483
  text: `${index + 1} of ${count}`
457
484
  } : undefined;
458
- $[10] = count;
459
- $[11] = index;
460
- $[12] = t6;
485
+ $[11] = count;
486
+ $[12] = index;
487
+ $[13] = t7;
461
488
  } else {
462
- t6 = $[12];
489
+ t7 = $[13];
463
490
  }
464
- let t7;
465
- if ($[13] !== count) {
466
- t7 = count > 1 ? [{
491
+ let t8;
492
+ if ($[14] !== count) {
493
+ t8 = count > 1 ? [{
467
494
  name: "increment"
468
495
  }, {
469
496
  name: "decrement"
470
497
  }] : undefined;
471
- $[13] = count;
472
- $[14] = t7;
498
+ $[14] = count;
499
+ $[15] = t8;
473
500
  } else {
474
- t7 = $[14];
501
+ t8 = $[15];
475
502
  }
476
- let t8;
477
- if ($[15] !== count || $[16] !== index || $[17] !== onIndexChange) {
478
- t8 = event_0 => {
503
+ let t9;
504
+ if ($[16] !== count || $[17] !== index || $[18] !== onIndexChange) {
505
+ t9 = event_0 => {
479
506
  const next = event_0.nativeEvent.actionName === "increment" ? index + 1 : index - 1;
480
507
  if (next >= 0 && next < count) {
481
508
  onIndexChange(next);
482
509
  }
483
510
  };
484
- $[15] = count;
485
- $[16] = index;
486
- $[17] = onIndexChange;
487
- $[18] = t8;
511
+ $[16] = count;
512
+ $[17] = index;
513
+ $[18] = onIndexChange;
514
+ $[19] = t9;
488
515
  } else {
489
- t8 = $[18];
516
+ t9 = $[19];
517
+ }
518
+ let t10;
519
+ if ($[20] !== images || $[21] !== index || $[22] !== t2 || $[23] !== t3 || $[24] !== t4 || $[25] !== t5 || $[26] !== t6 || $[27] !== t7 || $[28] !== t8 || $[29] !== t9) {
520
+ t10 = <FlatList data={images} horizontal={true} pagingEnabled={true} scrollEnabled={t2} showsHorizontalScrollIndicator={false} initialScrollIndex={index} getItemLayout={t3} keyExtractor={_temp3} onMomentumScrollEnd={t4} renderItem={t5} accessibilityRole="adjustable" accessibilityLabel={t6} accessibilityValue={t7} accessibilityActions={t8} onAccessibilityAction={t9} />;
521
+ $[20] = images;
522
+ $[21] = index;
523
+ $[22] = t2;
524
+ $[23] = t3;
525
+ $[24] = t4;
526
+ $[25] = t5;
527
+ $[26] = t6;
528
+ $[27] = t7;
529
+ $[28] = t8;
530
+ $[29] = t9;
531
+ $[30] = t10;
532
+ } else {
533
+ t10 = $[30];
490
534
  }
491
- let t9;
492
- if ($[19] !== images || $[20] !== index || $[21] !== t2 || $[22] !== t3 || $[23] !== t4 || $[24] !== t5 || $[25] !== t6 || $[26] !== t7 || $[27] !== t8) {
493
- t9 = <FlatList data={images} horizontal={true} pagingEnabled={true} showsHorizontalScrollIndicator={false} initialScrollIndex={index} getItemLayout={t2} keyExtractor={_temp3} onMomentumScrollEnd={t3} renderItem={t4} accessibilityRole="adjustable" accessibilityLabel={t5} accessibilityValue={t6} accessibilityActions={t7} onAccessibilityAction={t8} />;
494
- $[19] = images;
495
- $[20] = index;
496
- $[21] = t2;
497
- $[22] = t3;
498
- $[23] = t4;
499
- $[24] = t5;
500
- $[25] = t6;
501
- $[26] = t7;
502
- $[27] = t8;
503
- $[28] = t9;
504
- } else {
505
- t9 = $[28];
506
- }
507
- const t10 = insets.top + 8;
508
- let t11;
509
- if ($[29] !== t10) {
510
- t11 = {
511
- top: t10
535
+ const t11 = insets.top + 8;
536
+ let t12;
537
+ if ($[31] !== t11) {
538
+ t12 = {
539
+ top: t11
512
540
  };
513
- $[29] = t10;
514
- $[30] = t11;
541
+ $[31] = t11;
542
+ $[32] = t12;
515
543
  } else {
516
- t11 = $[30];
544
+ t12 = $[32];
517
545
  }
518
- let t12;
519
- if ($[31] !== current || $[32] !== onOpenImage) {
520
- t12 = onOpenImage && current ? <Pressable accessibilityRole="link" onPress={() => onOpenImage(current.url)} className="min-h-11 flex-row items-center gap-1.5 rounded-full px-3 active:opacity-70" style={{
546
+ let t13;
547
+ if ($[33] !== current || $[34] !== onOpenImage) {
548
+ t13 = onOpenImage && current ? <Pressable accessibilityRole="link" onPress={() => onOpenImage(current.url)} className="min-h-11 flex-row items-center gap-1.5 rounded-full px-3 active:opacity-70" style={{
521
549
  backgroundColor: withAlpha(tokens.color.black, 0.6)
522
550
  }}><IconExternalLink size={16} color={white} /><Text className="font-instrument-sans text-sm" style={{
523
551
  color: white
524
552
  }}>Open in browser</Text></Pressable> : <View />;
525
- $[31] = current;
526
- $[32] = onOpenImage;
527
- $[33] = t12;
528
- } else {
529
- t12 = $[33];
530
- }
531
- let t13;
532
- if ($[34] !== onClose) {
533
- t13 = <IconButton icon={IconX} label="Close" variant="overlay" size="lg" onPress={onClose} />;
534
- $[34] = onClose;
553
+ $[33] = current;
554
+ $[34] = onOpenImage;
535
555
  $[35] = t13;
536
556
  } else {
537
557
  t13 = $[35];
538
558
  }
539
559
  let t14;
540
- if ($[36] !== t11 || $[37] !== t12 || $[38] !== t13) {
541
- t14 = <View className="absolute left-0 right-0 flex-row items-center justify-between px-3" style={t11}>{t12}{t13}</View>;
542
- $[36] = t11;
543
- $[37] = t12;
544
- $[38] = t13;
545
- $[39] = t14;
560
+ if ($[36] !== onClose) {
561
+ t14 = <IconButton icon={IconX} label="Close" variant="overlay" size="lg" onPress={onClose} />;
562
+ $[36] = onClose;
563
+ $[37] = t14;
546
564
  } else {
547
- t14 = $[39];
565
+ t14 = $[37];
548
566
  }
549
567
  let t15;
550
- if ($[40] !== count || $[41] !== index || $[42] !== insets.bottom) {
551
- t15 = count > 1 ? <View className="absolute left-0 right-0 items-center" style={{
568
+ if ($[38] !== t12 || $[39] !== t13 || $[40] !== t14) {
569
+ t15 = <View className="absolute left-0 right-0 flex-row items-center justify-between px-3" style={t12}>{t13}{t14}</View>;
570
+ $[38] = t12;
571
+ $[39] = t13;
572
+ $[40] = t14;
573
+ $[41] = t15;
574
+ } else {
575
+ t15 = $[41];
576
+ }
577
+ let t16;
578
+ if ($[42] !== count || $[43] !== index || $[44] !== insets.bottom) {
579
+ t16 = count > 1 ? <View className="absolute left-0 right-0 items-center" style={{
552
580
  bottom: insets.bottom + 16
553
581
  }} pointerEvents="none"><View className="rounded-full px-3 py-1" style={{
554
582
  backgroundColor: withAlpha(tokens.color.black, 0.6)
@@ -556,24 +584,24 @@ function ViewerPages(t0) {
556
584
  color: white,
557
585
  fontVariant: ["tabular-nums"]
558
586
  }}>{`${index + 1} / ${count}`}</Text></View></View> : null;
559
- $[40] = count;
560
- $[41] = index;
561
- $[42] = insets.bottom;
562
- $[43] = t15;
587
+ $[42] = count;
588
+ $[43] = index;
589
+ $[44] = insets.bottom;
590
+ $[45] = t16;
563
591
  } else {
564
- t15 = $[43];
565
- }
566
- let t16;
567
- if ($[44] !== t14 || $[45] !== t15 || $[46] !== t9) {
568
- t16 = <View className="flex-1" style={t1}>{t9}{t14}{t15}</View>;
569
- $[44] = t14;
570
- $[45] = t15;
571
- $[46] = t9;
572
- $[47] = t16;
592
+ t16 = $[45];
593
+ }
594
+ let t17;
595
+ if ($[46] !== t10 || $[47] !== t15 || $[48] !== t16) {
596
+ t17 = <View className="flex-1" style={t1}>{t10}{t15}{t16}</View>;
597
+ $[46] = t10;
598
+ $[47] = t15;
599
+ $[48] = t16;
600
+ $[49] = t17;
573
601
  } else {
574
- t16 = $[47];
602
+ t17 = $[49];
575
603
  }
576
- return t16;
604
+ return t17;
577
605
  }
578
606
  function _temp3(image, item_0) {
579
607
  return `${item_0}:${image.url}`;
@@ -0,0 +1,74 @@
1
+ // The zoom rules of `ImageViewer`, as plain worklets: the gesture part
2
+ // (`zoom-image-gesture`) runs them on the UI thread, and tests call them.
3
+
4
+ /** Fitted size is 1; a pinch goes up to 5 and rubber-bands a little past both ends. */
5
+ export const ZOOM = {
6
+ min: 1,
7
+ max: 5,
8
+ doubleTap: 2.5
9
+ };
10
+
11
+ /** The scale while pinching: free inside [min, max], and a quarter of the overshoot past either end. */
12
+ export function zoomScale(raw) {
13
+ "worklet";
14
+
15
+ if (raw < ZOOM.min) return ZOOM.min - (ZOOM.min - raw) / 4;
16
+ if (raw > ZOOM.max) return ZOOM.max + (raw - ZOOM.max) / 4;
17
+ return raw;
18
+ }
19
+
20
+ /** Where a released pinch settles: back inside [min, max]. */
21
+ export function settleScale(scale) {
22
+ "worklet";
23
+
24
+ return Math.min(ZOOM.max, Math.max(ZOOM.min, scale));
25
+ }
26
+
27
+ /**
28
+ * The farthest the zoomed image may move from centre on one axis: half of
29
+ * what overflows the screen. At fitted size it cannot move.
30
+ */
31
+ export function panLimit(scale, size) {
32
+ "worklet";
33
+
34
+ return Math.max(0, (size * scale - size) / 2);
35
+ }
36
+
37
+ /** A translation kept inside `panLimit` on one axis. */
38
+ export function clampPan(translate, scale, size) {
39
+ "worklet";
40
+
41
+ const limit = panLimit(scale, size);
42
+ return Math.min(limit, Math.max(-limit, translate));
43
+ }
44
+
45
+ /**
46
+ * A double tap: zoomed out, it zooms to `ZOOM.doubleTap` on the tapped point
47
+ * (that point stays under the finger, as far as the edges allow); zoomed in,
48
+ * it goes back to fitted. `point` and `size` are in the page's coordinates.
49
+ */
50
+ export function doubleTapZoom(scale, point, size) {
51
+ "worklet";
52
+
53
+ if (scale > ZOOM.min) return {
54
+ scale: ZOOM.min,
55
+ x: 0,
56
+ y: 0
57
+ };
58
+ const next = ZOOM.doubleTap;
59
+ // The tapped point, from centre, moves out by `next`; shift back by that much.
60
+ const x = -(point.x - size.width / 2) * (next - 1);
61
+ const y = -(point.y - size.height / 2) * (next - 1);
62
+ return {
63
+ scale: next,
64
+ x: clampPan(x, next, size.width),
65
+ y: clampPan(y, next, size.height)
66
+ };
67
+ }
68
+
69
+ /** Whether the image is zoomed in: then a drag pans it instead of turning the page. */
70
+ export function isZoomed(scale) {
71
+ "worklet";
72
+
73
+ return scale > ZOOM.min + 0.01;
74
+ }
@@ -0,0 +1,201 @@
1
+ import { c as _c } from "react/compiler-runtime";
2
+ import { useState } from "react";
3
+ import { Image, StyleSheet, View } from "react-native";
4
+ import { Gesture, GestureDetector, GestureHandlerRootView } from "react-native-gesture-handler";
5
+ import Animated, { ReduceMotion, runOnJS, useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated";
6
+ import { clampPan, doubleTapZoom, isZoomed, settleScale, zoomScale, ZOOM } from "./imageZoom";
7
+
8
+ // Pinch, pan and double tap for an `ImageViewer` page, on Gesture Handler: the
9
+ // track phase runs as worklets on the UI thread, so the image never lags the
10
+ // fingers. The rules are plain worklets in `imageZoom`.
11
+
12
+ /** The settle after a release or a double tap: quick, no bounce past the edges. */
13
+ const ZOOM_SPRING = {
14
+ duration: 300,
15
+ dampingRatio: 1,
16
+ reduceMotion: ReduceMotion.System
17
+ };
18
+ function ZoomImage(t0) {
19
+ const $ = _c(22);
20
+ const {
21
+ image,
22
+ width,
23
+ height,
24
+ onZoomChange
25
+ } = t0;
26
+ const scale = useSharedValue(ZOOM.min);
27
+ const x = useSharedValue(0);
28
+ const y = useSharedValue(0);
29
+ const startScale = useSharedValue(ZOOM.min);
30
+ const startX = useSharedValue(0);
31
+ const startY = useSharedValue(0);
32
+ const focalX = useSharedValue(0);
33
+ const focalY = useSharedValue(0);
34
+ const [zoomed, setZoomed] = useState(false);
35
+ let t1;
36
+ if ($[0] !== onZoomChange) {
37
+ t1 = next => {
38
+ setZoomed(next);
39
+ onZoomChange(next);
40
+ };
41
+ $[0] = onZoomChange;
42
+ $[1] = t1;
43
+ } else {
44
+ t1 = $[1];
45
+ }
46
+ const report = t1;
47
+ const settle = () => {
48
+ "worklet";
49
+
50
+ const next_0 = settleScale(scale.get());
51
+ scale.set(withSpring(next_0, ZOOM_SPRING));
52
+ x.set(withSpring(clampPan(x.get(), next_0, width), ZOOM_SPRING));
53
+ y.set(withSpring(clampPan(y.get(), next_0, height), ZOOM_SPRING));
54
+ runOnJS(report)(isZoomed(next_0));
55
+ };
56
+ const pinch = Gesture.Pinch().onStart(event => {
57
+ startScale.set(scale.get());
58
+ startX.set(x.get());
59
+ startY.set(y.get());
60
+ focalX.set(event.focalX - width / 2);
61
+ focalY.set(event.focalY - height / 2);
62
+ }).onUpdate(event_0 => {
63
+ const next_1 = zoomScale(startScale.get() * event_0.scale);
64
+ const ratio = next_1 / startScale.get();
65
+ x.set(focalX.get() - (focalX.get() - startX.get()) * ratio);
66
+ y.set(focalY.get() - (focalY.get() - startY.get()) * ratio);
67
+ scale.set(next_1);
68
+ }).onEnd(() => {
69
+ settle();
70
+ });
71
+ const pan = Gesture.Pan().enabled(zoomed).averageTouches(true).onStart(() => {
72
+ startX.set(x.get());
73
+ startY.set(y.get());
74
+ }).onUpdate(event_1 => {
75
+ x.set(clampPan(startX.get() + event_1.translationX, scale.get(), width));
76
+ y.set(clampPan(startY.get() + event_1.translationY, scale.get(), height));
77
+ });
78
+ const doubleTap = Gesture.Tap().numberOfTaps(2).onEnd((event_2, success) => {
79
+ if (!success) {
80
+ return;
81
+ }
82
+ const target = doubleTapZoom(scale.get(), {
83
+ x: event_2.x,
84
+ y: event_2.y
85
+ }, {
86
+ width,
87
+ height
88
+ });
89
+ scale.set(withSpring(target.scale, ZOOM_SPRING));
90
+ x.set(withSpring(target.x, ZOOM_SPRING));
91
+ y.set(withSpring(target.y, ZOOM_SPRING));
92
+ runOnJS(report)(isZoomed(target.scale));
93
+ });
94
+ const style = useAnimatedStyle(() => ({
95
+ transform: [{
96
+ translateX: x.get()
97
+ }, {
98
+ translateY: y.get()
99
+ }, {
100
+ scale: scale.get()
101
+ }]
102
+ }));
103
+ const T0 = GestureDetector;
104
+ const t2 = Gesture.Simultaneous(pinch, pan, doubleTap);
105
+ let t3;
106
+ if ($[2] !== height || $[3] !== width) {
107
+ t3 = {
108
+ width,
109
+ height
110
+ };
111
+ $[2] = height;
112
+ $[3] = width;
113
+ $[4] = t3;
114
+ } else {
115
+ t3 = $[4];
116
+ }
117
+ let t4;
118
+ if ($[5] !== style) {
119
+ t4 = [styles.fill, style];
120
+ $[5] = style;
121
+ $[6] = t4;
122
+ } else {
123
+ t4 = $[6];
124
+ }
125
+ let t5;
126
+ if ($[7] !== image.url) {
127
+ t5 = {
128
+ uri: image.url
129
+ };
130
+ $[7] = image.url;
131
+ $[8] = t5;
132
+ } else {
133
+ t5 = $[8];
134
+ }
135
+ const t6 = image.alt ?? "Image";
136
+ let t7;
137
+ if ($[9] !== t5 || $[10] !== t6) {
138
+ t7 = <Image source={t5} resizeMode="contain" style={styles.fill} accessibilityLabel={t6} />;
139
+ $[9] = t5;
140
+ $[10] = t6;
141
+ $[11] = t7;
142
+ } else {
143
+ t7 = $[11];
144
+ }
145
+ let t8;
146
+ if ($[12] !== t4 || $[13] !== t7) {
147
+ t8 = <Animated.View style={t4}>{t7}</Animated.View>;
148
+ $[12] = t4;
149
+ $[13] = t7;
150
+ $[14] = t8;
151
+ } else {
152
+ t8 = $[14];
153
+ }
154
+ let t9;
155
+ if ($[15] !== t3 || $[16] !== t8) {
156
+ t9 = <View style={t3} collapsable={false}>{t8}</View>;
157
+ $[15] = t3;
158
+ $[16] = t8;
159
+ $[17] = t9;
160
+ } else {
161
+ t9 = $[17];
162
+ }
163
+ let t10;
164
+ if ($[18] !== T0 || $[19] !== t2 || $[20] !== t9) {
165
+ t10 = <T0 gesture={t2}>{t9}</T0>;
166
+ $[18] = T0;
167
+ $[19] = t2;
168
+ $[20] = t9;
169
+ $[21] = t10;
170
+ } else {
171
+ t10 = $[21];
172
+ }
173
+ return t10;
174
+ }
175
+
176
+ /** Gestures inside a `Modal` need a root view of their own. */
177
+ function ZoomRoot(t0) {
178
+ const $ = _c(2);
179
+ const {
180
+ children
181
+ } = t0;
182
+ let t1;
183
+ if ($[0] !== children) {
184
+ t1 = <GestureHandlerRootView style={styles.fill}>{children}</GestureHandlerRootView>;
185
+ $[0] = children;
186
+ $[1] = t1;
187
+ } else {
188
+ t1 = $[1];
189
+ }
190
+ return t1;
191
+ }
192
+ export const ZOOM_IMAGE_PARTS = {
193
+ Root: ZoomRoot,
194
+ Image: ZoomImage
195
+ };
196
+ const styles = StyleSheet.create({
197
+ fill: {
198
+ width: "100%",
199
+ height: "100%"
200
+ }
201
+ });
@@ -0,0 +1,13 @@
1
+ import { createContext } from "react";
2
+
3
+ // The seam between `ImageViewer` and its optional zoom (`zoom-image-gesture`),
4
+ // kept apart from both so the gesture provider does not load the viewer.
5
+
6
+ /** The zoomable page and the root its gestures need inside the viewer's `Modal`. */
7
+
8
+ /**
9
+ * Pinch, pan and double-tap zoom for `ImageViewer`, from
10
+ * `GestureHandlerProvider` (`@vvedantb/native-ui/gesture-handler`). Without
11
+ * it the viewer shows each image fitted, with no zoom.
12
+ */
13
+ export const ZoomImageContext = createContext(null);
@@ -10,6 +10,8 @@ import { settleSheet, sheetCatches, sheetTakesDrag, SheetMotionView, SheetGestur
10
10
  import { PagerGestureContext } from "../Pager";
11
11
  import { ToastDragContext } from "../toastDrag";
12
12
  import { GesturePager } from "./pager-gesture";
13
+ import { ZoomImageContext } from "../media/zoomImage";
14
+ import { ZOOM_IMAGE_PARTS } from "../media/zoom-image-gesture";
13
15
  import { GestureSwipeableRow } from "./swipe-row-gesture";
14
16
  import { GestureToastDrag } from "./toast-drag-gesture";
15
17
 
@@ -193,7 +195,8 @@ const PARTS = {
193
195
  /**
194
196
  * Runs the kit's sheet drags (`SheetContent`, the compact `DialogContent`)
195
197
  * on Gesture Handler instead of `PanResponder`, and turns on the swipes of
196
- * `SwipeableRow`, `Pager` and the `Toaster` toasts (up to dismiss). Put it
198
+ * `SwipeableRow`, `Pager` and the `Toaster` toasts (up to dismiss), and the
199
+ * `ImageViewer` zoom (pinch, double tap, pan). Put it
197
200
  * inside the app's `GestureHandlerRootView`; each overlay adds its own root
198
201
  * view inside its `Modal`.
199
202
  */
@@ -204,7 +207,7 @@ export function GestureHandlerProvider(t0) {
204
207
  } = t0;
205
208
  let t1;
206
209
  if ($[0] !== children) {
207
- t1 = <SheetGestureContext.Provider value={PARTS}><SwipeRowGestureContext.Provider value={GestureSwipeableRow}><PagerGestureContext.Provider value={GesturePager}><ToastDragContext.Provider value={GestureToastDrag}>{children}</ToastDragContext.Provider></PagerGestureContext.Provider></SwipeRowGestureContext.Provider></SheetGestureContext.Provider>;
210
+ t1 = <SheetGestureContext.Provider value={PARTS}><SwipeRowGestureContext.Provider value={GestureSwipeableRow}><PagerGestureContext.Provider value={GesturePager}><ToastDragContext.Provider value={GestureToastDrag}><ZoomImageContext.Provider value={ZOOM_IMAGE_PARTS}>{children}</ZoomImageContext.Provider></ToastDragContext.Provider></PagerGestureContext.Provider></SwipeRowGestureContext.Provider></SheetGestureContext.Provider>;
208
211
  $[0] = children;
209
212
  $[1] = t1;
210
213
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.21.0-next-90c9e82619627a37726c0e2115cd6bbacfd21bcb",
3
+ "version": "0.21.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared React Native / Expo UI primitives. Floating surfaces, forms, overlays, toasts, settings chrome, tab bar, AI chat parts, the family app marks, a NativeWind preset, the Expo web root and Clerk + Convex auth screens.",
6
6
  "type": "module",
@@ -1,4 +1,4 @@
1
- import { useState } from "react";
1
+ import { useContext, useState, type ReactNode } from "react";
2
2
  import { FlatList, Image, Modal, Pressable, Text, View, useWindowDimensions } from "react-native";
3
3
  import { useSafeAreaInsets } from "react-native-safe-area-context";
4
4
  import { IconExternalLink, IconX } from "@tabler/icons-react-native";
@@ -8,6 +8,7 @@ import { withAlpha } from "../palette";
8
8
  import { tokens } from "../tokens";
9
9
  import { useReduceMotion } from "../useReduceMotion";
10
10
  import { useScheme } from "../useScheme";
11
+ import { ZoomImageContext } from "./zoomImage";
11
12
  import { imageAspect, imageGridTiles, imageViewerPage, MEDIA, type GalleryImage, type ImageGridTile } from "./imageGrid";
12
13
 
13
14
  export type ImageGalleryProps = {
@@ -148,21 +149,35 @@ export type ImageViewerProps = {
148
149
  * The web `ImageLightbox` on a phone: the images full screen on black, one
149
150
  * page each, swiped sideways (TalkBack and VoiceOver step with the adjustable
150
151
  * actions). Close (X, or Android back) and "Open in browser" sit at the top,
151
- * "2 / 5" at the bottom when there is more than one. No pinch zoom. It fades
152
- * in, or appears at once with Reduce Motion.
152
+ * "2 / 5" at the bottom when there is more than one. Under
153
+ * `GestureHandlerProvider` each image zooms: pinch (up to 5x, about the
154
+ * fingers), double tap (2.5x on the tapped point, again to fit), and a drag
155
+ * pans it while zoomed instead of turning the page. It fades in, or appears
156
+ * at once with Reduce Motion.
153
157
  */
154
158
  export function ImageViewer({ images, index, onIndexChange, onClose, onOpenImage }: ImageViewerProps) {
155
159
  const reduceMotion = useReduceMotion();
156
160
  if (index === null) return null;
157
161
  return (
158
162
  <Modal visible transparent statusBarTranslucent animationType={reduceMotion ? "none" : "fade"} onRequestClose={onClose}>
159
- <ViewerPages images={images} index={index} onIndexChange={onIndexChange} onClose={onClose} onOpenImage={onOpenImage} />
163
+ <ViewerRoot>
164
+ <ViewerPages images={images} index={index} onIndexChange={onIndexChange} onClose={onClose} onOpenImage={onOpenImage} />
165
+ </ViewerRoot>
160
166
  </Modal>
161
167
  );
162
168
  }
163
169
 
170
+ /** The zoom parts' gesture root, when there are zoom parts. */
171
+ function ViewerRoot({ children }: { children: ReactNode }) {
172
+ const zoom = useContext(ZoomImageContext);
173
+ return zoom ? <zoom.Root>{children}</zoom.Root> : children;
174
+ }
175
+
164
176
  function ViewerPages({ images, index, onIndexChange, onClose, onOpenImage }: Omit<ImageViewerProps, "index"> & { index: number }) {
165
177
  const { width, height } = useWindowDimensions();
178
+ const zoom = useContext(ZoomImageContext);
179
+ // While the image on screen is zoomed, a drag pans it: the pages hold still.
180
+ const [zoomed, setZoomed] = useState(false);
166
181
  const insets = useSafeAreaInsets();
167
182
  const count = images.length;
168
183
  const current = images[index];
@@ -173,16 +188,21 @@ function ViewerPages({ images, index, onIndexChange, onClose, onOpenImage }: Omi
173
188
  data={images}
174
189
  horizontal
175
190
  pagingEnabled
191
+ scrollEnabled={!zoomed}
176
192
  showsHorizontalScrollIndicator={false}
177
193
  initialScrollIndex={index}
178
194
  getItemLayout={(_, item) => ({ length: width, offset: width * item, index: item })}
179
195
  keyExtractor={(image, item) => `${item}:${image.url}`}
180
196
  onMomentumScrollEnd={(event) => onIndexChange(imageViewerPage(event.nativeEvent.contentOffset.x, width, count))}
181
- renderItem={({ item }) => (
182
- <View style={{ width, height }} className="items-center justify-center">
183
- <Image source={{ uri: item.url }} resizeMode="contain" style={{ width: "100%", height: "100%" }} accessibilityLabel={item.alt ?? "Image"} />
184
- </View>
185
- )}
197
+ renderItem={({ item }) =>
198
+ zoom ? (
199
+ <zoom.Image image={item} width={width} height={height} onZoomChange={setZoomed} />
200
+ ) : (
201
+ <View style={{ width, height }} className="items-center justify-center">
202
+ <Image source={{ uri: item.url }} resizeMode="contain" style={{ width: "100%", height: "100%" }} accessibilityLabel={item.alt ?? "Image"} />
203
+ </View>
204
+ )
205
+ }
186
206
  accessibilityRole="adjustable"
187
207
  accessibilityLabel={current?.alt ?? "Image"}
188
208
  accessibilityValue={count > 1 ? { text: `${index + 1} of ${count}` } : undefined}
@@ -0,0 +1,60 @@
1
+ // The zoom rules of `ImageViewer`, as plain worklets: the gesture part
2
+ // (`zoom-image-gesture`) runs them on the UI thread, and tests call them.
3
+
4
+ /** Fitted size is 1; a pinch goes up to 5 and rubber-bands a little past both ends. */
5
+ export const ZOOM = { min: 1, max: 5, doubleTap: 2.5 } as const;
6
+
7
+ /** The scale while pinching: free inside [min, max], and a quarter of the overshoot past either end. */
8
+ export function zoomScale(raw: number): number {
9
+ "worklet";
10
+ if (raw < ZOOM.min) return ZOOM.min - (ZOOM.min - raw) / 4;
11
+ if (raw > ZOOM.max) return ZOOM.max + (raw - ZOOM.max) / 4;
12
+ return raw;
13
+ }
14
+
15
+ /** Where a released pinch settles: back inside [min, max]. */
16
+ export function settleScale(scale: number): number {
17
+ "worklet";
18
+ return Math.min(ZOOM.max, Math.max(ZOOM.min, scale));
19
+ }
20
+
21
+ /**
22
+ * The farthest the zoomed image may move from centre on one axis: half of
23
+ * what overflows the screen. At fitted size it cannot move.
24
+ */
25
+ export function panLimit(scale: number, size: number): number {
26
+ "worklet";
27
+ return Math.max(0, (size * scale - size) / 2);
28
+ }
29
+
30
+ /** A translation kept inside `panLimit` on one axis. */
31
+ export function clampPan(translate: number, scale: number, size: number): number {
32
+ "worklet";
33
+ const limit = panLimit(scale, size);
34
+ return Math.min(limit, Math.max(-limit, translate));
35
+ }
36
+
37
+ /**
38
+ * A double tap: zoomed out, it zooms to `ZOOM.doubleTap` on the tapped point
39
+ * (that point stays under the finger, as far as the edges allow); zoomed in,
40
+ * it goes back to fitted. `point` and `size` are in the page's coordinates.
41
+ */
42
+ export function doubleTapZoom(
43
+ scale: number,
44
+ point: { x: number; y: number },
45
+ size: { width: number; height: number },
46
+ ): { scale: number; x: number; y: number } {
47
+ "worklet";
48
+ if (scale > ZOOM.min) return { scale: ZOOM.min, x: 0, y: 0 };
49
+ const next = ZOOM.doubleTap;
50
+ // The tapped point, from centre, moves out by `next`; shift back by that much.
51
+ const x = -(point.x - size.width / 2) * (next - 1);
52
+ const y = -(point.y - size.height / 2) * (next - 1);
53
+ return { scale: next, x: clampPan(x, next, size.width), y: clampPan(y, next, size.height) };
54
+ }
55
+
56
+ /** Whether the image is zoomed in: then a drag pans it instead of turning the page. */
57
+ export function isZoomed(scale: number): boolean {
58
+ "worklet";
59
+ return scale > ZOOM.min + 0.01;
60
+ }
@@ -0,0 +1,111 @@
1
+ import { useState, type ReactNode } from "react";
2
+ import { Image, StyleSheet, View } from "react-native";
3
+ import { Gesture, GestureDetector, GestureHandlerRootView } from "react-native-gesture-handler";
4
+ import Animated, { ReduceMotion, runOnJS, useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated";
5
+ import type { ZoomImageParts, ZoomImageProps } from "./zoomImage";
6
+ import { clampPan, doubleTapZoom, isZoomed, settleScale, zoomScale, ZOOM } from "./imageZoom";
7
+
8
+ // Pinch, pan and double tap for an `ImageViewer` page, on Gesture Handler: the
9
+ // track phase runs as worklets on the UI thread, so the image never lags the
10
+ // fingers. The rules are plain worklets in `imageZoom`.
11
+
12
+ /** The settle after a release or a double tap: quick, no bounce past the edges. */
13
+ const ZOOM_SPRING = { duration: 300, dampingRatio: 1, reduceMotion: ReduceMotion.System } as const;
14
+
15
+ function ZoomImage({ image, width, height, onZoomChange }: ZoomImageProps) {
16
+ const scale = useSharedValue<number>(ZOOM.min);
17
+ const x = useSharedValue(0);
18
+ const y = useSharedValue(0);
19
+ // Where the gesture started: the scale and translation, and the pinch's focal point from centre.
20
+ const startScale = useSharedValue<number>(ZOOM.min);
21
+ const startX = useSharedValue(0);
22
+ const startY = useSharedValue(0);
23
+ const focalX = useSharedValue(0);
24
+ const focalY = useSharedValue(0);
25
+ // Pan only while zoomed: at fitted size a drag turns the page.
26
+ const [zoomed, setZoomed] = useState(false);
27
+ const report = (next: boolean) => {
28
+ setZoomed(next);
29
+ onZoomChange(next);
30
+ };
31
+
32
+ const settle = () => {
33
+ "worklet";
34
+ const next = settleScale(scale.get());
35
+ scale.set(withSpring(next, ZOOM_SPRING));
36
+ x.set(withSpring(clampPan(x.get(), next, width), ZOOM_SPRING));
37
+ y.set(withSpring(clampPan(y.get(), next, height), ZOOM_SPRING));
38
+ runOnJS(report)(isZoomed(next));
39
+ };
40
+
41
+ const pinch = Gesture.Pinch()
42
+ .onStart((event) => {
43
+ startScale.set(scale.get());
44
+ startX.set(x.get());
45
+ startY.set(y.get());
46
+ focalX.set(event.focalX - width / 2);
47
+ focalY.set(event.focalY - height / 2);
48
+ })
49
+ .onUpdate((event) => {
50
+ const next = zoomScale(startScale.get() * event.scale);
51
+ const ratio = next / startScale.get();
52
+ // The point under the fingers stays under them.
53
+ x.set(focalX.get() - (focalX.get() - startX.get()) * ratio);
54
+ y.set(focalY.get() - (focalY.get() - startY.get()) * ratio);
55
+ scale.set(next);
56
+ })
57
+ .onEnd(() => {
58
+ settle();
59
+ });
60
+
61
+ const pan = Gesture.Pan()
62
+ .enabled(zoomed)
63
+ .averageTouches(true)
64
+ .onStart(() => {
65
+ startX.set(x.get());
66
+ startY.set(y.get());
67
+ })
68
+ .onUpdate((event) => {
69
+ x.set(clampPan(startX.get() + event.translationX, scale.get(), width));
70
+ y.set(clampPan(startY.get() + event.translationY, scale.get(), height));
71
+ });
72
+
73
+ const doubleTap = Gesture.Tap()
74
+ .numberOfTaps(2)
75
+ .onEnd((event, success) => {
76
+ if (!success) return;
77
+ const target = doubleTapZoom(scale.get(), { x: event.x, y: event.y }, { width, height });
78
+ scale.set(withSpring(target.scale, ZOOM_SPRING));
79
+ x.set(withSpring(target.x, ZOOM_SPRING));
80
+ y.set(withSpring(target.y, ZOOM_SPRING));
81
+ runOnJS(report)(isZoomed(target.scale));
82
+ });
83
+
84
+ const style = useAnimatedStyle(() => ({
85
+ transform: [{ translateX: x.get() }, { translateY: y.get() }, { scale: scale.get() }],
86
+ }));
87
+
88
+ return (
89
+ <GestureDetector gesture={Gesture.Simultaneous(pinch, pan, doubleTap)}>
90
+ <View style={{ width, height }} collapsable={false}>
91
+ <Animated.View style={[styles.fill, style]}>
92
+ <Image source={{ uri: image.url }} resizeMode="contain" style={styles.fill} accessibilityLabel={image.alt ?? "Image"} />
93
+ </Animated.View>
94
+ </View>
95
+ </GestureDetector>
96
+ );
97
+ }
98
+
99
+ /** Gestures inside a `Modal` need a root view of their own. */
100
+ function ZoomRoot({ children }: { children: ReactNode }) {
101
+ return <GestureHandlerRootView style={styles.fill}>{children}</GestureHandlerRootView>;
102
+ }
103
+
104
+ export const ZOOM_IMAGE_PARTS: ZoomImageParts = { Root: ZoomRoot, Image: ZoomImage };
105
+
106
+ const styles = StyleSheet.create({
107
+ fill: {
108
+ width: "100%",
109
+ height: "100%",
110
+ },
111
+ });
@@ -0,0 +1,28 @@
1
+ import { createContext, type ComponentType, type ReactNode } from "react";
2
+ import type { GalleryImage } from "./imageGrid";
3
+
4
+ // The seam between `ImageViewer` and its optional zoom (`zoom-image-gesture`),
5
+ // kept apart from both so the gesture provider does not load the viewer.
6
+
7
+ export type ZoomImageProps = {
8
+ image: GalleryImage;
9
+ /** The page's size: the image fits it at zoom 1. */
10
+ width: number;
11
+ height: number;
12
+ /** Called as the image zooms in past fitted size, and back: while zoomed, a drag pans it instead of turning the page. */
13
+ onZoomChange: (zoomed: boolean) => void;
14
+ };
15
+
16
+ /** The zoomable page and the root its gestures need inside the viewer's `Modal`. */
17
+ export type ZoomImageParts = {
18
+ Root: ComponentType<{ children: ReactNode }>;
19
+ Image: ComponentType<ZoomImageProps>;
20
+ };
21
+
22
+ /**
23
+ * Pinch, pan and double-tap zoom for `ImageViewer`, from
24
+ * `GestureHandlerProvider` (`@vvedantb/native-ui/gesture-handler`). Without
25
+ * it the viewer shows each image fitted, with no zoom.
26
+ */
27
+ export const ZoomImageContext = createContext<ZoomImageParts | null>(null);
28
+
@@ -33,6 +33,8 @@ import {
33
33
  import { PagerGestureContext } from "../Pager";
34
34
  import { ToastDragContext } from "../toastDrag";
35
35
  import { GesturePager } from "./pager-gesture";
36
+ import { ZoomImageContext } from "../media/zoomImage";
37
+ import { ZOOM_IMAGE_PARTS } from "../media/zoom-image-gesture";
36
38
  import { GestureSwipeableRow } from "./swipe-row-gesture";
37
39
  import { GestureToastDrag } from "./toast-drag-gesture";
38
40
 
@@ -158,7 +160,8 @@ const PARTS: SheetGestureParts = {
158
160
  /**
159
161
  * Runs the kit's sheet drags (`SheetContent`, the compact `DialogContent`)
160
162
  * on Gesture Handler instead of `PanResponder`, and turns on the swipes of
161
- * `SwipeableRow`, `Pager` and the `Toaster` toasts (up to dismiss). Put it
163
+ * `SwipeableRow`, `Pager` and the `Toaster` toasts (up to dismiss), and the
164
+ * `ImageViewer` zoom (pinch, double tap, pan). Put it
162
165
  * inside the app's `GestureHandlerRootView`; each overlay adds its own root
163
166
  * view inside its `Modal`.
164
167
  */
@@ -167,7 +170,9 @@ export function GestureHandlerProvider({ children }: { children: ReactNode }) {
167
170
  <SheetGestureContext.Provider value={PARTS}>
168
171
  <SwipeRowGestureContext.Provider value={GestureSwipeableRow}>
169
172
  <PagerGestureContext.Provider value={GesturePager}>
170
- <ToastDragContext.Provider value={GestureToastDrag}>{children}</ToastDragContext.Provider>
173
+ <ToastDragContext.Provider value={GestureToastDrag}>
174
+ <ZoomImageContext.Provider value={ZOOM_IMAGE_PARTS}>{children}</ZoomImageContext.Provider>
175
+ </ToastDragContext.Provider>
171
176
  </PagerGestureContext.Provider>
172
177
  </SwipeRowGestureContext.Provider>
173
178
  </SheetGestureContext.Provider>