|
| 1 | +<script lang="ts"> |
| 2 | + /** |
| 3 | + * In-title search filter for the expandable TOC. |
| 4 | + * Filters sections by keyword, auto-expanding matching chapters |
| 5 | + * and hiding non-matching sections. Zero-overhead when empty. |
| 6 | + */ |
| 7 | +
|
| 8 | + interface Props { |
| 9 | + /** CSS selector for the TOC container */ |
| 10 | + tocSelector?: string; |
| 11 | + /** Placeholder text */ |
| 12 | + placeholder?: string; |
| 13 | + } |
| 14 | +
|
| 15 | + let { tocSelector = '#title-toc', placeholder = 'Filter sections...' }: Props = $props(); |
| 16 | + let query = $state(''); |
| 17 | + let matchCount = $state(0); |
| 18 | + let totalCount = $state(0); |
| 19 | + let debounceTimer: ReturnType<typeof setTimeout> | undefined; |
| 20 | +
|
| 21 | + function applyFilter(q: string): void { |
| 22 | + const toc = document.querySelector(tocSelector); |
| 23 | + if (!toc) return; |
| 24 | +
|
| 25 | + const details = toc.querySelectorAll('details'); |
| 26 | + const normalizedQuery = q.toLowerCase().trim(); |
| 27 | + let matches = 0; |
| 28 | + let total = 0; |
| 29 | +
|
| 30 | + for (const detail of details) { |
| 31 | + const items = detail.querySelectorAll('li'); |
| 32 | + let chapterHasMatch = false; |
| 33 | +
|
| 34 | + for (const item of items) { |
| 35 | + total++; |
| 36 | + const text = item.textContent?.toLowerCase() ?? ''; |
| 37 | + if (!normalizedQuery || text.includes(normalizedQuery)) { |
| 38 | + (item as HTMLElement).style.display = ''; |
| 39 | + matches++; |
| 40 | + chapterHasMatch = true; |
| 41 | + } else { |
| 42 | + (item as HTMLElement).style.display = 'none'; |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + // Auto-expand chapters with matches, collapse empty ones |
| 47 | + if (normalizedQuery) { |
| 48 | + detail.open = chapterHasMatch; |
| 49 | + (detail as HTMLElement).style.display = chapterHasMatch ? '' : 'none'; |
| 50 | + } else { |
| 51 | + // Reset: show all, restore default open state |
| 52 | + (detail as HTMLElement).style.display = ''; |
| 53 | + } |
| 54 | + } |
| 55 | +
|
| 56 | + matchCount = matches; |
| 57 | + totalCount = total; |
| 58 | + } |
| 59 | +
|
| 60 | + $effect(() => { |
| 61 | + const q = query; |
| 62 | + if (debounceTimer !== undefined) clearTimeout(debounceTimer); |
| 63 | + debounceTimer = setTimeout(() => applyFilter(q), 150); |
| 64 | + return () => { |
| 65 | + if (debounceTimer !== undefined) clearTimeout(debounceTimer); |
| 66 | + }; |
| 67 | + }); |
| 68 | +</script> |
| 69 | + |
| 70 | +<div class="relative font-sans"> |
| 71 | + <div class="relative"> |
| 72 | + <svg class="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" aria-hidden="true"> |
| 73 | + <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /> |
| 74 | + </svg> |
| 75 | + <input |
| 76 | + type="text" |
| 77 | + bind:value={query} |
| 78 | + placeholder={placeholder} |
| 79 | + class="w-full rounded border border-gray-300 bg-white py-1.5 pl-8 pr-8 text-xs text-gray-900 placeholder-gray-400 focus:border-teal focus:outline-none dark:border-gray-700 dark:bg-gray-900 dark:text-gray-100 dark:placeholder-gray-600 sm:w-72" |
| 80 | + aria-label="Filter sections within this title" |
| 81 | + /> |
| 82 | + {#if query} |
| 83 | + <button |
| 84 | + class="absolute right-2 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600" |
| 85 | + onclick={() => { query = ''; }} |
| 86 | + aria-label="Clear filter" |
| 87 | + >×</button> |
| 88 | + {/if} |
| 89 | + </div> |
| 90 | + {#if query} |
| 91 | + <p class="mt-1 text-[11px] text-gray-400"> |
| 92 | + {matchCount} of {totalCount} sections match |
| 93 | + </p> |
| 94 | + {/if} |
| 95 | +</div> |
0 commit comments