From 33b2d6d367f9236fa32a349203e0a6a003757437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Fri, 19 Dec 2025 01:48:34 -0800 Subject: [PATCH] Add support for flex props to AnimationBackend (#54898) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54898 ## Summary: Adds support for flex props to be passed as `AnimatedProp` to the animation backend. ## Changelog: [General][Added] - Added support for flex props to the AnimationBackend. Reviewed By: zeyap Differential Revision: D89283451 fbshipit-source-id: b091c89e562a950b281e78d8fd99e75899e2943d --- .../AnimatedPropSerializer.cpp | 14 ++++ .../renderer/animationbackend/AnimatedProps.h | 72 ++++++++++++++++++- .../animationbackend/AnimatedPropsBuilder.h | 56 +++++++++++++++ .../animationbackend/AnimatedPropsRegistry.h | 56 +++++++++++++++ .../animationbackend/AnimationBackend.cpp | 17 +++-- 5 files changed, 209 insertions(+), 6 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropSerializer.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropSerializer.cpp index abe8ed9fd23..10926fee90b 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropSerializer.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropSerializer.cpp @@ -265,6 +265,20 @@ void packAnimatedProp( case MARGIN: case POSITION: case BORDER_WIDTH: + case ALIGN_CONTENT: + case ALIGN_ITEMS: + case ALIGN_SELF: + case ASPECT_RATIO: + case BOX_SIZING: + case DISPLAY: + case FLEX_BASIS: + case FLEX_DIRECTION: + case ROW_GAP: + case COLUMN_GAP: + case FLEX_GROW: + case FLEX_SHRINK: + case FLEX_WRAP: + case JUSTIFY_CONTENT: throw std::runtime_error("Tried to synchronously update layout props"); } } diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedProps.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedProps.h index dfcc45a3560..bc2f35d88a3 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedProps.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedProps.h @@ -33,7 +33,21 @@ enum PropName { OUTLINE_COLOR, OUTLINE_OFFSET, OUTLINE_STYLE, - OUTLINE_WIDTH + OUTLINE_WIDTH, + ALIGN_CONTENT, + ALIGN_ITEMS, + ALIGN_SELF, + ASPECT_RATIO, + BOX_SIZING, + DISPLAY, + FLEX_BASIS, + FLEX_DIRECTION, + ROW_GAP, + COLUMN_GAP, + FLEX_GROW, + FLEX_SHRINK, + FLEX_WRAP, + JUSTIFY_CONTENT }; struct AnimatedPropBase { @@ -265,6 +279,62 @@ inline void cloneProp(BaseViewProps &viewProps, const AnimatedPropBase &animated viewProps.outlineWidth = get(animatedProp); break; + case ALIGN_CONTENT: + viewProps.yogaStyle.setAlignContent(get(animatedProp)); + break; + + case ALIGN_ITEMS: + viewProps.yogaStyle.setAlignItems(get(animatedProp)); + break; + + case ALIGN_SELF: + viewProps.yogaStyle.setAlignSelf(get(animatedProp)); + break; + + case ASPECT_RATIO: + viewProps.yogaStyle.setAspectRatio(get(animatedProp)); + break; + + case BOX_SIZING: + viewProps.yogaStyle.setBoxSizing(get(animatedProp)); + break; + + case DISPLAY: + viewProps.yogaStyle.setDisplay(get(animatedProp)); + break; + + case FLEX_BASIS: + viewProps.yogaStyle.setFlexBasis(get(animatedProp)); + break; + + case FLEX_DIRECTION: + viewProps.yogaStyle.setFlexDirection(get(animatedProp)); + break; + + case ROW_GAP: + viewProps.yogaStyle.setGap(yoga::Gutter::Row, get(animatedProp)); + break; + + case COLUMN_GAP: + viewProps.yogaStyle.setGap(yoga::Gutter::Column, get(animatedProp)); + break; + + case FLEX_GROW: + viewProps.yogaStyle.setFlexGrow(get(animatedProp)); + break; + + case FLEX_SHRINK: + viewProps.yogaStyle.setFlexShrink(get(animatedProp)); + break; + + case FLEX_WRAP: + viewProps.yogaStyle.setFlexWrap(get(animatedProp)); + break; + + case JUSTIFY_CONTENT: + viewProps.yogaStyle.setJustifyContent(get(animatedProp)); + break; + default: break; } diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsBuilder.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsBuilder.h index 301c45cd351..72f8da28517 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsBuilder.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsBuilder.h @@ -96,6 +96,62 @@ struct AnimatedPropsBuilder { { props.push_back(std::make_unique>(OUTLINE_WIDTH, value)); } + void setAlignContent(yoga::Align value) + { + props.push_back(std::make_unique>(ALIGN_CONTENT, value)); + } + void setAlignItems(yoga::Align value) + { + props.push_back(std::make_unique>(ALIGN_ITEMS, value)); + } + void setAlignSelf(yoga::Align value) + { + props.push_back(std::make_unique>(ALIGN_SELF, value)); + } + void setAspectRatio(yoga::FloatOptional value) + { + props.push_back(std::make_unique>(ASPECT_RATIO, value)); + } + void setBoxSizing(yoga::BoxSizing value) + { + props.push_back(std::make_unique>(BOX_SIZING, value)); + } + void setDisplay(yoga::Display value) + { + props.push_back(std::make_unique>(DISPLAY, value)); + } + void setFlexBasis(yoga::Style::SizeLength value) + { + props.push_back(std::make_unique>(FLEX_BASIS, value)); + } + void setFlexDirection(yoga::FlexDirection value) + { + props.push_back(std::make_unique>(FLEX_DIRECTION, value)); + } + void setRowGap(yoga::Style::Length value) + { + props.push_back(std::make_unique>(ROW_GAP, value)); + } + void setColumnGap(yoga::Style::Length value) + { + props.push_back(std::make_unique>(COLUMN_GAP, value)); + } + void setFlexGrow(yoga::FloatOptional value) + { + props.push_back(std::make_unique>(FLEX_GROW, value)); + } + void setFlexShrink(yoga::FloatOptional value) + { + props.push_back(std::make_unique>(FLEX_SHRINK, value)); + } + void setFlexWrap(yoga::Wrap value) + { + props.push_back(std::make_unique>(FLEX_WRAP, value)); + } + void setJustifyContent(yoga::Justify value) + { + props.push_back(std::make_unique>(JUSTIFY_CONTENT, value)); + } void storeDynamic(folly::dynamic &d) { rawProps = std::make_unique(std::move(d)); diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h index ecdee85f658..3a982ef2245 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h @@ -162,6 +162,62 @@ inline void updateProp(const PropName propName, BaseViewProps &viewProps, const case OUTLINE_WIDTH: viewProps.outlineWidth = snapshot.props.outlineWidth; break; + + case ALIGN_CONTENT: + viewProps.yogaStyle.setAlignContent(snapshot.props.yogaStyle.alignContent()); + break; + + case ALIGN_ITEMS: + viewProps.yogaStyle.setAlignItems(snapshot.props.yogaStyle.alignItems()); + break; + + case ALIGN_SELF: + viewProps.yogaStyle.setAlignSelf(snapshot.props.yogaStyle.alignSelf()); + break; + + case ASPECT_RATIO: + viewProps.yogaStyle.setAspectRatio(snapshot.props.yogaStyle.aspectRatio()); + break; + + case BOX_SIZING: + viewProps.yogaStyle.setBoxSizing(snapshot.props.yogaStyle.boxSizing()); + break; + + case DISPLAY: + viewProps.yogaStyle.setDisplay(snapshot.props.yogaStyle.display()); + break; + + case FLEX_BASIS: + viewProps.yogaStyle.setFlexBasis(snapshot.props.yogaStyle.flexBasis()); + break; + + case FLEX_DIRECTION: + viewProps.yogaStyle.setFlexDirection(snapshot.props.yogaStyle.flexDirection()); + break; + + case ROW_GAP: + viewProps.yogaStyle.setGap(yoga::Gutter::Row, snapshot.props.yogaStyle.gap(yoga::Gutter::Row)); + break; + + case COLUMN_GAP: + viewProps.yogaStyle.setGap(yoga::Gutter::Column, snapshot.props.yogaStyle.gap(yoga::Gutter::Column)); + break; + + case FLEX_GROW: + viewProps.yogaStyle.setFlexGrow(snapshot.props.yogaStyle.flexGrow()); + break; + + case FLEX_SHRINK: + viewProps.yogaStyle.setFlexShrink(snapshot.props.yogaStyle.flexShrink()); + break; + + case FLEX_WRAP: + viewProps.yogaStyle.setFlexWrap(snapshot.props.yogaStyle.flexWrap()); + break; + + case JUSTIFY_CONTENT: + viewProps.yogaStyle.setJustifyContent(snapshot.props.yogaStyle.justifyContent()); + break; } } diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index 829a3c3981e..ddf079c3800 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp @@ -9,10 +9,21 @@ #include #include #include +#include #include "AnimatedPropsRegistry.h" namespace facebook::react { +static const auto layoutProps = std::set{ + PropName::WIDTH, PropName::HEIGHT, PropName::FLEX, + PropName::MARGIN, PropName::PADDING, PropName::POSITION, + PropName::BORDER_WIDTH, PropName::ALIGN_CONTENT, PropName::ALIGN_ITEMS, + PropName::ALIGN_SELF, PropName::ASPECT_RATIO, PropName::BOX_SIZING, + PropName::DISPLAY, PropName::FLEX_BASIS, PropName::FLEX_DIRECTION, + PropName::ROW_GAP, PropName::COLUMN_GAP, PropName::FLEX_GROW, + PropName::FLEX_SHRINK, PropName::FLEX_WRAP, PropName::JUSTIFY_CONTENT, +}; + UIManagerNativeAnimatedDelegateBackendImpl:: UIManagerNativeAnimatedDelegateBackendImpl( std::weak_ptr animationBackend) @@ -53,11 +64,7 @@ static inline bool mutationHasLayoutUpdates( facebook::react::AnimationMutation& mutation) { for (auto& animatedProp : mutation.props.props) { // TODO: there should also be a check for the dynamic part - if (animatedProp->propName == WIDTH || animatedProp->propName == HEIGHT || - animatedProp->propName == FLEX || animatedProp->propName == MARGIN || - animatedProp->propName == PADDING || - animatedProp->propName == POSITION || - animatedProp->propName == BORDER_WIDTH) { + if (layoutProps.contains(animatedProp->propName)) { return true; } }