// ─────────────────────────────────────────────────────────────────────────────
// Skill Drill: element symbols and names
//
// A flashcard drill over the 118 elements. Shares no code with
// _skill-drill-nomenclature.qmd by design: that widget carries real domain logic
// (formula parsing, naming rules, 18 rule-links) while this one is a flat lookup, so
// the only genuine overlap is the flashcard chrome. Borrowing its LOOK rather than its
// selectors keeps the two independent -- reusing `.flashcard-widget` and `.fc-*` would
// collide if both ever appeared on one page. If a third flashcard drill turns up, the
// shared CSS is the thing worth lifting into assets/, not the logic.
//
// Data is the same file the classification widget uses, so symbol, name and the
// classification shown on the answer card can never drift between the two.
// ─────────────────────────────────────────────────────────────────────────────
sdeElements = await FileAttachment("/files/json/periodic-classification.json").json()Skill Drill: Element Symbols
Interactive drill for element symbols and names. Work symbol to name, name to symbol, or both, over the common 74 elements or all 118.
Recall each element’s name from its symbol, or its symbol from its name.
mutable sdeDark = document.documentElement.getAttribute('data-bs-theme') === 'dark'
|| document.body.classList.contains('quarto-dark')
sdeThemeObserver = {
const check = () => {
const dark = document.documentElement.getAttribute('data-bs-theme') === 'dark'
|| document.body.classList.contains('quarto-dark');
if (dark !== mutable sdeDark) mutable sdeDark = dark;
};
const obs = new MutationObserver(check);
obs.observe(document.documentElement, {attributes: true, attributeFilter: ['data-bs-theme']});
obs.observe(document.body, {attributes: true, attributeFilter: ['class']});
invalidation.then(() => obs.disconnect());
return true;
}sdeLabels = ({
type: {metal: "Metal", metalloid: "Metalloid", nonmetal: "Nonmetal"},
family: {
"alkali": "Alkali metal", "alkaline-earth": "Alkaline earth metal",
"transition": "Transition metal", "post-transition": "Post-transition metal",
"lanthanide": "Lanthanide", "actinide": "Actinide", "pnictogen": "Pnictogen",
"chalcogen": "Chalcogen", "halogen": "Halogen", "noble-gas": "Noble gas",
"other": "No named family"
},
block: {s: "s-block", p: "p-block", d: "d-block", f: "f-block"}
})sdeWidget = {
const EL = sdeElements;
// ── state ──
let mode = "symbol_to_name"; // symbol_to_name | name_to_symbol | mixed
let scope = "common"; // common | all
let deck = [];
let idx = 0;
let revealed = false;
let screen = "setup"; // setup | study | summary
let fullscreen = false;
const root = document.createElement("div");
root.className = "sde-widget";
// ── chrome button (floats over the card) ──
const fsBtn = document.createElement("button");
fsBtn.type = "button";
fsBtn.className = "sde-chrome-btn sde-fs-btn";
fsBtn.title = "Toggle fullscreen";
fsBtn.setAttribute("aria-label", "Toggle fullscreen");
const body = document.createElement("div");
body.className = "sde-body";
root.append(fsBtn, body);
// ── deck ──
const scopeOf = () => scope === "common" ? EL.filter(e => e.common) : EL.slice();
function buildDeck() {
const pool = scopeOf();
deck = pool.map(e => ({
el: e,
dir: mode === "mixed" ? (Math.random() < 0.5 ? "s2n" : "n2s")
: (mode === "symbol_to_name" ? "s2n" : "n2s")
}));
for (let i = deck.length - 1; i > 0; i--) { // Fisher-Yates
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
idx = 0; revealed = false;
}
// ── setup screen ──
const MODES = [
["symbol_to_name", "Symbol → Name", "See the symbol, give the element name"],
["name_to_symbol", "Name → Symbol", "See the element name, give the symbol"],
["mixed", "Mixed", "Both directions, shuffled together"]
];
const SCOPES = [
["common", "Common", "74 elements: Z = 1–56, 72–88, and 92"],
["all", "All 118", "Every element, including the full f-block"]
];
function renderSetup() {
body.textContent = "";
const wrap = document.createElement("div");
wrap.className = "sde-setup";
const mk = (title, opts, current, onPick, name) => {
const sec = document.createElement("section");
sec.className = "sde-section";
const h = document.createElement("h3");
h.className = "sde-h";
h.textContent = title;
const grid = document.createElement("div");
grid.className = "sde-optgrid";
grid.setAttribute("role", "group");
grid.setAttribute("aria-label", title);
for (const [key, label, desc] of opts) {
const b = document.createElement("button");
b.type = "button";
b.className = "sde-opt" + (current === key ? " sde-opt-on" : "");
b.setAttribute("aria-pressed", current === key ? "true" : "false");
const t = document.createElement("span");
t.className = "sde-opt-title";
t.textContent = label;
const d = document.createElement("span");
d.className = "sde-opt-desc";
d.textContent = desc;
b.append(t, d);
b.addEventListener("click", () => { onPick(key); renderSetup(); });
grid.appendChild(b);
}
sec.append(h, grid);
return sec;
};
wrap.append(
mk("Study mode", MODES, mode, k => mode = k),
mk("Which elements", SCOPES, scope, k => scope = k)
);
const count = document.createElement("p");
count.className = "sde-count";
count.textContent = scopeOf().length + " cards in this deck.";
const go = document.createElement("button");
go.type = "button";
go.className = "sde-btn sde-btn-primary";
go.textContent = "Start studying";
go.addEventListener("click", () => { buildDeck(); screen = "study"; render(); });
wrap.append(count, go);
body.appendChild(wrap);
}
// ── study screen ──
function promptOf(card) {
return card.dir === "s2n" ? card.el.sym : card.el.name;
}
function answerOf(card) {
return card.dir === "s2n" ? card.el.name : card.el.sym;
}
function renderStudy() {
body.textContent = "";
const card = deck[idx];
const wrap = document.createElement("div");
wrap.className = "sde-study";
// progress
const prog = document.createElement("div");
prog.className = "sde-progress";
const bar = document.createElement("div");
bar.className = "sde-bar";
const fill = document.createElement("div");
fill.className = "sde-bar-fill";
fill.style.width = ((idx) / deck.length * 100) + "%";
bar.appendChild(fill);
const ptxt = document.createElement("span");
ptxt.className = "sde-progress-text";
ptxt.textContent = (idx + 1) + " of " + deck.length;
prog.append(bar, ptxt);
// the card
const face = document.createElement("div");
face.className = "sde-card";
const kicker = document.createElement("div");
kicker.className = "sde-kicker";
kicker.textContent = card.dir === "s2n" ? "Name this element" : "Give the symbol";
const q = document.createElement("div");
q.className = "sde-q" + (card.dir === "s2n" ? " sde-q-sym" : "");
q.textContent = promptOf(card);
face.append(kicker, q);
// answer, revealed on demand — announced so it reaches a screen reader
const ans = document.createElement("div");
ans.className = "sde-answer";
ans.setAttribute("role", "status");
ans.setAttribute("aria-live", "polite");
if (revealed) {
const a = document.createElement("div");
a.className = "sde-a" + (card.dir === "n2s" ? " sde-q-sym" : "");
a.textContent = answerOf(card);
const meta = document.createElement("div");
meta.className = "sde-meta";
for (const [k, v] of [["Z", card.el.z],
["Type", sdeLabels.type[card.el.type]],
["Family", sdeLabels.family[card.el.family]],
["Block", sdeLabels.block[card.el.block]]]) {
const chip = document.createElement("span");
chip.className = "sde-metachip";
const kk = document.createElement("span");
kk.className = "sde-metakey";
kk.textContent = k;
const vv = document.createElement("span");
vv.textContent = String(v);
chip.append(kk, vv);
meta.appendChild(chip);
}
ans.append(a, meta);
if (card.el.note) {
const n = document.createElement("p");
n.className = "sde-note";
n.textContent = card.el.note;
ans.appendChild(n);
}
}
face.appendChild(ans);
// controls
const ctl = document.createElement("div");
ctl.className = "sde-controls";
if (!revealed) {
const rev = document.createElement("button");
rev.type = "button";
rev.className = "sde-btn sde-btn-primary";
rev.textContent = "Show answer";
rev.addEventListener("click", () => { revealed = true; render(); });
ctl.appendChild(rev);
} else {
const next = document.createElement("button");
next.type = "button";
next.className = "sde-btn sde-btn-primary";
next.textContent = idx + 1 >= deck.length ? "Finish" : "Next";
next.addEventListener("click", advance);
ctl.appendChild(next);
}
const quit = document.createElement("button");
quit.type = "button";
quit.className = "sde-link";
quit.textContent = "Start over";
quit.addEventListener("click", () => { screen = "setup"; render(); });
wrap.append(prog, face, ctl, quit);
body.appendChild(wrap);
}
function advance() {
revealed = false;
if (idx + 1 >= deck.length) { screen = "summary"; }
else { idx += 1; }
render();
}
// ── summary ──
function renderSummary() {
body.textContent = "";
const wrap = document.createElement("div");
wrap.className = "sde-setup";
const h = document.createElement("h3");
h.className = "sde-h";
h.textContent = "Deck finished";
const p = document.createElement("p");
p.className = "sde-count";
p.textContent = "You worked through all " + deck.length + " cards.";
wrap.append(h, p);
// Nothing is scored, so the useful offer is another shuffle of the same deck
// rather than a report. `Change deck` goes back for a different mode or scope.
const again = document.createElement("button");
again.type = "button";
again.className = "sde-btn sde-btn-primary";
again.textContent = "Shuffle and go again";
again.addEventListener("click", () => { buildDeck(); screen = "study"; render(); });
const restart = document.createElement("button");
restart.type = "button";
restart.className = "sde-btn";
restart.textContent = "Change deck";
restart.addEventListener("click", () => { screen = "setup"; render(); });
const row = document.createElement("div");
row.className = "sde-controls";
row.append(again, restart);
wrap.appendChild(row);
body.appendChild(wrap);
}
// ── fullscreen ──
fsBtn.addEventListener("click", () => {
if (document.fullscreenElement === root) { document.exitFullscreen && document.exitFullscreen(); return; }
if (root.requestFullscreen) root.requestFullscreen().catch(() => {
fullscreen = !fullscreen; root.classList.toggle("sde-faux-fs", fullscreen); render();
});
else { fullscreen = !fullscreen; root.classList.toggle("sde-faux-fs", fullscreen); render(); }
});
document.addEventListener("fullscreenchange", () => {
fullscreen = document.fullscreenElement === root;
root.classList.toggle("sde-fs", fullscreen);
render();
});
// keyboard: one key drives the whole drill - reveal, then move on. The BUTTON
// guard matters because a focused button already fires on space and enter, and
// handling them here as well would skip a card.
root.addEventListener("keydown", (e) => {
if (screen !== "study") return;
if (e.target.tagName === "BUTTON" && e.key !== "ArrowRight") return;
const go = e.key === " " || e.key === "Enter" || e.key === "ArrowRight";
if (!go) return;
e.preventDefault();
if (!revealed) { revealed = true; render(); } else advance();
});
function render() {
fsBtn.textContent = fullscreen ? "✕" : "⛶";
fsBtn.setAttribute("aria-pressed", fullscreen ? "true" : "false");
if (screen === "setup") renderSetup();
else if (screen === "study") renderStudy();
else renderSummary();
}
root.tabIndex = 0;
render();
root.sdeRender = render;
return root;
}