2024-04-25 00:13:09 -07:00
|
|
|
import { AppTheme } from 'wasm-lib/kcl/bindings/AppTheme'
|
|
|
|
|
2023-08-28 20:31:49 -04:00
|
|
|
export enum Themes {
|
|
|
|
Light = 'light',
|
|
|
|
Dark = 'dark',
|
|
|
|
System = 'system',
|
|
|
|
}
|
|
|
|
|
2024-04-25 00:13:09 -07:00
|
|
|
export function appThemeToTheme(
|
|
|
|
theme: AppTheme | undefined
|
|
|
|
): Themes | undefined {
|
|
|
|
switch (theme) {
|
|
|
|
case 'light':
|
|
|
|
return Themes.Light
|
|
|
|
case 'dark':
|
|
|
|
return Themes.Dark
|
|
|
|
case 'system':
|
|
|
|
return Themes.System
|
|
|
|
default:
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 09:34:13 -04:00
|
|
|
// Get the theme from the system settings manually
|
2023-08-28 20:31:49 -04:00
|
|
|
export function getSystemTheme(): Exclude<Themes, 'system'> {
|
2024-03-22 09:35:07 -04:00
|
|
|
return typeof globalThis.window !== 'undefined' &&
|
|
|
|
'matchMedia' in globalThis.window
|
2023-08-31 09:34:13 -04:00
|
|
|
? window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
|
|
? Themes.Dark
|
|
|
|
: Themes.Light
|
2023-08-28 20:31:49 -04:00
|
|
|
: Themes.Light
|
|
|
|
}
|
|
|
|
|
2023-08-31 09:34:13 -04:00
|
|
|
// Set the theme class on the body element
|
2023-08-28 20:31:49 -04:00
|
|
|
export function setThemeClass(theme: Themes) {
|
2023-08-31 09:34:13 -04:00
|
|
|
if (theme === Themes.Dark) {
|
2023-08-28 20:31:49 -04:00
|
|
|
document.body.classList.add('dark')
|
|
|
|
} else {
|
|
|
|
document.body.classList.remove('dark')
|
|
|
|
}
|
|
|
|
}
|
2024-03-22 09:35:07 -04:00
|
|
|
|
2024-04-24 13:59:25 -07:00
|
|
|
// Returns the resolved theme in use (Dark || Light)
|
|
|
|
export function getResolvedTheme(theme: Themes) {
|
|
|
|
return theme === Themes.System ? getSystemTheme() : theme
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the opposing theme
|
|
|
|
export function getOppositeTheme(theme: Themes) {
|
|
|
|
const resolvedTheme = getResolvedTheme(theme)
|
|
|
|
return resolvedTheme === Themes.Dark ? Themes.Light : Themes.Dark
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:58:32 -04:00
|
|
|
/**
|
|
|
|
* The engine takes RGBA values from 0-1
|
|
|
|
* So we convert from the conventional 0-255 found in Figma
|
|
|
|
* @param theme
|
|
|
|
* @returns { r: number, g: number, b: number, a: number }
|
|
|
|
*/
|
2024-03-22 09:35:07 -04:00
|
|
|
export function getThemeColorForEngine(theme: Themes) {
|
2024-04-24 13:59:25 -07:00
|
|
|
const resolvedTheme = getResolvedTheme(theme)
|
2024-03-22 09:35:07 -04:00
|
|
|
const dark = 28 / 255
|
2024-04-15 13:40:09 -04:00
|
|
|
const light = 249 / 255
|
2024-03-22 09:35:07 -04:00
|
|
|
return resolvedTheme === Themes.Dark
|
|
|
|
? { r: dark, g: dark, b: dark, a: 1 }
|
|
|
|
: { r: light, g: light, b: light, a: 1 }
|
|
|
|
}
|