@radioactive-labs/plutonium 0.48.0 → 0.49.1

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
  }
@@ -54,7 +54,30 @@ export default class extends Controller {
54
54
  }
55
55
 
56
56
  if (this.modal) {
57
+ // Inside a <dialog> opened via showModal(), the dialog establishes its
58
+ // own containing block in the top layer. flatpickr's default positioning
59
+ // computes document coordinates but the calendar (appended to the
60
+ // dialog) interprets them relative to the dialog's box, placing the
61
+ // calendar far from the input. Append to the modal and reposition
62
+ // manually relative to the modal's bounding rect.
57
63
  options.appendTo = this.modal;
64
+ options.position = (instance) => {
65
+ const input = instance.altInput || instance.input;
66
+ const inputRect = input.getBoundingClientRect();
67
+ const modalRect = this.modal.getBoundingClientRect();
68
+ const cal = instance.calendarContainer;
69
+ const calHeight = cal.offsetHeight;
70
+ const spaceBelow = window.innerHeight - inputRect.bottom;
71
+ const showAbove = spaceBelow < calHeight && inputRect.top > calHeight;
72
+ const top = showAbove
73
+ ? inputRect.top - modalRect.top - calHeight - 2
74
+ : inputRect.bottom - modalRect.top + 2;
75
+ cal.style.top = `${top}px`;
76
+ cal.style.left = `${inputRect.left - modalRect.left}px`;
77
+ cal.style.right = "auto";
78
+ cal.classList.toggle("arrowTop", !showAbove);
79
+ cal.classList.toggle("arrowBottom", showAbove);
80
+ };
58
81
  }
59
82
 
60
83
  return options;
@@ -1,3 +1,30 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
 
3
- export default class extends Controller {}
3
+ // Persists across controller reconnects so the value saved on
4
+ // turbo:before-render is still available on turbo:render, even though
5
+ // the <aside> hosting this controller is replaced during navigation.
6
+ let savedScrollTop = 0;
7
+
8
+ export default class extends Controller {
9
+ static targets = ["scroll"];
10
+
11
+ connect() {
12
+ this.beforeRender = this.beforeRender.bind(this);
13
+ this.afterRender = this.afterRender.bind(this);
14
+ document.addEventListener("turbo:before-render", this.beforeRender);
15
+ document.addEventListener("turbo:render", this.afterRender);
16
+ }
17
+
18
+ disconnect() {
19
+ document.removeEventListener("turbo:before-render", this.beforeRender);
20
+ document.removeEventListener("turbo:render", this.afterRender);
21
+ }
22
+
23
+ beforeRender() {
24
+ if (this.hasScrollTarget) savedScrollTop = this.scrollTarget.scrollTop;
25
+ }
26
+
27
+ afterRender() {
28
+ if (this.hasScrollTarget) this.scrollTarget.scrollTop = savedScrollTop;
29
+ }
30
+ }