Skip to content

Instantly share code, notes, and snippets.

@CruciformHawk7
Last active June 14, 2021 07:52
Show Gist options
  • Save CruciformHawk7/c43374f947e0cc3a5afc577fc40a07d4 to your computer and use it in GitHub Desktop.
Save CruciformHawk7/c43374f947e0cc3a5afc577fc40a07d4 to your computer and use it in GitHub Desktop.
Change Theme automatically in OpenUI5/SAPUI5

Copy the code from BaseController in this gist to BaseController in your project, then attach setMode to onInit() of your views, or the base view.

setMode: function() {
const colorScheme = window.matchMedia("(prefers-color-scheme: dark)");
this.determineColoringScheme(colorScheme);
colorScheme.addEventListener("change", scheme => this.determineColoringScheme(scheme));
},
determineColoringScheme: function(colorScheme) {
if (colorScheme.matches)
sap.ui.getCore().applyTheme("sap_fiori_3_dark");
else
sap.ui.getCore().applyTheme("sap_fiori_3");
}
@pubmikeb
Copy link

Copy the code from BaseController to BaseController

Is it correct or there is a typo? From one controller to the same controller?

@CruciformHawk7
Copy link
Author

Copy the code from BaseController to BaseController

Is it correct or there is a typo? From one controller to the same controller?

Whoops. I meant copy from BaseController in this gist to the BaseController in your module.

@pubmikeb
Copy link

Thanks, I'll try it!

@pubmikeb
Copy link

pubmikeb commented Jun 14, 2021

It works! But addListener is deprecated.

I would replace it with addEventListener:

colorScheme.addEventListener("scheme", (scheme) => this.determineColoringScheme(scheme));

@CruciformHawk7
Copy link
Author

CruciformHawk7 commented Jun 14, 2021

It works! But addListener is deprecated.

I would replace it with addEventListener:

colorScheme.addEventListener("scheme", (scheme) => this.determineColoringScheme(scheme));

Thank you for that. I probably forgot to update it here.
Edit: I've updated the gist, thank you so much!

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