@quaffui/quaff 1.0.0-beta12 → 1.0.0-beta13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ type DisplayMode = "light" | "dark";
1
2
  declare class Quaff {
2
3
  version: string;
3
4
  private static readonly breakpointList;
@@ -15,6 +16,8 @@ declare class Quaff {
15
16
  router: import("@sveltejs/kit").Page<Record<string, string>, string | null>;
16
17
  protected dark: boolean;
17
18
  init(): void;
19
+ protected getCurrentDisplayMode(): DisplayMode;
20
+ protected applyDisplayMode(displayMode: DisplayMode, persist?: boolean): void;
18
21
  protected toggleDarkMode(): void;
19
22
  protected setDarkMode(newVal: boolean): void;
20
23
  get darkMode(): {
@@ -2,6 +2,7 @@ import { onMount } from "svelte";
2
2
  import { innerWidth } from "svelte/reactivity/window";
3
3
  import { version } from "../helpers";
4
4
  import { page } from "$app/state";
5
+ const DISPLAY_MODE_STORAGE_KEY = "displayMode";
5
6
  class Quaff {
6
7
  version = version;
7
8
  static breakpointList = {
@@ -53,28 +54,31 @@ class Quaff {
53
54
  dark = $state(false);
54
55
  init() {
55
56
  onMount(() => {
56
- const currentMode = localStorage.getItem("current_mode") ||
57
- (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
58
- if (currentMode === "dark") {
59
- this.dark = true;
60
- }
61
- else {
62
- this.dark = false;
63
- }
57
+ this.applyDisplayMode(this.getCurrentDisplayMode(), false);
64
58
  });
65
59
  }
60
+ getCurrentDisplayMode() {
61
+ const savedDisplayMode = localStorage.getItem(DISPLAY_MODE_STORAGE_KEY);
62
+ if (savedDisplayMode === "dark" || savedDisplayMode === "light") {
63
+ return savedDisplayMode;
64
+ }
65
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
66
+ }
67
+ applyDisplayMode(displayMode, persist = true) {
68
+ this.dark = displayMode === "dark";
69
+ if (typeof document !== "undefined") {
70
+ document.body.classList.remove("body--light", "body--dark");
71
+ document.body.classList.add(`body--${displayMode}`);
72
+ }
73
+ if (persist && typeof localStorage !== "undefined") {
74
+ localStorage.setItem(DISPLAY_MODE_STORAGE_KEY, displayMode);
75
+ }
76
+ }
66
77
  toggleDarkMode() {
67
- this.dark = !this.dark;
68
- const mode = {
69
- from: this.dark ? "light" : "dark",
70
- to: this.dark ? "dark" : "light",
71
- };
72
- const body = document.querySelector("body");
73
- body?.classList.replace(`body--${mode.from}`, `body--${mode.to}`);
74
- localStorage.setItem("current_mode", mode.to);
78
+ this.applyDisplayMode(this.dark ? "light" : "dark");
75
79
  }
76
80
  setDarkMode(newVal) {
77
- this.dark = newVal;
81
+ this.applyDisplayMode(newVal ? "dark" : "light");
78
82
  }
79
83
  get darkMode() {
80
84
  // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -1,2 +1,2 @@
1
- declare const _default: "1.0.0-beta12";
1
+ declare const _default: "1.0.0-beta13";
2
2
  export default _default;
@@ -1 +1 @@
1
- export default "1.0.0-beta12";
1
+ export default "1.0.0-beta13";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quaffui/quaff",
3
- "version": "1.0.0-beta12",
3
+ "version": "1.0.0-beta13",
4
4
  "scripts": {
5
5
  "dev": "tsc -p plugins/tsconfig.json && vite dev",
6
6
  "open": "tsc -p plugins/tsconfig.json && vite dev --open",