Skip to content

Commit c5eee26

Browse files
authored
[ML] Apply theme based on the User Profile settings (#158258)
## Summary With the release of Per User Dark Mode, code should no longer rely on calling `uiSettings` to determine which theme Kibana is displayed with. With theme settings now configurable from User Profiles and Adv. Settings, the code that was calling uiSettings to determine the Kibana theme will not take into account which theme is currently being displayed. Applies an appropriate EUI theme based on the profile settings. In particular for the following components: - Anomaly swim lane (Fixes #158155 ) - Job tree map view (Fixes #158304) - Charts-related theme settings, e.g. the Single Metric Viewer
1 parent d14ed5f commit c5eee26

File tree

36 files changed

+251
-154
lines changed

36 files changed

+251
-154
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ x-pack/packages/ml/date_picker @elastic/ml-ui
465465
x-pack/packages/ml/error_utils @elastic/ml-ui
466466
x-pack/packages/ml/is_defined @elastic/ml-ui
467467
x-pack/packages/ml/is_populated_object @elastic/ml-ui
468+
x-pack/packages/ml/kibana_theme @elastic/ml-ui
468469
x-pack/packages/ml/local_storage @elastic/ml-ui
469470
x-pack/packages/ml/nested_property @elastic/ml-ui
470471
x-pack/packages/ml/number_utils @elastic/ml-ui

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@
480480
"@kbn/ml-error-utils": "link:x-pack/packages/ml/error_utils",
481481
"@kbn/ml-is-defined": "link:x-pack/packages/ml/is_defined",
482482
"@kbn/ml-is-populated-object": "link:x-pack/packages/ml/is_populated_object",
483+
"@kbn/ml-kibana-theme": "link:x-pack/packages/ml/kibana_theme",
483484
"@kbn/ml-local-storage": "link:x-pack/packages/ml/local_storage",
484485
"@kbn/ml-nested-property": "link:x-pack/packages/ml/nested_property",
485486
"@kbn/ml-number-utils": "link:x-pack/packages/ml/number_utils",

tsconfig.base.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,8 @@
924924
"@kbn/ml-is-defined/*": ["x-pack/packages/ml/is_defined/*"],
925925
"@kbn/ml-is-populated-object": ["x-pack/packages/ml/is_populated_object"],
926926
"@kbn/ml-is-populated-object/*": ["x-pack/packages/ml/is_populated_object/*"],
927+
"@kbn/ml-kibana-theme": ["x-pack/packages/ml/kibana_theme"],
928+
"@kbn/ml-kibana-theme/*": ["x-pack/packages/ml/kibana_theme/*"],
927929
"@kbn/ml-local-storage": ["x-pack/packages/ml/local_storage"],
928930
"@kbn/ml-local-storage/*": ["x-pack/packages/ml/local_storage/*"],
929931
"@kbn/ml-nested-property": ["x-pack/packages/ml/nested_property"],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @kbn/ml-kibana-theme
2+
3+
Provides hooks to retrieve currently applied theme and EUI theme variables.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
export { useIsDarkTheme, useCurrentEuiThemeVars, type EuiThemeType } from './src/hooks';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
module.exports = {
9+
preset: '@kbn/test/jest_node',
10+
rootDir: '../../../..',
11+
roots: ['<rootDir>/x-pack/packages/ml/kibana_theme'],
12+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "shared-common",
3+
"id": "@kbn/ml-kibana-theme",
4+
"owner": "@elastic/ml-ui"
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@kbn/ml-kibana-theme",
3+
"private": true,
4+
"version": "1.0.0",
5+
"license": "Elastic License 2.0"
6+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { useMemo } from 'react';
9+
import { of } from 'rxjs';
10+
import useObservable from 'react-use/lib/useObservable';
11+
import { euiDarkVars as euiThemeDark, euiLightVars as euiThemeLight } from '@kbn/ui-theme';
12+
import { ThemeServiceStart } from '@kbn/core-theme-browser';
13+
14+
export type EuiThemeType = typeof euiThemeLight | typeof euiThemeDark;
15+
16+
const themeDefault = { darkMode: false };
17+
18+
/**
19+
* Indicates if the currently applied theme is either dark or light.
20+
* @return {boolean} - Returns true if the currently applied theme is dark.
21+
*/
22+
export function useIsDarkTheme(theme: ThemeServiceStart): boolean {
23+
const themeObservable$ = useMemo(() => {
24+
return theme?.theme$ ?? of(themeDefault);
25+
}, [theme]);
26+
27+
const { darkMode } = useObservable(themeObservable$, themeDefault);
28+
29+
return darkMode;
30+
}
31+
32+
/**
33+
* Returns an EUI theme definition based on the currently applied theme.
34+
*/
35+
export function useCurrentEuiThemeVars(theme: ThemeServiceStart): { euiTheme: EuiThemeType } {
36+
const isDarkMode = useIsDarkTheme(theme);
37+
return useMemo(() => ({ euiTheme: isDarkMode ? euiThemeDark : euiThemeLight }), [isDarkMode]);
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": "../../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "target/types",
5+
"types": [
6+
"jest",
7+
"node"
8+
]
9+
},
10+
"include": [
11+
"**/*.ts",
12+
],
13+
"exclude": [
14+
"target/**/*"
15+
],
16+
"kbn_references": [
17+
"@kbn/ui-theme",
18+
"@kbn/core-theme-browser",
19+
]
20+
}

0 commit comments

Comments
 (0)