Skip to content

Commit

Permalink
add theme utilities to core-ui-settings-common and refactor theme loa…
Browse files Browse the repository at this point in the history
…der to only load themes that are supposed to be bundled
  • Loading branch information
tkajtoch committed Nov 13, 2024
1 parent 6e85833 commit e385ad2
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 204 deletions.
14 changes: 13 additions & 1 deletion packages/core/ui-settings/core-ui-settings-common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ export type {
GetUiSettingsContext,
} from './src/ui_settings';
export { type DarkModeValue, parseDarkModeValue } from './src/dark_mode';
export { parseThemeNameValue } from './src/theme_name';
export {
DEFAULT_THEME_TAGS,
SUPPORTED_THEME_TAGS,
DEFAULT_THEME_NAME,
SUPPORTED_THEME_NAMES,
FALLBACK_THEME_TAG,
parseThemeTags,
hasNonDefaultThemeTags,
parseThemeNameValue,
type ThemeName,
type ThemeTag,
type ThemeTags,
} from './src/theme';

export { TIMEZONE_OPTIONS } from './src/timezones';
97 changes: 97 additions & 0 deletions packages/core/ui-settings/core-ui-settings-common/src/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export const DEFAULT_THEME_NAME = 'amsterdam';
export const SUPPORTED_THEME_NAMES = ['amsterdam', 'borealis'];

export type ThemeName = typeof SUPPORTED_THEME_NAMES[number];

/**
* Theme tags of the Amsterdam theme
*/
export const ThemeAmsterdamTags = ['v8light', 'v8dark'] as const;

/**
* Theme tags of the experimental Borealis theme
*/
export const ThemeBorealisTags = ['borealislight', 'borealisdark'] as const;

/**
* An array of all theme tags supported by Kibana. Note that this list doesn't
* reflect what theme tags are available in a Kibana build.
*/
export const SUPPORTED_THEME_TAGS = [...ThemeAmsterdamTags, ...ThemeBorealisTags] as const;

export type ThemeTag = (typeof SUPPORTED_THEME_TAGS)[number];
export type ThemeTags = readonly ThemeTag[];

/**
* An array of theme tags available in Kibana by default when not customized
* using KBN_OPTIMIZER_THEMES environment variable.
*/
export const DEFAULT_THEME_TAGS: ThemeTags = ThemeAmsterdamTags;

export const FALLBACK_THEME_TAG: ThemeTag = 'v8light';

const isValidTag = (tag: unknown) =>
SUPPORTED_THEME_TAGS.includes(tag as (typeof SUPPORTED_THEME_TAGS)[number]);

export function parseThemeTags(input?: unknown): ThemeTags {
if (!input) {
return DEFAULT_THEME_TAGS;
}

if (input === '*') {
// TODO: Replace with SUPPORTED_THEME_TAGS when Borealis is in public beta
return DEFAULT_THEME_TAGS;
}

let rawTags: string[];
if (typeof input === 'string') {
rawTags = input.split(',').map((tag) => tag.trim());
} else if (Array.isArray(input)) {
rawTags = input;
} else {
throw new Error('Invalid theme tags, must be an array of strings');
}

if (!rawTags.length) {
throw new Error(
`Invalid theme tags, you must specify at least one of [${SUPPORTED_THEME_TAGS.join(', ')}]`
);
}

const invalidTags = rawTags.filter((t) => !isValidTag(t));
if (invalidTags.length) {
throw new Error(
`Invalid theme tags [${invalidTags.join(', ')}], options: [${SUPPORTED_THEME_TAGS.join(
', '
)}]`
);
}

return rawTags as ThemeTags;
}

export const hasNonDefaultThemeTags = (tags: ThemeTags) =>
tags.length !== DEFAULT_THEME_TAGS.length ||
tags.some((tag) => !DEFAULT_THEME_TAGS.includes(tag as (typeof DEFAULT_THEME_TAGS)[number]));

export const parseThemeNameValue = (value: unknown): ThemeName => {
if (typeof value !== 'string') {
return DEFAULT_THEME_NAME;
}

const themeName = value.toLowerCase();
if (SUPPORTED_THEME_NAMES.includes(themeName.toLowerCase() as ThemeName)) {
return themeName as ThemeName;
}

return DEFAULT_THEME_NAME;
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import type { ThemeVersion } from '@kbn/ui-shared-deps-npm';
import type { UiSettingsParams } from '@kbn/core-ui-settings-common';

function parseThemeTags() {
if (!process.env.KBN_OPTIMIZER_THEMES || process.env.KBN_OPTIMIZER_THEMES === '*') {
return ['v8light', 'v8dark'];
}

return process.env.KBN_OPTIMIZER_THEMES.split(',').map((t) => t.trim());
}
import { type UiSettingsParams, parseThemeTags, SUPPORTED_THEME_NAMES } from '@kbn/core-ui-settings-common';

function getThemeInfo(options: GetThemeSettingsOptions) {
if (options?.isDist ?? true) {
Expand All @@ -27,7 +19,7 @@ function getThemeInfo(options: GetThemeSettingsOptions) {
};
}

const themeTags = parseThemeTags();
const themeTags = parseThemeTags(process.env.KBN_OPTIMIZER_THEMES);
return {
defaultDarkMode: themeTags[0].endsWith('dark'),
};
Expand Down Expand Up @@ -98,11 +90,14 @@ export const getThemeSettings = (
defaultMessage: 'Theme',
}),
type: 'select',
options: ['amsterdam'],
options: SUPPORTED_THEME_NAMES,
optionLabels: {
amsterdam: i18n.translate('core.ui_settings.params.themeName.options.amsterdam', {
defaultMessage: 'Amsterdam',
}),
borealis: i18n.translate('core.ui_settings.params.themeName.options.borealis', {
defaultMessage: 'Borealis',
}),
},
value: 'amsterdam',
readonly: Object.hasOwn(options, 'isThemeSwitcherEnabled')
Expand All @@ -111,6 +106,7 @@ export const getThemeSettings = (
requiresPageReload: true,
schema: schema.oneOf([
schema.literal('amsterdam'),
schema.literal('borealis'),
// Allow experimental themes
schema.string(),
]),
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-optimizer/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export * from './rxjs_helpers';
export * from './array_helpers';
export * from './event_stream_helpers';
export * from './parse_path';
export * from './theme_tags';
export * from './obj_helpers';
export * from './hashes';
export * from './dll_manifest';
79 changes: 0 additions & 79 deletions packages/kbn-optimizer/src/common/theme_tags.test.ts

This file was deleted.

55 changes: 0 additions & 55 deletions packages/kbn-optimizer/src/common/theme_tags.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/kbn-optimizer/src/common/worker_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Path from 'path';

import { UnknownVals } from './ts_helpers';
import { ThemeTags, parseThemeTags } from './theme_tags';
import { ThemeTags, parseThemeTags } from '@kbn/core-ui-settings-common';

export interface WorkerConfig {
readonly repoRoot: string;
Expand Down
7 changes: 4 additions & 3 deletions packages/kbn-optimizer/src/log_optimizer_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import { inspect } from 'util';

import { ToolingLog } from '@kbn/tooling-log';
import { hasNonDefaultThemeTags } from '@kbn/core-ui-settings-common';
import { tap } from 'rxjs';

import { OptimizerConfig } from './optimizer';
import { OptimizerUpdate$ } from './run_optimizer';
import { CompilerMsg, pipeClosure, ALL_THEMES } from './common';
import { CompilerMsg, pipeClosure } from './common';

export function logOptimizerState(log: ToolingLog, config: OptimizerConfig) {
return pipeClosure((update$: OptimizerUpdate$) => {
Expand Down Expand Up @@ -80,9 +81,9 @@ export function logOptimizerState(log: ToolingLog, config: OptimizerConfig) {
);
}

if (config.themeTags.length !== ALL_THEMES.length) {
if (hasNonDefaultThemeTags(config.themeTags)) {
log.warning(
`only building [${config.themeTags}] themes, customize with the KBN_OPTIMIZER_THEMES environment variable`
`running with non-default [${config.themeTags}] set of themes, customize with the KBN_OPTIMIZER_THEMES environment variable`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/

jest.mock('@kbn/repo-packages');
jest.mock('@kbn/core-ui-settings-common');
jest.mock('./assign_bundles_to_workers');
jest.mock('./kibana_platform_plugins');
jest.mock('./get_plugin_bundles');
jest.mock('../common/theme_tags');
jest.mock('./filter_by_id');
jest.mock('./focus_bundles');
jest.mock('../limits');
Expand All @@ -29,7 +29,7 @@ import { REPO_ROOT } from '@kbn/repo-info';
import { createAbsolutePathSerializer } from '@kbn/jest-serializers';

import { OptimizerConfig, ParsedOptions } from './optimizer_config';
import { parseThemeTags } from '../common';
import { parseThemeTags } from '@kbn/core-ui-settings-common';

expect.addSnapshotSerializer(createAbsolutePathSerializer());

Expand Down
Loading

0 comments on commit e385ad2

Please sign in to comment.