|
|
@@ -0,0 +1,95 @@
|
|
|
+const color_modes = Object.freeze({
|
|
|
+ DARK: true,
|
|
|
+ LIGHT: false,
|
|
|
+})
|
|
|
+
|
|
|
+class color_mode {
|
|
|
+ #app;
|
|
|
+ #current;
|
|
|
+ #dark_class;
|
|
|
+ #light_class;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ app = null,
|
|
|
+ dark_class = "darkmode",
|
|
|
+ light_class = "lightmode",
|
|
|
+ defaults = color_modes.DARK
|
|
|
+ ) {
|
|
|
+ if (!(app instanceof HTMLElement)) {
|
|
|
+ throw new TypeError("App to color control must be HTMLElement.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (typeof(dark_class) !== "string") {
|
|
|
+ throw new TypeError("Name of darkmode class must be string.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (typeof(light_class) !== "string") {
|
|
|
+ throw new TypeError("Name of lightmode class must be String.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dark_class === light_class) {
|
|
|
+ throw new Error("Dark modeclass and lightmode class is same.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (defaults !== color_modes.DARK && defaults !== color_modes.LIGHT) {
|
|
|
+ throw new TypeError("Default color mode must be color modes.");
|
|
|
+ }
|
|
|
+
|
|
|
+ this.#current = defaults;
|
|
|
+ this.#app = app;
|
|
|
+ this.#dark_class = dark_class;
|
|
|
+ this.#light_class = light_class;
|
|
|
+ this.update();
|
|
|
+ }
|
|
|
+
|
|
|
+ get mode() {
|
|
|
+ return this.#current;
|
|
|
+ }
|
|
|
+
|
|
|
+ set mode(target) {
|
|
|
+ if (target !== color_modes.DARK && target !== color_modes.LIGHT) {
|
|
|
+ throw new TypeError("New color mode must be in color modes.");
|
|
|
+ }
|
|
|
+
|
|
|
+ this.#current = target;
|
|
|
+ update();
|
|
|
+ }
|
|
|
+
|
|
|
+ reverse(now = true) {
|
|
|
+ if (this.#current === color_modes.DARK) {
|
|
|
+ this.#current = color_modes.LIGHT
|
|
|
+ } else {
|
|
|
+ this.#current = color_modes.DARK;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (now) {
|
|
|
+ this.update();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ update() {
|
|
|
+ const app = this.#app;
|
|
|
+ const darkmode = this.#dark_class;
|
|
|
+ const lightmode = this.#light_class;
|
|
|
+ const current = this.#current;
|
|
|
+
|
|
|
+ if (app.classList.contains(darkmode)) {
|
|
|
+ app.classList.remove(darkmode);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (app.classList.contains(lightmode)) {
|
|
|
+ app.classList.remove(lightmode);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current === color_modes.DARK) {
|
|
|
+ app.classList.add(darkmode);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current === color_modes.LIGHT) {
|
|
|
+ app.classList.add(lightmode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export { color_mode };
|