theme-o-rama
Version:
A TypeScript library for dynamic theme management in react + shadcn + tailwind applications
39 lines (38 loc) • 983 B
JavaScript
import light from "./light.json" with { type: "json" };
export class ThemeCache {
constructor() {
this.themesCache = new Map();
}
getThemeSafe(themeName) {
let theme = this.themesCache.get(themeName || "light");
if (theme) {
return theme;
}
theme = this.themesCache.get("light");
if (theme) {
return theme;
}
if (this.themesCache.size > 0) {
return Array.from(this.themesCache.values())[0];
}
return light;
}
getTheme(name) {
return this.themesCache.get(name);
}
addTheme(theme) {
this.themesCache.set(theme.name, theme);
}
addThemes(themes) {
themes.forEach((theme) => this.addTheme(theme));
}
getThemes() {
return Array.from(this.themesCache.values());
}
removeTheme(name) {
this.themesCache.delete(name);
}
invalidate() {
this.themesCache.clear();
}
}