TypeScript
fix(mobile): resize Android image previews with the window (#13859)
Dylan Zahnaf17eb93311e
diff --git a/patches/[email protected] b/patches/[email protected]new file mode 100644index 00000000000..6e2b719704e--- /dev/null+++ b/patches/[email protected]@@ -0,0 +1,274 @@+diff --git a/dist/ImageViewing.js b/dist/ImageViewing.js+index 3d7641a9a94864415d207c678b71f8c592c90aa1..2b9ef77c3eb58cd284152ad22b2e711a91d50e7d 100644+--- a/dist/ImageViewing.js++++ b/dist/ImageViewing.js+@@ -5,8 +5,8 @@+ * LICENSE file in the root directory of this source tree.+ *+ */+-import React, { useCallback, useRef, useEffect } from "react";+-import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, } from "react-native";++import React, { useCallback, useRef, useEffect, useLayoutEffect } from "react";++import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, Platform, useWindowDimensions, } from "react-native";+ import ImageItem from "./components/ImageItem/ImageItem";+ import ImageDefaultHeader from "./components/ImageDefaultHeader";+ import StatusBarManager from "./components/StatusBarManager";+@@ -17,23 +17,34 @@ const DEFAULT_ANIMATION_TYPE = "fade";+ const DEFAULT_BG_COLOR = "#000";+ const DEFAULT_DELAY_LONG_PRESS = 800;+ const SCREEN = Dimensions.get("screen");+-const SCREEN_WIDTH = SCREEN.width;++const useViewingDimensions = Platform.OS === "android" ? useWindowDimensions : () => SCREEN;+ function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClose, onLongPress = () => { }, onImageIndexChange, animationType = DEFAULT_ANIMATION_TYPE, backgroundColor = DEFAULT_BG_COLOR, presentationStyle, swipeToCloseEnabled, doubleTapToZoomEnabled, delayLongPress = DEFAULT_DELAY_LONG_PRESS, HeaderComponent, FooterComponent, }) {++ const viewport = useViewingDimensions();++ const SCREEN_WIDTH = viewport.width;++ const viewportKey = Platform.OS === "android" ? `${viewport.width}:${viewport.height}` : undefined;+ const imageList = useRef(null);+ const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose);+- const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, SCREEN);+- const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents();++ const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, viewport);++ const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents(viewportKey);+ useEffect(() => {+ if (onImageIndexChange) {+ onImageIndexChange(currentImageIndex);+ }+ }, [currentImageIndex]);+- const onZoom = useCallback((isScaled) => {++ const activeZoom = useRef(null);++ const onZoom = useCallback(function handleZoom(isScaled) {++ if (Platform.OS === "android" && activeZoom.current !== handleZoom) {++ return;++ }+ var _a, _b;+ // @ts-ignore+ (_b = (_a = imageList) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.setNativeProps({ scrollEnabled: !isScaled });+ toggleBarsVisible(!isScaled);+- }, [imageList]);++ }, [imageList, toggleBarsVisible]);++ useLayoutEffect(() => {++ activeZoom.current = onZoom;++ return () => { activeZoom.current = null; };++ }, [onZoom]);+ if (!visible) {+ return null;+ }+@@ -45,7 +56,8 @@ function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClos+ imageIndex: currentImageIndex,+ })) : (<ImageDefaultHeader onRequestClose={onRequestCloseEnhanced}/>)}+ </Animated.View>+- <VirtualizedList ref={imageList} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={imageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({++ {/* Reset Android image gestures on resize while preserving custom header state. */}++ <VirtualizedList key={viewportKey} ref={imageList} onScroll={Platform.OS === "android" ? onScroll : undefined} scrollEventThrottle={Platform.OS === "android" ? 16 : undefined} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={currentImageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({+ length: SCREEN_WIDTH,+ offset: SCREEN_WIDTH * index,+ index,+diff --git a/dist/components/ImageItem/ImageItem.android.js b/dist/components/ImageItem/ImageItem.android.js+index 5fcaa9f9a7d893817fee89b85186ee3936118e69..3effe838fa54495d165b81cc7501e72d51fbee1b 100644+--- a/dist/components/ImageItem/ImageItem.android.js++++ b/dist/components/ImageItem/ImageItem.android.js+@@ -6,17 +6,17 @@+ *+ */+ import React, { useCallback, useRef, useState } from "react";+-import { Animated, ScrollView, Dimensions, StyleSheet, } from "react-native";++import { Animated, ScrollView, Dimensions, } from "react-native";+ import useImageDimensions from "../../hooks/useImageDimensions";+ import usePanResponder from "../../hooks/usePanResponder";+ import { getImageStyles, getImageTransform } from "../../utils";+ import { ImageLoading } from "./ImageLoading";+ const SWIPE_CLOSE_OFFSET = 75;+ const SWIPE_CLOSE_VELOCITY = 1.75;+-const SCREEN = Dimensions.get("window");+-const SCREEN_WIDTH = SCREEN.width;+-const SCREEN_HEIGHT = SCREEN.height;+ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled = true, doubleTapToZoomEnabled = true, }) => {++ const SCREEN = Dimensions.get("window");++ const SCREEN_WIDTH = SCREEN.width;++ const SCREEN_HEIGHT = SCREEN.height;+ const imageContainer = useRef(null);+ const imageDimensions = useImageDimensions(imageSrc);+ const [translate, scale] = getImageTransform(imageDimensions, SCREEN);+@@ -31,7 +31,7 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre+ scrollEnabled: !isZoomed,+ });+ }+- }, [imageContainer]);++ }, [imageContainer, onZoom]);+ const onLongPressHandler = useCallback(() => {+ onLongPress(imageSrc);+ }, [imageSrc, onLongPress]);+@@ -64,7 +64,7 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre+ const offsetY = (_c = (_b = (_a = nativeEvent) === null || _a === void 0 ? void 0 : _a.contentOffset) === null || _b === void 0 ? void 0 : _b.y, (_c !== null && _c !== void 0 ? _c : 0));+ scrollValueY.setValue(offsetY);+ };+- return (<ScrollView ref={imageContainer} style={styles.listItem} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={styles.imageScrollContainer} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {++ return (<ScrollView ref={imageContainer} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={{ height: SCREEN_HEIGHT * 2 }} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {+ onScroll,+ onScrollEndDrag,+ })}>+@@ -72,13 +72,4 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre+ {(!isLoaded || !imageDimensions) && <ImageLoading />}+ </ScrollView>);+ };+-const styles = StyleSheet.create({+- listItem: {+- width: SCREEN_WIDTH,+- height: SCREEN_HEIGHT,+- },+- imageScrollContainer: {+- height: SCREEN_HEIGHT * 2,+- },+-});+ export default React.memo(ImageItem);+diff --git a/dist/components/ImageItem/ImageLoading.js b/dist/components/ImageItem/ImageLoading.js+index 9770f7db158a2fb2b1c69a49e5fcbc89d8624603..995fab7524bb8673258d44e5eda361c62dae0324 100644+--- a/dist/components/ImageItem/ImageLoading.js++++ b/dist/components/ImageItem/ImageLoading.js+@@ -6,25 +6,17 @@+ *+ */+ import React from "react";+-import { ActivityIndicator, Dimensions, StyleSheet, View } from "react-native";++import { ActivityIndicator, Dimensions, Platform, StyleSheet, View } from "react-native";+ const SCREEN = Dimensions.get("screen");+-const SCREEN_WIDTH = SCREEN.width;+-const SCREEN_HEIGHT = SCREEN.height;+-export const ImageLoading = () => (<View style={styles.loading}>++export const ImageLoading = () => {++ const { width, height } = Platform.OS === "android" ? Dimensions.get("window") : SCREEN;++ return (<View style={[styles.loading, { width, height }]}>+ <ActivityIndicator size="small" color="#FFF"/>+ </View>);++};+ const styles = StyleSheet.create({+- listItem: {+- width: SCREEN_WIDTH,+- height: SCREEN_HEIGHT,+- },+ loading: {+- width: SCREEN_WIDTH,+- height: SCREEN_HEIGHT,+ alignItems: "center",+ justifyContent: "center",+ },+- imageScrollContainer: {+- height: SCREEN_HEIGHT,+- },+ });+diff --git a/dist/hooks/useAnimatedComponents.d.ts b/dist/hooks/useAnimatedComponents.d.ts+index de5957458fea62f33743103acaa170a2c90d3638..dbafa8c5c5f3f86161e8520822cf10e19e88ea25 100644+--- a/dist/hooks/useAnimatedComponents.d.ts++++ b/dist/hooks/useAnimatedComponents.d.ts+@@ -6,7 +6,7 @@+ *+ */+ import { Animated } from "react-native";+-declare const useAnimatedComponents: () => readonly [{++declare const useAnimatedComponents: (resetKey?: string) => readonly [{+ [key: string]: Animated.Value;+ }[], {+ [key: string]: Animated.Value;+diff --git a/dist/hooks/useAnimatedComponents.js b/dist/hooks/useAnimatedComponents.js+index 4730fe40735bd80afc97abdd14150a2fb4abf3e5..0ed2100744ccc9bcee7f1b15b0328a729d47bb16 100644+--- a/dist/hooks/useAnimatedComponents.js++++ b/dist/hooks/useAnimatedComponents.js+@@ -5,16 +5,19 @@+ * LICENSE file in the root directory of this source tree.+ *+ */++import { useCallback, useMemo } from "react";+ import { Animated } from "react-native";+ const INITIAL_POSITION = { x: 0, y: 0 };+ const ANIMATION_CONFIG = {+ duration: 200,+ useNativeDriver: true,+ };+-const useAnimatedComponents = () => {+- const headerTranslate = new Animated.ValueXY(INITIAL_POSITION);+- const footerTranslate = new Animated.ValueXY(INITIAL_POSITION);+- const toggleVisible = (isVisible) => {++const useAnimatedComponents = (resetKey) => {++ const [headerTranslate, footerTranslate] = useMemo(() => [++ new Animated.ValueXY(INITIAL_POSITION),++ new Animated.ValueXY(INITIAL_POSITION),++ ], [resetKey]);++ const toggleVisible = useCallback((isVisible) => {+ if (isVisible) {+ Animated.parallel([+ Animated.timing(headerTranslate.y, { ...ANIMATION_CONFIG, toValue: 0 }),+@@ -33,7 +36,7 @@ const useAnimatedComponents = () => {+ }),+ ]).start();+ }+- };++ }, [headerTranslate, footerTranslate]);+ const headerTransform = headerTranslate.getTranslateTransform();+ const footerTransform = footerTranslate.getTranslateTransform();+ return [headerTransform, footerTransform, toggleVisible];+diff --git a/dist/hooks/useImageIndexChange.js b/dist/hooks/useImageIndexChange.js+index cb5f0f798e5cfeedf459b7d22e92871db3092020..97ca2c9c326b1c53e6a5d90c3bb95d53342088d1 100644+--- a/dist/hooks/useImageIndexChange.js++++ b/dist/hooks/useImageIndexChange.js+@@ -5,16 +5,34 @@+ * LICENSE file in the root directory of this source tree.+ *+ */+-import { useState } from "react";++import { useCallback, useLayoutEffect, useRef, useState } from "react";++import { Platform } from "react-native";+ const useImageIndexChange = (imageIndex, screen) => {+ const [currentImageIndex, setImageIndex] = useState(imageIndex);+- const onScroll = (event) => {++ const indexRef = useRef(imageIndex);++ const activeScroll = useRef(null);++ const onScroll = useCallback(function handleScroll(event) {++ if (Platform.OS === "android") {++ // Events at this width stay valid until commit; replaced or relaid-out lists do not.++ if (activeScroll.current !== handleScroll ||++ Math.abs(event.nativeEvent.layoutMeasurement.width - screen.width) > 1) {++ return;++ }++ }+ const { nativeEvent: { contentOffset: { x: scrollX }, }, } = event;+ if (screen.width) {+ const nextIndex = Math.round(scrollX / screen.width);+- setImageIndex(nextIndex < 0 ? 0 : nextIndex);++ const boundedIndex = nextIndex < 0 ? 0 : nextIndex;++ if (boundedIndex !== indexRef.current) {++ indexRef.current = boundedIndex;++ setImageIndex(boundedIndex);++ }+ }+- };++ }, [screen]);++ useLayoutEffect(() => {++ activeScroll.current = onScroll;++ return () => { activeScroll.current = null; };++ }, [onScroll]);+ return [currentImageIndex, onScroll];+ };+ export default useImageIndexChange;+diff --git a/dist/hooks/usePanResponder.js b/dist/hooks/usePanResponder.js+index 99dcbc7efea4eba86a8bc2bad71dde73c8ebf1b4..3a374c0923879548ec3826a78be16f79c6e01432 100644+--- a/dist/hooks/usePanResponder.js++++ b/dist/hooks/usePanResponder.js+@@ -8,14 +8,14 @@+ import { useMemo, useEffect } from "react";+ import { Animated, Dimensions, } from "react-native";+ import { createPanResponder, getDistanceBetweenTouches, getImageTranslate, getImageDimensionsByTranslate, } from "../utils";+-const SCREEN = Dimensions.get("window");+-const SCREEN_WIDTH = SCREEN.width;+-const SCREEN_HEIGHT = SCREEN.height;+-const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);+ const SCALE_MAX = 2;+ const DOUBLE_TAP_DELAY = 300;+ const OUT_BOUND_MULTIPLIER = 0.75;+ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, }) => {++ const SCREEN = Dimensions.get("window");++ const SCREEN_WIDTH = SCREEN.width;++ const SCREEN_HEIGHT = SCREEN.height;++ const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);+ let numberInitialTouches = 1;+ let initialTouches = [];+ let currentScale = initialScale;diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 6a8e06b7722..62bd52bbbb2 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -185,6 +185,7 @@ patchedDependencies:[email protected]: 49ab4815c18b003f6af998dcc6c258c17a21b50b2cc87745f12d4bd8b6d591f9[email protected]: f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651[email protected]: abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626+ [email protected]: a479085493f2404ecdefea18f46c6be3ad49cfb54da8e78dc7746651263cb5a0[email protected]: f28d08f1233e3cce8b9324b4357d2b10f1ee040b8051bc87235b0fb95069b9bb[email protected]: 6f52854210f61bef499850ebd498229a111bc34ec5b2a4925e644f2f48f42760[email protected]: 81caa279ec3dc1f81209062133b66e8f824e13f106cca57f1fa732a72c2ab378@@ -569,7 +570,7 @@ importers:version: 3.3.0(patch_hash=abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])react-native-image-viewing:specifier: ^0.2.2- version: 0.2.2([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])+ version: 0.2.2(patch_hash=a479085493f2404ecdefea18f46c6be3ad49cfb54da8e78dc7746651263cb5a0)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])react-native-keyboard-controller:specifier: 1.22.7version: 1.22.7(patch_hash=f28d08f1233e3cce8b9324b4357d2b10f1ee040b8051bc87235b0fb95069b9bb)([email protected]([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected]))([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected]))([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])@@ -22590,7 +22591,7 @@ snapshots:react: 19.3.0react-native: 0.88.0(@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])- [email protected]([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected]):+ [email protected](patch_hash=a479085493f2404ecdefea18f46c6be3ad49cfb54da8e78dc7746651263cb5a0)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected]):dependencies:react: 19.3.0react-native: 0.88.0(@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex dbe0c9dedf0..73827916a40 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -317,6 +317,7 @@ packageExtensions:vite-plus: "catalog:"patchedDependencies:+ [email protected]: patches/[email protected][email protected]: patches/[email protected][email protected]: patches/[email protected][email protected]: patches/[email protected]