|
| 1 | +import { CSS_PREFIX } from '../config.ts'; |
| 2 | + |
| 3 | +export interface CategoryFilterComponent { |
| 4 | + readonly element: HTMLDivElement; |
| 5 | + /** Update the list of available categories. Resets selection. */ |
| 6 | + setCategories(categories: readonly string[]): void; |
| 7 | + /** Get the currently selected category, or null if "All" is selected. */ |
| 8 | + getSelected(): string | null; |
| 9 | + /** Reset selection to "All" without triggering onChange. */ |
| 10 | + reset(): void; |
| 11 | +} |
| 12 | + |
| 13 | +export function createCategoryFilter( |
| 14 | + onChange: (category: string | null) => void, |
| 15 | +): CategoryFilterComponent { |
| 16 | + const wrap = document.createElement('div'); |
| 17 | + wrap.className = `${CSS_PREFIX}-category-filter`; |
| 18 | + |
| 19 | + let selectedCategory: string | null = null; |
| 20 | + let currentCategories: readonly string[] = []; |
| 21 | + |
| 22 | + function setCategories(categories: readonly string[]): void { |
| 23 | + // Avoid re-rendering if categories haven't changed |
| 24 | + if ( |
| 25 | + categories.length === currentCategories.length && |
| 26 | + categories.every((c, i) => c === currentCategories[i]) |
| 27 | + ) { |
| 28 | + return; |
| 29 | + } |
| 30 | + currentCategories = categories; |
| 31 | + // Preserve selection if the selected category still exists in the new list |
| 32 | + if (selectedCategory && !categories.includes(selectedCategory)) { |
| 33 | + selectedCategory = null; |
| 34 | + } |
| 35 | + renderChips(); |
| 36 | + } |
| 37 | + |
| 38 | + function renderChips(): void { |
| 39 | + wrap.innerHTML = ''; |
| 40 | + |
| 41 | + // Hide entirely when there are 0 or 1 categories (no filtering useful) |
| 42 | + if (currentCategories.length <= 1) { |
| 43 | + wrap.style.display = 'none'; |
| 44 | + return; |
| 45 | + } |
| 46 | + wrap.style.display = ''; |
| 47 | + |
| 48 | + // "All" chip |
| 49 | + const allChip = createChip('All', selectedCategory === null); |
| 50 | + allChip.addEventListener('click', () => { |
| 51 | + if (selectedCategory === null) return; |
| 52 | + selectedCategory = null; |
| 53 | + updateActiveState(); |
| 54 | + onChange(null); |
| 55 | + }); |
| 56 | + wrap.appendChild(allChip); |
| 57 | + |
| 58 | + // Category chips |
| 59 | + for (const cat of currentCategories) { |
| 60 | + const chip = createChip(cat, selectedCategory === cat); |
| 61 | + chip.addEventListener('click', () => { |
| 62 | + if (selectedCategory === cat) { |
| 63 | + // Toggle off -> back to "All" |
| 64 | + selectedCategory = null; |
| 65 | + } else { |
| 66 | + selectedCategory = cat; |
| 67 | + } |
| 68 | + updateActiveState(); |
| 69 | + onChange(selectedCategory); |
| 70 | + }); |
| 71 | + wrap.appendChild(chip); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + function createChip(label: string, active: boolean): HTMLButtonElement { |
| 76 | + const btn = document.createElement('button'); |
| 77 | + btn.type = 'button'; |
| 78 | + btn.className = `${CSS_PREFIX}-category-chip`; |
| 79 | + if (active) btn.classList.add(`${CSS_PREFIX}-category-chip--active`); |
| 80 | + btn.textContent = label; |
| 81 | + return btn; |
| 82 | + } |
| 83 | + |
| 84 | + function updateActiveState(): void { |
| 85 | + const chips = wrap.querySelectorAll<HTMLButtonElement>(`.${CSS_PREFIX}-category-chip`); |
| 86 | + let idx = 0; |
| 87 | + for (const chip of chips) { |
| 88 | + if (idx === 0) { |
| 89 | + // "All" chip |
| 90 | + chip.classList.toggle(`${CSS_PREFIX}-category-chip--active`, selectedCategory === null); |
| 91 | + } else { |
| 92 | + const cat = currentCategories[idx - 1]; |
| 93 | + chip.classList.toggle(`${CSS_PREFIX}-category-chip--active`, selectedCategory === cat); |
| 94 | + } |
| 95 | + idx++; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + function reset(): void { |
| 100 | + selectedCategory = null; |
| 101 | + updateActiveState(); |
| 102 | + } |
| 103 | + |
| 104 | + function getSelected(): string | null { |
| 105 | + return selectedCategory; |
| 106 | + } |
| 107 | + |
| 108 | + return { element: wrap, setCategories, getSelected, reset }; |
| 109 | +} |
0 commit comments