Loading...
Loading...
Use this skill when a vanilla static SPA scaffold exists and needs to feel like a polished product — progress persistence, sidebar + scrollspy navigation, dark/light theme, content zoom, responsive (mobile sidebar overlay), keyboard nav, fade-in entrances, toast notifications, iframe modal viewers, quiz scoring with section back-links. Triggers on phrases like "加進度勾選", "響應式", "暗色模式", "縮放", "scrollspy", "sidebar 收合", "手機版", "RWD", "dark mode", "progress tracking", "quiz UX", "interactive polish". Always invoke AFTER `static-spa-conversion` (renders working), as a standalone enhancement layer.
npx skill4agent add kevintsai1202/teaching-site-skills static-spa-interactionsSchema authority: theprimitive (id-as-localStorage-key rules) and theTaskprimitive (storage of selected option) come fromQuizItem§8 and §10. localStorage schema lives in §8._shared/domain-primitives.mdReference implementation:ford:/GitHub/ai-workshop/index.html:2900-3030patterns.renderSidebar / setupScrollSpy / applyTheme / setupFadeIn
The patterns here are documented because they all hide subtle bugs that took real time to find in the example workshop. Read the "Why" notes carefully — they're not optional context.
const STORAGE_KEY = 'your-app-progress-v1'; // version suffix lets you breaking-change later
const store = {
load() {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY)) || { tasks: {}, quiz: {}, theme: 'light' };
} catch { return { tasks: {}, quiz: {}, theme: 'light' }; }
},
save(state) { localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); },
reset() { localStorage.removeItem(STORAGE_KEY); location.reload(); }
};-v2init():root { --sidebar-w: 280px; }
/* Desktop: sidebar visible by default */
.sidebar { position: fixed; left: 0; top: 0; width: var(--sidebar-w); height: 100vh; }
.main { margin-left: var(--sidebar-w); transition: margin-left .2s ease; }
.app.sidebar-closed .sidebar { transform: translateX(-100%); }
.app.sidebar-closed .main { margin-left: 0; }
@media (max-width: 768px) {
/* Mobile: sidebar hidden by default */
.sidebar { transform: translateX(-100%); }
.main { margin-left: 0; }
.app.sidebar-open .sidebar { transform: translateX(0); }
.sidebar-backdrop { /* overlay shown only when sidebar-open */ }
}const mql = matchMedia('(max-width: 768px)');
function toggleSidebar() {
if (mql.matches) document.querySelector('.app').classList.toggle('sidebar-open');
else document.querySelector('.app').classList.toggle('sidebar-closed');
}
// On viewport crossing 768px, clear both classes so the new viewport's default takes over:
mql.addEventListener('change', () => {
document.querySelector('.app').classList.remove('sidebar-open', 'sidebar-closed');
});position: fixedstickyoverflow: hiddenoverflow-x: hiddenoverflow-y: autofunction setupScrollSpy() {
const sections = document.querySelectorAll('[data-section-id]');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const id = entry.target.dataset.sectionId;
document.querySelectorAll('.sidebar-link').forEach(a => a.classList.toggle('active', a.dataset.target === id));
}
});
}, { rootMargin: '-30% 0px -60% 0px', threshold: 0 });
sections.forEach(s => observer.observe(s));
}rootMargin: '-30% 0px -60% 0px'thresholdfunction applyTheme(theme) {
document.documentElement.dataset.theme = theme;
const s = store.load(); s.theme = theme; store.save(s);
}
applyTheme(store.load().theme); // on load[data-theme="dark"]:rootprefers-color-scheme:root { --content-zoom: 1; }
.content { zoom: var(--content-zoom); }
/* Sidebar is OUTSIDE .content — never inherits zoom */function applyZoom(value) {
if (mql.matches) {
// On mobile, kill the CSS variable so @media rules govern
document.documentElement.style.removeProperty('--content-zoom');
} else {
document.documentElement.style.setProperty('--content-zoom', value);
}
}html { zoom: X }zoomhtml.content1.35@media (max-width: 768px) :root { --content-zoom: 1 }removeProperty()function setupFadeIn() {
const observer = new IntersectionObserver(entries => {
entries.forEach(e => { if (e.isIntersecting) e.target.classList.add('visible'); });
}, { threshold: 0.15 });
document.querySelectorAll('.fade-in').forEach(el => observer.observe(el));
}.fade-in { opacity: 0; transform: translateY(20px); transition: opacity .5s, transform .5s; }
.fade-in.visible { opacity: 1; transform: translateY(0); }zoomopacity: 0@keyframesopacity: 0 → 1async function copyPrompt(text, btn) {
try {
await navigator.clipboard.writeText(text);
showToast('已複製');
} catch {
// Fallback for non-secure context (e.g. file://)
const ta = document.createElement('textarea');
ta.value = text; document.body.appendChild(ta);
ta.select(); document.execCommand('copy');
ta.remove();
showToast('已複製');
}
}file://navigator.clipboardfunction showToast(msg) {
const t = document.createElement('div');
t.className = 'toast'; t.textContent = msg;
document.body.appendChild(t);
setTimeout(() => t.classList.add('show'), 10);
setTimeout(() => { t.classList.remove('show'); setTimeout(() => t.remove(), 300); }, 1800);
}sourceUnitcourse-content-authoringfunction gradeQuiz() {
const state = store.load();
const results = window.COURSE.quiz.map(q => ({
q, given: state.quiz[q.id], correct: q.answer, isWrong: state.quiz[q.id] !== q.answer
}));
// Render with back-links for wrong ones; scroll behaviour:
// document.querySelector(`[data-section-id="${q.sourceUnit}"]`).scrollIntoView({ behavior: 'smooth' });
}s >= Kwindow.COURSE.quiz.lengthfunction openViewer(url) {
const modal = document.createElement('div'); modal.className = 'viewer-modal';
modal.innerHTML = `<div class="viewer-backdrop"></div><div class="viewer-frame">
<button class="viewer-close">×</button>
<iframe src="${url}" loading="lazy"></iframe>
</div>`;
document.body.appendChild(modal);
modal.querySelector('.viewer-close').onclick = () => modal.remove();
modal.querySelector('.viewer-backdrop').onclick = () => modal.remove();
document.addEventListener('keydown', function esc(e) {
if (e.key === 'Escape') { modal.remove(); document.removeEventListener('keydown', esc); }
});
}<a href="..." download>getMaterialUrl()function openViewer(url) { /* ... */ document.body.style.overflow = 'hidden'; }
function closeViewer() { document.body.style.overflow = ''; }web-visual-verificationweb-visual-assets