2024-10-24 10:24:11 +02:00
|
|
|
import { ThemeOptions, ThemeClassPrefix } from "./constants/themes"
|
|
|
|
import { Theme } from "@budibase/types"
|
2024-03-26 11:43:56 +01:00
|
|
|
|
2024-10-24 10:07:36 +02:00
|
|
|
// Gets the CSS class names for the specified theme
|
2024-10-24 10:24:11 +02:00
|
|
|
export const getThemeClassNames = (theme: Theme): string => {
|
2024-10-24 10:07:36 +02:00
|
|
|
theme = ensureValidTheme(theme)
|
|
|
|
let classNames = `${ThemeClassPrefix}${theme}`
|
|
|
|
|
|
|
|
// Prefix with base class if required
|
|
|
|
const base = ThemeOptions.find(x => x.id === theme)?.base
|
|
|
|
if (base) {
|
|
|
|
classNames = `${ThemeClassPrefix}${base} ${classNames}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return classNames
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures a theme value is a valid option
|
2024-10-24 10:24:11 +02:00
|
|
|
export const ensureValidTheme = (
|
2024-10-24 11:22:43 +02:00
|
|
|
theme?: Theme,
|
2024-10-24 10:24:11 +02:00
|
|
|
fallback: Theme = Theme.DARKEST
|
|
|
|
): Theme => {
|
2024-03-26 11:43:56 +01:00
|
|
|
if (!theme) {
|
2024-10-24 10:07:36 +02:00
|
|
|
return fallback
|
2024-03-26 11:43:56 +01:00
|
|
|
}
|
2024-10-24 10:07:36 +02:00
|
|
|
|
|
|
|
// Ensure we aren't using the spectrum prefix
|
|
|
|
if (theme.startsWith(ThemeClassPrefix)) {
|
2024-10-24 10:24:11 +02:00
|
|
|
theme = theme.split(ThemeClassPrefix)[1] as Theme
|
2024-10-24 10:07:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check we aren't using a deprecated theme, and migrate
|
|
|
|
// to the nearest valid theme if we are
|
|
|
|
if (!ThemeOptions.some(x => x.id === theme)) {
|
2024-10-24 10:24:11 +02:00
|
|
|
if (theme === Theme.LIGHTEST) {
|
|
|
|
return Theme.LIGHT
|
|
|
|
} else if (theme === Theme.DARK) {
|
|
|
|
return Theme.DARKEST
|
2024-10-24 10:07:36 +02:00
|
|
|
} else {
|
|
|
|
return fallback
|
|
|
|
}
|
2024-03-26 11:43:56 +01:00
|
|
|
}
|
2024-10-24 10:07:36 +02:00
|
|
|
return theme
|
2024-03-26 11:43:56 +01:00
|
|
|
}
|