grafana/public/app/core/services/toggleTheme.ts
Torkel Ödegaard b929822d72
Theme: Renames theme.palette to theme.colors (#33214)
* Theme: Rename theme.palette to theme.colors

* renaming files
2021-04-21 15:34:08 +02:00

54 lines
1.6 KiB
TypeScript

import { ThemeChangedEvent } from 'app/types/events';
import appEvents from '../app_events';
import { config } from '../config';
import { PreferencesService } from './PreferencesService';
import { contextSrv } from '../core';
import { createTheme } from '@grafana/data';
export async function toggleTheme(runtimeOnly: boolean) {
const currentTheme = config.theme;
const newTheme = createTheme({
colors: {
mode: currentTheme.isDark ? 'light' : 'dark',
},
});
appEvents.publish(new ThemeChangedEvent(newTheme));
if (runtimeOnly) {
return;
}
// Add css file for new theme
const newCssLink = document.createElement('link');
newCssLink.rel = 'stylesheet';
newCssLink.href = config.bootData.themePaths[newTheme.colors.mode];
document.body.appendChild(newCssLink);
// Remove old css file
const bodyLinks = document.getElementsByTagName('link');
for (let i = 0; i < bodyLinks.length; i++) {
const link = bodyLinks[i];
if (link.href && link.href.indexOf(`build/grafana.${currentTheme.type}`) > 0) {
// Remove existing link after a 500ms to allow new css to load to avoid flickering
// If we add new css at the same time we remove current one the page will be rendered without css
// As the new css file is loading
setTimeout(() => link.remove(), 500);
}
}
if (!contextSrv.isSignedIn) {
return;
}
// Persist new theme
const service = new PreferencesService('user');
const currentPref = await service.load();
await service.update({
...currentPref,
theme: newTheme.colors.mode,
});
}