Repository navigation
Expand file tree
/
Copy paththeme-toggle.js
More file actions
65 lines (61 loc) · 3.17 KB
/
Copy paththeme-toggle.js
File metadata and controls
65 lines (61 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* theme-toggle.js — light/dark toggle, same pattern as grade6-singapore-math-cpa.
Loaded in <head> right after course.css so the theme applies before first
paint (no flash of the wrong theme). With no saved choice it follows the
system setting; a click saves an explicit choice. */
(function(){
var KEY="ai-literacy-theme";
var root=document.documentElement;
var saved=null;
try{ saved=localStorage.getItem(KEY); }catch(e){}
var mq=window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null;
function initial(){
if(saved==="dark"||saved==="light") return saved;
return mq && mq.matches ? "dark" : "light";
}
root.setAttribute("data-theme", initial());
// Motion follows the device's own reduce-motion setting (an accessibility setting for people whom
// movement makes unwell). There is no on-site switch (David, 2026-10-16); set before first paint.
var mqMotion=window.matchMedia ? window.matchMedia("(prefers-reduced-motion: reduce)") : null;
root.setAttribute("data-motion", mqMotion && mqMotion.matches ? "off" : "on");
function syncAll(){
var dark=root.getAttribute("data-theme")==="dark";
var btns=document.querySelectorAll("[data-theme-toggle]");
for(var i=0;i<btns.length;i++){
// The visible words are the accessible name, so it always names what a click will switch to.
btns[i].innerHTML = dark ? '<span aria-hidden="true">☀</span> Light mode' : '<span aria-hidden="true">🌙</span> Dark mode';
}
}
if(mq && mq.addEventListener){
mq.addEventListener("change", function(e){
if(saved) return;
root.setAttribute("data-theme", e.matches ? "dark" : "light");
syncAll();
});
}
if(mqMotion && mqMotion.addEventListener){
mqMotion.addEventListener("change", function(e){
root.setAttribute("data-motion", e.matches ? "off" : "on");
});
}
document.addEventListener("DOMContentLoaded", function(){
syncAll();
document.addEventListener("click", function(e){
if(!e.target.closest || !e.target.closest("[data-theme-toggle]")) return;
var next = root.getAttribute("data-theme")==="dark" ? "light" : "dark";
saved=next;
try{ localStorage.setItem(KEY, next); }catch(err){}
function apply(){ root.setAttribute("data-theme", next); syncAll(); }
// The new theme spreads in a circle from the button (a View Transition). With reduce motion on,
// it's a plain cross-fade instead: a fade doesn't travel (David, 2026-10-20, after Apple's
// guidelines). A browser without View Transitions changes it at once.
if(typeof document.startViewTransition!=="function"){ apply(); return; }
if(root.getAttribute("data-motion")==="off"){ document.startViewTransition(apply); return; }
var b = e.target.closest("[data-theme-toggle]").getBoundingClientRect();
root.style.setProperty("--vt-x", Math.round(b.left + b.width/2) + "px");
root.style.setProperty("--vt-y", Math.round(b.top + b.height/2) + "px");
root.classList.add("vt-theme");
var t = document.startViewTransition(apply);
t.finished.then(function(){ root.classList.remove("vt-theme"); }, function(){ root.classList.remove("vt-theme"); });
});
});
})();