@c8y/bootstrap
Version:
Bootstrap layer
45 lines • 1.59 kB
JavaScript
export const localStorageKeyForThemePreference = 'c8y-theme-preference';
export function getThemePreference() {
return window.localStorage.getItem(localStorageKeyForThemePreference);
}
export function setThemePreference(preference) {
return window.localStorage.setItem(localStorageKeyForThemePreference, preference);
}
let currentThemePreference = getThemePreference();
export function applyTheme(newThemePreference) {
if (newThemePreference !== currentThemePreference) {
setThemeClass(newThemePreference, currentThemePreference);
currentThemePreference = newThemePreference;
}
}
export function applyThemePreferenceAndListenForChanges() {
setThemeClass(currentThemePreference);
window.addEventListener('storage', () => {
const newThemePreference = getThemePreference();
applyTheme(newThemePreference);
});
}
function setThemeClass(themePreference, previousThemePreference) {
if (previousThemePreference?.length) {
document.body.classList.remove(`c8y-${previousThemePreference}-theme`);
}
if (!themePreference?.length) {
return;
}
document.body.classList.add(`c8y-${themePreference}-theme`);
}
/**
* Will revert back to the light theme if the dark theme is not available.
* @param options ApplicationOptions
*/
export function applyOptionsToTheming(options) {
if (!options) {
return;
}
if (!options.darkThemeAvailable) {
const lightTheme = 'light';
applyTheme(lightTheme);
setThemePreference(lightTheme);
}
}
//# sourceMappingURL=theming.js.map