@@ -5,74 +5,6 @@ import type { EuiThemeColorMode } from '@elastic/eui';
5
5
import type { Maybe } from '../../common/types.js' ;
6
6
import { LocalStorage } from './local-storage.js' ;
7
7
8
- /**
9
- * Find all the theme links on the page that we could switch between.
10
- * See `_document.tsx` for how these are added to the page.
11
- */
12
- const getAllThemeLinks = ( ) : Array < HTMLLinkElement > => {
13
- return [
14
- ...document . querySelectorAll < HTMLLinkElement > (
15
- 'link[data-name="eui-theme"]'
16
- ) ,
17
- ] ;
18
- } ;
19
-
20
- /**
21
- * Find the theme link on the page that matches the given theme name.
22
- */
23
- const getThemeLink = ( themeName : EuiThemeColorMode ) : Maybe < HTMLLinkElement > => {
24
- return getAllThemeLinks ( ) . find ( ( themeLink ) => {
25
- return themeLink . dataset . theme === themeName ;
26
- } ) ;
27
- } ;
28
-
29
- /**
30
- * Sets the user's theme preference and actively updates the UI to match.
31
- * To only set the preference without updating the UI, use `setThemeName`.
32
- */
33
- export const enableTheme = ( newThemeName : EuiThemeColorMode ) : void => {
34
- const oldThemeName = getThemeName ( ) ;
35
- setThemeName ( newThemeName ) ;
36
-
37
- const newThemeLink = getThemeLink ( newThemeName ) ;
38
-
39
- if ( ! newThemeLink ) {
40
- return ;
41
- }
42
-
43
- // When toggling the theme, to prevent a flash of unstyled content
44
- // then preload the new theme's CSS before enabling it.
45
- const preloadThemeLink = document . createElement ( 'link' ) ;
46
- preloadThemeLink . rel = 'preload' ;
47
- preloadThemeLink . as = 'style' ;
48
- preloadThemeLink . href = newThemeLink . href ;
49
- document . head . appendChild ( preloadThemeLink ) ;
50
-
51
- // Once the new theme is loaded then we can disable the old theme.
52
- // Because the new theme is preloaded, the change should be instant.
53
- preloadThemeLink . onload = ( ) => {
54
- for ( const themeLink of getAllThemeLinks ( ) ) {
55
- // Disable all theme links, except for the desired theme, which we enable
56
- themeLink . disabled = themeLink . dataset . theme !== newThemeName ;
57
- themeLink . ariaDisabled = String ( themeLink . dataset . theme !== newThemeName ) ;
58
- }
59
-
60
- // Add a class to the `body` element that indicates which theme we're using.
61
- // This allows any custom styling to adapt to the current theme.
62
- if ( document . body . classList . contains ( `appTheme-${ oldThemeName } ` ) ) {
63
- document . body . classList . replace (
64
- `appTheme-${ oldThemeName } ` ,
65
- `appTheme-${ newThemeName } `
66
- ) ;
67
- } else {
68
- document . body . classList . add ( `appTheme-${ newThemeName } ` ) ;
69
- }
70
-
71
- // Remove the preload link element.
72
- document . head . removeChild ( preloadThemeLink ) ;
73
- } ;
74
- } ;
75
-
76
8
/**
77
9
* Gets the user's theme preference.
78
10
*/
@@ -83,7 +15,6 @@ export const getThemeName = (): EuiThemeColorMode => {
83
15
/**
84
16
* Sets the user's theme preference.
85
17
* Does not actively change the UI look and feel.
86
- * To do that, use `enableTheme` or reload the app.
87
18
*/
88
19
export const setThemeName = ( themeName : EuiThemeColorMode ) : void => {
89
20
setStoredThemeName ( themeName ) ;
0 commit comments