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

feat: Added objectFit style prop for Image #34585

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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 Libraries/Image/Image.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import NativeImageLoaderAndroid from './NativeImageLoaderAndroid';
import TextInlineImageNativeComponent from './TextInlineImageNativeComponent';

import type {ImageProps as ImagePropsType} from './ImageProps';
import type {ImageObjectFit} from './ImageObjectFit';
import type {ImageResizeMode} from './ImageResizeMode';
import type {RootTag} from '../Types/RootTagTypes';

let _requestId = 1;
Expand Down Expand Up @@ -51,6 +53,21 @@ function getSize(
);
}

function getResizeModeEquivalentFromObjectFit(
objectFit?: ?ImageObjectFit,
): ?ImageResizeMode {
if (!objectFit) {
return null;
}
const objectFitMappingToResizeMode = {
contain: 'contain',
cover: 'cover',
fill: 'stretch',
'scale-down': 'contain',
};
return objectFitMappingToResizeMode[objectFit] ?? null;
}

/**
* Retrieve the width and height (in pixels) of an image prior to displaying it
* with the ability to provide the headers for the request
Expand Down Expand Up @@ -188,6 +205,9 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
ref: forwardedRef,
};

const updatedResizeMode =
getResizeModeEquivalentFromObjectFit(style?.objectFit) || props.resizeMode;

return (
<ImageAnalyticsTagContext.Consumer>
{analyticTag => {
Expand All @@ -206,15 +226,20 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
return (
<TextInlineImageNativeComponent
style={style}
resizeMode={props.resizeMode}
resizeMode={updatedResizeMode}
headers={nativeProps.headers}
src={src}
ref={forwardedRef}
/>
);
}

return <ImageViewNativeComponent {...nativePropsWithAnalytics} />;
return (
<ImageViewNativeComponent
resizeMode={updatedResizeMode}
{...nativePropsWithAnalytics}
/>
);
}}
</TextAncestor.Consumer>
);
Expand Down
24 changes: 23 additions & 1 deletion Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import flattenStyle from '../StyleSheet/flattenStyle';
import resolveAssetSource from './resolveAssetSource';

import type {ImageProps as ImagePropsType} from './ImageProps';
import type {ImageObjectFit} from './ImageObjectFit';
import type {ImageResizeMode} from './ImageResizeMode';

import type {ImageStyleProp} from '../StyleSheet/StyleSheet';
import NativeImageLoaderIOS from './NativeImageLoaderIOS';
Expand All @@ -39,6 +41,21 @@ function getSize(
);
}

function getResizeModeEquivalentFromObjectFit(
objectFit?: ?ImageObjectFit,
): ?ImageResizeMode {
if (!objectFit) {
return null;
}
const objectFitMappingToResizeMode = {
contain: 'contain',
cover: 'cover',
fill: 'stretch',
'scale-down': 'contain',
};
return objectFitMappingToResizeMode[objectFit] ?? null;
}

function getSizeWithHeaders(
uri: string,
headers: {[string]: string, ...},
Expand Down Expand Up @@ -128,6 +145,11 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {

// $FlowFixMe[prop-missing]
const resizeMode = props.resizeMode || style.resizeMode || 'cover';

const updatedResizeMode =
// $FlowFixMe[prop-missing]
getResizeModeEquivalentFromObjectFit(style?.objectFit) || resizeMode;

// $FlowFixMe[prop-missing]
const tintColor = style.tintColor;

Expand All @@ -151,7 +173,7 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
{...props}
ref={forwardedRef}
style={style}
resizeMode={resizeMode}
resizeMode={updatedResizeMode}
tintColor={tintColor}
source={sources}
internal_analyticTag={analyticTag}
Expand Down
32 changes: 32 additions & 0 deletions Libraries/Image/ImageObjectFit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

'use strict';

/**
* ImageObjectFit partially equals to ImageResizeMode, defines valid values for different image resizing modes set
* via the `objectFit` style property on `<Image>`.
*/
export type ImageObjectFit =
// Resize such that the entire area of the view is covered by the image,
// potentially clipping parts of the image.
| 'cover'

// Resize by stretching it to fill the entire frame of the view without
// clipping. This may change the aspect ratio of the image, distorting it.
| 'fill'

// Resize such that it will be completely visible, contained within the frame
// of the View.
| 'scale-down'

// Resize such that it will be completely visible, contained within the frame
// of the View.
| 'contain';
2 changes: 2 additions & 0 deletions Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ export type ____TextStyle_Internal = $ReadOnly<{
export type ____ImageStyle_InternalCore = $ReadOnly<{
...$Exact<____ViewStyle_Internal>,
resizeMode?: 'contain' | 'cover' | 'stretch' | 'center' | 'repeat',
objectFit?: 'cover' | 'fill' | 'scale-down' | 'contain',
tintColor?: ____ColorValue_Internal,
overlayColor?: string,
}>;
Expand All @@ -654,6 +655,7 @@ export type ____ImageStyle_Internal = $ReadOnly<{
export type ____DangerouslyImpreciseStyle_InternalCore = $ReadOnly<{
...$Exact<____TextStyle_Internal>,
resizeMode?: 'contain' | 'cover' | 'stretch' | 'center' | 'repeat',
objectFit?: 'cover' | 'fill' | 'scale-down' | 'contain',
tintColor?: ____ColorValue_Internal,
overlayColor?: string,
}>;
Expand Down
72 changes: 72 additions & 0 deletions packages/rn-tester/js/examples/Image/ImageExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,28 @@ const styles = StyleSheet.create({
fontSize: 11,
marginBottom: 3,
},
objectFit: {
width: 90,
height: 60,
borderWidth: 0.5,
borderColor: 'black',
},
objectFitCover: {
objectFit: 'cover',
},
objectFitFill: {
objectFit: 'fill',
},
objectFitScaleDown: {
objectFit: 'scale-down',
},
objectFitContain: {
objectFit: 'contain',
},
objectFitText: {
fontSize: 11,
marginBottom: 3,
},
icon: {
width: 15,
height: 15,
Expand Down Expand Up @@ -1046,6 +1068,56 @@ exports.examples = [
);
},
},
{
title: 'Object Fit',
description:
('The `objectFit` style props that are partially equivalent to resizeMode style prop, controls how the image is ' +
'rendered within the frame.': string),
render: function (): React.Node {
return (
<View>
{[smallImage, fullImage].map((image, index) => {
return (
<View key={index}>
<View style={styles.horizontal}>
<View>
<Text style={styles.objectFitText}>Cover</Text>
<Image
style={[styles.objectFit, styles.objectFitCover]}
source={image}
/>
</View>
<View style={styles.leftMargin}>
<Text style={styles.objectFitText}>Fill</Text>
<Image
style={[styles.objectFit, styles.objectFitFill]}
source={image}
/>
</View>
</View>
<View style={styles.horizontal}>
<View>
<Text style={styles.objectFitText}>Scale Down</Text>
<Image
style={[styles.objectFit, styles.objectFitScaleDown]}
source={image}
/>
</View>
<View style={styles.leftMargin}>
<Text style={styles.objectFitText}>Contain</Text>
<Image
style={[styles.objectFit, styles.objectFitContain]}
source={image}
/>
</View>
</View>
</View>
);
})}
</View>
);
},
},
{
title: 'Animated GIF',
render: function (): React.Node {
Expand Down