diff --git a/packages/react-native/Libraries/Components/View/__tests__/View-paddingBlockLonghands-itest.js b/packages/react-native/Libraries/Components/View/__tests__/View-paddingBlockLonghands-itest.js new file mode 100644 index 000000000000..ac5475cae8b6 --- /dev/null +++ b/packages/react-native/Libraries/Components/View/__tests__/View-paddingBlockLonghands-itest.js @@ -0,0 +1,84 @@ +/** + * 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-local + * @format + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import type {HostInstance, ViewStyle} from 'react-native'; + +import * as Fantom from '@react-native/fantom'; +import nullthrows from 'nullthrows'; +import * as React from 'react'; +import {createRef} from 'react'; +import {StyleSheet, View} from 'react-native'; + +// Lays out a container with `style` around a 10x10 child, and returns where +// the child sits in the container and how tall the container is. +function layOut(style: ViewStyle): { + childTop: number, + childLeft: number, + containerHeight: number, +} { + const root = Fantom.createRoot(); + const containerRef = createRef(); + const childRef = createRef(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + const container = nullthrows(containerRef.current).getBoundingClientRect(); + const child = nullthrows(childRef.current).getBoundingClientRect(); + root.destroy(); + + return { + childTop: child.y - container.y, + childLeft: child.x - container.x, + containerHeight: container.height, + }; +} + +const styles = StyleSheet.create({ + container: {alignSelf: 'flex-start'}, + child: {width: 10, height: 10}, +}); + +test('block-axis padding longhands take precedence over the physical edges', () => { + expect( + layOut({ + paddingTop: 0, + paddingBlockStart: 8, + paddingBottom: 0, + paddingBlockEnd: 4, + }), + ).toEqual({childTop: 8, childLeft: 0, containerHeight: 22}); +}); + +test('inline-axis padding longhands take precedence over the physical edges', () => { + expect(layOut({paddingLeft: 0, paddingInlineStart: 8}).childLeft).toBe(8); +}); + +test('the physical edges apply when no longhand is set', () => { + expect(layOut({paddingTop: 6, paddingBottom: 3})).toEqual({ + childTop: 6, + childLeft: 0, + containerHeight: 19, + }); +}); + +test('the physical edges take precedence over the paddingBlock shorthand', () => { + expect(layOut({paddingTop: 2, paddingBlock: 8}).childTop).toBe(2); +}); diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 825334daf3d5..5b04896532f9 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -501,6 +501,15 @@ void YogaLayoutableShadowNode::updateYogaProps() { if (props.paddingBlock.isDefined()) { result.setPadding(yoga::Edge::Vertical, props.paddingBlock); } + // Yoga has no block-start or block-end edge. Setting the physical edges + // makes these win over `paddingTop` and `paddingBottom`, as Yoga's `Start` + // and `End` win over `Left` and `Right`. + if (props.paddingBlockStart.isDefined()) { + result.setPadding(yoga::Edge::Top, props.paddingBlockStart); + } + if (props.paddingBlockEnd.isDefined()) { + result.setPadding(yoga::Edge::Bottom, props.paddingBlockEnd); + } if (props.borderBlockWidth.isDefined()) { result.setBorder(yoga::Edge::Vertical, props.borderBlockWidth); } @@ -527,12 +536,6 @@ void YogaLayoutableShadowNode::updateYogaProps() { if (result.margin(yoga::Edge::Bottom).isUndefined()) { result.setMargin(yoga::Edge::Bottom, props.marginBlockEnd); } - if (result.padding(yoga::Edge::Top).isUndefined()) { - result.setPadding(yoga::Edge::Top, props.paddingBlockStart); - } - if (result.padding(yoga::Edge::Bottom).isUndefined()) { - result.setPadding(yoga::Edge::Bottom, props.paddingBlockEnd); - } if (result.border(yoga::Edge::Top).isUndefined()) { result.setBorder(yoga::Edge::Top, props.borderBlockStartWidth); }