@marktiderman/genesis-ui-native 3.5.0 → 3.6.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.
- package/README.md +71 -7
- package/dist/{chunk-LBVD7XXL.js → chunk-2Z72MYHB.js} +627 -20
- package/dist/chunk-2Z72MYHB.js.map +1 -0
- package/dist/chunk-4OQOJ2OC.js +80 -0
- package/dist/chunk-4OQOJ2OC.js.map +1 -0
- package/dist/{chunk-6KWEAGRD.js → chunk-6PGLX6ET.js} +101 -6
- package/dist/chunk-6PGLX6ET.js.map +1 -0
- package/dist/{chunk-ONH2PCGH.js → chunk-AGTBABAH.js} +9 -15
- package/dist/chunk-AGTBABAH.js.map +1 -0
- package/dist/{chunk-ABVSSJNT.js → chunk-FUMSL7VZ.js} +198 -168
- package/dist/chunk-FUMSL7VZ.js.map +1 -0
- package/dist/{chunk-GHYUNNNU.js → chunk-NDXNSSM6.js} +175 -93
- package/dist/chunk-NDXNSSM6.js.map +1 -0
- package/dist/chunk-SEJ6RBP7.js +361 -0
- package/dist/chunk-SEJ6RBP7.js.map +1 -0
- package/dist/{chunk-D5RKSPMH.js → chunk-ZRQAFFTN.js} +4 -3
- package/dist/chunk-ZRQAFFTN.js.map +1 -0
- package/dist/components/index.d.ts +331 -4
- package/dist/components/index.js +5 -5
- package/dist/data/index.d.ts +68 -4
- package/dist/data/index.js +235 -31
- package/dist/data/index.js.map +1 -1
- package/dist/hooks/index.d.ts +60 -1
- package/dist/hooks/index.js +2 -3
- package/dist/icons/lucide.d.ts +1 -1
- package/dist/icons/lucide.js +7 -1
- package/dist/icons/lucide.js.map +1 -1
- package/dist/{inbox-page-BEarNEIP.d.ts → inbox-page-B7_Oi2fS.d.ts} +2 -44
- package/dist/index.d.ts +8 -5
- package/dist/index.js +8 -8
- package/dist/layouts/index.d.ts +40 -51
- package/dist/layouts/index.js +5 -4
- package/dist/{native-modules-PwnGltOU.d.ts → native-modules-Cl3zw3kh.d.ts} +27 -1
- package/package.json +78 -4
- package/src/__tests__/doctor-rules.test.ts +60 -0
- package/src/__tests__/native-modules-lists.test.tsx +1 -0
- package/src/components/__tests__/composer-bar.test.tsx +55 -0
- package/src/components/__tests__/gallery-card.test.tsx +59 -0
- package/src/components/__tests__/gallery-grid.test.tsx +160 -0
- package/src/components/__tests__/lightbox-resnap.test.tsx +50 -0
- package/src/components/__tests__/lightbox-zoom.test.ts +33 -0
- package/src/components/__tests__/lightbox.test.tsx +141 -0
- package/src/components/__tests__/live-indicator.test.tsx +72 -0
- package/src/components/__tests__/rating.test.tsx +82 -0
- package/src/components/__tests__/thread-list.test.tsx +156 -0
- package/src/components/composer-bar.tsx +198 -0
- package/src/components/drawer.tsx +15 -9
- package/src/components/gallery-card.tsx +109 -0
- package/src/components/gallery-grid.tsx +285 -0
- package/src/components/gallery-layout.ts +2 -0
- package/src/components/gallery-types.ts +2 -0
- package/src/components/index.ts +44 -0
- package/src/components/lightbox-zoom.tsx +282 -0
- package/src/components/lightbox.tsx +342 -0
- package/src/components/live-indicator.tsx +139 -0
- package/src/components/rating.tsx +128 -0
- package/src/components/skeleton.tsx +9 -4
- package/src/components/thread-list.tsx +427 -0
- package/src/components/toast.tsx +7 -4
- package/src/data/NativeConflictSheet.tsx +181 -0
- package/src/data/NativeResourceForm.tsx +15 -1
- package/src/data/index.ts +2 -0
- package/src/data/resource-view/NativeLinkPicker.tsx +1 -1
- package/src/data/resource-view/NativeRecordView.tsx +85 -8
- package/src/data/resource-view/NativeResourceView.tsx +79 -32
- package/src/data/resource-view/__tests__/native-link-picker.test.tsx +16 -0
- package/src/data/resource-view/__tests__/native-record-view.test.tsx +95 -1
- package/src/data/resource-view/__tests__/native-resource-gallery.test.tsx +125 -0
- package/src/data/resource-view/mapping.ts +3 -0
- package/src/hooks/__tests__/use-motion.test.tsx +106 -0
- package/src/hooks/__tests__/use-thread-jump.test.tsx +88 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/use-motion.ts +128 -0
- package/src/hooks/use-thread-jump.ts +110 -0
- package/src/icon-names.ts +6 -0
- package/src/icons/lucide.ts +12 -0
- package/src/layouts/__tests__/inbox-page.test.tsx +6 -0
- package/src/layouts/__tests__/thread-page.test.tsx +114 -14
- package/src/layouts/inbox-page.tsx +6 -55
- package/src/layouts/index.ts +2 -0
- package/src/layouts/thread-page.tsx +326 -262
- package/src/native-modules.tsx +16 -2
- package/dist/chunk-6KWEAGRD.js.map +0 -1
- package/dist/chunk-7PCWEY6R.js +0 -27
- package/dist/chunk-7PCWEY6R.js.map +0 -1
- package/dist/chunk-ABVSSJNT.js.map +0 -1
- package/dist/chunk-D5RKSPMH.js.map +0 -1
- package/dist/chunk-GHYUNNNU.js.map +0 -1
- package/dist/chunk-LBVD7XXL.js.map +0 -1
- package/dist/chunk-ONH2PCGH.js.map +0 -1
- package/dist/chunk-Z3AQYVUA.js +0 -36
- package/dist/chunk-Z3AQYVUA.js.map +0 -1
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A photo page that zooms, for `NativeLightbox`: pinch or double-tap to zoom (up to 4×), pan
|
|
3
|
+
* while zoomed, and at full size a pull down past the edge (or a quick flick) closes the viewer.
|
|
4
|
+
*
|
|
5
|
+
* No gesture library. On iOS each page is the system's zooming `ScrollView` (its own pinch, and
|
|
6
|
+
* the pull down is its overscroll); on Android a `PanResponder` pinches, pans and drags down.
|
|
7
|
+
* Built from genesis-chat's photo viewer, which moves onto NativeLightbox next so there is one
|
|
8
|
+
* viewer (G4/G5); until then chat keeps its own copy. The pure helpers
|
|
9
|
+
* are exported for tests and for anyone building a viewer of their own.
|
|
10
|
+
*
|
|
11
|
+
* @stability Beta
|
|
12
|
+
*/
|
|
13
|
+
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
14
|
+
import { Animated, PanResponder, Platform, Pressable, ScrollView, View, type GestureResponderEvent } from "react-native";
|
|
15
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
16
|
+
import { NativeImage } from "./image";
|
|
17
|
+
|
|
18
|
+
/** Points pulled down (or dragged, on Android) that close the viewer on release. */
|
|
19
|
+
export const LIGHTBOX_DISMISS_DISTANCE = 110;
|
|
20
|
+
/** Or a quick flick down, in points per millisecond. */
|
|
21
|
+
export const LIGHTBOX_DISMISS_VELOCITY = 1.1;
|
|
22
|
+
/** How far a photo zooms in. */
|
|
23
|
+
export const LIGHTBOX_MAX_ZOOM = 4;
|
|
24
|
+
/** Between two taps (each fires on release) for a double tap: a quick one measured 330ms apart on iOS. */
|
|
25
|
+
export const LIGHTBOX_DOUBLE_TAP_MS = 400;
|
|
26
|
+
|
|
27
|
+
/** Whether a drag down that ended here closes the viewer. */
|
|
28
|
+
export function lightboxShouldDismiss(dy: number, vy: number): boolean {
|
|
29
|
+
return dy > LIGHTBOX_DISMISS_DISTANCE || (dy > 24 && vy > LIGHTBOX_DISMISS_VELOCITY);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The zoom a pinch has reached: from where it started, by how far the fingers spread. */
|
|
33
|
+
export function lightboxPinchScale(startScale: number, startDistance: number, distance: number): number {
|
|
34
|
+
if (startDistance <= 0) return startScale;
|
|
35
|
+
return Math.min(LIGHTBOX_MAX_ZOOM, Math.max(0.8, startScale * (distance / startDistance)));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Points between two touches. */
|
|
39
|
+
export function lightboxTouchDistance(touches: readonly { pageX: number; pageY: number }[]): number {
|
|
40
|
+
const [a, b] = touches;
|
|
41
|
+
if (!a || !b) return 0;
|
|
42
|
+
return Math.hypot(a.pageX - b.pageX, a.pageY - b.pageY);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** How far a zoomed photo may pan before an edge shows, per axis. */
|
|
46
|
+
export function lightboxPanLimit(scale: number, size: number): number {
|
|
47
|
+
return Math.max(0, ((scale - 1) * size) / 2);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const clamp = (v: number, limit: number) => Math.max(-limit, Math.min(limit, v));
|
|
51
|
+
|
|
52
|
+
export interface NativeZoomablePhotoProps {
|
|
53
|
+
src: string;
|
|
54
|
+
alt?: string;
|
|
55
|
+
width: number;
|
|
56
|
+
height: number;
|
|
57
|
+
/** The page on screen: another page resets its zoom. */
|
|
58
|
+
active: boolean;
|
|
59
|
+
onZoomChange: (zoomed: boolean) => void;
|
|
60
|
+
/** Pinch and double-tap zoom (on by default). Off, the page still pulls down to close. */
|
|
61
|
+
zoomable?: boolean;
|
|
62
|
+
/** How far it's pulled down, for the backdrop's fade; null when swiping down doesn't close. */
|
|
63
|
+
onPull: ((dy: number) => void) | null;
|
|
64
|
+
onDismiss: () => void;
|
|
65
|
+
/** A single tap (shows or hides the viewer's chrome). */
|
|
66
|
+
onTap: () => void;
|
|
67
|
+
testID: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The page: iOS's zooming scroll view, or Android's pinch and pan. */
|
|
71
|
+
export function NativeZoomablePhoto(props: NativeZoomablePhotoProps) {
|
|
72
|
+
return Platform.OS === "ios" ? <ZoomableIos {...props} /> : <ZoomablePan {...props} />;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function Photo({ src, alt, width, height, testID }: { src: string; alt?: string; width: number; height: number; testID: string }) {
|
|
76
|
+
return <NativeImage src={src} alt={alt} width={width} height={height} contentFit="contain" className="bg-transparent" testID={testID} />;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A tap that waits to see whether it's the first half of a double tap. */
|
|
80
|
+
function useTaps(onSingle: () => void, onDouble: (at: { locationX: number; locationY: number }) => void) {
|
|
81
|
+
const last = useRef(0);
|
|
82
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
83
|
+
useEffect(
|
|
84
|
+
() => () => {
|
|
85
|
+
if (timer.current) clearTimeout(timer.current);
|
|
86
|
+
},
|
|
87
|
+
[],
|
|
88
|
+
);
|
|
89
|
+
return (e: GestureResponderEvent) => {
|
|
90
|
+
const now = Date.now();
|
|
91
|
+
const at = { locationX: e.nativeEvent?.locationX ?? 0, locationY: e.nativeEvent?.locationY ?? 0 };
|
|
92
|
+
if (now - last.current < LIGHTBOX_DOUBLE_TAP_MS) {
|
|
93
|
+
last.current = 0;
|
|
94
|
+
if (timer.current) clearTimeout(timer.current);
|
|
95
|
+
onDouble(at);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
last.current = now;
|
|
99
|
+
timer.current = setTimeout(onSingle, LIGHTBOX_DOUBLE_TAP_MS);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type ZoomRect = { x: number; y: number; width: number; height: number };
|
|
104
|
+
type ZoomScrollView = { scrollResponderZoomTo?: (rect: ZoomRect & { animated?: boolean }, animated?: boolean) => void };
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Zooms to a rect. React Native 0.88 drops `rect.animated` and animates per the deprecated second
|
|
108
|
+
* argument (ScrollView.js `scrollResponderZoomTo`), so both are given: the rect's keeps it from
|
|
109
|
+
* warning, the second is the one it uses.
|
|
110
|
+
*/
|
|
111
|
+
function zoomTo(view: ZoomScrollView | null, rect: ZoomRect, animated: boolean) {
|
|
112
|
+
view?.scrollResponderZoomTo?.({ ...rect, animated }, animated);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** iOS: the system's zooming scroll view; at full size, a pull down past the top closes. */
|
|
116
|
+
function ZoomableIos({ src, alt, width, height, active, onZoomChange, zoomable = true, onPull, onDismiss, onTap, testID }: NativeZoomablePhotoProps) {
|
|
117
|
+
const ref = useRef<ZoomScrollView | null>(null);
|
|
118
|
+
const scale = useRef(1);
|
|
119
|
+
const reducedMotion = useReducedMotion();
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!active && scale.current > 1.01) {
|
|
122
|
+
zoomTo(ref.current, { x: 0, y: 0, width, height }, false);
|
|
123
|
+
scale.current = 1;
|
|
124
|
+
}
|
|
125
|
+
}, [active, width, height]);
|
|
126
|
+
const tap = useTaps(onTap, (at) => {
|
|
127
|
+
if (!zoomable) return;
|
|
128
|
+
if (scale.current > 1.01) return zoomTo(ref.current, { x: 0, y: 0, width, height }, !reducedMotion);
|
|
129
|
+
const w = width / 2.5;
|
|
130
|
+
const h = height / 2.5;
|
|
131
|
+
zoomTo(ref.current, { x: at.locationX - w / 2, y: at.locationY - h / 2, width: w, height: h }, !reducedMotion);
|
|
132
|
+
});
|
|
133
|
+
return (
|
|
134
|
+
<ScrollView
|
|
135
|
+
ref={ref as never}
|
|
136
|
+
testID={testID}
|
|
137
|
+
style={{ width, height }}
|
|
138
|
+
contentContainerStyle={{ width, height }}
|
|
139
|
+
maximumZoomScale={zoomable ? LIGHTBOX_MAX_ZOOM : 1}
|
|
140
|
+
minimumZoomScale={1}
|
|
141
|
+
bouncesZoom
|
|
142
|
+
centerContent
|
|
143
|
+
alwaysBounceVertical={Boolean(onPull)}
|
|
144
|
+
showsHorizontalScrollIndicator={false}
|
|
145
|
+
showsVerticalScrollIndicator={false}
|
|
146
|
+
scrollEventThrottle={16}
|
|
147
|
+
onScroll={(e) => {
|
|
148
|
+
const z = e.nativeEvent.zoomScale ?? 1;
|
|
149
|
+
if (z > 1.01 !== scale.current > 1.01) onZoomChange(z > 1.01);
|
|
150
|
+
scale.current = z;
|
|
151
|
+
if (z <= 1.01) onPull?.(-e.nativeEvent.contentOffset.y);
|
|
152
|
+
}}
|
|
153
|
+
onScrollEndDrag={(e) => {
|
|
154
|
+
if (scale.current > 1.01 || !onPull) return;
|
|
155
|
+
// iOS reports the release velocity in points per millisecond, negative while pulling down.
|
|
156
|
+
if (lightboxShouldDismiss(-e.nativeEvent.contentOffset.y, -(e.nativeEvent.velocity?.y ?? 0))) onDismiss();
|
|
157
|
+
else onPull(0);
|
|
158
|
+
}}
|
|
159
|
+
>
|
|
160
|
+
<Pressable accessible={false} onPress={tap} style={{ width, height }}>
|
|
161
|
+
<Photo src={src} alt={alt} width={width} height={height} testID={`${testID}-image`} />
|
|
162
|
+
</Pressable>
|
|
163
|
+
</ScrollView>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Android (no zooming ScrollView): two fingers pinch, one pans a zoomed photo or drags it down to close. */
|
|
168
|
+
function ZoomablePan({ src, alt, width, height, active, onZoomChange, zoomable = true, onPull, onDismiss, onTap, testID }: NativeZoomablePhotoProps) {
|
|
169
|
+
const reducedMotion = useReducedMotion();
|
|
170
|
+
const scale = useRef(new Animated.Value(1)).current;
|
|
171
|
+
const tx = useRef(new Animated.Value(0)).current;
|
|
172
|
+
const ty = useRef(new Animated.Value(0)).current;
|
|
173
|
+
const s = useRef({ scale: 1, x: 0, y: 0, startScale: 1, startDistance: 0, startX: 0, startY: 0, pinching: false, dragging: false });
|
|
174
|
+
|
|
175
|
+
const settle = useCallback(
|
|
176
|
+
(to: { scale: number; x: number; y: number }) => {
|
|
177
|
+
s.current.scale = to.scale;
|
|
178
|
+
s.current.x = to.x;
|
|
179
|
+
s.current.y = to.y;
|
|
180
|
+
onZoomChange(to.scale > 1.01);
|
|
181
|
+
if (reducedMotion) {
|
|
182
|
+
scale.setValue(to.scale);
|
|
183
|
+
tx.setValue(to.x);
|
|
184
|
+
ty.setValue(to.y);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
Animated.parallel([
|
|
188
|
+
Animated.spring(scale, { toValue: to.scale, useNativeDriver: true }),
|
|
189
|
+
Animated.spring(tx, { toValue: to.x, useNativeDriver: true }),
|
|
190
|
+
Animated.spring(ty, { toValue: to.y, useNativeDriver: true }),
|
|
191
|
+
]).start();
|
|
192
|
+
},
|
|
193
|
+
[reducedMotion, onZoomChange, scale, tx, ty],
|
|
194
|
+
);
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
if (!active && s.current.scale > 1.01) settle({ scale: 1, x: 0, y: 0 });
|
|
197
|
+
}, [active, settle]);
|
|
198
|
+
|
|
199
|
+
const tap = useTaps(onTap, (at) => {
|
|
200
|
+
if (!zoomable) return;
|
|
201
|
+
if (s.current.scale > 1.01) return settle({ scale: 1, x: 0, y: 0 });
|
|
202
|
+
const z = 2.5;
|
|
203
|
+
settle({
|
|
204
|
+
scale: z,
|
|
205
|
+
x: clamp((width / 2 - at.locationX) * (z - 1), lightboxPanLimit(z, width)),
|
|
206
|
+
y: clamp((height / 2 - at.locationY) * (z - 1), lightboxPanLimit(z, height)),
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const responder = useMemo(
|
|
211
|
+
() =>
|
|
212
|
+
PanResponder.create({
|
|
213
|
+
onStartShouldSetPanResponder: () => false,
|
|
214
|
+
onMoveShouldSetPanResponder: (e, g) =>
|
|
215
|
+
(zoomable && e.nativeEvent.touches.length >= 2) || s.current.scale > 1.01 || (Boolean(onPull) && g.dy > 8 && Math.abs(g.dy) > Math.abs(g.dx) * 1.5),
|
|
216
|
+
onPanResponderTerminationRequest: () => false,
|
|
217
|
+
onPanResponderGrant: (e) => {
|
|
218
|
+
const st = s.current;
|
|
219
|
+
st.pinching = zoomable && e.nativeEvent.touches.length >= 2;
|
|
220
|
+
st.dragging = !st.pinching && st.scale <= 1.01 && Boolean(onPull);
|
|
221
|
+
st.startScale = st.scale;
|
|
222
|
+
st.startDistance = lightboxTouchDistance(e.nativeEvent.touches);
|
|
223
|
+
st.startX = st.x;
|
|
224
|
+
st.startY = st.y;
|
|
225
|
+
},
|
|
226
|
+
onPanResponderMove: (e, g) => {
|
|
227
|
+
const st = s.current;
|
|
228
|
+
const touches = e.nativeEvent.touches;
|
|
229
|
+
if (zoomable && touches.length >= 2) {
|
|
230
|
+
if (!st.pinching) {
|
|
231
|
+
st.pinching = true;
|
|
232
|
+
st.dragging = false;
|
|
233
|
+
st.startScale = st.scale;
|
|
234
|
+
st.startDistance = lightboxTouchDistance(touches);
|
|
235
|
+
}
|
|
236
|
+
st.scale = lightboxPinchScale(st.startScale, st.startDistance, lightboxTouchDistance(touches));
|
|
237
|
+
scale.setValue(st.scale);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (st.dragging) {
|
|
241
|
+
st.y = Math.max(0, g.dy);
|
|
242
|
+
ty.setValue(st.y);
|
|
243
|
+
onPull?.(st.y);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (st.scale > 1.01) {
|
|
247
|
+
st.x = clamp(st.startX + g.dx, lightboxPanLimit(st.scale, width));
|
|
248
|
+
st.y = clamp(st.startY + g.dy, lightboxPanLimit(st.scale, height));
|
|
249
|
+
tx.setValue(st.x);
|
|
250
|
+
ty.setValue(st.y);
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
onPanResponderRelease: (_e, g) => {
|
|
254
|
+
const st = s.current;
|
|
255
|
+
if (st.dragging) {
|
|
256
|
+
st.dragging = false;
|
|
257
|
+
if (lightboxShouldDismiss(g.dy, g.vy)) return onDismiss();
|
|
258
|
+
onPull?.(0);
|
|
259
|
+
return settle({ scale: 1, x: 0, y: 0 });
|
|
260
|
+
}
|
|
261
|
+
st.pinching = false;
|
|
262
|
+
const z = Math.max(1, st.scale);
|
|
263
|
+
settle({ scale: z, x: clamp(st.x, lightboxPanLimit(z, width)), y: clamp(st.y, lightboxPanLimit(z, height)) });
|
|
264
|
+
},
|
|
265
|
+
onPanResponderTerminate: () => {
|
|
266
|
+
onPull?.(0);
|
|
267
|
+
settle({ scale: Math.max(1, s.current.scale), x: 0, y: 0 });
|
|
268
|
+
},
|
|
269
|
+
}),
|
|
270
|
+
[width, height, zoomable, onPull, onDismiss, settle, scale, tx, ty],
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
<View testID={testID} style={{ width, height, overflow: "hidden" }} {...responder.panHandlers}>
|
|
275
|
+
<Animated.View style={{ width, height, transform: [{ translateX: tx }, { translateY: ty }, { scale }] }}>
|
|
276
|
+
<Pressable accessible={false} onPress={tap} style={{ width, height }}>
|
|
277
|
+
<Photo src={src} alt={alt} width={width} height={height} testID={`${testID}-image`} />
|
|
278
|
+
</Pressable>
|
|
279
|
+
</Animated.View>
|
|
280
|
+
</View>
|
|
281
|
+
);
|
|
282
|
+
}
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NativeLightbox — a full-screen photo viewer: swipe between the items, a
|
|
3
|
+
* close button, a "3 / 12" counter, the caption at the bottom and a slot for
|
|
4
|
+
* actions (share, save) at the top right. `title`/`description` sit in the
|
|
5
|
+
* top bar; `renderMedia` swaps a page's picture for a video or a compare view.
|
|
6
|
+
*
|
|
7
|
+
* The built-in photo pages pinch or double-tap to zoom (`zoomable`, on by
|
|
8
|
+
* default), and paging stops while one is zoomed; at full size a pull down
|
|
9
|
+
* closes the viewer (`swipeToDismiss`, on by default), fading the backdrop as
|
|
10
|
+
* it goes; a tap shows or hides the top bar and caption. Pages from
|
|
11
|
+
* `renderMedia` are left to their own gestures. With VoiceOver or TalkBack the
|
|
12
|
+
* pager is adjustable: swipe up or down for the next or previous item, and it
|
|
13
|
+
* reads "3 of 12". The gestures are built from genesis-chat's photo viewer
|
|
14
|
+
* (see `lightbox-zoom`), which moves onto this one next, so there is one viewer.
|
|
15
|
+
*
|
|
16
|
+
* Built on `NativeFullScreenModal`. The pictures are a horizontal paging
|
|
17
|
+
* `FlatList`, one per screen width, a few pages mounted at a time; a swipe
|
|
18
|
+
* reports `onIndexChange` when the page settles, and a new `index` from
|
|
19
|
+
* outside scrolls to it. No looping: the first and last page stop. The same
|
|
20
|
+
* props as the web `Lightbox`.
|
|
21
|
+
*
|
|
22
|
+
* @stability Beta
|
|
23
|
+
*/
|
|
24
|
+
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
|
|
25
|
+
import {
|
|
26
|
+
Animated,
|
|
27
|
+
Dimensions,
|
|
28
|
+
FlatList,
|
|
29
|
+
Pressable,
|
|
30
|
+
Text,
|
|
31
|
+
View,
|
|
32
|
+
type LayoutChangeEvent,
|
|
33
|
+
type NativeScrollEvent,
|
|
34
|
+
type NativeSyntheticEvent,
|
|
35
|
+
} from "react-native";
|
|
36
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
37
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
38
|
+
import { cn } from "../utils";
|
|
39
|
+
import { FULL_SCREEN_DARK, NativeFullScreenModal } from "./full-screen-modal";
|
|
40
|
+
import type { GalleryItem } from "./gallery-types";
|
|
41
|
+
import { NativeIcon } from "./icon";
|
|
42
|
+
import { NativeImage } from "./image";
|
|
43
|
+
import { NativeZoomablePhoto } from "./lightbox-zoom";
|
|
44
|
+
|
|
45
|
+
/** The viewer's words, for another language. */
|
|
46
|
+
export interface NativeLightboxLabels {
|
|
47
|
+
close: string;
|
|
48
|
+
/** The pager's value for screen readers: "3 of 12". */
|
|
49
|
+
position: (n: number, count: number) => string;
|
|
50
|
+
/** The pager's hint for screen readers, when there's more than one. */
|
|
51
|
+
pagerHint: string;
|
|
52
|
+
/** A picture with no `alt`. */
|
|
53
|
+
photo: (n: number, count: number) => string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const defaultNativeLightboxLabels: NativeLightboxLabels = {
|
|
57
|
+
close: "Close",
|
|
58
|
+
position: (n, count) => `${n} of ${count}`,
|
|
59
|
+
pagerHint: "Swipe up or down for the next or previous one",
|
|
60
|
+
photo: (n, count) => `Photo ${n} of ${count}`,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export interface NativeLightboxProps {
|
|
64
|
+
open: boolean;
|
|
65
|
+
onOpenChange: (open: boolean) => void;
|
|
66
|
+
items: GalleryItem[];
|
|
67
|
+
/** The item shown, 0-based. */
|
|
68
|
+
index: number;
|
|
69
|
+
onIndexChange: (index: number) => void;
|
|
70
|
+
/** Replaces the default caption (the item's `caption`). */
|
|
71
|
+
renderCaption?: (item: GalleryItem, index: number) => ReactNode;
|
|
72
|
+
/** Top-right slot: icon buttons for the current item. */
|
|
73
|
+
renderActions?: (item: GalleryItem, index: number) => ReactNode;
|
|
74
|
+
/** Replaces the page's picture (a video player, a before/after view); fills the page. */
|
|
75
|
+
renderMedia?: (item: GalleryItem, index: number) => ReactNode;
|
|
76
|
+
/** In the top bar, and the viewer's accessibility label. */
|
|
77
|
+
title?: string;
|
|
78
|
+
/** Under the title in the top bar. */
|
|
79
|
+
description?: ReactNode;
|
|
80
|
+
/** Pinch or double-tap the built-in photo pages to zoom. Default true. */
|
|
81
|
+
zoomable?: boolean;
|
|
82
|
+
/** A pull down at full size closes the viewer. Default true. */
|
|
83
|
+
swipeToDismiss?: boolean;
|
|
84
|
+
labels?: Partial<NativeLightboxLabels>;
|
|
85
|
+
testID?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The chrome is white in both schemes: the viewer is black in both (`FULL_SCREEN_DARK`), so no theme role fits. */
|
|
89
|
+
const CHROME_TINT = "white";
|
|
90
|
+
|
|
91
|
+
export function NativeLightbox({
|
|
92
|
+
open,
|
|
93
|
+
onOpenChange,
|
|
94
|
+
items,
|
|
95
|
+
index,
|
|
96
|
+
onIndexChange,
|
|
97
|
+
renderCaption,
|
|
98
|
+
renderActions,
|
|
99
|
+
renderMedia,
|
|
100
|
+
title,
|
|
101
|
+
description,
|
|
102
|
+
zoomable = true,
|
|
103
|
+
swipeToDismiss = true,
|
|
104
|
+
labels: labelOverrides,
|
|
105
|
+
testID = "native-lightbox",
|
|
106
|
+
}: NativeLightboxProps) {
|
|
107
|
+
const labels = { ...defaultNativeLightboxLabels, ...labelOverrides };
|
|
108
|
+
return (
|
|
109
|
+
<NativeFullScreenModal open={open} onOpenChange={onOpenChange} tone="none" testID={testID}>
|
|
110
|
+
{open ? (
|
|
111
|
+
<LightboxBody
|
|
112
|
+
testID={testID}
|
|
113
|
+
items={items}
|
|
114
|
+
index={index}
|
|
115
|
+
onIndexChange={onIndexChange}
|
|
116
|
+
onClose={() => onOpenChange(false)}
|
|
117
|
+
renderCaption={renderCaption}
|
|
118
|
+
renderActions={renderActions}
|
|
119
|
+
renderMedia={renderMedia}
|
|
120
|
+
title={title}
|
|
121
|
+
description={description}
|
|
122
|
+
zoomable={zoomable}
|
|
123
|
+
swipeToDismiss={swipeToDismiss}
|
|
124
|
+
labels={labels}
|
|
125
|
+
/>
|
|
126
|
+
) : null}
|
|
127
|
+
</NativeFullScreenModal>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface LightboxBodyProps {
|
|
132
|
+
testID: string;
|
|
133
|
+
items: GalleryItem[];
|
|
134
|
+
index: number;
|
|
135
|
+
onIndexChange: (index: number) => void;
|
|
136
|
+
onClose: () => void;
|
|
137
|
+
renderCaption?: (item: GalleryItem, index: number) => ReactNode;
|
|
138
|
+
renderActions?: (item: GalleryItem, index: number) => ReactNode;
|
|
139
|
+
renderMedia?: (item: GalleryItem, index: number) => ReactNode;
|
|
140
|
+
title?: string;
|
|
141
|
+
description?: ReactNode;
|
|
142
|
+
zoomable: boolean;
|
|
143
|
+
swipeToDismiss: boolean;
|
|
144
|
+
labels: NativeLightboxLabels;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Inside the modal, so `useSafeAreaInsets` reads the modal's own safe area. */
|
|
148
|
+
function LightboxBody({
|
|
149
|
+
testID,
|
|
150
|
+
items,
|
|
151
|
+
index,
|
|
152
|
+
onIndexChange,
|
|
153
|
+
onClose,
|
|
154
|
+
renderCaption,
|
|
155
|
+
renderActions,
|
|
156
|
+
renderMedia,
|
|
157
|
+
title,
|
|
158
|
+
description,
|
|
159
|
+
zoomable,
|
|
160
|
+
swipeToDismiss,
|
|
161
|
+
labels,
|
|
162
|
+
}: LightboxBodyProps) {
|
|
163
|
+
const insets = useSafeAreaInsets();
|
|
164
|
+
const reducedMotion = useReducedMotion();
|
|
165
|
+
const [size, setSize] = useState(() => Dimensions.get("window"));
|
|
166
|
+
const [zoomed, setZoomed] = useState(false);
|
|
167
|
+
const [chrome, setChrome] = useState(true);
|
|
168
|
+
const backdrop = useRef(new Animated.Value(1)).current;
|
|
169
|
+
const listRef = useRef<FlatList<GalleryItem>>(null);
|
|
170
|
+
const width = size.width;
|
|
171
|
+
const current = Math.min(Math.max(0, index), Math.max(0, items.length - 1));
|
|
172
|
+
const item = items[current];
|
|
173
|
+
const many = items.length > 1;
|
|
174
|
+
|
|
175
|
+
// Re-snap on a width change (rotation, resize) so the page stays aligned; that jump is not animated.
|
|
176
|
+
const snappedWidth = useRef(width);
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
if (items.length === 0) return;
|
|
179
|
+
const widthChanged = snappedWidth.current !== width;
|
|
180
|
+
snappedWidth.current = width;
|
|
181
|
+
listRef.current?.scrollToIndex({ index: current, animated: !widthChanged && !reducedMotion });
|
|
182
|
+
}, [current, items.length, width, reducedMotion]);
|
|
183
|
+
|
|
184
|
+
// Another page starts unzoomed.
|
|
185
|
+
useEffect(() => setZoomed(false), [current]);
|
|
186
|
+
|
|
187
|
+
const onLayout = (e: LayoutChangeEvent) => {
|
|
188
|
+
const { width: w, height: h } = e.nativeEvent.layout;
|
|
189
|
+
if (w > 0 && (w !== size.width || h !== size.height)) setSize({ width: w, height: h, scale: 1, fontScale: 1 } as typeof size);
|
|
190
|
+
};
|
|
191
|
+
// The index is the parent's: a swipe asks for a page, and if the parent keeps the old one, the
|
|
192
|
+
// pager scrolls back to it, so what's shown never drifts from `index`.
|
|
193
|
+
const currentRef = useRef(current);
|
|
194
|
+
currentRef.current = current;
|
|
195
|
+
const onMomentumEnd = (e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
196
|
+
const page = Math.round(e.nativeEvent.contentOffset.x / Math.max(1, width));
|
|
197
|
+
const clamped = Math.min(Math.max(0, page), items.length - 1);
|
|
198
|
+
if (clamped === current) return;
|
|
199
|
+
onIndexChange(clamped);
|
|
200
|
+
setTimeout(() => {
|
|
201
|
+
if (currentRef.current !== clamped) listRef.current?.scrollToIndex({ index: currentRef.current, animated: !reducedMotion });
|
|
202
|
+
}, 0);
|
|
203
|
+
};
|
|
204
|
+
const goTo = (i: number) => {
|
|
205
|
+
if (i >= 0 && i < items.length && i !== current) onIndexChange(i);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const onPull = useCallback((dy: number) => backdrop.setValue(Math.max(0.35, 1 - Math.max(0, dy) / 400)), [backdrop]);
|
|
209
|
+
const toggleChrome = useCallback(() => setChrome((c) => !c), []);
|
|
210
|
+
const caption = item ? (renderCaption ? renderCaption(item, current) : item.caption) : null;
|
|
211
|
+
const position = items.length > 0 ? labels.position(current + 1, items.length) : "";
|
|
212
|
+
const pagerA11y = {
|
|
213
|
+
testID: `${testID}-pager-a11y`,
|
|
214
|
+
accessible: true,
|
|
215
|
+
accessibilityRole: "adjustable" as const,
|
|
216
|
+
accessibilityLabel: item ? (item.alt ?? labels.photo(current + 1, items.length)) : title,
|
|
217
|
+
accessibilityValue: many ? { text: position } : undefined,
|
|
218
|
+
accessibilityHint: many ? labels.pagerHint : undefined,
|
|
219
|
+
accessibilityActions: many ? [{ name: "increment" }, { name: "decrement" }] : [],
|
|
220
|
+
onAccessibilityAction: (e: { nativeEvent: { actionName: string } }) => {
|
|
221
|
+
if (e.nativeEvent.actionName === "increment") goTo(current + 1);
|
|
222
|
+
if (e.nativeEvent.actionName === "decrement") goTo(current - 1);
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<View testID={`${testID}-content`} accessibilityLabel={title} onLayout={onLayout} style={{ flex: 1 }}>
|
|
228
|
+
<Animated.View pointerEvents="none" style={{ position: "absolute", top: 0, right: 0, bottom: 0, left: 0, backgroundColor: FULL_SCREEN_DARK, opacity: backdrop }} />
|
|
229
|
+
{/* Built-in photo pages: the page itself is the adjustable pager. A renderMedia page keeps its
|
|
230
|
+
own controls reachable (an accessible wrapper would group them away from VoiceOver), so the
|
|
231
|
+
pager is the counter in the top bar instead. */}
|
|
232
|
+
<View style={{ flex: 1 }} testID={`${testID}-pages`} {...(renderMedia ? {} : pagerA11y)}>
|
|
233
|
+
<FlatList
|
|
234
|
+
ref={listRef}
|
|
235
|
+
testID={`${testID}-pager`}
|
|
236
|
+
data={items}
|
|
237
|
+
horizontal
|
|
238
|
+
pagingEnabled
|
|
239
|
+
scrollEnabled={many && !zoomed}
|
|
240
|
+
showsHorizontalScrollIndicator={false}
|
|
241
|
+
keyExtractor={(it) => it.id}
|
|
242
|
+
initialScrollIndex={current}
|
|
243
|
+
getItemLayout={(_data, i) => ({ length: width, offset: width * i, index: i })}
|
|
244
|
+
onMomentumScrollEnd={onMomentumEnd}
|
|
245
|
+
// A page or two each side: a video from renderMedia mounts only near the screen.
|
|
246
|
+
windowSize={3}
|
|
247
|
+
initialNumToRender={1}
|
|
248
|
+
extraData={current}
|
|
249
|
+
renderItem={({ item: it, index: i }) => (
|
|
250
|
+
<View testID={`${testID}-page-${it.id}`} style={{ width, flex: 1 }} className="items-center justify-center">
|
|
251
|
+
{renderMedia ? (
|
|
252
|
+
renderMedia(it, i)
|
|
253
|
+
) : zoomable || swipeToDismiss ? (
|
|
254
|
+
<NativeZoomablePhoto
|
|
255
|
+
src={it.src}
|
|
256
|
+
alt={it.alt ?? labels.photo(i + 1, items.length)}
|
|
257
|
+
width={width}
|
|
258
|
+
height={size.height}
|
|
259
|
+
active={i === current}
|
|
260
|
+
zoomable={zoomable}
|
|
261
|
+
onZoomChange={setZoomed}
|
|
262
|
+
onPull={swipeToDismiss ? onPull : null}
|
|
263
|
+
onDismiss={onClose}
|
|
264
|
+
onTap={toggleChrome}
|
|
265
|
+
testID={`${testID}-photo-${it.id}`}
|
|
266
|
+
/>
|
|
267
|
+
) : (
|
|
268
|
+
<Pressable accessible={false} onPress={toggleChrome} style={{ width, flex: 1 }}>
|
|
269
|
+
<NativeImage src={it.src} alt={it.alt ?? labels.photo(i + 1, items.length)} className="h-full w-full" contentFit="contain" />
|
|
270
|
+
</Pressable>
|
|
271
|
+
)}
|
|
272
|
+
</View>
|
|
273
|
+
)}
|
|
274
|
+
style={{ flex: 1 }}
|
|
275
|
+
/>
|
|
276
|
+
</View>
|
|
277
|
+
{chrome ? (
|
|
278
|
+
<View
|
|
279
|
+
pointerEvents="box-none"
|
|
280
|
+
testID={`${testID}-chrome`}
|
|
281
|
+
className="absolute left-0 right-0 top-0 flex-row items-center justify-between px-3"
|
|
282
|
+
style={{ paddingTop: insets.top + 8 }}
|
|
283
|
+
>
|
|
284
|
+
<Pressable
|
|
285
|
+
testID={`${testID}-close`}
|
|
286
|
+
accessibilityRole="button"
|
|
287
|
+
accessibilityLabel={labels.close}
|
|
288
|
+
onPress={onClose}
|
|
289
|
+
hitSlop={8}
|
|
290
|
+
className="h-11 w-11 items-center justify-center rounded-full bg-black/45"
|
|
291
|
+
>
|
|
292
|
+
<NativeIcon name="close" size={20} color={CHROME_TINT} />
|
|
293
|
+
</Pressable>
|
|
294
|
+
<View className="flex-1 items-center px-2">
|
|
295
|
+
{title ? (
|
|
296
|
+
<Text testID={`${testID}-title`} accessibilityRole="header" numberOfLines={1} className="text-sm font-semibold text-white">
|
|
297
|
+
{title}
|
|
298
|
+
</Text>
|
|
299
|
+
) : null}
|
|
300
|
+
{description ? (
|
|
301
|
+
typeof description === "string" ? (
|
|
302
|
+
<Text testID={`${testID}-description`} numberOfLines={1} className="text-xs text-white/70">
|
|
303
|
+
{description}
|
|
304
|
+
</Text>
|
|
305
|
+
) : (
|
|
306
|
+
<View testID={`${testID}-description`}>{description}</View>
|
|
307
|
+
)
|
|
308
|
+
) : null}
|
|
309
|
+
{/* With photo pages the page reads the position and the counter is for the eye; with
|
|
310
|
+
renderMedia pages the counter is the pager. One picture has no counter. */}
|
|
311
|
+
{renderMedia ? (
|
|
312
|
+
<View {...pagerA11y}>
|
|
313
|
+
{many ? (
|
|
314
|
+
<Text testID={`${testID}-counter`} className="text-xs font-medium text-white/70">
|
|
315
|
+
{`${current + 1} / ${items.length}`}
|
|
316
|
+
</Text>
|
|
317
|
+
) : null}
|
|
318
|
+
</View>
|
|
319
|
+
) : many ? (
|
|
320
|
+
<Text testID={`${testID}-counter`} importantForAccessibility="no-hide-descendants" accessibilityElementsHidden className="text-xs font-medium text-white/70">
|
|
321
|
+
{`${current + 1} / ${items.length}`}
|
|
322
|
+
</Text>
|
|
323
|
+
) : null}
|
|
324
|
+
</View>
|
|
325
|
+
<View testID={`${testID}-actions`} className="min-w-11 flex-row items-center justify-end gap-1">
|
|
326
|
+
{item && renderActions ? renderActions(item, current) : null}
|
|
327
|
+
</View>
|
|
328
|
+
</View>
|
|
329
|
+
) : null}
|
|
330
|
+
{chrome && caption ? (
|
|
331
|
+
<View
|
|
332
|
+
pointerEvents="box-none"
|
|
333
|
+
testID={`${testID}-caption`}
|
|
334
|
+
className={cn("absolute bottom-0 left-0 right-0 px-4 pt-3")}
|
|
335
|
+
style={{ paddingBottom: insets.bottom + 12 }}
|
|
336
|
+
>
|
|
337
|
+
{typeof caption === "string" ? <Text className="text-center text-sm text-white">{caption}</Text> : caption}
|
|
338
|
+
</View>
|
|
339
|
+
) : null}
|
|
340
|
+
</View>
|
|
341
|
+
);
|
|
342
|
+
}
|