Skip to content

Commit

Permalink
Incorporate gap space into main axis overflow flag (#1173)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/yoga#1173

In #35351 we see incorrect child item height when the flex-wrap is enabled, the cross-axis is to be stretched, and main-axis overflow is caused by gap.

In YGDistributeFreeSpaceSecondPass, if we do not have overflow (determined by flexBasisOverflows), we have stretch cross-alignment, and we reason that nothing can add to main axis dimensions, we know we're a single line and want to take full cross dimensions. and can set YGMeasureModeExactly which uses parent dimensions. Guessing an optimization?

If we do have overflow, then we set YGMeasureModeAtMost to find minimum possible cross-axis dimensions instead.

`flexBasisOverflows` incorporates both computed flex basis, and margin, so it is more generally a flag for whether we will wrap. So we should incorporate gap spacing into it. E.g. it is also used for whether we should the match main axis parent dimension of the overall container. This change does just that, and renames the flag to `mainAxisOverflows`.

We will want to cherry-pick the fix for this into RN 0.71 since we have not yet introduced the community to the incorrect behavior, and we expect a lot of usage of flex-gap.

Changelog:
[General][Fixed] - Fix incorrect height when gap causes main axis to overflow and cross-axis is stretched

Reviewed By: yungsters

Differential Revision: D41311424

fbshipit-source-id: bd0c3b5aac478a56878703b6da84fc3993cc14da
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 16, 2022
1 parent 11c8bf3 commit 1aa157b
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ static float YGDistributeFreeSpaceSecondPass(
const float availableInnerCrossDim,
const float availableInnerWidth,
const float availableInnerHeight,
const bool flexBasisOverflows,
const bool mainAxisOverflows,
const YGMeasureMode measureModeCrossDim,
const bool performLayout,
const YGConfigRef config,
Expand Down Expand Up @@ -2175,7 +2175,7 @@ static float YGDistributeFreeSpaceSecondPass(
!YGNodeIsStyleDimDefined(
currentRelativeChild, crossAxis, availableInnerCrossDim) &&
measureModeCrossDim == YGMeasureModeExactly &&
!(isNodeFlexWrap && flexBasisOverflows) &&
!(isNodeFlexWrap && mainAxisOverflows) &&
YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch &&
currentRelativeChild->marginLeadingValue(crossAxis).unit !=
YGUnitAuto &&
Expand Down Expand Up @@ -2383,7 +2383,7 @@ static void YGResolveFlexibleLength(
const float availableInnerCrossDim,
const float availableInnerWidth,
const float availableInnerHeight,
const bool flexBasisOverflows,
const bool mainAxisOverflows,
const YGMeasureMode measureModeCrossDim,
const bool performLayout,
const YGConfigRef config,
Expand Down Expand Up @@ -2411,7 +2411,7 @@ static void YGResolveFlexibleLength(
availableInnerCrossDim,
availableInnerWidth,
availableInnerHeight,
flexBasisOverflows,
mainAxisOverflows,
measureModeCrossDim,
performLayout,
config,
Expand Down Expand Up @@ -2884,7 +2884,9 @@ static void YGNodelayoutImpl(

// STEP 3: DETERMINE FLEX BASIS FOR EACH ITEM

float totalOuterFlexBasis = YGNodeComputeFlexBasisForChildren(
// Computed basis + margins + gap
float totalMainDim = 0;
totalMainDim += YGNodeComputeFlexBasisForChildren(
node,
availableInnerWidth,
availableInnerHeight,
Expand All @@ -2899,10 +2901,17 @@ static void YGNodelayoutImpl(
depth,
generationCount);

const bool flexBasisOverflows = measureModeMainDim == YGMeasureModeUndefined
? false
: totalOuterFlexBasis > availableInnerMainDim;
if (isNodeFlexWrap && flexBasisOverflows &&
if (childCount > 1) {
totalMainDim +=
node->getGapForAxis(mainAxis, availableInnerCrossDim).unwrap() *
(childCount - 1);
}

const bool mainAxisOverflows =
(measureModeMainDim != YGMeasureModeUndefined) &&
totalMainDim > availableInnerMainDim;

if (isNodeFlexWrap && mainAxisOverflows &&
measureModeMainDim == YGMeasureModeAtMost) {
measureModeMainDim = YGMeasureModeExactly;
}
Expand Down Expand Up @@ -3025,7 +3034,7 @@ static void YGNodelayoutImpl(
availableInnerCrossDim,
availableInnerWidth,
availableInnerHeight,
flexBasisOverflows,
mainAxisOverflows,
measureModeCrossDim,
performLayout,
config,
Expand Down

0 comments on commit 1aa157b

Please sign in to comment.