Skip to content

Commit e88962b

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Add toDynamic conversion for ValueUnit and ColorStop (facebook#54039)
Summary: Pull Request resolved: facebook#54039 Adding toDynamic conversion to `ColorStop` and `ValueUnit` structs which are being used by the linear and radial gradient data structures. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D83788006 fbshipit-source-id: 1ec0300ac4f17b65b6896d4d2c131eb05a059a22
1 parent 26502c6 commit e88962b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/react-native/ReactCommon/react/renderer/graphics/ColorStop.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ struct ColorStop {
1717
bool operator==(const ColorStop& other) const = default;
1818
SharedColor color;
1919
ValueUnit position;
20+
21+
#ifdef RN_SERIALIZABLE_STATE
22+
folly::dynamic toDynamic() const {
23+
folly::dynamic result = folly::dynamic::object();
24+
result["color"] = *color;
25+
result["position"] = position.toDynamic();
26+
return result;
27+
}
28+
#endif
2029
};
2130

2231
struct ProcessedColorStop {

packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#pragma once
99

10+
#ifdef RN_SERIALIZABLE_STATE
11+
#include <folly/dynamic.h>
12+
#endif
13+
1014
namespace facebook::react {
1115

1216
enum class UnitType {
@@ -44,5 +48,18 @@ struct ValueUnit {
4448
constexpr operator bool() const {
4549
return unit != UnitType::Undefined;
4650
}
51+
52+
#ifdef RN_SERIALIZABLE_STATE
53+
folly::dynamic toDynamic() const {
54+
switch (unit) {
55+
case UnitType::Undefined:
56+
return nullptr;
57+
case UnitType::Point:
58+
return value;
59+
case UnitType::Percent:
60+
return std::format("{}%", value);
61+
}
62+
}
63+
#endif
4764
};
4865
} // namespace facebook::react

0 commit comments

Comments
 (0)