Fix issue where marginStart and marginEnd were not working with rowReverse flex direction (#1420)
Summary: X-link: https://github.com/facebook/litho/pull/962 X-link: https://github.com/facebook/react-native/pull/40804 Pull Request resolved: https://github.com/facebook/yoga/pull/1420 This stack is ultimately aiming to solve https://github.com/facebook/yoga/issues/1208 **The problem** Turns out that we do not even check direction when determining which edge is the leading (start) and trailing (end) edges. This is not how web does it as the start/end is based on the writing direction NOT the flex direction: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox#start_and_end_lines. While web does not have marginStart and marginEnd, they do have margin-inline-start/end which relies on the writing mode to determine the "start"/"end": https://developer.mozilla.org/en-US/docs/Web/CSS/margin-inline-start. This means that if you do something like ``` export default function Playground(props: Props): React.Node { return ( <View style={styles.container}> <View style={styles.item} /> </View> ); } const styles = StyleSheet.create({ container: { marginEnd: 100, flexDirection: 'row-reverse', backgroundColor: 'red', display: 'flex', width: 100, height: 100, }, item: { backgroundColor: 'blue', width: 10, }, }); ``` You get {F1116264350} As you can see the margin gets applied to the left edge even thought the direction is ltr and it should be applied to the right edge. **The solution** I ended up fixing this by creating a new `leadingLayoutEdge` and `trailingLayoutEdge` function that take the flex direction as well as the direction. Based on the errata, the a few functions will use these new functions to determine which `YGEdge` is the starting/ending. You might be wondering why I did not put this logic inside of `leadingEdge(flexDirection)` / `trailingEdge(flexDirection)` since other areas could potentially have the same bug like `getLeadingPadding`. These functions are a bit overloaded and there are cases where we actually want to use the flexDirection to get the edge in question. For example, many of the calls to `setLayoutPosition` in `CalculateLayout.cpp` call `leadingEdge()` / `trailingEdge()` to set the proper position for cases like row-reverse where items need to line up in a different direction. Reviewed By: NickGerleman Differential Revision: D50140503 fbshipit-source-id: 5b580c7570f6ae1e2d031971926ac4e8f52dd362
This commit is contained in:
committed by
Facebook GitHub Bot
parent
50dc5ae2d1
commit
f4337fbb07
@@ -50,6 +50,13 @@ class YG_EXPORT Node : public ::YGNode {
|
||||
|
||||
float relativePosition(FlexDirection axis, const float axisSize) const;
|
||||
|
||||
YGEdge getLeadingLayoutEdgeUsingErrata(
|
||||
FlexDirection flexDirection,
|
||||
Direction direction) const;
|
||||
YGEdge getTrailingLayoutEdgeUsingErrata(
|
||||
FlexDirection flexDirection,
|
||||
Direction direction) const;
|
||||
|
||||
void useWebDefaults() {
|
||||
style_.flexDirection() = FlexDirection::Row;
|
||||
style_.alignContent() = Align::Stretch;
|
||||
@@ -193,8 +200,14 @@ class YG_EXPORT Node : public ::YGNode {
|
||||
bool isTrailingPosDefined(FlexDirection axis) const;
|
||||
float getLeadingPosition(FlexDirection axis, float axisSize) const;
|
||||
float getTrailingPosition(FlexDirection axis, float axisSize) const;
|
||||
float getLeadingMargin(FlexDirection axis, float widthSize) const;
|
||||
float getTrailingMargin(FlexDirection axis, float widthSize) const;
|
||||
float getLeadingMargin(
|
||||
FlexDirection axis,
|
||||
Direction direction,
|
||||
float widthSize) const;
|
||||
float getTrailingMargin(
|
||||
FlexDirection axis,
|
||||
Direction direction,
|
||||
float widthSize) const;
|
||||
float getLeadingBorder(FlexDirection flexDirection) const;
|
||||
float getTrailingBorder(FlexDirection flexDirection) const;
|
||||
float getLeadingPadding(FlexDirection axis, float widthSize) const;
|
||||
|
Reference in New Issue
Block a user