Skip to content

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

Closed
hank121314 wants to merge 12 commits into
react:mainfrom
hank121314:fix/DynamicColorText
Closed

[Fix] Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent#29830
hank121314 wants to merge 12 commits into
react:mainfrom
hank121314:fix/DynamicColorText

Conversation

@hank121314

@hank121314hank121314 commented Sep 2, 2020

Copy link
Copy Markdown
Contributor

Summary

This pr:

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 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-botfacebook-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-botreact-native-bot added Bug Platform: Android Android applications. labels Sep 2, 2020
@analysis-bot

ghost commented Sep 2, 2020

Copy link
Copy Markdown
PlatformEngineArchSize (bytes)Diff
androidhermesarm64-v8a9,239,782-11
androidhermesarmeabi-v7a8,749,720-5
androidhermesx869,702,093-6
androidhermesx86_649,667,310-16
androidjscarm64-v8a10,886,756-47
androidjscarmeabi-v7a9,787,976-54
androidjscx8610,944,476-53
androidjscx86_6411,551,223-47

Base commit: 4246c75

@analysis-bot

ghost commented Sep 2, 2020

Copy link
Copy Markdown
PlatformEngineArchSize (bytes)Diff
ios-universaln/a--

Base commit: 4246c75

@safaiyeh

ghost commented Sep 4, 2020

Copy link
Copy Markdown
Contributor

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

ghost commented Sep 4, 2020

Copy link
Copy Markdown
Contributor

cc @JoshuaGross

@hank121314

ghost commented Sep 5, 2020

Copy link
Copy Markdown
ContributorAuthor

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! 😄

@hank121314hank121314 changed the title [Fix] Android/Text: Conditionally change <Text /> color cause text invisible[Fix] Android/ColorProps: ColorProps with null should be defaultColor instead of transparentOct 13, 2020
@hank121314hank121314 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 transparentOct 13, 2020
@Danite

ghost commented Oct 20, 2020

Copy link
Copy Markdown

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

ghost commented Oct 21, 2020

Copy link
Copy Markdown
ContributorAuthor

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.

ghost left a comment

Copy link
Copy Markdown

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);

Copy link
Copy Markdown

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);

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

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.

@Babar-Memon

ghost commented Feb 4, 2021

Copy link
Copy Markdown

i have a same problem on image component anyone can help me out to resolve this issue

@hank121314

ghost commented Feb 4, 2021

Copy link
Copy Markdown
ContributorAuthor

i have a same problem on image component anyone can help me out to resolve this issue

Hi @Babar-Memon , you can try to patch this pr with Building from source.
Or set color props to any colors except null.

ghost left a comment

Copy link
Copy Markdown
Contributor

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

BEFOREAFTER

Thanks a lot. 🙏 ☮️

@hank121314

ghost commented Feb 5, 2021

Copy link
Copy Markdown
ContributorAuthor

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

@StanislavMayorov

ghost commented Apr 7, 2021

Copy link
Copy Markdown

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

@taylorkline

ghost commented Jun 22, 2021

Copy link
Copy Markdown

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

@taylorkline

ghost commented Jun 23, 2021

Copy link
Copy Markdown

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

@hank121314

ghost commented Jun 26, 2021

Copy link
Copy Markdown
ContributorAuthor

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!

ghost left a comment

Copy link
Copy Markdown
Contributor

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

ghost commented Aug 9, 2021

Copy link
Copy Markdown

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

@facebook-github-bot

ghost commented Aug 10, 2021

Copy link
Copy Markdown
Contributor

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

@lunaleapslunaleaps self-assigned this Aug 10, 2021
@facebook-github-botfacebook-github-bot added the Merged This PR has been merged. label Aug 18, 2021
@facebook-github-bot

ghost commented Aug 18, 2021

Copy link
Copy Markdown
Contributor

@lunaleaps merged this pull request in 842bcb9.

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

Labels

BugCLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.MergedThis PR has been merged.Needs: React Native Team AttentionPlatform: AndroidAndroid applications.

Projects

None yet

14 participants

@hank121314@analysis-bot@safaiyeh@Danite@Babar-Memon@StanislavMayorov@taylorkline@facebook-github-bot@JeffGuKang@janicduplessis@fabOnReact@lunaleaps@sota000@react-native-bot