add light mode

This commit is contained in:
John Gatward committed 2026-10-08 12:21:58 +01:00
1 parent 874ea1f8bf
commit c744979818
11 files changed
+689 -35

No files matched your search

+121
View File
@@ -37,6 +37,7 @@ function element(dataset = {}, textContent = '') {
scrollIntoView() {
this.scrolledIntoView = true;
},
contains: () => false,
};
}
@@ -112,6 +113,126 @@ function fixture({ missingFilter = false, reducedMotion = false } = {}) {
};
}
function animatedFixture(options) {
const f = fixture(options);
const animations = [];
const listeners = new Map();
const media = {
matches: options?.reducedMotion ?? false,
addEventListener: (name, listener) =>
listeners.set(`motion-${name}`, listener),
};
f.context.window.matchMedia = () => media;
f.context.window.addEventListener = (name, listener) =>
listeners.set(name, listener);
f.context.window.getComputedStyle = () => ({ opacity: '1' });
for (const card of f.cards) {
card.getBoundingClientRect = () => {
const index = f.visible().indexOf(card);
return { left: (index % 3) * 300, top: Math.floor(index / 3) * 200 };
};
card.animate = (frames, timing) => {
const animation = {
card,
frames,
timing,
cancelled: false,
cancel() {
this.cancelled = true;
},
addEventListener(name, listener) {
if (name === 'finish') this.finish = listener;
},
};
animations.push(animation);
return animation;
};
}
return { ...f, animations, listeners, media };
}
test('filtering moves retained cards and fades in new cards without delaying results', () => {
const f = animatedFixture();
f.start();
assert.equal(f.animations.length, 0);
const rust = f.chips.find((chip) => chip.dataset.filter === 'rust');
rust.click();
assert.equal(f.visible().length, 1);
assert.equal(
f.controls.get('#project-count').textContent,
'Showing 1 of 1 project (Rust)'
);
assert.ok(
f.animations.some(
(animation) => animation.frames[0].transform !== 'translate(0px, 0px)'
)
);
assert.ok(
f.animations.every((animation) => animation.timing.duration === 280)
);
f.chips[0].click();
assert.equal(f.visible().length, 9);
assert.ok(
f.animations.some((animation) => animation.frames[0].opacity === 0)
);
});
test('rapid filter changes cancel old animations and finish on the latest selection', () => {
const f = animatedFixture();
f.start();
f.chips.find((chip) => chip.dataset.filter === 'rust').click();
const previous = [...f.animations];
f.chips[0].click();
assert.ok(previous.every((animation) => animation.cancelled));
const current = f.animations.filter((animation) => !animation.cancelled);
previous.forEach((animation) => animation.finish());
f.listeners.get('resize')();
assert.ok(current.every((animation) => animation.cancelled));
assert.equal(f.visible().length, 9);
});
test('reduced motion is instant and enabling it cancels in-flight animations', () => {
const reduced = animatedFixture({ reducedMotion: true });
reduced.start();
reduced.chips.find((chip) => chip.dataset.filter === 'rust').click();
assert.equal(reduced.visible().length, 1);
assert.equal(reduced.animations.length, 0);
const f = animatedFixture();
f.start();
f.chips.find((chip) => chip.dataset.filter === 'rust').click();
f.media.matches = true;
f.listeners.get('motion-change')();
assert.ok(f.animations.every((animation) => animation.cancelled));
const count = f.animations.length;
f.chips[0].click();
assert.equal(f.animations.length, count);
assert.equal(f.visible().length, 9);
});
test('filtering restores focus when the focused project becomes hidden', () => {
const f = fixture();
f.start();
const focused = f.cards.find((card) => !card.dataset.tech.includes('rust'));
f.context.document.activeElement = focused;
focused.contains = (target) => target === focused;
const rust = f.chips.find((chip) => chip.dataset.filter === 'rust');
rust.click();
assert.equal(focused.hidden, true);
assert.equal(rust.focused, true);
});
test('keyboard focus immediately settles a moving project', () => {
const f = animatedFixture();
f.start();
f.chips.find((chip) => chip.dataset.filter === 'rust').click();
const animation = f.animations[0];
f.controls.get('#project-grid').dispatch('focusin', {
target: { closest: () => animation.card },
});
assert.equal(animation.cancelled, true);
});
test('projects start collapsed with an accurate count and remaining label', () => {
const f = fixture();
f.start();
+187
View File
@@ -0,0 +1,187 @@
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const { resolve } = require('node:path');
const { test } = require('node:test');
const { runInNewContext } = require('node:vm');
const root = resolve(__dirname, '..');
const script = readFileSync(resolve(root, 'theme.js'), 'utf8');
const html = readFileSync(resolve(root, 'index.html'), 'utf8');
function fixture({
dark = false,
saved = null,
storageError = null,
writeError = null,
} = {}) {
const dataset = {};
const storage = new Map(saved === null ? [] : [['havox-theme', saved]]);
const meta = [{ content: '#1e1e2e' }, { content: '#eff1f5' }];
const listeners = new Map();
const warnings = [];
const attributes = new Map();
const button = {
hidden: true,
dataset: {},
setAttribute: (name, value) => attributes.set(name, value),
getAttribute: (name) => attributes.get(name),
addEventListener: (name, listener) => listeners.set(name, listener),
};
const media = {
matches: dark,
addEventListener: (name, listener) =>
listeners.set(`system-${name}`, listener),
};
const context = {
document: {
documentElement: { dataset },
querySelectorAll: () => meta,
querySelector: () => button,
addEventListener: (name, listener) => listeners.set(name, listener),
},
window: {
matchMedia: (query) => {
assert.equal(query, '(prefers-color-scheme: dark)');
return media;
},
},
localStorage: {
getItem: (key) => {
if (storageError) throw storageError;
return storage.get(key) ?? null;
},
setItem: (key, value) => {
if (storageError) throw storageError;
if (writeError) throw writeError;
storage.set(key, value);
},
},
console: { warn: (...args) => warnings.push(args) },
};
runInNewContext(script, context);
return {
dataset,
storage,
meta,
button,
warnings,
ready: () => listeners.get('DOMContentLoaded')(),
toggle: () => listeners.get('click')(),
setSystemDark(value) {
media.matches = value;
listeners.get('system-change')();
},
};
}
test('system preference applies immediately and follows operating-system changes', () => {
for (const dark of [false, true]) {
const f = fixture({ dark });
assert.equal(f.dataset.theme, undefined);
assert.ok(
f.meta.every((meta) => meta.content === (dark ? '#1e1e2e' : '#eff1f5'))
);
assert.equal(f.button.hidden, true);
f.ready();
assert.equal(f.button.hidden, false);
assert.equal(f.button.dataset.theme, dark ? 'dark' : 'light');
assert.equal(
f.button.getAttribute('aria-label'),
`Switch to ${dark ? 'light' : 'dark'} theme`
);
f.setSystemDark(!dark);
assert.equal(f.button.dataset.theme, dark ? 'light' : 'dark');
assert.equal(
f.button.getAttribute('aria-label'),
`Switch to ${dark ? 'dark' : 'light'} theme`
);
assert.ok(
f.meta.every((meta) => meta.content === (dark ? '#eff1f5' : '#1e1e2e'))
);
}
});
test('saved themes override the system before first paint', () => {
for (const saved of ['light', 'dark']) {
const f = fixture({ dark: saved === 'light', saved });
assert.equal(f.dataset.theme, saved);
f.ready();
assert.equal(f.button.dataset.theme, saved);
f.setSystemDark(saved === 'dark');
assert.equal(f.dataset.theme, saved);
assert.ok(
f.meta.every(
(meta) => meta.content === (saved === 'dark' ? '#1e1e2e' : '#eff1f5')
)
);
}
assert.ok(html.indexOf('src="theme.js"') < html.indexOf('href="style.css"'));
const notFound = readFileSync(resolve(root, '404.html'), 'utf8');
assert.ok(
notFound.indexOf('src="theme.js"') < notFound.indexOf('href="style.css"')
);
assert.match(
notFound,
/class="theme-toggle"[\s\S]*?type="button"[\s\S]*?hidden/
);
});
test('the button toggles the effective theme and persists the explicit choice', () => {
const f = fixture({ dark: true });
f.ready();
f.toggle();
assert.equal(f.dataset.theme, 'light');
assert.equal(f.storage.get('havox-theme'), 'light');
f.toggle();
assert.equal(f.dataset.theme, 'dark');
assert.equal(f.storage.get('havox-theme'), 'dark');
f.setSystemDark(false);
assert.equal(f.dataset.theme, 'dark');
assert.equal(f.button.dataset.theme, 'dark');
assert.ok(f.meta.every((meta) => meta.content === '#1e1e2e'));
const reloaded = fixture({
dark: false,
saved: f.storage.get('havox-theme'),
});
reloaded.ready();
assert.equal(reloaded.button.dataset.theme, 'dark');
reloaded.toggle();
assert.equal(reloaded.dataset.theme, 'light');
});
test('invalid saved values do not become theme attributes', () => {
const f = fixture({ saved: 'unexpected' });
f.ready();
assert.equal(f.dataset.theme, undefined);
assert.equal(f.button.dataset.theme, 'light');
});
test('blocked storage warns but does not prevent switching themes', () => {
const storageError = new Error('Storage denied');
storageError.name = 'SecurityError';
const f = fixture({ storageError });
f.ready();
f.toggle();
assert.equal(f.dataset.theme, 'dark');
assert.ok(f.meta.every((meta) => meta.content === '#1e1e2e'));
assert.equal(f.warnings.length, 2);
});
test('unexpected storage errors are not swallowed', () => {
assert.throws(
() => fixture({ storageError: new Error('Unexpected failure') }),
/Unexpected failure/
);
});
test('full storage does not prevent toggling in either direction', () => {
const writeError = new Error('Storage full');
writeError.name = 'QuotaExceededError';
const f = fixture({ writeError });
f.ready();
f.toggle();
assert.equal(f.dataset.theme, 'dark');
f.toggle();
assert.equal(f.dataset.theme, 'light');
assert.equal(f.warnings.length, 2);
});