From f419ba5a13b2cdbd83ef433aa10241fe937f6e11 Mon Sep 17 00:00:00 2001 From: Bruno Canepa <8711973+bruncanepa@users.noreply.github.com> Date: Sun, 7 Dec 2025 20:07:06 -0300 Subject: [PATCH] refactor from PR feedback: update theme controller to use private methods for theme management --- .../controllers/theme_controller.js | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/app/javascript/controllers/theme_controller.js b/app/javascript/controllers/theme_controller.js index 150864622..26ab67817 100644 --- a/app/javascript/controllers/theme_controller.js +++ b/app/javascript/controllers/theme_controller.js @@ -4,34 +4,38 @@ export default class extends Controller { static targets = ["lightBtn", "darkBtn", "autoBtn"] connect() { - this.applyStoredTheme() - this.updateButtons() + this.#applyStoredTheme() + this.#updateButtons() } setLight() { - this.setTheme("light") + this.#setTheme("light") document.documentElement.setAttribute("data-theme", "light") - this.updateButtons() + this.#updateButtons() } setDark() { - this.setTheme("dark") + this.#setTheme("dark") document.documentElement.setAttribute("data-theme", "dark") - this.updateButtons() + this.#updateButtons() } setAuto() { - this.setTheme("auto") + this.#setTheme("auto") document.documentElement.removeAttribute("data-theme") - this.updateButtons() + this.#updateButtons() } - setTheme(theme) { + get #storedTheme() { + return localStorage.getItem("theme") || "auto" + } + + #setTheme(theme) { localStorage.setItem("theme", theme) } - applyStoredTheme() { - const storedTheme = localStorage.getItem("theme") || "auto" + #applyStoredTheme() { + const storedTheme = this.#storedTheme if (storedTheme === "light") { document.documentElement.setAttribute("data-theme", "light") @@ -43,8 +47,8 @@ export default class extends Controller { } } - updateButtons() { - const storedTheme = localStorage.getItem("theme") || "auto" + #updateButtons() { + const storedTheme = this.#storedTheme // Reset all buttons if (this.hasLightBtnTarget) this.lightBtnTarget.removeAttribute("aria-selected")