-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add theme utilities to core-ui-settings-common and refactor theme loa…
…der to only load themes that are supposed to be bundled
- Loading branch information
Showing
13 changed files
with
137 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/core/ui-settings/core-ui-settings-common/src/theme.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
16 changes: 0 additions & 16 deletions
16
packages/core/ui-settings/core-ui-settings-common/src/theme_name.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.