Skip to content

Commit

Permalink
[8.x] Respect system dark mode in ESQL editor (#200233) (#200602)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [Respect system dark mode in ESQL editor
(#200233)](#200233)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nathan L
Smith","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-18T15:49:10Z","message":"Respect
system dark mode in ESQL editor (#200233)\n\nWhen
`uiSettings.overrides.theme:darkMode: true` is set, the ESQL
editor\r\nuses dark mode.\r\n\r\nWhen
`uiSettings.overrides.theme:darkMode: system` is set, the ESQL\r\neditor
uses light mode, while the rest of the UI uses dark mode.\r\n\r\nUpdate
the ESQL theme creation to create both light and dark variations\r\nof
the theme at startup and use the theme in React component
to\r\ndetermine which ESQL theme to use at
runtime.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>\r\nCo-authored-by:
Stratoula Kalafateli
<[email protected]>","sha":"f48ded9686505d63fe40427c26d6f2b22e462fa0","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","v9.0.0","Feature:ES|QL","Team:ESQL","backport:version","v8.17.0"],"title":"Respect
system dark mode in ESQL
editor","number":200233,"url":"https://github.com/elastic/kibana/pull/200233","mergeCommit":{"message":"Respect
system dark mode in ESQL editor (#200233)\n\nWhen
`uiSettings.overrides.theme:darkMode: true` is set, the ESQL
editor\r\nuses dark mode.\r\n\r\nWhen
`uiSettings.overrides.theme:darkMode: system` is set, the ESQL\r\neditor
uses light mode, while the rest of the UI uses dark mode.\r\n\r\nUpdate
the ESQL theme creation to create both light and dark variations\r\nof
the theme at startup and use the theme in React component
to\r\ndetermine which ESQL theme to use at
runtime.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>\r\nCo-authored-by:
Stratoula Kalafateli
<[email protected]>","sha":"f48ded9686505d63fe40427c26d6f2b22e462fa0"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/200233","number":200233,"mergeCommit":{"message":"Respect
system dark mode in ESQL editor (#200233)\n\nWhen
`uiSettings.overrides.theme:darkMode: true` is set, the ESQL
editor\r\nuses dark mode.\r\n\r\nWhen
`uiSettings.overrides.theme:darkMode: system` is set, the ESQL\r\neditor
uses light mode, while the rest of the UI uses dark mode.\r\n\r\nUpdate
the ESQL theme creation to create both light and dark variations\r\nof
the theme at startup and use the theme in React component
to\r\ndetermine which ESQL theme to use at
runtime.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>\r\nCo-authored-by:
Stratoula Kalafateli
<[email protected]>","sha":"f48ded9686505d63fe40427c26d6f2b22e462fa0"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nathan L Smith <[email protected]>
  • Loading branch information
kibanamachine and smith authored Nov 18, 2024
1 parent 85c830d commit cdd8d06
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 172 deletions.
7 changes: 2 additions & 5 deletions packages/kbn-esql-editor/src/esql_editor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { ESQLEditor } from './esql_editor';
import type { ESQLEditorProps } from './types';
import { ReactWrapper } from 'enzyme';
import { of } from 'rxjs';
import { coreMock } from '@kbn/core/server/mocks';

describe('ESQLEditor', () => {
const uiConfig: Record<string, any> = {};
const uiSettings = {
get: (key: string) => uiConfig[key],
} as IUiSettingsClient;
const theme = {
theme$: of({ darkMode: false }),
};

const services = {
uiSettings,
settings: {
client: uiSettings,
},
theme,
core: coreMock.createStart(),
};

function renderESQLEditorComponent(testProps: ESQLEditorProps) {
Expand Down
14 changes: 11 additions & 3 deletions packages/kbn-esql-editor/src/esql_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
import type { AggregateQuery } from '@kbn/es-query';
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { ESQLLang, ESQL_LANG_ID, ESQL_THEME_ID, monaco, type ESQLCallbacks } from '@kbn/monaco';
import {
ESQLLang,
ESQL_LANG_ID,
ESQL_DARK_THEME_ID,
ESQL_LIGHT_THEME_ID,
monaco,
type ESQLCallbacks,
} from '@kbn/monaco';
import memoize from 'lodash/memoize';
import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
Expand Down Expand Up @@ -91,7 +98,8 @@ export const ESQLEditor = memo(function ESQLEditor({
fieldsMetadata,
uiSettings,
} = kibana.services;
const timeZone = core?.uiSettings?.get('dateFormat:tz');
const darkMode = core.theme?.getTheme().darkMode;
const timeZone = uiSettings?.get('dateFormat:tz');
const histogramBarTarget = uiSettings?.get('histogram:barTarget') ?? 50;
const [code, setCode] = useState<string>(query.esql ?? '');
// To make server side errors less "sticky", register the state of the code when submitting
Expand Down Expand Up @@ -597,7 +605,7 @@ export const ESQLEditor = memo(function ESQLEditor({
vertical: 'auto',
},
scrollBeyondLastLine: false,
theme: ESQL_THEME_ID,
theme: darkMode ? ESQL_DARK_THEME_ID : ESQL_LIGHT_THEME_ID,
wordWrap: 'on',
wrappingIndent: 'none',
};
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-monaco/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export {
} from './src/monaco_imports';
export { XJsonLang } from './src/xjson';
export { SQLLang } from './src/sql';
export { ESQL_LANG_ID, ESQL_THEME_ID, ESQLLang } from './src/esql';
export { ESQL_LANG_ID, ESQL_DARK_THEME_ID, ESQL_LIGHT_THEME_ID, ESQLLang } from './src/esql';
export type { ESQLCallbacks } from '@kbn/esql-validation-autocomplete';

export * from './src/painless';
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-monaco/src/esql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export { ESQL_LANG_ID, ESQL_THEME_ID } from './lib/constants';
export { ESQL_LANG_ID, ESQL_DARK_THEME_ID, ESQL_LIGHT_THEME_ID } from './lib/constants';
export { ESQLLang } from './language';
export { buildESQlTheme } from './lib/esql_theme';
export { buildESQLTheme } from './lib/esql_theme';
3 changes: 2 additions & 1 deletion packages/kbn-monaco/src/esql/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

export const ESQL_LANG_ID = 'esql';
export const ESQL_THEME_ID = 'esqlTheme';
export const ESQL_LIGHT_THEME_ID = 'esqlThemeLight';
export const ESQL_DARK_THEME_ID = 'esqlThemeDark';

export const ESQL_TOKEN_POSTFIX = '.esql';
8 changes: 4 additions & 4 deletions packages/kbn-monaco/src/esql/lib/esql_theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import { ESQLErrorListener, getLexer as _getLexer } from '@kbn/esql-ast';
import { ESQL_TOKEN_POSTFIX } from './constants';
import { buildESQlTheme } from './esql_theme';
import { buildESQLTheme } from './esql_theme';
import { CharStreams } from 'antlr4';

describe('ESQL Theme', () => {
it('should not have multiple rules for a single token', () => {
const theme = buildESQlTheme();
const theme = buildESQLTheme({ darkMode: false });

const seen = new Set<string>();
const duplicates: string[] = [];
Expand All @@ -40,7 +40,7 @@ describe('ESQL Theme', () => {
.map((name) => name!.toLowerCase());

it('every rule should apply to a valid lexical name', () => {
const theme = buildESQlTheme();
const theme = buildESQLTheme({ darkMode: false });

// These names aren't from the lexer... they are added on our side
// see packages/kbn-monaco/src/esql/lib/esql_token_helpers.ts
Expand All @@ -62,7 +62,7 @@ describe('ESQL Theme', () => {
});

it('every valid lexical name should have a corresponding rule', () => {
const theme = buildESQlTheme();
const theme = buildESQLTheme({ darkMode: false });
const tokenIDs = theme.rules.map((rule) => rule.token.replace(ESQL_TOKEN_POSTFIX, ''));

const validExceptions = [
Expand Down
Loading

0 comments on commit cdd8d06

Please sign in to comment.