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: Adding support for objectFit a partial equivalent to the resizeMode style and prop of <Image>. #34576

Closed
wants to merge 5 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
1 change: 1 addition & 0 deletions Libraries/Components/View/ReactNativeStyleAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
overlayColor: colorAttributes,
resizeMode: true,
tintColor: colorAttributes,
objectFit: true,
};

module.exports = ReactNativeStyleAttributes;
19 changes: 17 additions & 2 deletions Libraries/Image/Image.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import NativeImageLoaderAndroid from './NativeImageLoaderAndroid';

import TextInlineImageNativeComponent from './TextInlineImageNativeComponent';

import {convertObjectFitToResizeMode} from './ImageUtils';

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

Expand Down Expand Up @@ -188,6 +190,14 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
ref: forwardedRef,
};

const objectFit =
style && style.objectFit
? convertObjectFitToResizeMode(style.objectFit)
: null;
// $FlowFixMe[prop-missing]
const resizeMode =
objectFit || props.resizeMode || (style && style.resizeMode) || 'cover';

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

return <ImageViewNativeComponent {...nativePropsWithAnalytics} />;
return (
<ImageViewNativeComponent
{...nativePropsWithAnalytics}
resizeMode={resizeMode}
/>
);
}}
</TextAncestor.Consumer>
);
Expand Down
12 changes: 10 additions & 2 deletions Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import type {ImageProps as ImagePropsType} from './ImageProps';
import type {ImageStyleProp} from '../StyleSheet/StyleSheet';
import NativeImageLoaderIOS from './NativeImageLoaderIOS';

import {convertObjectFitToResizeMode} from './ImageUtils';

import ImageViewNativeComponent from './ImageViewNativeComponent';
import type {RootTag} from 'react-native/Libraries/Types/RootTagTypes';

Expand Down Expand Up @@ -126,8 +128,14 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
}
}

// $FlowFixMe[prop-missing]
const resizeMode = props.resizeMode || style.resizeMode || 'cover';
const objectFit =
// $FlowFixMe[prop-missing]
style && style.objectFit
? convertObjectFitToResizeMode(style.objectFit)
: null;
const resizeMode =
// $FlowFixMe[prop-missing]
objectFit || props.resizeMode || (style && style.resizeMode) || 'cover';
// $FlowFixMe[prop-missing]
const tintColor = props.tintColor || style.tintColor;

Expand Down
21 changes: 21 additions & 0 deletions Libraries/Image/ImageUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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
* @format
*/

type ResizeMode = 'cover' | 'contain' | 'stretch' | 'repeat' | 'center';

export function convertObjectFitToResizeMode(objectFit: string): ResizeMode {
const objectFitMap = {
contain: 'contain',
cover: 'cover',
fill: 'stretch',
'scale-down': 'contain',
};
return objectFitMap[objectFit];
}
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' | 'contain' | 'fill' | 'scale-down',
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' | 'contain' | 'fill' | 'scale-down',
tintColor?: ____ColorValue_Internal,
overlayColor?: string,
}>;
Expand Down
49 changes: 49 additions & 0 deletions packages/rn-tester/js/examples/Image/ImageExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,55 @@ exports.examples = [
);
},
},
{
title: 'Object Fit',
description: ('The `objectFit` 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.resizeModeText}>Contain</Text>
<Image
style={[styles.resizeMode, {objectFit: 'contain'}]}
source={image}
/>
</View>
<View style={styles.leftMargin}>
<Text style={styles.resizeModeText}>Cover</Text>
<Image
style={[styles.resizeMode, {objectFit: 'cover'}]}
source={image}
/>
</View>
</View>
<View style={styles.horizontal}>
<View>
<Text style={styles.resizeModeText}>Fill</Text>
<Image
style={[styles.resizeMode, {objectFit: 'fill'}]}
source={image}
/>
</View>
<View style={styles.leftMargin}>
<Text style={styles.resizeModeText}>Scale Down</Text>
<Image
style={[styles.resizeMode, {objectFit: 'scale-down'}]}
source={image}
/>
</View>
</View>
</View>
);
})}
</View>
);
},
},
{
title: 'Resize Mode',
description: ('The `resizeMode` style prop controls how the image is ' +
Expand Down