You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
App is changing from Light to Dark Theme after Webpack HMR #289
Describe the bug
I am trying to ensure the app remains in light mode. When I am debugging I set my phone settings to enable dark mode. I then debug the app and initially it appears in light mode however when webpack reloads on a change during development, it then appears to apply the dark theme.
To Reproduce
Turn dark mode on in the phone settings
ns debug android on my connected Pixel 5 (Android 11)
Change text in a component to trigger HMR
App reloads and appears in the dark theme
Expected behavior
Dark mode should remain disabled and the ns-dark theme should not apply.
Additional context
I have tried the following to ensure dark mode does not apply, none prevent the app from changing from light to dark on HMR.
Setting <item name="android:forceDarkAllowed">false</item> in styles.xml (values-v29)
Annotating class="ns-light" on <page-router-outlet>
Setting Theme.setMode(Theme.Light); manually on ngOnInit in the app component and main.ts
Adding the following snipplet to force it to disable night mode in main.ts
changed the title [-]App is applying Dark Mode after Webpack HMR[/-][+]App is changing from Light Mode to Dark Mode after Webpack HMR[/+]on Feb 17, 2021
changed the title [-]App is changing from Light Mode to Dark Mode after Webpack HMR[/-][+]App is changing from Light to Dark Theme after Webpack HMR[/+]on Feb 17, 2021
@Tyler-V Any news on this? We're having the same problem. I want to cut Dark Mode short and make sure the system setting won't override the app's theme.
@csimpi I have not found a solution for this, the only other option I can think of if it affects the actual production built app is to create a custom theme and override the dark theme that is applied by importing the nativescript prebuilt themes to use the same light theme.
After testing I can confirm this issue is isolated to Android and only during debugging after the first HMR, this issue thankfully does not exist in production on the built mobile app.
As a temporary solution, you can modify your App_Resources/Android/src/main/res/values/colors.xml and change the ns_primaryDark color to whatever your ns_primary color is.
@NathanaelA in the ticket additional context I've tried every possible option to disable dark mode completely which works when the application is built but resets as you've stated during HMR.
I think the only way to disable dark theme if using the nativescript core themes is to create a custom theme / mixin where the dark theme color values are set to the light theme values although I have not tested this.
This is strictly found in HMR on Android as you stated and does not impact production, just makes it a pain to continuously restart debugging if you are dealing with small text changes that may not show up against the dark theme based on your project.
@NathanaelA - I repeatedly opened the app, then closed or supended it, did something else on the phone, then opened the app again and once in a while, although rarely, it would open with the dark mode theme.
I am experiencing a similar problem when I have the app running on the device and I change the display mode setting to dark-mode (tested on an Samsung Galaxy 9+). The colours will change to dark-mode but Theme.getMode() will report ns-light. It seems that the the two are out of sync. The only way I was able to fix it was by adding the following on main.tns.ts:
If you remove setting the mode to Theme.Auto it will not work. Also notice that from starting the app and as you change dark-modes on the settings page while the app is running you will see the following sequence of values for Theme.getMode():
auto, ns-light, ns-light, ns-light, ns-light, ... no matter whether the app is rendering in dark-mode or not. Adding the Theme.setMode(Theme.Auto); seems to get things back in sync.
Activity
[-]App is applying Dark Mode after Webpack HMR[/-][+]App is changing from Light Mode to Dark Mode after Webpack HMR[/+][-]App is changing from Light Mode to Dark Mode after Webpack HMR[/-][+]App is changing from Light to Dark Theme after Webpack HMR[/+]csimpi commentedon Feb 28, 2021
@Tyler-V Any news on this? We're having the same problem. I want to cut Dark Mode short and make sure the system setting won't override the app's theme.
Tyler-V commentedon Feb 28, 2021
@csimpi I have not found a solution for this, the only other option I can think of if it affects the actual production built app is to create a custom theme and override the dark theme that is applied by importing the nativescript prebuilt themes to use the same light theme.
Tyler-V commentedon Mar 1, 2021
After testing I can confirm this issue is isolated to Android and only during debugging after the first HMR, this issue thankfully does not exist in production on the built mobile app.
As a temporary solution, you can modify your
App_Resources/Android/src/main/res/values/colors.xmland change thens_primaryDarkcolor to whatever yourns_primarycolor is.senner008 commentedon Mar 5, 2021
I have this happening seemingly at random with release builds also.
As indicated I have not found a reliable way to reproduce yet.
Phone : OnePlus 6T, Android 10
Dependecies:
NathanaelA commentedon Mar 5, 2021
Tyler-V commentedon Mar 5, 2021
@NathanaelA in the ticket additional context I've tried every possible option to disable dark mode completely which works when the application is built but resets as you've stated during HMR.
I think the only way to disable dark theme if using the nativescript core themes is to create a custom theme / mixin where the dark theme color values are set to the light theme values although I have not tested this.
This is strictly found in HMR on Android as you stated and does not impact production, just makes it a pain to continuously restart debugging if you are dealing with small text changes that may not show up against the dark theme based on your project.
NathanaelA commentedon Mar 5, 2021
senner007 commentedon Mar 5, 2021
It happens in production also.
main.tns.ts :
App_Resources\Android\values-v29\styles.xml :
App_Resources\Android\values\colors.xml :
NathanaelA commentedon Mar 5, 2021
senner007 commentedon Mar 5, 2021
No sure what you mean by too late.
The lauchEvent code runs in main.tns.ts. Is that too late?
I have this in my app.scss
if that what you are referring to?
And yes Im calling the forceLightTheme() in app.module.tns.ts
the node_modules/@nativescript/theme/package.json says :
"version": "2.3.4",
NathanaelA commentedon Mar 10, 2021
senner007 commentedon Mar 10, 2021
@NathanaelA - No and its difficult to get it to happen and unfortunately I don't have an Android 10 phone myself.
NathanaelA commentedon Mar 10, 2021
senner007 commentedon Mar 10, 2021
@NathanaelA - I repeatedly opened the app, then closed or supended it, did something else on the phone, then opened the app again and once in a while, although rarely, it would open with the dark mode theme.
jalberto-ghub commentedon Mar 26, 2021
I am experiencing a similar problem when I have the app running on the device and I change the display mode setting to dark-mode (tested on an Samsung Galaxy 9+). The colours will change to dark-mode but
Theme.getMode()will report ns-light. It seems that the the two are out of sync. The only way I was able to fix it was by adding the following on main.tns.ts:If you remove setting the mode to
Theme.Autoit will not work. Also notice that from starting the app and as you change dark-modes on the settings page while the app is running you will see the following sequence of values forTheme.getMode():auto, ns-light, ns-light, ns-light, ns-light, ...no matter whether the app is rendering in dark-mode or not. Adding theTheme.setMode(Theme.Auto);seems to get things back in sync.ishiharas commentedon Mar 22, 2022
I'm running:
"@nativescript/theme": "^3.0.2",
"@nativescript/angular": "^12.2.0",
"@nativescript/core": "^8.1.5",
I've tried all the solutions, that can be found regarding this topic.
@jalberto-ghub solution came close but wasn't consistent. Probably due the timeout. From time to time the dark mode would appear.
I also tried force-dark-allowed with "ns clean" and rebuilding everything.
I also tried setting the MODE_NIGHT_NO enum in onCreate/onStart in the android activity itself.
The only solution through the various threads, that worked consistently for me, was the following.
and called the method in RadSidedrawer's - loaded method.