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 only] StatusBar automatically becomes "light-content" every time a RN Modal is opened #34350

Open
garkuwa opened this issue Aug 5, 2022 · 26 comments

Comments

@garkuwa
Copy link

garkuwa commented Aug 5, 2022

Description

I've created a very basic test project using native cli npx react-native init AwesomeTSProject --template react-native-template-typescriptD, and added a simple component with RN Modal like here https://reactnative.dev/docs/modal. Every time I click "Show Modal", the status bar becomes light, and I can't change it to dark even by setting barStyle="dark-content" for StatusBar. When the modal is closed, the status bar becomes dark again. The issue is reproducible only on Android, which in my case is Pixel 4a.

Version

0.69.3

Output of npx react-native info

info Fetching system and libraries information...
System:
OS: macOS 12.4
CPU: (10) x64 Apple M1 Pro
Memory: 38.39 MB / 32.00 GB
Shell: 3.2.57 - /bin/sh
Binaries:
Node: 16.13.1 - /usr/local/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 8.1.2 - /usr/local/bin/npm
Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
Android SDK:
API Levels: 28, 29, 30, 31, 32
Build Tools: 30.0.2, 30.0.3, 31.0.0, 32.0.0, 32.1.0
System Images: android-30 | Google APIs ARM 64 v8a, android-32 | Google APIs ARM 64 v8a, android-32 | Google Play ARM 64 v8a
Android NDK: Not Found
IDEs:
Android Studio: 2021.1 AI-211.7628.21.2111.8139111
Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild
Languages:
Java: 11.0.12 - /opt/homebrew/opt/openjdk@11/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.0.0 => 18.0.0
react-native: 0.69.3 => 0.69.3
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Steps to reproduce

  1. Start a new RN app from a template npx react-native init AwesomeTSProject --template react-native-template-typescriptD
  2. Add a modal like here https://reactnative.dev/docs/modal#statusbartranslucent-android
  3. Click the button to open the modal
  4. The StatusBar automatically becomes light

Snack, code example, screenshot, or link to a repository

Screen Shot 2022-08-05 at 5 29 29 pm

Screen Shot 2022-08-05 at 5 28 54 pm

Screen Shot 2022-08-05 at 5 33 46 pm

@dakshbhardwaj
Copy link
Contributor

I have tried to reproduce it on my Samsung device but it's not reproducible. seems to be device specific bug.

@sudharsanvishnu
Copy link

for work-around refer this #7474 (comment)

@garkuwa
Copy link
Author

garkuwa commented Aug 15, 2022

@dakshbhardwaj We can reproduce this issue on Samsung Galaxy Note 10 plus. Which phone are you testing on?

@dakshbhardwaj
Copy link
Contributor

@dakshbhardwaj We can reproduce this issue on Samsung Galaxy Note 10 plus. Which phone are you testing on?

I am using Samsung galaxy tab a7

@garkuwa
Copy link
Author

garkuwa commented Aug 15, 2022

for work-around refer this #7474 (comment)

@sudharsanvishnu this workaround doesn't work. After disabling the status bar in styles.xml, the one added by < StatusBar .../> produces the same issue, which means I can't control whether it's light or dark.

@sudharsanvishnu
Copy link

@dakshbhardwaj We can reproduce this issue on Samsung Galaxy Note 10 plus. Which phone are you testing on?

can reprodue on all models (google pixel) emulator

@sudharsanvishnu
Copy link

for work-around refer this #7474 (comment)

@sudharsanvishnu this workaround doesn't work. After disabling the status bar in styles.xml, the one added by < StatusBar .../> produces the same issue, which means I can't control whether it's light or dark.

have you tried after syncing gradle

  1. cd android > ./gradlew clean
  2. cd .. > npm run android ?

@garkuwa
Copy link
Author

garkuwa commented Aug 15, 2022

for work-around refer this #7474 (comment)

@sudharsanvishnu this workaround doesn't work. After disabling the status bar in styles.xml, the one added by < StatusBar .../> produces the same issue, which means I can't control whether it's light or dark.

have you tried after syncing gradle

  1. cd android > ./gradlew clean
  2. cd .. > npm run android ?

@sudharsanvishnu it hasn't made any difference. If you've got a minute, check out the following test example https://github.com/garkuwa/RN_statusbar_test/tree/main/AwesomeTSProject

@jgo80
Copy link

jgo80 commented Aug 24, 2022

I'm experiencing the same issue with Google Pixel Emulator.
<StatusBar barStyle={yourPreferedStyleHere} /> should be respected.

@dseipp
Copy link

dseipp commented Sep 7, 2022

Seeing the problem on Android v12 (Samsung S20, Platform Version 31). barStyle seems to be ignored within modals along with backgroundColor. backgroundColor can be set but only in an effect, barStyle never changes, however.

@jjenzz
Copy link

jjenzz commented Oct 5, 2022

also experiencing this on Pixel 5 emulator (API 33) and a physical Pixel 6 Pro device but in reverse. I have set barStyle to light-content by default and it switches to dark-content when modals open.

"expo": "46.0.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.70.0-rc.2",

@Abbondanzo
Copy link
Contributor

Abbondanzo commented Oct 7, 2022

Shot in the dark, but we were running into this same issue. I think it's caused by the fact that the StatusBarModule is only updating the WindowInsetsController.systemBarsAppearance for devices API >30, and never touching the old View.systemUiVisibility:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
WindowInsetsController insetsController = activity.getWindow().getInsetsController();
if ("dark-content".equals(style)) {
// dark-content means dark icons on a light status bar
insetsController.setSystemBarsAppearance(
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
} else {
insetsController.setSystemBarsAppearance(
0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

However, the modal host tries to read an untouched View.systemUiVisibility from the root activity. Since the SYSTEM_UI_FLAG_LIGHT_STATUS_BAR mask is never set on that integer, it reads as 0 (a.k.a. "dark mode"):

if (currentActivity != null && !currentActivity.isFinishing()) {
mDialog.show();
if (context instanceof Activity) {
mDialog
.getWindow()
.getDecorView()
.setSystemUiVisibility(
((Activity) context).getWindow().getDecorView().getSystemUiVisibility());
}
mDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}

We added a manual override to the root activity to set that systemUIVisibility flag in all cases, and the modal appears to be respecting it now (this is in Kotlin but can easily be translated back to Java):

override fun onResume() {
  super.onResume()
   if (VERSION.SDK_INT >= VERSION_CODES.M) {
      val newSystemUiVisibility = if (isInDarkMode) {
        // Remove light status bar flag in dark mode
        window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
      } else {
        // Add light status bar flag in light mode
        window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
      }
      window.decorView.systemUiVisibility = newSystemUiVisibility
    }
  }
}

facebook-github-bot pushed a commit that referenced this issue Oct 11, 2022
Summary:
As identified in #34350, modals do not honor the system's status bar colors because they may not be set by the deprecated `systemUiVisibility` flags. Unless `android:windowLightStatusBar` is set to true, the default flag is a zero-integer (a.k.a. "dark mode", where the icons show as white). Since the `StatusBar` component is using the new `setSystemBarsAppearance` API, the ModalHost should also infer its status bar settings from the same API.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Android] [Fixed] - Fixed an issue on Android API 31+ where modals would turn status bar icons white by default

Pull Request resolved: #34899

Test Plan:
- On a screen with the `StatusBar` bar style set to `dark-content`, the modal also uses white icons
- On a screen with the `StatusBar` bar style set to `light-content`, the modal also uses black icons

### Preview

Here, I change the `barStyle` from `light-content` to `dark-content` and demonstrate that the proper attributes are retained. The "Before" is a recording from `main` and the "After" is this branch. Notice how in "Before", the status bar is always turning the icons white when the modal opens.

|Before|After|
|-|-|
|![ezgif-5-586e81991d](https://user-images.githubusercontent.com/10366495/194954666-71f69bd6-c02a-4725-9562-e1f5fcfdeddf.gif)|![ezgif-5-b212d7bb01](https://user-images.githubusercontent.com/10366495/194954244-9c205821-1d7f-4630-861b-f5dbe207f7cd.gif)|

## Other considerations

There's some argument towards removing this check entirely--the status bar appearance should be derived from the theming and/or the parent activity's settings, thereby removing the need to apply separate styling

Reviewed By: lunaleaps

Differential Revision: D40243122

Pulled By: lunaleaps

fbshipit-source-id: ffa56c7d6a1906f89658f95a12f6bf1cefd5be8e
@Abbondanzo
Copy link
Contributor

Should be fixed by #34899

@garkuwa
Copy link
Author

garkuwa commented Dec 7, 2022

cheers @Abbondanzo

@way-jm
Copy link

way-jm commented Jan 4, 2023

Still have the issue in 0.70.6

@Abbondanzo
Copy link
Contributor

Still have the issue in 0.70.6

The fix made it out in the first release candidate of 0.71.0, so 0.70.6 will still have that issue

@saadi-ninjasCode
Copy link

saadi-ninjasCode commented Feb 6, 2023

@Abbondanzo Facing this issue in 0.71.1 & 0.71.2
Devices

  • Google Pixel 4A (Android 13)
  • Xiaomi note 10 lite (Android 12)

OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this issue May 22, 2023
Summary:
As identified in facebook#34350, modals do not honor the system's status bar colors because they may not be set by the deprecated `systemUiVisibility` flags. Unless `android:windowLightStatusBar` is set to true, the default flag is a zero-integer (a.k.a. "dark mode", where the icons show as white). Since the `StatusBar` component is using the new `setSystemBarsAppearance` API, the ModalHost should also infer its status bar settings from the same API.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Android] [Fixed] - Fixed an issue on Android API 31+ where modals would turn status bar icons white by default

Pull Request resolved: facebook#34899

Test Plan:
- On a screen with the `StatusBar` bar style set to `dark-content`, the modal also uses white icons
- On a screen with the `StatusBar` bar style set to `light-content`, the modal also uses black icons

### Preview

Here, I change the `barStyle` from `light-content` to `dark-content` and demonstrate that the proper attributes are retained. The "Before" is a recording from `main` and the "After" is this branch. Notice how in "Before", the status bar is always turning the icons white when the modal opens.

|Before|After|
|-|-|
|![ezgif-5-586e81991d](https://user-images.githubusercontent.com/10366495/194954666-71f69bd6-c02a-4725-9562-e1f5fcfdeddf.gif)|![ezgif-5-b212d7bb01](https://user-images.githubusercontent.com/10366495/194954244-9c205821-1d7f-4630-861b-f5dbe207f7cd.gif)|

## Other considerations

There's some argument towards removing this check entirely--the status bar appearance should be derived from the theming and/or the parent activity's settings, thereby removing the need to apply separate styling

Reviewed By: lunaleaps

Differential Revision: D40243122

Pulled By: lunaleaps

fbshipit-source-id: ffa56c7d6a1906f89658f95a12f6bf1cefd5be8e
@jaydonlau
Copy link

@Abbondanzo I'm seeing a related issue where the icons in the status bar are always black, and not respecting light-content. This is on RN 0.72.4, Android 13 Google Pixel 5.

I think this is related to your fix in 34899, specifically this line:

mDialog.getWindow().getInsetsController().setSystemBarsAppearance(appearance, appearance);

If the status bar goes from black -> white, we need to call
setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS) (where APPEARANCE_LIGHT_STATUS_BARS := 8), but I think the code I linked above would call setSystemBarsAppearance(0, 0) instead. See related Android docs

@Abbondanzo
Copy link
Contributor

Abbondanzo commented Oct 13, 2023

@Abbondanzo I'm seeing a related issue where the icons in the status bar are always black, and not respecting light-content. This is on RN 0.72.4, Android 13 Google Pixel 5.

I think this is related to your fix in 34899, specifically this line:

mDialog.getWindow().getInsetsController().setSystemBarsAppearance(appearance, appearance);

If the status bar goes from black -> white, we need to call setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS) (where APPEARANCE_LIGHT_STATUS_BARS := 8), but I think the code I linked above would call setSystemBarsAppearance(0, 0) instead. See related Android docs

A reversal of bugs! In this case, the bitmask is wrong. Great catch! I'll cook up a PR tomorrow morning but in essence we should be grabbing the unmatched bits and writing them over for the dialog's window. It would look something like this:

int activityAppearance = ((Activity) context).getWindow().getInsetsController().getSystemBarsAppearance();
int dialogAppearance = mDialog.getWindow().getInsetsController().getSystemBarsAppearance();
// Only write over each of the bits that do not match
int bitmask = activityAppearance ^ dialogAppearance;
mDialog.getWindow().getInsetsController().setSystemBarsAppearance(activityAppearance, bitmask); 

In the current implementation, if the activity has cleared the flag but the dialog creates windows that default to having the flag set, the activity's appearance values for each of those flags does not actually get set. I'll give that a try in the morning and file a new PR.

I am curious why a bitmask of 0 is changing anything, however. This behavior is not very well documented, but intuition tells me that a mask of 0 is just a no-op. Even if a mask of 0 means every flag and you have light content on the backing activity (the flag is cleared), then applying 0 to every flag should keep the foreground icons white. Likewise, if the status bar flag is set then the mask and appearance values should also set. This means black->black and white->white. Are you perhaps applying any dialog styles in a theme somewhere and this is interfering? Or is this just the case where you change your status bar from dark-content to light-content after the dialog has been created once and future loads of the dialog still show dark icons?

@jaydonlau
Copy link

@Abbondanzo thanks for the prompt reply, I'll keep an eye out for the next release with the fix!

I am curious why a bitmask of 0 is changing anything, however. This behavior is not very well documented, but intuition tells me that a mask of 0 is just a no-op. Even if a mask of 0 means every flag and you have light content on the backing activity (the flag is cleared), then applying 0 to every flag should keep the foreground icons white. Likewise, if the status bar flag is set then the mask and appearance values should also set. This means black->black and white->white. Are you perhaps applying any dialog styles in a theme somewhere and this is interfering? Or is this just the case where you change your status bar from dark-content to light-content after the dialog has been created once and future loads of the dialog still show dark icons?

Yeah good point, and it's why I'm not entirely convinced this will fix my specific issue but I'm hoping it'll narrow down the cause for the behaviour I'm seeing. I couldn't find anywhere else in the RN code that was setting the system bar's appearance (the StatusBarModule seems to be doing the right thing).

In my application I'm just setting light-content, but on transition the status bar changes from white icons to black. I was hoping the bitmask of 0 was causing some sort of odd undefined behaviour, but I'll investigate further once your fix is out.

@Abbondanzo
Copy link
Contributor

In my application I'm just setting light-content, but on transition the status bar changes from white icons to black. I was hoping the bitmask of 0 was causing some sort of odd undefined behaviour, but I'll investigate further once your fix is out.

I've been trying to replicate this bug but (fortunately) haven't had any luck in doing so. I've got a small snack set up here to toggle the status bar before opening a modal: https://snack.expo.dev/@abbondanzo/status-bar-tester. Could you share a code snippet or snack that's causing the issue for you? I'd like to guarantee that the fix will actually solve your issue and detail the exact cause in the pull request.

facebook-github-bot pushed a commit that referenced this issue Oct 31, 2023
… modals (#40979)

Summary:
The current ReactModalHostView implementation incorrectly applies system bar appearances by providing the wrong mask to the `setSystemBarsAppearance` method invocation. Per [this issue comment](#34350 (comment)), jaydonlau correctly identified that when the status bar is set to `light-content` (light icons, dark background), the function is called with both a `0` appearance and `0` mask, which should instead be provided with the `APPEARANCE_LIGHT_STATUS_BARS` mask.

The first pass at this PR attempted to pull out the entire appearance from the activity, compare it against the dialog's appearance, and only use a mask of differing bits (see the `appearanceMask` variable). However, if the `android:windowLightStatusBar` attribute is ever set to true, this does not impact the appearance of the status bar but rather the system UI visibility. As a result, the derived mask from system bars appearance would be 0 since both the activity and dialog would have appearances of 0.

Rather than try and "future-proof" this implementation for other uses of system bar appearance, this change is directed only at updating the `APPEARANCE_LIGHT_STATUS_BARS` bit in the dialog's system bar appearance. The only other native code that touches status bars is the `StatusBarModule` and that only touches this flag.

This is a follow-up to #34899.

## Changelog:

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

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

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

[ANDROID] [FIXED] - Fixed an issue where the status bar colors would not match when opening modals

Pull Request resolved: #40979

Test Plan:
First test:
- Replace the `RNTesterAppShared` implementation with the implementation from [this Expo snack](https://snack.expo.dev/abbondanzo/status-bar-tester)
- Toggle the status bar to show dark icons, open the modal and ensure that dark icons are displayed
- Toggle the status bar to show light icons, open the modal and ensure that light icons are displayed

Second test:
- Set the `android:windowLightStatusBar` attribute to true in the `AppTheme`
- Follow the steps from the First test above, guaranteeing that status bar appearance overrides the theme

Reviewed By: NickGerleman

Differential Revision: D50329714

Pulled By: luluwu2032

fbshipit-source-id: 26ecaca05f8e00a52e13767e468b552ac167fc98
@melnikovkolya
Copy link

Hi,

this issue also surfaces on the iOS 17.

"expo": "^49.0.0",
"react-native": "0.72.6",

I tested it both in a simulator and on a device.

The font style of the StatusBar switches to the dark mode after the 'Save Password' modal is discarded.

@RichardLindhout
Copy link

We're also having problems with this issue in our date-picker library web-ridge/react-native-paper-dates#363

@RichardLindhout
Copy link

@Abbondanzo strange it does not happen in your snack

Othinn pushed a commit to Othinn/react-native that referenced this issue Jan 9, 2024
… modals (facebook#40979)

Summary:
The current ReactModalHostView implementation incorrectly applies system bar appearances by providing the wrong mask to the `setSystemBarsAppearance` method invocation. Per [this issue comment](facebook#34350 (comment)), jaydonlau correctly identified that when the status bar is set to `light-content` (light icons, dark background), the function is called with both a `0` appearance and `0` mask, which should instead be provided with the `APPEARANCE_LIGHT_STATUS_BARS` mask.

The first pass at this PR attempted to pull out the entire appearance from the activity, compare it against the dialog's appearance, and only use a mask of differing bits (see the `appearanceMask` variable). However, if the `android:windowLightStatusBar` attribute is ever set to true, this does not impact the appearance of the status bar but rather the system UI visibility. As a result, the derived mask from system bars appearance would be 0 since both the activity and dialog would have appearances of 0.

Rather than try and "future-proof" this implementation for other uses of system bar appearance, this change is directed only at updating the `APPEARANCE_LIGHT_STATUS_BARS` bit in the dialog's system bar appearance. The only other native code that touches status bars is the `StatusBarModule` and that only touches this flag.

This is a follow-up to facebook#34899.

## Changelog:

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

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

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

[ANDROID] [FIXED] - Fixed an issue where the status bar colors would not match when opening modals

Pull Request resolved: facebook#40979

Test Plan:
First test:
- Replace the `RNTesterAppShared` implementation with the implementation from [this Expo snack](https://snack.expo.dev/abbondanzo/status-bar-tester)
- Toggle the status bar to show dark icons, open the modal and ensure that dark icons are displayed
- Toggle the status bar to show light icons, open the modal and ensure that light icons are displayed

Second test:
- Set the `android:windowLightStatusBar` attribute to true in the `AppTheme`
- Follow the steps from the First test above, guaranteeing that status bar appearance overrides the theme

Reviewed By: NickGerleman

Differential Revision: D50329714

Pulled By: luluwu2032

fbshipit-source-id: 26ecaca05f8e00a52e13767e468b552ac167fc98
@lukaszkurantdev
Copy link

I know that this is old thread, but It also happens on RN 0.73.6. After analysis, it looks like this is a problem with mask as @Abbondanzo mentioned. The problem only affects Android version >= 12. To avoid modifying the native code (AND not building the RN locally) until real fix, just add in styles.xml in AppTheme:

 <item name="android:windowLightStatusBar">true</item>

or when we want to have icons in white color in the model

 <item name="android:windowLightStatusBar">false</item>

However, it is then possible to have color problems on SplashScreen. The rest of the colors after the modals can be quietly set through the StatusBar component.

@JJSLIoT
Copy link

JJSLIoT commented Jul 12, 2024

I am facing the same issue on Android after upgrading to RN 0.71.19. This issue was not there in 0.68.7.

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

No branches or pull requests