Skip to content

Instantly share code, notes, and snippets.

@hirbod
Last active May 15, 2024 00:00
Show Gist options
  • Save hirbod/d6bb5d0fc946a12ba9e3cf01120c604a to your computer and use it in GitHub Desktop.
Save hirbod/d6bb5d0fc946a12ba9e3cf01120c604a to your computer and use it in GitHub Desktop.
Expo Config Plugin to disable forced Dark mode.
// disable forced dark mode to prevent weird color changes on
// certain android devices (Xiaomi MIUI and others enforcing dark mode with view analyzing)
// create a file like "plugins/withDisableForcedDarkModeAndroid.js". Insert this content and edit your app.config.js/app.json
/*
"expo": {
...
...
...
"plugins": [
...,
...,
'./plugins/withDisableForcedDarkModeAndroid.js'
]
*/
const {
createRunOncePlugin,
withAndroidStyles,
AndroidConfig
} = require('@expo/config-plugins');
function setForceDarkModeToFalse(styles) {
styles = AndroidConfig.Styles.assignStylesValue(styles, {
add: true,
parent: AndroidConfig.Styles.getAppThemeLightNoActionBarGroup(),
name: `android:forceDarkAllowed`,
value: "false",
});
return styles;
}
const withDisableForcedDarkModeAndroid = (config) => {
return withAndroidStyles(config, (config) => {
config.modResults = setForceDarkModeToFalse(config.modResults);
return config;
});
};
module.exports = createRunOncePlugin(withDisableForcedDarkModeAndroid, 'disable-forced-dark-mode', '1.0.0');
@top-kat
Copy link

top-kat commented Oct 1, 2022

Hi, thanks for such a piece of code
however it has not worked on my side

Here is my app.json
image

The exact syntax you put plugins: [['./plugins/withDisableForcedDarkModeAndroid.js']] was not working:

image

Did I miss something ?

@dayvid-s
Copy link

have to change something?

@top-kat
Copy link

top-kat commented Oct 14, 2022

For me it has never worked although some people recommend replacing value: "false",by value: false,. I have tried both with no success...

@dayvid-s
Copy link

while I don't have any definitive answer to this problem, I solved it in the configuration of my Xiaomi note 8, I disabled dark mode in individual applications

@hirbod
Copy link
Author

hirbod commented Oct 15, 2022

I have this running since Expo SDK 44 and its working fine for me.

@deywersonp
Copy link

Hey buddy, ty for the code! Solved the problem here :D

@deywersonp
Copy link

deywersonp commented Oct 28, 2022

For me it has never worked although some people recommend replacing value: "false",by value: false,. I have tried both with no success...

Hey friend! I have writted an answer on stackoverflow to help others and maybe will be useful for you too.
You can check the full solution for dark mode problem here -> https://stackoverflow.com/a/74239739/16539417

@cosimonegri
Copy link

@top-kat did you manage to solve the problem?

@top-kat
Copy link

top-kat commented Nov 18, 2022

@cosimonegri sorry I didn't, I gave up trying...

@Sorvereign
Copy link

Sorvereign commented Dec 11, 2022

@top-kat I think i found the solution:

image

The plugin key accepts arrays of arrays, you have to wrap the plugin on another array. Hope it helps!

@top-kat
Copy link

top-kat commented Dec 12, 2022

Thanks @Sorvereign, I'll try that 👍

@AnatuGreen
Copy link

For me it has never worked although some people recommend replacing value: "false",by value: false,. I have tried both with no success...

Hey friend! I have writted an answer on stackoverflow to help others and maybe will be useful for you too. You can check the full solution for dark mode problem here -> https://stackoverflow.com/a/74239739/16539417

Hi @deywersonp I have followed your steps in the stack overflow post but still does not work for me.

@mariomurrent-softwaresolutions

Thanks @Sorvereign, I'll try that 👍

Did it help?

@isaacbatst
Copy link

isaacbatst commented Jan 25, 2023

Unfortunatedly as commented here it does not work for Redmi Note 9 or Redmi 9T (Expo SDK 47) 😢

@lynxjr4
Copy link

lynxjr4 commented Jan 30, 2023

Thank you @Sorvereign, it helped me! -- SDK 45, phone Redmi note 8 (2021)
In my case, the path was like this:

"plugins": [
[
"./src/plugins/withDisableForcedDarkModeAndroid.js",
{}
]
]

and don't forget to install @expo/config-plugins

@hirbod
Copy link
Author

hirbod commented Mar 18, 2023

A simple

"./plugins/withDisableForcedDarkModeAndroid.js"

is enough. No need to put inside an array. Just tested today on a new project of mine.

@P0sseid0n
Copy link

P0sseid0n commented Apr 8, 2023

I have tried on the Poco F3 (SDK 47), but it does not work. This issue started before I updated to MIUI 14 - Android 13.

@chulanovskyi-bs
Copy link

Completely insane issue, makes me really mad that at the very start of the new project Expo forces you to eject to solve this 😡. Xiaomi Mi9T Pro, thanks for asking...

@hirbod
Copy link
Author

hirbod commented Apr 27, 2023

You don't need to eject, just use a custom dev-client!

@cecarmona
Copy link

This code works great! the perfect solution for my app. (using SDK 48)

@iSnakeBuzz
Copy link

it doesn't work for me :(

@HallexCosta
Copy link

HallexCosta commented Jul 1, 2023

I would like to remind you that plugin mods only run in the expo prebuild process, that is, for the changes to take effect it is necessary to generate an .APK (for testing) or .AAB (for playstore).

I don't know why this setting doesn't apply to the expo client, I believe it's possible to generate the .android folder with the npx expo prebuild command and run it on any android emulator (without expo client), that might solve the problem :)

Tested on Xiomi Redmi 9 it worked after building an .apk

References:
How to works plugins and mods
React Native Expo Android Dark Mode issue
Problem when trying to disable dark mode on Android
How to disable night mode in my application even if night mode is enable in android 9.0 (pie)?

@danbeo95
Copy link

danbeo95 commented Jan 8, 2024

Awesome
It work for me

@artkrv
Copy link

artkrv commented May 2, 2024

Worked for me too.

@willpower18
Copy link

Worked here, thanks for this.

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