Basic setup, launch config and layout

This commit is contained in:
David Schärer 2024-07-15 14:31:54 +02:00
parent 535a051755
commit 1a3d172fe5
41 changed files with 382 additions and 105 deletions

View file

@ -6,3 +6,6 @@ import { application } from "./application"
import HelloController from "./hello_controller"
application.register("hello", HelloController)
import SetThemeController from "./set_theme_controller"
application.register("set-theme", SetThemeController)

View file

@ -0,0 +1,18 @@
import { Controller } from "@hotwired/stimulus"
import Cookie from "../lib/cookies"
// Connects to data-controller="set-theme"
export default class extends Controller {
COOKIE_NAME = "modeTheme";
connect() {
const cookieValue = Cookie.get(this.COOKIE_NAME);
console.log("cookieValue", cookieValue);
if(cookieValue) {
return true;
}
const darkMode = window.matchMedia("(prefers-color-scheme:dark)").matches ? "dark" : "light";
Cookie.set(this.COOKIE_NAME, darkMode);
window.document.getElementsByTagName("html")[0].setAttribute("data-bs-theme", darkMode)
}
}

View file

@ -0,0 +1,23 @@
export default class Cookies {
static set(cname, cvalue, exdays = 365) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
static get(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
}