react-native-ui-lib icon indicating copy to clipboard operation
react-native-ui-lib copied to clipboard

Dark mode view modifiers no longer working

Open dariuscosden opened this issue 3 years ago • 43 comments

Description

Dark mode no longer works with view modifiers. Regardless of device scheme, the view background colors will be "light".

Related to

  • [x] Components
  • [ ] Demo
  • [ ] Docs
  • [ ] Typings

Steps to reproduce

  1. Load light and dark schemes using Colors.loadSchemes.
  2. Add background color modifier to any view
  3. Switch from light to dark color scheme on device
  4. Views stay light

More Info

I've traced it down to this commit, where the default scheme was changed from default to light. Currently I have patched the package to change it back to default and it works.

Environment

  • React Native: 0.68.2
  • React Native UI Lib: 6.18.0

Affected platforms

  • [x] Android
  • [x] iOS
  • [ ] Web

dariuscosden avatar Jul 18 '22 15:07 dariuscosden

@dariusmandres please add the following require in your app, in an initial place, before importing react-native-ui-lib at the first time.

require('react-native-ui-lib/config').setConfig({appScheme: 'default'});

lidord-wix avatar Aug 21 '22 11:08 lidord-wix

@lidord-wix Let's add this to our docs. Under Foundation -> Colors

ethanshar avatar Aug 22 '22 06:08 ethanshar

@lidord-wix

I have tried that, it didn't work unfortunately. The dark mode is still not respected. I made sure to have it at the topmost level before anything else is imported.

Have you gotten this to work on your end? Was there anything else to keep in mind that could affect?

dariuscosden avatar Aug 22 '22 12:08 dariuscosden

I investigated a little bit, 6.13.0 works perfectly but 6.14.0 breaks the color changes.

After adding this:

require('react-native-ui-lib/config').setConfig({appScheme: 'default'});

6.18.0 works great, but 6.19.0 breaks. Maybe something related to #2146?


Update: backgroundColor doesnt work. Rest of the props work.

sallar avatar Aug 25 '22 16:08 sallar

The same happens to me with 6.20.* version. When downgrading to 6.18.*, View changes background color correctly when toggling appearance mode.

kanzitelli avatar Aug 29 '22 20:08 kanzitelli

I created a new react-native app with version 6.20.* of react-native-ui-lib and when I call

require('react-native-ui-lib/config').setConfig({appScheme: 'default'});

at the very beginning point of the app, it works. Your issue might happen because you import your app (where you use ui-lib) before you require the config. (imports are hoisted and called before requires). I can suggest you the following solution:

  1. Add a new file to your project root called setup.js.
  2. Add the following line to your setup.js file:
require('react-native-ui-lib/config').setConfig({appScheme: 'default'});
  1. import the setup file on your index.js, and make sure it's your first import (or at least before importing your app). Please check that and let me know if it works for you

lidord-wix avatar Sep 01 '22 09:09 lidord-wix

Thanks @lidord-wix I did the exact thing you mentioned:

index.js:

import './setup';
import 'react-native-gesture-handler';
import {registerRootComponent} from 'rnn-screens';

// ...

setup.ts:

require('react-native-ui-lib/config').setConfig({appScheme: 'default'});

and everything works, except background colors when appearance mode is updated:

Simulator Screen Recording - iPhone 13 - 2022-09-01 at 13 11 09

(using react-native-ui-lib's View: <View flex bg-bgColor>)

sallar avatar Sep 01 '22 10:09 sallar

@sallar do you load the bgColor using Colors.loadScheme()? can you share a code snippet?

lidord-wix avatar Sep 01 '22 10:09 lidord-wix

@lidord-wix yes,

const colors: DesignSystemColors = {
  primary: '#5383b8', // blue
  secondary: '#469c57', // green
  accent: '#fed330', // yellow
  blackish: Colors.rgba(20, 20, 20, 1),
  blackish2: Colors.rgba(50, 50, 50, 1),
  whitish: Colors.rgba(250, 250, 250, 1),
  whitish2: Colors.rgba(230, 230, 230, 1),
};

const themes: Record<AppearanceMode, ThemeColors> = {
  light: {
    textColor: colors.blackish,
    bgColor: colors.whitish,
    bg2Color: colors.whitish2,
  },
  dark: {
    textColor: colors.whitish,
    bgColor: colors.blackish,
    bg2Color: colors.blackish2,
  },
};

Colors.loadColors(colors);
Colors.loadSchemes(themes);

sallar avatar Sep 01 '22 10:09 sallar

Another issue, <Text> also only works between appearance modes when I explicitly set the color myself: <Text marginB-s2 text60R textColor>, otherwise the default colors from react-native-ui-lib's design tokens don't come through.

so this works between scheme changes: <Text marginB-s2 text60R textColor>

but this doesnt, and stays black: <Text marginB-s2 text60R>

sallar avatar Sep 01 '22 10:09 sallar

@sallar for now, after a scheme change you'll need to restart the app. after the restart, the Text color should be in the right color. for the background issue, where do you call this loadSchemes(themes)?

lidord-wix avatar Sep 01 '22 10:09 lidord-wix

@lidord-wix if the app needs to reload then everything works as expected. all my comments regarding stuff not working are related to "dynamic" scheme changes. do you think this will be fixed in the future?

sallar avatar Sep 01 '22 10:09 sallar

@sallar we hope so, we're using react-native's platform color and we still have some issues there (mainly on android).

lidord-wix avatar Sep 01 '22 11:09 lidord-wix

@lidord-wix thanks- if platformColor or dyanmicColorIOS is used then things should work at least on iOS

sallar avatar Sep 01 '22 11:09 sallar

hey @lidord-wix @sallar

after digging into the code and using the power of console.logs, I've found out that when toggling appearance in system mode, themeProps are not being updated for the views which are currently displayed (also for components on other tabs). And if you push a new screen, then themeProps will be depended on system appearance.

the problem is in useThemeProps() hook which is actually a pure function and doesn't have any deps. I have added useColorScheme() hook from react-native to the useThemeProps(), so themeProps will be updated for views with updated theme values and color scheme.

I'm not sure if it's the best solution for this case in terms of performance and etc., as I don't know the whole structure of the library. Maybe @ethanshar can comment on this, and if it's okay, here is PR #2234.

Patch file - rnn-starter/patches/react-native-ui-lib+6.20.3.patch. Working example - rnn-starter

kanzitelli avatar Sep 03 '22 13:09 kanzitelli

Thanks @kanzitelli! I was actually testing this library with rnn-starter haha. But your patch works flawlessly. Thank you! Just one thing, the default design tokens, eg, $textDefault still don't work without reloading, even with this change. But that doesn't matter. I'll try to not use any of them :(

sallar avatar Sep 04 '22 16:09 sallar

hey @sallar! Great to hear that! It would be great if you could elaborate more on default design tokens. I couldn't find any information about them in RN UI Lib docs.

And thanks for using the rnn-starter haha. I've caught it on the video and it seems like you are using old version 😁

kanzitelli avatar Sep 04 '22 16:09 kanzitelli

@kanzitelli I will use the new rnn-starter 👍 Design tokens are the colors that are used for light/dark mode by default in new versions of the library. For example Text component uses the colors from here: https://github.com/wix/react-native-ui-lib/blob/master/src/style/designTokens.ts

for example when you use <Text> the default color is used inside the component: <Text $textDefault>

sallar avatar Sep 05 '22 14:09 sallar

@sallar that's great! I've never used design tokens tbh, they seem nice to use. The only import of designTokens is here and seems to be loading schemes correctly. Probably it needs to reloaded somewhere but needs more digging. I'll see what I can to help with this.

kanzitelli avatar Sep 06 '22 21:09 kanzitelli

Seems to have been fixed as of 6.21.2 without any patch or extra configuration.

@sallar confirm?

dariuscosden avatar Sep 21 '22 15:09 dariuscosden

Switching theme for View also does not work for me. Package version: 6.23.1. Components Text and Button work correctly, but the view does not respond to theme switching

izversky avatar Nov 09 '22 09:11 izversky

Using Expo here. I did all of the suggested fixes however, even with an app reload, the colors still don't change at all when I change to dark mode on iOS. Any ideas? package: 6.25.0 expo: 47.0.6 react: 18.1.0 react-native: 0.70.5

DeveloperTheExplorer avatar Dec 07 '22 04:12 DeveloperTheExplorer