adwaita-web
Version:
A GTK inspired toolkit designed to build awesome web apps
49 lines (48 loc) • 1.61 kB
JavaScript
import mainStylesheet from "./adwaita.js";
import darkThemeVars from "./dark-color-variables.js";
import lightThemeVars from "./light-color-variables.js";
const currentStyles = {
theme: null,
stylesheet: null,
variables: null
};
class Adwaita {
static currentTheme() {
return currentStyles.theme;
}
static changeToLightTheme() {
if (currentStyles.theme === "light")
return;
if (currentStyles.variables) {
currentStyles.variables.textContent = lightThemeVars;
currentStyles.theme = "light";
}
}
static changeToDarkTheme() {
if (currentStyles.theme === "dark")
return;
if (currentStyles.variables) {
currentStyles.variables.textContent = darkThemeVars;
currentStyles.theme = "dark";
}
}
static getVariables() {
const varsStylesheet = currentStyles.theme === "light" ? lightThemeVars : darkThemeVars;
const varsList = varsStylesheet.replace(/^.*:root\s*{/, "").replace(/}\s*$/, "").trim().split(";").map((line) => line.trim());
const varsMap = new Map(varsList.map((line) => line.split(":")).filter((splitLine) => splitLine.length === 2));
return varsMap;
}
}
if (typeof document !== "undefined") {
const themeVariablesElem = document.createElement("style");
currentStyles.variables = themeVariablesElem;
document.head.appendChild(themeVariablesElem);
const stylesheetElem = document.createElement("style");
currentStyles.stylesheet = stylesheetElem;
document.head.appendChild(stylesheetElem);
stylesheetElem.textContent = mainStylesheet;
Adwaita.changeToLightTheme();
}
export {
Adwaita
};