Skip to content

Conversation

@intergalacticspacehighway
Copy link
Contributor

@intergalacticspacehighway intergalacticspacehighway commented Sep 5, 2025

Summary:

This PR adds native CSS parser for backgroundImage property. Currently, it supports linear-gradient and radial-gradient spec compliant CSS syntax.

Changelog:

[GENERAL] [ADDED] - background image native parser.

Test Plan:

  • Replicated existing testcases from JS. Currently i've added CSS syntax testcases. Checkout CSSBackgroundImageTest.cpp

Verify example screens in RNTester

  • Set enableNativeCSSParsing to true in ReactNativeFeatureFlags.config.js and run yarn featureflags --update
  • Rebuild the project and verify LinearGradientExample and RadialGradientExample screens on both platforms.

Notes

  • Currently it is difficult to run CSS renderer tests. I made a custom cmake config to get it working, some steps would be helpful.
  • Right now the new CSS renderer seems to be only working on iOS. @NickGerleman mentioned there is some WIP to get it working on android. So please test this PR on iOS.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 5, 2025
{
if (colorStops.empty()) {
return {};
}
Copy link
Contributor Author

@intergalacticspacehighway intergalacticspacehighway Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not trigger, but added this just in case!

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 5, 2025
linear-gradient(45deg, white, rgba(243, 119, 54, 0.8), rgba(243, 119, 54, 0) 70%),
linear-gradient(90deg, white, rgba(253, 244, 152, 0.8), rgba(253, 244, 152, 0) 70%),
linear-gradient(135deg, white, rgba(123, 192, 67, 0.8), rgba(123, 192, 67, 0) 70%),
linear-gradient(180deg, white, rgba(3, 146, 207, 0.8), rgba(3, 146, 207, 0) 70%);
Copy link
Contributor Author

@intergalacticspacehighway intergalacticspacehighway Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the native parser does not support ; at the end of syntax so i had to change it. I guess it makes sense, we don't need to support semi-colons at the end. but lmk

@cortinico cortinico added p: Expo Partner: Expo Partner labels Sep 10, 2025
Copy link
Member

@javache javache left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll import this to get some signal on unit tests!

#include <unordered_map>

namespace facebook::react {
inline void parseProcessedBackgroundImage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should this be inline?

#include <unordered_map>

namespace facebook::react {
inline void parseProcessedBackgroundImage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should this be inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it is not needed, i had pasted them from BoxShadowPropsConversions.h code. Only fromRawValue needs to be inline. Updated

Comment on lines +83 to +86
if (!valueUnit) {
result = {};
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the expected behaviour that any intermediate invalid value cause the entire parse to fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, to match the web behaviour. e.g. linear-gradient(red 25xp, 42px) will draw an empty box on web. We did the same for JS parser

Comment on lines 111 to 112
auto direction = CSSLinearGradientDirection{CSSLinearGradientDirectionKeyword::ToTopLeft};
return direction;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit return directly

Suggested change
auto direction = CSSLinearGradientDirection{CSSLinearGradientDirectionKeyword::ToTopLeft};
return direction;
return CSSLinearGradientDirection{CSSLinearGradientDirectionKeyword::ToTopLeft};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Meta employee, you can view this in D83341309.

};


std::optional<CSSLinearGradientDirection> parseLinearGradientDirection(CSSSyntaxParser& parser) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's match the pattern of other CSSFiler etc and put these helpers as static private members of the CSSDataTypeParser struct.

template <>
struct CSSDataTypeParser<CSSDropShadowFilter> {
   ...
 private:
  static constexpr std::optional<CSSLinearGradientDirection> parseLinearGradientDirection(CSSSyntaxParser& parser))

@meta-codesync
Copy link

meta-codesync bot commented Oct 17, 2025

@javache has imported this pull request. If you are a Meta employee, you can view this in D83341309.

javache pushed a commit to javache/react-native that referenced this pull request Oct 17, 2025
Summary:
This PR adds native CSS parser for `backgroundImage` property. Currently, it supports linear-gradient and radial-gradient spec compliant CSS syntax.

## Changelog:

[GENERAL] [ADDED] - background image native parser.

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#53609

Test Plan:
- Replicated existing testcases from JS. Currently i've added CSS syntax testcases. Checkout `CSSBackgroundImageTest.cpp`

### Verify example screens in RNTester
- Set `enableNativeCSSParsing` to true in `ReactNativeFeatureFlags.config.js` and run `yarn featureflags --update`
- Rebuild the project and verify `LinearGradientExample` and `RadialGradientExample` screens on both platforms.

### Notes
- Currently it is difficult to run CSS renderer tests. I made a custom cmake config to get it working, some steps would be helpful.
- Right now the new CSS renderer seems to be only working on iOS. NickGerleman mentioned there is some WIP to get it working on android. So please test this PR on iOS.

Differential Revision: D83341309

Pulled By: javache
javache pushed a commit to javache/react-native that referenced this pull request Oct 17, 2025
Summary:
This PR adds native CSS parser for `backgroundImage` property. Currently, it supports linear-gradient and radial-gradient spec compliant CSS syntax.

## Changelog:

[GENERAL] [ADDED] - background image native parser.

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#53609

Test Plan:
- Replicated existing testcases from JS. Currently i've added CSS syntax testcases. Checkout `CSSBackgroundImageTest.cpp`

### Verify example screens in RNTester
- Set `enableNativeCSSParsing` to true in `ReactNativeFeatureFlags.config.js` and run `yarn featureflags --update`
- Rebuild the project and verify `LinearGradientExample` and `RadialGradientExample` screens on both platforms.

### Notes
- Currently it is difficult to run CSS renderer tests. I made a custom cmake config to get it working, some steps would be helpful.
- Right now the new CSS renderer seems to be only working on iOS. NickGerleman mentioned there is some WIP to get it working on android. So please test this PR on iOS.

Differential Revision: D83341309

Pulled By: javache
javache pushed a commit to javache/react-native that referenced this pull request Oct 20, 2025
Summary:
This PR adds native CSS parser for `backgroundImage` property. Currently, it supports linear-gradient and radial-gradient spec compliant CSS syntax.

## Changelog:

[GENERAL] [ADDED] - background image native parser.

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#53609

Test Plan:
- Replicated existing testcases from JS. Currently i've added CSS syntax testcases. Checkout `CSSBackgroundImageTest.cpp`

### Verify example screens in RNTester
- Set `enableNativeCSSParsing` to true in `ReactNativeFeatureFlags.config.js` and run `yarn featureflags --update`
- Rebuild the project and verify `LinearGradientExample` and `RadialGradientExample` screens on both platforms.

### Notes
- Currently it is difficult to run CSS renderer tests. I made a custom cmake config to get it working, some steps would be helpful.
- Right now the new CSS renderer seems to be only working on iOS. NickGerleman mentioned there is some WIP to get it working on android. So please test this PR on iOS.

Differential Revision: D83341309

Pulled By: javache
javache pushed a commit to javache/react-native that referenced this pull request Oct 20, 2025
Summary:
This PR adds native CSS parser for `backgroundImage` property. Currently, it supports linear-gradient and radial-gradient spec compliant CSS syntax.

## Changelog:

[GENERAL] [ADDED] - background image native parser.

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#53609

Test Plan:
- Replicated existing testcases from JS. Currently i've added CSS syntax testcases. Checkout `CSSBackgroundImageTest.cpp`

### Verify example screens in RNTester
- Set `enableNativeCSSParsing` to true in `ReactNativeFeatureFlags.config.js` and run `yarn featureflags --update`
- Rebuild the project and verify `LinearGradientExample` and `RadialGradientExample` screens on both platforms.

### Notes
- Currently it is difficult to run CSS renderer tests. I made a custom cmake config to get it working, some steps would be helpful.
- Right now the new CSS renderer seems to be only working on iOS. NickGerleman mentioned there is some WIP to get it working on android. So please test this PR on iOS.

Differential Revision: D83341309

Pulled By: javache
@meta-codesync meta-codesync bot closed this in a9780f9 Oct 20, 2025
@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Oct 20, 2025
@meta-codesync
Copy link

meta-codesync bot commented Oct 20, 2025

@javache merged this pull request in a9780f9.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @intergalacticspacehighway in a9780f9

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Expo Partner: Expo Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants