2024-07-16 20:22:59 +02:00
|
|
|
import { Controller } from "@hotwired/stimulus"
|
|
|
|
|
import Cookie from "../lib/cookies"
|
|
|
|
|
|
|
|
|
|
// Connects to data-controller="theme-switcher"
|
|
|
|
|
export default class extends Controller {
|
|
|
|
|
COOKIE_NAME = "modeTheme"
|
|
|
|
|
|
|
|
|
|
connect() {
|
2024-11-01 13:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() {
|
|
|
|
|
const cookieValue = Cookie.get(this.COOKIE_NAME);
|
|
|
|
|
const newThemeValue = cookieValue == "dark" ? "dark" : "light";
|
|
|
|
|
|
|
|
|
|
Cookie.set("init modeTheme", newThemeValue);
|
|
|
|
|
window.document.getElementsByTagName("html")[0].setAttribute("data-bs-theme", newThemeValue);
|
|
|
|
|
|
|
|
|
|
const icon = this.element.getElementsByTagName("i")[0];
|
|
|
|
|
if (newThemeValue == "dark") {
|
|
|
|
|
icon.classList.remove("bi-sun-fill")
|
|
|
|
|
icon.classList.add("bi-moon-stars-fill")
|
|
|
|
|
} else {
|
|
|
|
|
icon.classList.add("bi-sun-fill")
|
|
|
|
|
icon.classList.remove("bi-moon-stars-fill")
|
|
|
|
|
}
|
2024-07-16 20:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch() {
|
|
|
|
|
const cookieValue = Cookie.get(this.COOKIE_NAME);
|
|
|
|
|
const newThemeValue = cookieValue == "dark" ? "light" : "dark";
|
2024-11-01 13:16:26 +01:00
|
|
|
|
2024-07-16 20:22:59 +02:00
|
|
|
Cookie.set("modeTheme", newThemeValue);
|
|
|
|
|
window.document.getElementsByTagName("html")[0].setAttribute("data-bs-theme", newThemeValue);
|
|
|
|
|
|
|
|
|
|
const icon = this.element.getElementsByTagName("i")[0];
|
2024-11-01 13:16:26 +01:00
|
|
|
if (newThemeValue == "dark") {
|
2024-07-16 20:22:59 +02:00
|
|
|
icon.classList.remove("bi-sun-fill")
|
|
|
|
|
icon.classList.add("bi-moon-stars-fill")
|
|
|
|
|
} else {
|
|
|
|
|
icon.classList.add("bi-sun-fill")
|
|
|
|
|
icon.classList.remove("bi-moon-stars-fill")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|