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

[Android] Fix ReactSwitch for non RippleDrawable backgrounds #32468

Closed
wants to merge 2 commits into from

Conversation

smarki
Copy link
Contributor

@smarki smarki commented Oct 24, 2021

Summary

ReactSwitch component is crashing on Android when it is initialised with both a backgroundColor and thumbColor, style={{ backgroundColor: "anyColor" }} thumbColor="anyColor", due to IllegalCastException.

When setting a background color, BaseViewManagerDelegate is calling setBackgroundColor which replaces the background drawable with a ColorDrawale, hence this line fails.

Instead, given the ripple effect needs to be preserved, one should initialise a RippleDrawable using the current background drawable and set it as the background of the switch.

Given the RippleDrawable should be preserved, overriding the setBackgroundColor seemed the sensible thing to do.

Changelog

[Android] [Fixed] - Fix crash when a Switch is initialised with both backgroundColor and thumbColor.

Test Plan

Setup:

Initialise an empty React Native project. Add a switch component:
<Switch style={{backgroundColor: 'red'}} thumbColor={'#356'} />
Run the project yarn android

Current state (RN 65+):

Red screen will show highlighting an IllegalCastException.

With fix:

  • The component is expected to have a red background.
  • When pressed a ripple effect shows inside the backgrounds bounding box.
  • Business as usual otherwise.

backgroundColor with thumbColor:
backgroundColor + thumbColor

Just thumbColor:
Screen Recording 2021-10-25 at 00 23 57

@facebook-github-bot
Copy link
Contributor

Hi @smarki!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels Oct 24, 2021
@smarki smarki force-pushed the fix/reactswitch-ripple branch from ee41708 to df8822f Compare October 24, 2021 23:07
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:

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Oct 24, 2021
@smarki smarki force-pushed the fix/reactswitch-ripple branch from df8822f to 50d2993 Compare October 24, 2021 23:16
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:

@pull-bot
Copy link

PR build artifact for 50d2993 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@analysis-bot
Copy link

analysis-bot commented Oct 24, 2021

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,289,552 +42
android hermes armeabi-v7a 7,853,673 +31
android hermes x86 8,656,601 +36
android hermes x86_64 8,615,741 +38
android jsc arm64-v8a 9,791,111 -121
android jsc armeabi-v7a 8,752,046 -116
android jsc x86 9,740,031 -121
android jsc x86_64 10,340,876 -122

Base commit: 24d4184
Branch: main

@analysis-bot
Copy link

analysis-bot commented Oct 24, 2021

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

Base commit: 24d4184
Branch: main

@smarki smarki force-pushed the fix/reactswitch-ripple branch from 50d2993 to 1a26207 Compare October 25, 2021 06:18
@pull-bot
Copy link

PR build artifact for 1a26207 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@smarki
Copy link
Contributor Author

smarki commented Oct 25, 2021

What is the process to back-port an MR? It would be nice if we can have it on v0.66 given that v0.67 is still RC.

@facebook-github-bot
Copy link
Contributor

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

@cortinico
Copy link
Contributor

What is the process to back-port an MR? It would be nice if we can have it on v0.66 given that v0.67 is still RC.

You would have to comment here reactwg/react-native-releases#3
However I don't believe this will be backported to 0.66.2 as it's not a critical bug nor a regression introduced in the latest version.

@ShikaSD
Copy link
Contributor

ShikaSD commented Oct 25, 2021

@smarki, thanks for submitting this change!
Do you mind updating SwitchExample.js with this prop as well?

@pull-bot
Copy link

PR build artifact for 80a643f is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@facebook-github-bot
Copy link
Contributor

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

@facebook-github-bot
Copy link
Contributor

@cortinico merged this pull request in 456cf3d.

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. Platform: Android Android applications. 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.

7 participants