You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte-kit-vice/src/arts/prefs/dimensions/theme.ts

50 lines
1.5 KiB

import type { PrefsDimension } from '$libs/prefs';
import {
resolveTheme,
THEMES_INTENT,
type ThemeEffective,
type ThemeIntent
} from '$libs/theme';
export interface ThemeDimensionOptions {
/**
* Allowed intent values. Defaults to the full `THEMES_INTENT`
* (`'light' | 'dark' | 'system'`). Apps that don't want users to
* follow the OS pass `['light', 'dark']`.
*/
readonly intents?: readonly ThemeIntent[];
/**
* Final fallback applied when neither intent nor `env.colorScheme`
* provide a value. Must be a `ThemeEffective`.
*/
readonly default?: ThemeEffective;
}
/**
* @deprecated UIX visual mode belongs to `ActiveEidos`, not to the standard
* prefs preset. Keep this only for legacy/custom app schemas outside UIX.
*
* `TIntent` includes `'system'`, `TEffective` does not: `resolve` folds
* `'system'` into a concrete `'light' | 'dark'` using `env.colorScheme`.
*/
export function themeDimension(
options: ThemeDimensionOptions = {}
): PrefsDimension<ThemeIntent, ThemeEffective> {
const intents = options.intents ?? THEMES_INTENT;
const fallback = options.default ?? 'light';
return {
defaultValue: fallback,
validate(value) {
if (typeof value !== 'string') return { ok: false, reason: 'unsupported_theme' };
if (!intents.includes(value as ThemeIntent)) {
return { ok: false, reason: 'unsupported_theme' };
}
return { ok: true, value: value as ThemeIntent };
},
resolve(intent, env) {
return resolveTheme(intent, env.colorScheme, fallback);
},
catalog: () => intents
};
}

Powered by TurnKey Linux.