saturn
- 187 commits
- 2 branches
- 0 tags
- updated
- CI 2/2
theme.js
1.5 KB · 43 lines
1// one palette per visit: it holds until the browser closes, and the next visit gets2// the next one. without js the css stays moss and the theme button stays hidden.3// a `theme` cookie on danieldaum.net carries the palette to code.danieldaum.net4// and back, so both sites show the same one. elsewhere (localhost, pages.dev)5// the cookie is host-only.6const themes = ["moss", "ink", "wine", "plum", "teal"]; // "flexoki"7const host = location.hostname;8const shared = host === "danieldaum.net" || host.endsWith(".danieldaum.net")9 ? "; domain=danieldaum.net; secure"10 : "";1112function after(theme) {13 return themes[(themes.indexOf(theme) + 1) % themes.length];14}1516function use(theme) {17 document.documentElement.dataset.theme = theme;18 try {19 document.cookie = `theme=${theme}; path=/; samesite=lax${shared}`;20 sessionStorage.setItem("theme", theme);21 localStorage.setItem("theme", theme);22 } catch {}23}2425// the cookie first (it may come from code.danieldaum.net), then this tab's26let current = "moss";27try {28 const cookie = /(?:^|;\s*)theme=([a-z]+)/.exec(document.cookie)?.[1];29 const visit = sessionStorage.getItem("theme");30 current = themes.includes(cookie)31 ? cookie32 : themes.includes(visit)33 ? visit34 : after(localStorage.getItem("theme"));35} catch {}36use(current);3738// footer button: next palette, kept for the rest of the visit39document.addEventListener("click", (event) => {40 if (!event.target.closest(".theme-button")) return;41 current = after(current);42 use(current);43});