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

+79 -4
View File
@@ -128,8 +128,45 @@ function initProjectFilters() {
let selectedFilter = 'all';
let isExpanded = false;
const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
const canAnimate = projectCards.every(
(card) => typeof card.animate === 'function'
);
const animations = new Map();
const cancelAnimations = () => {
animations.forEach((animation) => animation.cancel());
animations.clear();
};
const applyFilter = () => {
if (canAnimate) {
window.addEventListener('resize', cancelAnimations);
reducedMotion.addEventListener('change', () => {
if (reducedMotion.matches) cancelAnimations();
});
projectGrid?.addEventListener('focusin', (event) => {
const card = event.target.closest('.project-card');
animations.get(card)?.cancel();
animations.delete(card);
});
}
const applyFilter = (animate = false) => {
const shouldAnimate = animate && canAnimate && !reducedMotion.matches;
// Capture the current visual positions before cancelling an interrupted move.
const previous = new Map(
shouldAnimate
? projectCards
.filter((card) => !card.hidden)
.map((card) => [
card,
{
rect: card.getBoundingClientRect(),
opacity: window.getComputedStyle(card).opacity,
},
])
: []
);
cancelAnimations();
const matchingCards = projectCards.filter(
(card) =>
selectedFilter === 'all' ||
@@ -139,6 +176,9 @@ function initProjectFilters() {
? matchingCards
: matchingCards.slice(0, DEFAULT_VISIBLE_PROJECTS);
const shownSet = new Set(shownCards);
const focusedCard = projectCards.find((card) =>
card.contains(document.activeElement)
);
projectCards.forEach((card) => {
card.hidden = !shownSet.has(card);
});
@@ -171,6 +211,41 @@ function initProjectFilters() {
? 'Show fewer projects'
: `Show ${remainingCount} more project${remainingCount === 1 ? '' : 's'}`;
}
if (focusedCard && !shownSet.has(focusedCard)) {
filterChips
.find((chip) => chip.dataset.filter === selectedFilter)
.focus();
}
if (shouldAnimate) {
const positions = shownCards.map((card) => [
card,
card.getBoundingClientRect(),
]);
positions.forEach(([card, rect]) => {
const before = previous.get(card);
const x = before ? before.rect.left - rect.left : 0;
const y = before ? before.rect.top - rect.top : 12;
const opacity = before ? before.opacity : 0;
if (
card.contains(document.activeElement) ||
(x === 0 && y === 0 && Number(opacity) === 1)
) {
return;
}
const animation = card.animate(
[
{ transform: `translate(${x}px, ${y}px)`, opacity },
{ transform: 'translate(0px, 0px)', opacity: 1 },
],
{ duration: 280, easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)' }
);
animations.set(card, animation);
animation.addEventListener('finish', () => {
if (animations.get(card) === animation) animations.delete(card);
});
});
}
};
filterChips.forEach((chip) => {
@@ -178,14 +253,14 @@ function initProjectFilters() {
selectedFilter = chip.dataset.filter || 'all';
isExpanded = false;
applyFilter();
applyFilter(true);
});
});
if (seeMoreButton) {
seeMoreButton.addEventListener('click', () => {
isExpanded = !isExpanded;
applyFilter();
applyFilter(true);
if (!isExpanded) {
seeMoreButton.scrollIntoView({ block: 'nearest', behavior: 'instant' });
}
@@ -196,7 +271,7 @@ function initProjectFilters() {
resetButton.addEventListener('click', () => {
selectedFilter = 'all';
isExpanded = false;
applyFilter();
applyFilter(true);
filterChips.find((chip) => chip.dataset.filter === 'all').focus();
});
}