@radioactive-labs/plutonium 0.48.0 → 0.49.0

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,66 +1,73 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  // Connects to data-controller="color-mode"
4
+ //
5
+ // Shared theme state across the app. localStorage key 'theme' holds one of:
6
+ // 'auto' — follow prefers-color-scheme (default when unset)
7
+ // 'light' — force light
8
+ // 'dark' — force dark
9
+ const ORDER = ['auto', 'light', 'dark'];
10
+
4
11
  export default class extends Controller {
5
12
  static values = { current: String };
6
13
 
7
14
  connect() {
8
- // Set initial mode from localStorage or default
9
- const mode = localStorage.getItem('theme') || "light";
10
- this.setMode(mode);
15
+ this.applyMode(this.readMode());
11
16
 
12
- // Listen for cross-tab theme changes
13
17
  this.handleStorageChange = (e) => {
14
- console.log('Storage event received in color-mode controller:', e.key, e.newValue, e.oldValue)
15
- if (e.key === 'theme' && e.newValue) {
16
- console.log('Updating color-mode theme to:', e.newValue)
17
- this.setMode(e.newValue);
18
- }
18
+ if (e.key === 'theme') this.applyMode(this.readMode());
19
19
  };
20
20
  window.addEventListener('storage', this.handleStorageChange);
21
+
22
+ this.mq = window.matchMedia('(prefers-color-scheme: dark)');
23
+ this.handleMqChange = () => {
24
+ if (this.readMode() === 'auto') this.applyMode('auto');
25
+ };
26
+ this.mq.addEventListener('change', this.handleMqChange);
21
27
  }
22
28
 
23
29
  disconnect() {
24
- // Clean up event listener
25
30
  window.removeEventListener('storage', this.handleStorageChange);
31
+ if (this.mq) this.mq.removeEventListener('change', this.handleMqChange);
26
32
  }
27
33
 
28
34
  toggleMode() {
29
- const current = this.currentValue || "light";
30
- const next = current === "light" ? "dark" : "light";
35
+ const current = this.readMode();
36
+ const next = ORDER[(ORDER.indexOf(current) + 1) % ORDER.length];
31
37
  this.setMode(next);
32
38
  }
33
39
 
34
40
  setMode(mode) {
35
- // Update html class
36
- if (mode === "dark") {
37
- document.documentElement.classList.add("dark");
38
- } else {
39
- document.documentElement.classList.remove("dark");
40
- }
41
+ localStorage.setItem('theme', mode);
42
+ this.applyMode(mode);
43
+ }
41
44
 
42
- // Update button state
45
+ applyMode(mode) {
46
+ const effective = this.effectiveMode(mode);
47
+ document.documentElement.classList.toggle('dark', effective === 'dark');
43
48
  this.currentValue = mode;
44
-
45
- // Show/hide icons
46
49
  this.toggleIcons(mode);
50
+ }
47
51
 
48
- // Store in localStorage to trigger storage events in other tabs
49
- localStorage.setItem('theme', mode);
52
+ readMode() {
53
+ const saved = localStorage.getItem('theme');
54
+ return ORDER.includes(saved) ? saved : 'auto';
50
55
  }
51
56
 
52
- toggleIcons(mode) {
53
- const sun = this.element.querySelector(".color-mode-icon-light");
54
- const moon = this.element.querySelector(".color-mode-icon-dark");
57
+ effectiveMode(mode) {
58
+ if (mode === 'light' || mode === 'dark') return mode;
59
+ return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
60
+ }
55
61
 
56
- if (sun && moon) {
57
- if (mode === "light") {
58
- sun.classList.remove("hidden");
59
- moon.classList.add("hidden");
60
- } else {
61
- sun.classList.add("hidden");
62
- moon.classList.remove("hidden");
63
- }
62
+ toggleIcons(mode) {
63
+ const icons = {
64
+ auto: this.element.querySelector(".color-mode-icon-auto"),
65
+ light: this.element.querySelector(".color-mode-icon-light"),
66
+ dark: this.element.querySelector(".color-mode-icon-dark"),
67
+ };
68
+ for (const [key, el] of Object.entries(icons)) {
69
+ if (!el) continue;
70
+ el.classList.toggle("hidden", key !== mode);
64
71
  }
65
72
  }
66
73
  }