@radioactive-labs/plutonium 0.4.10 → 0.4.11

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,50 @@
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.theme || "light";
10
+ this.setMode(mode);
10
11
  }
11
12
 
12
- disconnect() {
13
+ toggleMode() {
14
+ const current = this.currentValue || "light";
15
+ const next = current === "light" ? "dark" : "light";
16
+ this.setMode(next);
13
17
  }
14
18
 
15
- updateColorMode() {
16
- if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
17
- document.documentElement.classList.add('dark')
19
+ setMode(mode) {
20
+ // Update html class
21
+ if (mode === "dark") {
22
+ document.documentElement.classList.add("dark");
23
+ localStorage.theme = "dark";
18
24
  } else {
19
- document.documentElement.classList.remove('dark')
25
+ document.documentElement.classList.remove("dark");
26
+ localStorage.theme = "light";
20
27
  }
21
- }
22
28
 
23
- setLightColorMode() {
24
- // Whenever the user explicitly chooses light mode
25
- localStorage.theme = 'light'
26
- this.updateColorMode()
27
- }
29
+ // Update button state
30
+ this.currentValue = mode;
28
31
 
29
- setDarkColorMode() {
30
- // Whenever the user explicitly chooses dark mode
31
- localStorage.theme = 'dark'
32
- this.updateColorMode()
32
+ // Show/hide icons
33
+ this.toggleIcons(mode);
33
34
  }
34
35
 
35
- setSystemColorMode() {
36
- // Whenever the user explicitly chooses to respect the OS preference
37
- localStorage.removeItem('theme')
38
- this.updateColorMode()
36
+ toggleIcons(mode) {
37
+ const sun = this.element.querySelector(".color-mode-icon-light");
38
+ const moon = this.element.querySelector(".color-mode-icon-dark");
39
+
40
+ if (sun && moon) {
41
+ if (mode === "light") {
42
+ sun.classList.remove("hidden");
43
+ moon.classList.add("hidden");
44
+ } else {
45
+ sun.classList.add("hidden");
46
+ moon.classList.remove("hidden");
47
+ }
48
+ }
39
49
  }
40
50
  }