Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile] - Image - Workaround for Android and orientation changes #42900

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/components/src/mobile/image/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* External dependencies
*/
import { Image as RNImage, Text, View } from 'react-native';
import {
Image as RNImage,
Text,
View,
useWindowDimensions,
} from 'react-native';
import FastImage from 'react-native-fast-image';

/**
Expand All @@ -11,7 +16,7 @@ import { __ } from '@wordpress/i18n';
import { Icon } from '@wordpress/components';
import { image as icon } from '@wordpress/icons';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
import { useEffect, useState } from '@wordpress/element';
import { useEffect, useState, useRef, Platform } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -54,6 +59,10 @@ const ImageComponent = ( {
} ) => {
const [ imageData, setImageData ] = useState( null );
const [ containerSize, setContainerSize ] = useState( null );
const [ shouldUseFallback, setShouldUseFallback ] = useState( false );
const { height: windowHeight, width: windowWidth } = useWindowDimensions();
const isLandscape = useRef( windowWidth > windowHeight );

const Image = ! shouldUseFastImage ? RNImage : FastImage;
const imageResizeMode = ! shouldUseFastImage
? resizeMode
Expand All @@ -80,6 +89,21 @@ const ImageComponent = ( {
return () => ( isCurrent = false );
}, [ url ] );

// Workaround for Android where changing the orientation breaks FastImage
// for now if there's any orientation changes, it will use the fallback
// prop to use the default Image component.
// https://github.com/WordPress/gutenberg/issues/42869
useEffect( () => {
if ( Platform.isAndroid && shouldUseFastImage ) {
const isCurrentOrientationLandscape = windowWidth > windowHeight;

if ( isLandscape.current !== isCurrentOrientationLandscape ) {
setShouldUseFallback( true );
isLandscape.current = isCurrentOrientationLandscape;
}
}
}, [ windowHeight, windowWidth ] );

const onContainerLayout = ( event ) => {
const { height, width } = event.nativeEvent.layout;

Expand Down Expand Up @@ -226,6 +250,7 @@ const ImageComponent = ( {
resizeMethod: 'scale',
} ) }
resizeMode={ imageResizeMode }
fallback={ shouldUseFallback }
/>
</View>
) }
Expand Down