@radioactive-labs/plutonium 0.4.10 → 0.31.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,40 +1,66 @@
1
- import { Controller } from "@hotwired/stimulus"
2
-
1
+ import { Controller } from "@hotwired/stimulus";
3
2
 
4
3
  // Connects to data-controller="color-mode"
5
4
  export default class extends Controller {
6
- // static targets = ["trigger", "menu"]
5
+ static values = { current: String };
7
6
 
8
7
  connect() {
9
- this.updateColorMode()
8
+ // Set initial mode from localStorage or default
9
+ const mode = localStorage.getItem('theme') || "light";
10
+ this.setMode(mode);
11
+
12
+ // Listen for cross-tab theme changes
13
+ 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
+ }
19
+ };
20
+ window.addEventListener('storage', this.handleStorageChange);
10
21
  }
11
22
 
12
23
  disconnect() {
24
+ // Clean up event listener
25
+ window.removeEventListener('storage', this.handleStorageChange);
13
26
  }
14
27
 
15
- updateColorMode() {
16
- if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
17
- document.documentElement.classList.add('dark')
28
+ toggleMode() {
29
+ const current = this.currentValue || "light";
30
+ const next = current === "light" ? "dark" : "light";
31
+ this.setMode(next);
32
+ }
33
+
34
+ setMode(mode) {
35
+ // Update html class
36
+ if (mode === "dark") {
37
+ document.documentElement.classList.add("dark");
18
38
  } else {
19
- document.documentElement.classList.remove('dark')
39
+ document.documentElement.classList.remove("dark");
20
40
  }
21
- }
22
41
 
23
- setLightColorMode() {
24
- // Whenever the user explicitly chooses light mode
25
- localStorage.theme = 'light'
26
- this.updateColorMode()
27
- }
42
+ // Update button state
43
+ this.currentValue = mode;
44
+
45
+ // Show/hide icons
46
+ this.toggleIcons(mode);
28
47
 
29
- setDarkColorMode() {
30
- // Whenever the user explicitly chooses dark mode
31
- localStorage.theme = 'dark'
32
- this.updateColorMode()
48
+ // Store in localStorage to trigger storage events in other tabs
49
+ localStorage.setItem('theme', mode);
33
50
  }
34
51
 
35
- setSystemColorMode() {
36
- // Whenever the user explicitly chooses to respect the OS preference
37
- localStorage.removeItem('theme')
38
- this.updateColorMode()
52
+ toggleIcons(mode) {
53
+ const sun = this.element.querySelector(".color-mode-icon-light");
54
+ const moon = this.element.querySelector(".color-mode-icon-dark");
55
+
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
+ }
64
+ }
39
65
  }
40
66
  }