To make managing the active theme easier, we provide a helper class to get, set and toggle the theme. Additionally,
you can provide callbacks for when the theme gets changed!
Using the ThemeHelper class, you can listen to theme changes! This is useful when you have logic that needs to run
whenever the color scheme changes, for example in a three.js canvas where you need to change an image
(*cough cough* our login page *cough*).
script.ts
1
import{
classThemeHelper
A helper to toggle, set and get the current StudioCMS UI theme.
Allows for adding a callback that gets called whenever the theme changes.
@param ― callback The callback to be executed
onThemeChange((
newTheme: Theme
newTheme)=>{
8
// Your logic here!
9
});
Here’s a live example: Change the theme via the option in the top-right corner of your screen. If you’re on mobile,
open the navbar and scroll all the way down. After you’ve changed the theme, check the text below:
One of the few things the ThemeHelper does not do is saving the users theme preference. This is by design, since we don’t want to force websites operating in the EU (and other GDPR-enforcing countries) to have to add a cookie notice just for a UI library. Instead, implementation of this functionality is up to the developers themselves.
As a starting point, here’s a barebones example of how to implement this:
Script Tag
1
import{
classThemeHelper
A helper to toggle, set and get the current StudioCMS UI theme.