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: mapped layout props for view component #34590

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions Libraries/Components/View/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ViewNativeComponent from './ViewNativeComponent';
import TextAncestor from '../../Text/TextAncestor';
import flattenStyle from '../../StyleSheet/flattenStyle';
import * as React from 'react';
import processLayoutProps from '../../StyleSheet/processStyles';

export type Props = ViewProps;

Expand Down Expand Up @@ -139,8 +140,13 @@ const View: React.AbstractComponent<
};
const restWithDefaultProps = {...otherProps, accessibilityValue};

const flattenedStyle = flattenStyle(style);
const newPointerEvents = flattenedStyle?.pointerEvents || pointerEvents;
let flattendStyle = flattenStyle(style);

if (flattendStyle) {
flattendStyle = processLayoutProps(flattendStyle);
}

const newPointerEvents = flattendStyle?.pointerEvents || pointerEvents;

return (
<TextAncestor.Provider value={false}>
Expand All @@ -161,8 +167,8 @@ const View: React.AbstractComponent<
? 'no-hide-descendants'
: importantForAccessibility
}
style={flattendStyle}
{...restWithDefaultProps}
mayank-96 marked this conversation as resolved.
Show resolved Hide resolved
style={style}
pointerEvents={newPointerEvents}
ref={forwardedRef}
/>
Expand Down
4 changes: 4 additions & 0 deletions Libraries/Image/Image.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {convertObjectFitToResizeMode} from './ImageUtils';
import type {ImageProps as ImagePropsType} from './ImageProps';
import type {RootTag} from '../Types/RootTagTypes';
import {getImageSourcesFromImageProps} from './ImageSourceUtils';
import processLayoutProps from '../StyleSheet/processStyles';
necolas marked this conversation as resolved.
Show resolved Hide resolved

let _requestId = 1;
function generateRequestId() {
Expand Down Expand Up @@ -166,6 +167,9 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
}

const {height, width, ...restProps} = props;

style = flattenStyle(style);
mayank-96 marked this conversation as resolved.
Show resolved Hide resolved

const {onLoadStart, onLoad, onLoadEnd, onError} = props;
const nativeProps = {
...restProps,
Expand Down
5 changes: 4 additions & 1 deletion Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {convertObjectFitToResizeMode} from './ImageUtils';
import ImageViewNativeComponent from './ImageViewNativeComponent';
import type {RootTag} from 'react-native/Libraries/Types/RootTagTypes';
import {getImageSourcesFromImageProps} from './ImageSourceUtils';
import processLayoutProps from '../StyleSheet/processStyles';
necolas marked this conversation as resolved.
Show resolved Hide resolved

function getSize(
uri: string,
Expand Down Expand Up @@ -140,6 +141,8 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
// $FlowFixMe[prop-missing]
const tintColor = props.tintColor || style.tintColor;

let flattendStyle = flattenStyle(style);
necolas marked this conversation as resolved.
Show resolved Hide resolved

if (props.children != null) {
throw new Error(
'The <Image> component cannot contain children. If you want to render content on top of the image, consider using the <ImageBackground> component or absolute positioning.',
Expand Down Expand Up @@ -173,7 +176,7 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
accessibilityState={_accessibilityState}
{...restProps}
ref={forwardedRef}
style={style}
style={flattendStyle}
resizeMode={resizeMode}
tintColor={tintColor}
source={sources}
Expand Down
68 changes: 68 additions & 0 deletions Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ type ____LayoutStyle_Internal = $ReadOnly<{
*/
margin?: DimensionValue,

/** Setting `marginBlock` has the same effect as setting both
* `marginTop` and `marginBottom`.
*/
marginBlock?: DimensionValue,

/** `marginBlockEnd` works like `margin-bottom` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-bottom
* for more details.
*/
marginBlockEnd?: DimensionValue,

/** `marginBlockStart` works like `margin-top` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-top
* for more details.
*/
marginBlockStart?: DimensionValue,

/** `marginBottom` works like `margin-bottom` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-bottom
* for more details.
Expand All @@ -200,6 +217,23 @@ type ____LayoutStyle_Internal = $ReadOnly<{
*/
marginHorizontal?: DimensionValue,

/** Setting `marginInline` has the same effect as setting
* both `marginLeft` and `marginRight`.
*/
marginInline?: DimensionValue,

/**
* When direction is `ltr`, `marginInlineEnd` is equivalent to `marginRight`.
* When direction is `rtl`, `marginInlineEnd` is equivalent to `marginLeft`.
*/
marginInlineEnd?: DimensionValue,

/**
* When direction is `ltr`, `marginInlineStart` is equivalent to `marginLeft`.
* When direction is `rtl`, `marginInlineStart` is equivalent to `marginRight`.
*/
marginInlineStart?: DimensionValue,

/** `marginLeft` works like `margin-left` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left
* for more details.
Expand Down Expand Up @@ -236,6 +270,23 @@ type ____LayoutStyle_Internal = $ReadOnly<{
*/
padding?: DimensionValue,

/** Setting `paddingBlock` is like setting both of
* `paddingTop` and `paddingBottom`.
*/
paddingBlock?: DimensionValue,

/** `paddingBlockEnd` works like `padding-bottom` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-bottom
* for more details.
*/
paddingBlockEnd?: DimensionValue,

/** `paddingBlockStart` works like `padding-top` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-top
* for more details.
*/
paddingBlockStart?: DimensionValue,

/** `paddingBottom` works like `padding-bottom` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-bottom
* for more details.
Expand All @@ -253,6 +304,23 @@ type ____LayoutStyle_Internal = $ReadOnly<{
*/
paddingHorizontal?: DimensionValue,

/** Setting `paddingInline` is like setting both of
* `paddingLeft` and `paddingRight`.
*/
paddingInline?: DimensionValue,

/**
* When direction is `ltr`, `paddingInlineEnd` is equivalent to `paddingRight`.
* When direction is `rtl`, `paddingInlineEnd` is equivalent to `paddingLeft`.
*/
paddingInlineEnd?: DimensionValue,

/**
* When direction is `ltr`, `paddingInlineStart` is equivalent to `paddingLeft`.
* When direction is `rtl`, `paddingInlineStart` is equivalent to `paddingRight`.
*/
paddingInlineStart?: DimensionValue,

/** `paddingLeft` works like `padding-left` in CSS.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-left
* for more details.
Expand Down
43 changes: 43 additions & 0 deletions Libraries/StyleSheet/processStyles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.
*
* @format
* @flow strict-local
*/
'use strict';

import type {____FlattenStyleProp_Internal} from './StyleSheetTypes';
import type {ViewStyleProp} from './StyleSheet';

export default function processLayoutProps(
necolas marked this conversation as resolved.
Show resolved Hide resolved
flattendStyle: ____FlattenStyleProp_Internal<ViewStyleProp>,
necolas marked this conversation as resolved.
Show resolved Hide resolved
): ____FlattenStyleProp_Internal<ViewStyleProp> {
const flattend_Style = {...flattendStyle};
necolas marked this conversation as resolved.
Show resolved Hide resolved
const layoutPropMap = {
marginInlineStart: 'marginStart',
marginInlineEnd: 'marginEnd',
marginBlockStart: 'marginTop',
marginBlockEnd: 'marginBottom',
marginBlock: 'marginVertical',
marginInline: 'marginHorizontal',
paddingInlineStart: 'paddingStart',
paddingInlineEnd: 'paddingEnd',
paddingBlockStart: 'paddingTop',
paddingBlockEnd: 'paddingBottom',
paddingBlock: 'paddingVertical',
paddingInline: 'paddingHorizontal',
};
if (flattend_Style) {
Object.keys(layoutPropMap).forEach(key => {
if (flattend_Style && flattend_Style[key] !== undefined) {
flattend_Style[layoutPropMap[key]] = flattend_Style[key];
delete flattend_Style[key];
}
});
}

return flattend_Style;
}
22 changes: 22 additions & 0 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ const Text: React.AbstractComponent<
_selectable = userSelectToSelectableMap[style.userSelect];
}

Object.keys(layoutPropMap).forEach(key => {
necolas marked this conversation as resolved.
Show resolved Hide resolved
if (style && style[key] !== undefined) {
style[layoutPropMap[key]] = style[key];
delete style[key];
}
});

if (__DEV__) {
if (PressabilityDebug.isEnabled() && onPress != null) {
style = StyleSheet.compose(restProps.style, {
Expand Down Expand Up @@ -231,6 +238,21 @@ const Text: React.AbstractComponent<
);
});

const layoutPropMap = {
marginInlineStart: 'marginStart',
marginInlineEnd: 'marginEnd',
marginBlockStart: 'marginTop',
marginBlockEnd: 'marginBottom',
marginBlock: 'marginVertical',
marginInline: 'marginHorizontal',
paddingInlineStart: 'paddingStart',
paddingInlineEnd: 'paddingEnd',
paddingBlockStart: 'paddingTop',
paddingBlockEnd: 'paddingBottom',
paddingBlock: 'paddingVertical',
paddingInline: 'paddingHorizontal',
};

Text.displayName = 'Text';

/**
Expand Down