From ac9efe570eb6135b10bf54d71fc298480b082651 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Wed, 2 Dec 2026 06:17:01 -0800 Subject: [PATCH 1/2] Count the sizes nested boxes state in automatic minimum sizes (#2101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: * Count the sizes nested boxes state in automatic minimum sizes Why === With automatic minimum sizes on (`YGErrataMinSizeUndefinedInsteadOfAuto` off), a flex item does not shrink below its min-content size (css-flexbox-1 §4.5). That size is built from its children's min-content contributions (css-sizing-3 §5.2), and a child that states a size contributes that size. Yoga's min-content walk reads only content (measure functions, padding, border, margins) and ignores `width`, `height`, `min-*`, `max-*` and `aspect-ratio` on everything below the item: ```html
``` How === The walk takes each child's min-content contribution along the axis it is asked for: - the size the child states, with its padding and border for a content-box size; - otherwise its content's min-content size, as before, or, where larger, the size its aspect ratio transfers from the size it states on the other axis (css-sizing-4); - clamped to the minimum and maximum it states, and no less than its padding and border (`boundAxis`). A percentage size, minimum or maximum depends on the size being found, so it behaves as `auto` or as no limit (css-sizing-3 §5.2.1). A child that states its size on an axis is not measured, walked or cloned for that axis. The item's own specified size suggestion is unchanged. The walk still differs from the specifications or from Chrome in these ways: - A child's `flex-basis` and flex factors are not read. css-flexbox-1 §9.9.3 caps a flex item's contribution at its flex base size if it cannot grow and floors it there if it cannot shrink, and Chrome counts a definite flex basis for a child that cannot shrink. An item around a `flex: 0 0 70px` child is 70px in Chrome; with this change and without it, the item is the size of the child's content. - §9.9.3 also takes the larger of a flex item's min-content size and its stated size. The walk takes the stated size, as css-sizing-3 does. For a child that cannot grow, the cap gives the same result (`child_width_smaller_than_content`: 20px in Chrome). Chrome was not checked for a child that can grow. - A `display: contents` child is still walked, and counted, as a box of its own. - A percentage padding or margin resolves against the owner's width, as it does elsewhere in the walk. Chrome resolves it against zero for an intrinsic contribution (css-sizing-3 §5.2.1): a content-box child with `width: 30px; padding-left: 10%` contributes 30px in Chrome and 50px under a 200px owner in Yoga. Following Chrome would remove most of the walk's owner-width memo key, so it belongs in a change of its own. - The aspect-ratio transfer uses the border-box size, also for a content-box child. `container_min_content_memo_respects_owner_width` relied on a 20px spacer not counting toward its parent's min-content size. The parent now has `max-width: 70px`, which keeps the two probes the test compares apart. Test Plan ========= `yarn gentest -f YGAutoMinSizeTest` generates the new fixtures in `YGAutoMinSizeTest.html` from Chrome. `cmake --build tests/build && tests/build/yogatests` passes with them and with the hand-written tests for whether the probe runs and for the memo. With the change to `CalculateLayout.cpp` reverted, every new test fails. The width or height of the shrinking item, in Chrome and without the change: | Fixture | Chrome | Without the change | | --- | --- | --- | | `counts_child_width` | 100 | 20 | | `counts_child_height` | 100 | 20 | | `counts_widest_child_of_column` | 60 | 20 | | `item_width_larger_than_content` | 30 | 10 | | `child_width_larger_than_content` | 100 | 20 | | `child_width_smaller_than_content` | 20 | 10 | | `child_min_width` | 50 | 20 | | `child_max_width` | 40 | 20 | | `child_percent_width` | 30 | 20 | | `child_content_box_width` | 66 | 20 | | `child_padding_exceeds_width` | 40 | 20 | | `child_aspect_ratio` | 60 | 20 | | `child_aspect_ratio_column` | 60 | 20 | | `counts_content_of_overflow_hidden_child` | 130 | 60 | `child_with_stated_size_skips_min_content_probe` sees 2 probes where it expects none, and `stated_size_with_percentage_padding_follows_owner_width` sees 10 and 0 where it expects 50 and 40. `yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. The generated Java tests were not run, because the Gradle project has no unit-test source set, and neither were the generated JavaScript tests, which need Emscripten. Changelog: [General][Fixed] - Fix Yoga's automatic minimum size ignoring the width, height, min/max sizes and aspect ratio of boxes inside a flex item * Mark percentage padding in a child's min-content contribution An empty child, or one with a static min-content size, reports a contribution that does not depend on the owner's width, but boundAxis then floors it at the child's padding and border. With a percentage padding the result depends on the owner's width, so the memo of the container around it has to be keyed on that width. Co-Authored-By: Claude Opus 5.5 --------- Wearable integration ==================== The wearable UI adapter retains zero automatic minima for unmeasured, untyped app boxes and size-contained boxes, without disabling Yoga's native WSS and measured-text minima. Explicit minima remain authoritative; containment and type-rule changes remap both minimum axes so compatibility defaults do not persist after they stop applying. Differential Revision: D135304422 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 80 +++++++++++++++++-- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index db21f29fbff..03032cf0535 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -982,11 +982,11 @@ static std::optional readMinContentMainSize( return std::nullopt; } -static MinContentSize computeChildMinContentMainSize( +static MinContentSize computeChildMinContentContribution( yoga::Node* node, size_t index, - FlexDirection requestedAxis, - Direction ownerDirection, + FlexDirection axis, + Direction direction, float ownerWidth, float ownerHeight, uint32_t generationCount); @@ -1042,7 +1042,7 @@ static MinContentSize measureOrWalkMinContentMainSize( continue; } - const MinContentSize childMain = computeChildMinContentMainSize( + const MinContentSize childMain = computeChildMinContentContribution( node, i, nodeMainAxis, @@ -1050,7 +1050,7 @@ static MinContentSize measureOrWalkMinContentMainSize( ownerWidth, ownerHeight, generationCount); - const MinContentSize childCross = computeChildMinContentMainSize( + const MinContentSize childCross = computeChildMinContentContribution( node, i, nodeCrossAxis, @@ -1118,6 +1118,72 @@ static MinContentSize computeChildMinContentMainSize( generationCount); } +// The size `style` states on `dimension`, as a border-box size. A percentage +// resolves against the size the min-content walk is finding, so it behaves as +// auto there (css-sizing-3 §5.2.1). +static FloatOptional statedSize( + const Style& style, + const Direction direction, + const Dimension dimension, + const float ownerWidth) { + const FloatOptional size = style.dimension(dimension).resolve(YGUndefined); + if (size.isUndefined() || style.boxSizing() == BoxSizing::BorderBox) { + return size; + } + return size + + FloatOptional{style.computePaddingAndBorderForDimension( + direction, dimension, ownerWidth)}; +} + +// The min-content contribution along `axis` of the child of `node` at `index`, +// without its margins (css-sizing-3 §5.2): the size the child states on that +// axis, else its content's min-content size or, where that is larger, the size +// its aspect ratio transfers from the size it states on the other axis +// (css-sizing-4), within the minimum and maximum it states. A child that states +// its size on `axis` is not measured, walked or cloned for it. +static MinContentSize computeChildMinContentContribution( + yoga::Node* const node, + const size_t index, + const FlexDirection axis, + const Direction direction, + const float ownerWidth, + const float ownerHeight, + const uint32_t generationCount) { + // A clone made below has the same style. + const Style& style = node->getChild(index)->style(); + MinContentSize contribution{0.0f, false}; + const FloatOptional stated = + statedSize(style, direction, dimension(axis), ownerWidth); + if (stated.isDefined()) { + contribution = {stated.unwrap(), style.hasPercentPadding()}; + } else { + contribution = computeChildMinContentMainSize( + node, index, axis, direction, ownerWidth, ownerHeight, generationCount); + const FloatOptional aspectRatio = style.aspectRatio(); + const FloatOptional statedAcross = statedSize( + style, + direction, + isRow(axis) ? Dimension::Height : Dimension::Width, + ownerWidth); + if (aspectRatio.isDefined() && statedAcross.isDefined()) { + contribution.size = std::max( + contribution.size, + isRow(axis) ? statedAcross.unwrap() * aspectRatio.unwrap() + : statedAcross.unwrap() / aspectRatio.unwrap()); + } + } + + return { + boundAxis( + node->getChild(index), + axis, + direction, + contribution.size, + YGUndefined, + ownerWidth), + contribution.resolvesAgainstOwnerWidth || style.hasPercentPadding()}; +} + // Returns the min-content size of `node` along `requestedAxis`, used by CSS // Flexbox §4.5 automatic minimum sizing. // @@ -1135,8 +1201,8 @@ static MinContentSize computeChildMinContentMainSize( // respond with 0 along their scroll axis. // * Empty leaf: return 0. // * Container: iterate in-flow children. For each, take its -// min-content along the container's own main axis (sum into -// `mainTotal`) and along its cross axis (max into `crossMax`), +// min-content contribution along the container's own main axis (sum +// into `mainTotal`) and along its cross axis (max into `crossMax`), // plus the child's margins. Add the container's own padding and // border on both ends of each axis. Project onto `requestedAxis`. // From 0ec933aef2585f674df9e1978d39b80f4b4e6c79 Mon Sep 17 00:00:00 2001 From: GitHub Workflows Support Date: Wed, 2 Dec 2026 06:17:12 -0800 Subject: [PATCH 2/2] Walk only the requested axis for a container's min-content size (#2102) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: * Walk only the requested axis for a container's min-content size Why === The min-content walk behind automatic minimum sizes (css-flexbox-1 §4.5) is asked for a container's size on one axis. For every child it computed the size on both of the container's axes, summed one, took the largest of the other, and returned only the one it was asked for. The work on the other axis was wasted: a leaf was measured, and a container walked, on an axis no flex line needed. How === The walk asks each child for the requested axis alone. It sums the children where that is the container's main axis and takes the largest where it is the cross axis. Layout results are unchanged. The memo already kept a leaf to one probe per axis per layout, so the saving is one measure call per leaf, and one walk per container, on the axis nobody asked for. Test Plan ========= `cmake --build tests/build && tests/build/yogatests` passes. `nested_leaf_probed_at_most_once_per_axis_per_layout`, a measured leaf ten containers deep in a shrinking row, accepted up to two probes. It now expects one, and sees 2 without this change. `yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. Changelog: [General][Changed] - Yoga calls a measure function for an automatic minimum size only on the axis it needs * Simplify alongMainAxis check and assert probed axis in test --------- Reviewed By: javache Differential Revision: D135328393 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 58 ++++++------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 03032cf0535..8d06148f34b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1026,13 +1026,11 @@ static MinContentSize measureOrWalkMinContentMainSize( } const Direction direction = node->resolveDirection(ownerDirection); - const FlexDirection nodeMainAxis = - resolveDirection(node->style().flexDirection(), direction); - const FlexDirection nodeCrossAxis = - resolveCrossDirection(nodeMainAxis, direction); + // Children add up along the container's main axis; across it the largest + // counts. + const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow; - float mainTotal = 0.0f; - float crossMax = 0.0f; + float minContent = 0.0f; bool resolvesAgainstOwnerWidth = node->style().hasPercentPadding(); for (size_t i = 0; i < node->getChildCount(); i++) { @@ -1042,47 +1040,27 @@ static MinContentSize measureOrWalkMinContentMainSize( continue; } - const MinContentSize childMain = computeChildMinContentContribution( + const MinContentSize child = computeChildMinContentContribution( node, i, - nodeMainAxis, + requestedAxis, direction, ownerWidth, ownerHeight, generationCount); - const MinContentSize childCross = computeChildMinContentContribution( - node, - i, - nodeCrossAxis, - direction, - ownerWidth, - ownerHeight, - generationCount); - - mainTotal += childMain.size + - childStyle.computeMarginForAxis(nodeMainAxis, ownerWidth); - crossMax = std::max( - crossMax, - childCross.size + - childStyle.computeMarginForAxis(nodeCrossAxis, ownerWidth)); + const float childSize = + child.size + childStyle.computeMarginForAxis(requestedAxis, ownerWidth); + minContent = alongMainAxis ? minContent + childSize + : std::max(minContent, childSize); resolvesAgainstOwnerWidth = resolvesAgainstOwnerWidth || - childMain.resolvesAgainstOwnerWidth || - childCross.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin(); + child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin(); } - mainTotal += node->style().computeFlexStartPaddingAndBorder( - nodeMainAxis, direction, ownerWidth) + - node->style().computeFlexEndPaddingAndBorder( - nodeMainAxis, direction, ownerWidth); - crossMax += node->style().computeFlexStartPaddingAndBorder( - nodeCrossAxis, direction, ownerWidth) + + minContent += node->style().computeFlexStartPaddingAndBorder( + requestedAxis, direction, ownerWidth) + node->style().computeFlexEndPaddingAndBorder( - nodeCrossAxis, direction, ownerWidth); + requestedAxis, direction, ownerWidth); - const bool nodeMainIsRow = isRow(nodeMainAxis); - const float widthMin = nodeMainIsRow ? mainTotal : crossMax; - const float heightMin = nodeMainIsRow ? crossMax : mainTotal; - const float minContent = wantRow ? widthMin : heightMin; layout.minContent[axisIndex] = minContent; layout.minContentOwnerWidth[axisIndex] = ownerWidth; layout.setMinContentResolvesAgainstOwnerWidth( @@ -1201,10 +1179,10 @@ static MinContentSize computeChildMinContentContribution( // respond with 0 along their scroll axis. // * Empty leaf: return 0. // * Container: iterate in-flow children. For each, take its -// min-content contribution along the container's own main axis (sum -// into `mainTotal`) and along its cross axis (max into `crossMax`), -// plus the child's margins. Add the container's own padding and -// border on both ends of each axis. Project onto `requestedAxis`. +// min-content contribution along `requestedAxis` plus its margins: +// the sum of them where that is the container's main axis, the +// largest where it is its cross axis. Add the container's own +// padding and border on both ends of the axis. // // Container-level recursion does no layout writes (no positions, no // alignment, no flex distribution); only the descendant leaf measure