Skip to content

Commit 6aa3991

Browse files
authored
Add the order property for flex items (#2109) (#60423)
Summary: * Add the `order` property for flex items A flex container now lays out its items in order-modified document order (css-display-4 §3): by ascending `order`, and items with equal `order` in tree order. The order decides which line an item wraps to, where it sits on the main axis, and which item a container takes its baseline from (css-flexbox-1 §8.5). The tree itself does not change. `order` is an int32_t in `Style`, exposed as `YGNodeStyleSetOrder` and `YGNodeStyleGetOrder`, `YogaNode.order` in Java and `setOrder` and `getOrder` in JavaScript, and understood by gentest. `OrderedChildren` is the list the flex algorithm walks where the order of items matters: collecting flex lines, aligning multiple lines, and finding a container's baseline. It is a view of the node's child vector unless `display: contents` children have to be flattened into it or a child sets `order`: both copy the children, and the second sorts the copy. The `order` of an absolutely positioned or `display: none` child changes nothing, and neither does the `order` of a `display: contents` node, whose children are ordered among the container's other items. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Document how to set order and what it leaves alone The order page gains the setters in C, Java and JavaScript and a list of limitations: drawing, accessibility and the tree keep tree order, order applies only to nodes a flex container lays out, and it is an integer. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Avoid sorting already-ordered children, add capture/benchmark support, and expand order fixtures - Use std::ranges::is_sorted in OrderedChildren so single-child and already-ordered containers skip copying and sorting. - Serialize/deserialize order in capture/NodeToString.cpp and benchmark/Benchmark.cpp. - Add Chrome-generated fixtures for already-sorted items, empty display: contents, and column container baseline. --------- Changelog: [General][Added] - Add support for the `order` property on Yoga flex items Differential Revision: D135257691 Pull Request resolved: #60423
1 parent 97b3c62 commit 6aa3991

24 files changed

Lines changed: 270 additions & 26 deletions

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public object YogaNative {
146146
@JvmStatic
147147
public external fun jni_YGNodeStyleSetFlexShrinkJNI(nativePointer: Long, flexShrink: Float)
148148

149+
@JvmStatic public external fun jni_YGNodeStyleGetOrderJNI(nativePointer: Long): Int
150+
151+
@JvmStatic public external fun jni_YGNodeStyleSetOrderJNI(nativePointer: Long, order: Int)
152+
149153
@JvmStatic public external fun jni_YGNodeStyleGetFlexBasisJNI(nativePointer: Long): Long
150154

151155
@JvmStatic

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public abstract class YogaNode : YogaProps {
8686

8787
abstract override var flexShrink: Float
8888

89+
public abstract var order: Int
90+
8991
abstract override val flexBasis: YogaValue
9092

9193
abstract override fun setFlexBasis(flexBasis: Float)

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodeJNIBase.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ public abstract class YogaNodeJNIBase : YogaNode, Cloneable {
294294
YogaNative.jni_YGNodeStyleSetFlexShrinkJNI(nativePointer, value)
295295
}
296296

297+
override var order: Int
298+
get() = YogaNative.jni_YGNodeStyleGetOrderJNI(nativePointer)
299+
set(value) {
300+
YogaNative.jni_YGNodeStyleSetOrderJNI(nativePointer, value)
301+
}
302+
297303
override val flexBasis: YogaValue
298304
get() = valueFromLong(YogaNative.jni_YGNodeStyleGetFlexBasisJNI(nativePointer))
299305

‎packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ YG_NODE_JNI_STYLE_PROP(jint, YGDisplay, Display);
507507
YG_NODE_JNI_STYLE_PROP(jfloat, float, Flex);
508508
YG_NODE_JNI_STYLE_PROP(jfloat, float, FlexGrow);
509509
YG_NODE_JNI_STYLE_PROP(jfloat, float, FlexShrink);
510+
YG_NODE_JNI_STYLE_PROP(jint, int, Order);
510511

511512
YG_NODE_JNI_STYLE_UNIT_PROP_AUTO_INTRINSIC(FlexBasis);
512513
YG_NODE_JNI_STYLE_UNIT_PROP_AUTO_INTRINSIC(Width);
@@ -970,6 +971,8 @@ static JNINativeMethod methods[] = {
970971
{"jni_YGNodeStyleSetFlexShrinkJNI",
971972
"(JF)V",
972973
(void*)jni_YGNodeStyleSetFlexShrinkJNI},
974+
{"jni_YGNodeStyleGetOrderJNI", "(J)I", (void*)jni_YGNodeStyleGetOrderJNI},
975+
{"jni_YGNodeStyleSetOrderJNI", "(JI)V", (void*)jni_YGNodeStyleSetOrderJNI},
973976
{"jni_YGNodeStyleGetFlexBasisJNI",
974977
"(J)J",
975978
(void*)jni_YGNodeStyleGetFlexBasisJNI},

‎packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) {
203203
: node->style().flexShrink().unwrap();
204204
}
205205

206+
void YGNodeStyleSetOrder(const YGNodeRef node, const int order) {
207+
updateStyle<&Style::order, &Style::setOrder>(node, order);
208+
}
209+
210+
int YGNodeStyleGetOrder(const YGNodeConstRef node) {
211+
return resolveRef(node)->style().order();
212+
}
213+
206214
void YGNodeStyleSetFlexBasis(const YGNodeRef node, const float flexBasis) {
207215
updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
208216
node, StyleSizeLength::points(flexBasis));

‎packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ YG_EXPORT float YGNodeStyleGetFlexGrow(YGNodeConstRef node);
6969
YG_EXPORT void YGNodeStyleSetFlexShrink(YGNodeRef node, float flexShrink);
7070
YG_EXPORT float YGNodeStyleGetFlexShrink(YGNodeConstRef node);
7171

72+
YG_EXPORT void YGNodeStyleSetOrder(YGNodeRef node, int order);
73+
YG_EXPORT int YGNodeStyleGetOrder(YGNodeConstRef node);
74+
7275
YG_EXPORT void YGNodeStyleSetFlexBasis(YGNodeRef node, float flexBasis);
7376
YG_EXPORT void YGNodeStyleSetFlexBasisPercent(YGNodeRef node, float flexBasis);
7477
YG_EXPORT void YGNodeStyleSetFlexBasisAuto(YGNodeRef node);

‎packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <yoga/algorithm/Align.h>
1111
#include <yoga/algorithm/Baseline.h>
12+
#include <yoga/algorithm/OrderedChildren.h>
1213
#include <yoga/debug/AssertFatal.h>
1314
#include <yoga/event/event.h>
1415

@@ -32,7 +33,7 @@ float calculateBaseline(const yoga::Node* node) {
3233
}
3334

3435
yoga::Node* baselineChild = nullptr;
35-
for (auto child : node->getLayoutChildren()) {
36+
for (auto child : OrderedChildren{node}) {
3637
// Children that are not flex items take no part in baseline alignment, and
3738
// their line index is stale: only flex items are assigned to lines.
3839
if (child->style().positionType() == PositionType::Absolute ||

‎packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,9 +2229,10 @@ static void calculateLayoutImpl(
22292229
}
22302230
// STEP 4: COLLECT FLEX ITEMS INTO FLEX LINES
22312231

2232+
const OrderedChildren orderedChildren{node};
2233+
22322234
// Iterator representing the beginning of the current line
2233-
Node::LayoutableChildren::Iterator startOfLineIterator =
2234-
node->getLayoutChildren().begin();
2235+
OrderedChildren::Iterator startOfLineIterator = orderedChildren.begin();
22352236

22362237
// Number of lines.
22372238
size_t lineCount = 0;
@@ -2245,7 +2246,7 @@ static void calculateLayoutImpl(
22452246
// Max main dimension of all the lines.
22462247
float maxLineMainDim = 0;
22472248
const bool isNodeBaselineLayout = isBaselineLayout(node);
2248-
for (; startOfLineIterator != node->getLayoutChildren().end(); lineCount++) {
2249+
for (; startOfLineIterator != orderedChildren.end(); lineCount++) {
22492250
auto flexLine = calculateFlexLine(
22502251
node,
22512252
ownerDirection,
@@ -2254,6 +2255,7 @@ static void calculateLayoutImpl(
22542255
availableInnerWidth,
22552256
availableInnerMainDim,
22562257
startOfLineIterator,
2258+
orderedChildren.end(),
22572259
lineCount);
22582260

22592261
// If we don't need to measure the cross axis, we can skip the entire flex
@@ -2614,17 +2616,16 @@ static void calculateLayoutImpl(
26142616
case Align::Baseline:
26152617
break;
26162618
}
2617-
Node::LayoutableChildren::Iterator endIterator =
2618-
node->getLayoutChildren().begin();
2619+
OrderedChildren::Iterator endIterator = orderedChildren.begin();
26192620
for (size_t i = 0; i < lineCount; i++) {
2620-
const Node::LayoutableChildren::Iterator startIterator = endIterator;
2621+
const OrderedChildren::Iterator startIterator = endIterator;
26212622
auto iterator = startIterator;
26222623

26232624
// compute the line's height and find the endIndex
26242625
float lineHeight = 0;
26252626
float maxAscentForCurrentLine = 0;
26262627
float maxDescentForCurrentLine = 0;
2627-
for (; iterator != node->getLayoutChildren().end(); iterator++) {
2628+
for (; iterator != orderedChildren.end(); iterator++) {
26282629
const auto child = *iterator;
26292630
if (child->style().display() == Display::None) {
26302631
continue;

‎packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ FlexLine calculateFlexLine(
2121
const float mainAxisOwnerSize,
2222
const float availableInnerWidth,
2323
const float availableInnerMainDim,
24-
Node::LayoutableChildren::Iterator& iterator,
24+
OrderedChildren::Iterator& iterator,
25+
const OrderedChildren::Iterator childrenEnd,
2526
const size_t lineCount) {
2627
std::vector<yoga::Node*> itemsInFlow;
2728
itemsInFlow.reserve(node->getChildCount());
@@ -40,7 +41,6 @@ FlexLine calculateFlexLine(
4041
const float gap =
4142
node->style().computeGapForAxis(mainAxis, availableInnerMainDim);
4243

43-
const auto childrenEnd = node->getLayoutChildren().end();
4444
// Add items to the current line until it's full or we run out of items.
4545
for (; iterator != childrenEnd; iterator++) {
4646
auto child = *iterator;

‎packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <vector>
1111

1212
#include <yoga/Yoga.h>
13+
#include <yoga/algorithm/OrderedChildren.h>
1314
#include <yoga/node/Node.h>
1415

1516
namespace facebook::yoga {
@@ -56,8 +57,9 @@ struct FlexLine {
5657
FlexLineRunningLayout layout{};
5758
};
5859

59-
// Calculates where a line starting at a given index should break, returning
60-
// information about the collective children on the line.
60+
// Calculates where a line starting at a given child should break, returning
61+
// information about the collective children on the line and leaving the
62+
// iterator at the first child of the next line.
6163
//
6264
// This function assumes that all the children of node have their
6365
// computedFlexBasis properly computed (to do this, use the
@@ -69,7 +71,8 @@ FlexLine calculateFlexLine(
6971
float mainAxisOwnerSize,
7072
float availableInnerWidth,
7173
float availableInnerMainDim,
72-
Node::LayoutableChildren::Iterator& iterator,
74+
OrderedChildren::Iterator& iterator,
75+
OrderedChildren::Iterator childrenEnd,
7376
size_t lineCount);
7477

7578
} // namespace facebook::yoga

0 commit comments

Comments
 (0)