Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent #29830

Closed
wants to merge 12 commits into from

Conversation

hank121314
Copy link
Contributor

@hank121314 hank121314 commented Sep 2, 2020

Summary

This pr:

Because most of @ReactProps(name = ViewProps.COLOR) accept @ Nullable Integer.
For example:

@ReactProp(name = ViewProps.COLOR, customType = "Color")
public void setColor(@Nullable Integer color) {
mIsColorSet = (color != null);
if (mIsColorSet) {
mColor = color;
}
markUpdated();
}

After update to react-native 0.63.2 to make PlatformColor work, there is a new ColorPropSetter.

private static class ColorPropSetter extends PropSetter {
private final int mDefaultValue;
public ColorPropSetter(ReactProp prop, Method setter) {
this(prop, setter, 0);
}
public ColorPropSetter(ReactProp prop, Method setter, int defaultValue) {
super(prop, "mixed", setter);
mDefaultValue = defaultValue;
}
@Override
protected Object getValueOrDefault(Object value, Context context) {
if (value == null) {
return mDefaultValue;
}
return ColorPropConverter.getColor(value, context);
}
}

But ColorPropSetter won't return an nullable value with getValueOrDefault, it will always return it's defaultValue which is 0.
And 0 is equal to TRANSPARENT, will cause disappear.

Changelog

[Android] [Fixed] - ColorProps with value null should be defaultColor instead of transparent

Test Plan

Please initiated a new project and replaced the app with the following code:

import * as React from 'react';
import {Text, View, TouchableOpacity, PlatformColor} from 'react-native';

export default function App() {
  const [active, setActive] = React.useState(false);

  return (
    <View>
      <Text style={active ? {color: 'green'} : null}>Example</Text>
      <Text
        style={
          active ? {color: PlatformColor('@android:color/holo_purple')} : null
        }>
        Example2
      </Text>
      <TouchableOpacity onPress={() => setActive(!active)}>
        <Text>Toggle Active</Text>
      </TouchableOpacity>
    </View>
  );
}

Thanks you so much for your code review!

@facebook-github-bot facebook-github-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 2, 2020
@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels Sep 2, 2020
@analysis-bot
Copy link

analysis-bot commented Sep 2, 2020

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 9,239,782 -11
android hermes armeabi-v7a 8,749,720 -5
android hermes x86 9,702,093 -6
android hermes x86_64 9,667,310 -16
android jsc arm64-v8a 10,886,756 -47
android jsc armeabi-v7a 9,787,976 -54
android jsc x86 10,944,476 -53
android jsc x86_64 11,551,223 -47

Base commit: 4246c75

@analysis-bot
Copy link

analysis-bot commented Sep 2, 2020

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 4246c75

@safaiyeh
Copy link
Contributor

safaiyeh commented Sep 4, 2020

Nice work! @hank121314 I built RNTester to test this out:
Screen Recording 2020-09-03 at 11 32 06 PM

Working as expected, tagging PR submitted on the issue.

@safaiyeh
Copy link
Contributor

safaiyeh commented Sep 4, 2020

cc @JoshuaGross

@hank121314
Copy link
Contributor Author

Nice work! @hank121314 I built RNTester to test this out:
Screen Recording 2020-09-03 at 11 32 06 PM

Working as expected, tagging PR submitted on the issue.

Thank you so much for help and testing! 😄

yskoht added a commit to newn-team/react-native that referenced this pull request Oct 6, 2020
@hank121314 hank121314 changed the title [Fix] Android/Text: Conditionally change <Text /> color cause text invisible [Fix] Android/ColorProps: ColorProps with null should be defaultColor instead of transparent Oct 13, 2020
@hank121314 hank121314 changed the title [Fix] Android/ColorProps: ColorProps with null should be defaultColor instead of transparent [Fix] Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent Oct 13, 2020
@Danite
Copy link

Danite commented Oct 20, 2020

I think when the color is undefined it should also fallback to the defaultColor.

Take this case:
<Text style={[someTextStyleWithoutColor, isActive && activeTextStyle]}> Hello there! </Text>
it will make the text transparent once is not active.

@hank121314
Copy link
Contributor Author

I think when the color is undefined it should also fallback to the defaultColor.

Take this case:
<Text style={[someTextStyleWithoutColor, isActive && activeTextStyle]}> Hello there! </Text>
it will make the text transparent once is not active.

Yes, this pr is also included undefined in javascript.

Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

@Override
protected @Nullable Object getValueOrDefault(Object value, Context context) {
if (value != null) {
return ColorPropConverter.getColor(value, context);

Choose a reason for hiding this comment

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

google-java-format suggested changes:

@@ -337 +337 @@
-          return ColorPropConverter.getColor(value, context);
+        return ColorPropConverter.getColor(value, context);

set0gut1 pushed a commit to newn-team/react-native that referenced this pull request Dec 11, 2020
Copy link
Contributor Author

@hank121314 hank121314 left a comment

Choose a reason for hiding this comment

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

apply google-java-format suggested changes.

Copy link
Contributor

@fabOnReact fabOnReact left a comment

Choose a reason for hiding this comment

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

thanks a lot @hank121314. Your pr also fixes #29378 (not linked in your pr summary), I was searching for the solution as explained in #29412 (comment) and then found your PR from another related issue.

I checkout out your branch and as in the below test case, verified that it fixes also #29378

CLICK TO OPEN TESTS RESULTS

BEFORE AFTER

Thanks a lot. 🙏 ☮️

@hank121314
Copy link
Contributor Author

Hi @fabriziobertoglio1987 .
Thanks for your investigation and review!
Hope this pr can be merged soon. 😄

@StanislavMayorov
Copy link

@fabriziobertoglio1987 @hank121314 Hi. Could you merge it please?

@taylorkline
Copy link

This did not fix ActivityIndicator from being transparent for me. #30057 worked for me.

@taylorkline
Copy link

Actually, in general, this fix is not working for me on 0.64.2.

@hank121314
Copy link
Contributor Author

Hi @taylorkline, did you try to patch this pr with Building from source?
If it still does not work, could you give me some reproducible code?
Thanks!

Copy link
Contributor

@janicduplessis janicduplessis left a comment

Choose a reason for hiding this comment

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

LGTM, sorry this is taking forever to merge, will ping some people at fb.

@taylorkline
Copy link

@hank121314 No, I did not - my apologies, likely user error on my part then.

@facebook-github-bot
Copy link
Contributor

@lunaleaps has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@lunaleaps lunaleaps self-assigned this Aug 10, 2021
@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Aug 18, 2021
@facebook-github-bot
Copy link
Contributor

@lunaleaps merged this pull request in 842bcb9.

kelset pushed a commit that referenced this pull request Aug 19, 2021
…or instead of transparent (#29830)

Summary:
This pr:
- Fixes: #30183
- Fixes: #30056
- Fixes: #29950
- Fixes: #29717
- Fixes: #29495
- Fixes: #29412
- Fixes: #29378

Because most of ReactProps(name = ViewProps.COLOR) accept @ Nullable Integer.
For example:
https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L472-L479

After update to react-native 0.63.2 to make PlatformColor work, there is a new ColorPropSetter.
https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java#L194-L215

But ColorPropSetter won't return an nullable value with getValueOrDefault, it will always return it's defaultValue which is 0.
And 0 is equal to TRANSPARENT, will cause <Text /> disappear.
## Changelog

[Android] [Fixed] - ColorProps with value null should be defaultColor instead of transparent

Pull Request resolved: #29830

Test Plan:
Please initiated a new project and replaced the app with the following code:
```
import * as React from 'react';
import {Text, View, TouchableOpacity, PlatformColor} from 'react-native';

export default function App() {
  const [active, setActive] = React.useState(false);

  return (
    <View>
      <Text style={active ? {color: 'green'} : null}>Example</Text>
      <Text
        style={
          active ? {color: PlatformColor('android:color/holo_purple')} : null
        }>
        Example2
      </Text>
      <TouchableOpacity onPress={() => setActive(!active)}>
        <Text>Toggle Active</Text>
      </TouchableOpacity>
    </View>
  );
}
```

Thanks you so much for your code review!

Reviewed By: JoshuaGross

Differential Revision: D30209262

Pulled By: lunaleaps

fbshipit-source-id: bc223f84a92f742266cb7b40eb26722551940d76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 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. Needs: React Native Team Attention Platform: Android Android applications.
Projects
None yet