@@ -8,14 +8,15 @@ import { contextSrv } from '../core';
8
8
import { PreferencesService } from './PreferencesService' ;
9
9
10
10
export async function toggleTheme ( runtimeOnly : boolean ) {
11
- const currentTheme = config . theme ;
11
+ const currentTheme = config . theme2 ;
12
12
const newTheme = createTheme ( {
13
13
colors : {
14
14
mode : currentTheme . isDark ? 'light' : 'dark' ,
15
15
} ,
16
16
} ) ;
17
17
18
18
appEvents . publish ( new ThemeChangedEvent ( newTheme ) ) ;
19
+ config . theme2 . isDark = newTheme . isDark ;
19
20
20
21
if ( runtimeOnly ) {
21
22
return ;
@@ -25,20 +26,21 @@ export async function toggleTheme(runtimeOnly: boolean) {
25
26
const newCssLink = document . createElement ( 'link' ) ;
26
27
newCssLink . rel = 'stylesheet' ;
27
28
newCssLink . href = config . bootData . themePaths [ newTheme . colors . mode ] ;
28
- document . body . appendChild ( newCssLink ) ;
29
-
30
- // Remove old css file
31
- const bodyLinks = document . getElementsByTagName ( 'link' ) ;
32
- for ( let i = 0 ; i < bodyLinks . length ; i ++ ) {
33
- const link = bodyLinks [ i ] ;
34
-
35
- if ( link . href && link . href . indexOf ( `build/grafana. ${ currentTheme . type } ` ) > 0 ) {
36
- // Remove existing link after a 500ms to allow new css to load to avoid flickering
37
- // If we add new css at the same time we remove current one the page will be rendered without css
38
- // As the new css file is loading
39
- setTimeout ( ( ) => link . remove ( ) , 500 ) ;
29
+ newCssLink . onload = ( ) => {
30
+ // Remove old css file
31
+ const bodyLinks = document . getElementsByTagName ( 'link' ) ;
32
+ for ( let i = 0 ; i < bodyLinks . length ; i ++ ) {
33
+ const link = bodyLinks [ i ] ;
34
+
35
+ if ( link . href && link . href . includes ( `build/grafana. ${ ! newTheme . isDark ? 'dark' : 'light' } ` ) ) {
36
+ // Remove existing link once the new css has loaded to avoid flickering
37
+ // If we add new css at the same time we remove current one the page will be rendered without css
38
+ // As the new css file is loading
39
+ link . remove ( ) ;
40
+ }
40
41
}
41
- }
42
+ } ;
43
+ document . body . appendChild ( newCssLink ) ;
42
44
43
45
if ( ! contextSrv . isSignedIn ) {
44
46
return ;
0 commit comments