
The Appearence class manages the theme of anything that can have an appearance.
Storage interface. The storage system is provided as a parameter to the constructor, and if not provided, it defaults to MemoryStorage.DEFAULT_APPEARENCE_STORAGE_KEY.true if the theme is ‘dark’. If the theme is ‘system’, it checks the system theme. If the system theme is dark, it returns true. In all other cases, it returns false.true if the theme is ‘light’. If the theme is ‘system’, it checks the system theme. If the system theme is light, it returns true. In all other cases, it returns false.true if the theme is ‘system’, and false otherwise.The initialize method retrieves the theme from the storage and sets it.
import { Appearence } from '@aracna/core';
const appearence = new Appearence();
async () => {
await appearence.initialize();
console.log('will log "system"', appearence.theme);
appearence.setTheme('dark');
await appearence.store();
appearence.setTheme('light');
await appearence.initialize();
// will log "dark"
console.log(appearence.theme);
};The setTheme method sets the theme and emits a ‘change-theme’ event.
import { Appearence } from '@aracna/core';
const appearence = new Appearence();
// will log "system"
console.log(appearence.theme);
appearence.setTheme('dark');
// will log "dark"
console.log(appearence.theme);The toggleTheme method toggles the theme between dark and light. If the theme is set to system, it will be set to dark or light depending on the system theme.
import { Appearence } from '@aracna/core';
const appearence = new Appearence();
// will log "system"
console.log(appearence.theme);
appearence.toggleTheme();
// will log "dark" or "light" depending on the opposite of your system theme
console.log(appearence.theme);The store method stores the theme in the storage.
import { Appearence, MemoryStorage } from '@aracna/core';
const appearence = new Appearence();
(async () => {
appearence.setTheme('dark');
await appearence.store();
// will log { theme: "dark" }
console.log(MemoryStorage.get('appearence'));
})();The Appearence class emits a ‘change-theme’ event when the theme changes. You can listen to this event and do something when the theme changes.
import { Appearence } from '@aracna/core';
const appearence = new Appearence();
appearence.on('change-theme', (theme) => {
// will log "dark"
console.log(theme);
});
appearence.setTheme('dark');