Hi,
I have been having issues with Dark mode - I have a couple of apps that have been just fine, until users upgraded Android last week, and complained that the apps were opening in dark mode. I had the line as above in my variables.scss file, as per the docs.
I went through the ionic docs, and stopped using the
@media (prefers-color-scheme: dark)
instead switching to using body.dark and using the manual method of enabling dark mode: Dark Mode | Ionic Documentation
I now have code in app.component.ts which detects whether the system has dark mode set…and I enable it manually IF the user setting is not set to ‘Force off’
Trouble is, I find that this code doesn’t work - meaning the system can be in dark mode, but seemingly never adds the @media (prefers-color-scheme: dark) to (presumably) the webview.
Where I have left it is:
In variables.scss:
body.dark {
...
as per docs
In app.component.ts:
let darkMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
toggleDarkTheme(darkMediaQuery.matches);
try {
// Chrome & Firefox
darkMediaQuery.addEventListener('change', (e) => {
toggleDarkTheme(darkMediaQuery.matches);
});
} catch (e1) {
try {
// Safari
darkMediaQuery.addListener((e) => {
toggleDarkTheme(darkMediaQuery.matches);
});
} catch (e2) {
console.error(e2);
}
}
// Add or remove the "dark" class based on if the media query matches
function toggleDarkTheme(shouldAdd) {
if (!this.env.global.darkmode || this.env.global.darkmode == 'System settings')
{
document.body.classList.toggle('dark', shouldAdd);
}
}
This may run when the env.global.darkmode isn’t set…but I don’t care so much. Then, on my homepage init, I do:
this.env.global.darkmode = this.settingsDoc.doc.darkmode?this.settingsDoc.doc.darkmode:'System settings';
console.log(this.env.global.darkmode);
switch(this.env.global.darkmode)
{
case 'Force on':
document.body.classList.toggle('dark', true);
break;
case 'Force off':
document.body.classList.toggle('dark', false);
break;
default:
let darkMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
document.body.classList.toggle('dark', darkMediaQuery.matches);
break;
}
and then, lastly on my settings page I run the same code, based on the (change) event of the user settings,so they see the change straight away…The setting is a dropdown/ion-select with options [‘System settings’, ‘Force on’, ‘Force off’]
This means I think I have correct usage covered, but when the phone is put into dark mode, the app doesn’t pick it up…but I have another test phone that seems to work ok, so there seems to be inconsistencies in
- Android versions?
- Phone manufacturer UIs?
- ionic versions?
Some or all… but…at least I can tell the users that they “have control”, and its not the app that is broken.
Unless someone can tell me any better way of doing all this…!
Regards,
Steve
p.s.
An update - when testing all this with a debug build, the system settings were not adhered to, - i.e. if the phone was set to darkmode, the App did not detect dark mode…
Have just build a prod version, and now, when set to System Settings, the app detects that it is ‘in dark mode’ whether dark mode is set, or not…so there IS a problem somewhere, which has come to light with the Android update, but at least I can tell users to set to “force off” in the app settings…