|
| 1 | +--- |
| 2 | +// Single search modal rendered once in Base.astro. |
| 3 | +// Multiple DocsSearch trigger buttons open this shared modal. |
| 4 | +--- |
| 5 | +<div id="search-modal" class="fixed inset-0 z-[100] hidden" role="dialog" aria-modal="true" aria-label="Search documentation"> |
| 6 | + <div class="fixed inset-0 bg-black/60 backdrop-blur-sm" data-search-backdrop></div> |
| 7 | + <div class="fixed top-[15%] left-1/2 -translate-x-1/2 w-[calc(100%-2rem)] max-w-xl bg-surface-container border border-outline-variant shadow-2xl max-h-[60vh] flex flex-col"> |
| 8 | + <div class="flex items-center gap-3 px-4 py-3 border-b border-outline-variant"> |
| 9 | + <svg class="w-4 h-4 text-on-surface-variant shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 10 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /> |
| 11 | + </svg> |
| 12 | + <input |
| 13 | + data-search-input |
| 14 | + type="text" |
| 15 | + placeholder="Search documentation..." |
| 16 | + class="flex-1 bg-transparent text-on-surface text-sm outline-none placeholder:text-on-surface-variant/50 font-body" |
| 17 | + autocomplete="off" |
| 18 | + /> |
| 19 | + <kbd class="text-[10px] text-on-surface-variant/60 border border-outline-variant/50 px-1.5 py-0.5 rounded-sm">Esc</kbd> |
| 20 | + </div> |
| 21 | + <div data-search-results class="overflow-y-auto p-2 text-sm"> |
| 22 | + <p class="px-3 py-4 text-on-surface-variant/60 text-center text-xs">Type to search...</p> |
| 23 | + </div> |
| 24 | + </div> |
| 25 | +</div> |
| 26 | + |
| 27 | +<script is:inline> |
| 28 | +(function() { |
| 29 | + if (window.__searchInitialized) return; |
| 30 | + window.__searchInitialized = true; |
| 31 | + |
| 32 | + var pagefind = null; |
| 33 | + |
| 34 | + function loadPagefind() { |
| 35 | + if (pagefind) return Promise.resolve(pagefind); |
| 36 | + return import('/pagefind/pagefind.js').then(function(pf) { |
| 37 | + pagefind = pf; |
| 38 | + return pf.init().then(function() { return pf; }); |
| 39 | + }).catch(function() { pagefind = null; return null; }); |
| 40 | + } |
| 41 | + |
| 42 | + function getModal() { return document.getElementById('search-modal'); } |
| 43 | + function getInput() { return document.querySelector('[data-search-input]'); } |
| 44 | + function getResults() { return document.querySelector('[data-search-results]'); } |
| 45 | + |
| 46 | + function setResultsText(msg) { |
| 47 | + var r = getResults(); |
| 48 | + if (!r) return; |
| 49 | + while (r.firstChild) r.removeChild(r.firstChild); |
| 50 | + var p = document.createElement('p'); |
| 51 | + p.className = 'px-3 py-4 text-on-surface-variant/60 text-center text-xs'; |
| 52 | + p.textContent = msg; |
| 53 | + r.appendChild(p); |
| 54 | + } |
| 55 | + |
| 56 | + function openSearch() { |
| 57 | + var modal = getModal(); |
| 58 | + var input = getInput(); |
| 59 | + if (!modal || !input) return; |
| 60 | + modal.classList.remove('hidden'); |
| 61 | + input.value = ''; |
| 62 | + input.focus(); |
| 63 | + setResultsText('Type to search...'); |
| 64 | + } |
| 65 | + |
| 66 | + function closeSearch() { |
| 67 | + var modal = getModal(); |
| 68 | + if (modal) modal.classList.add('hidden'); |
| 69 | + } |
| 70 | + |
| 71 | + // Sanitize Pagefind excerpt: strip all HTML except <mark> tags |
| 72 | + function sanitizeExcerpt(html) { |
| 73 | + var tmp = document.createElement('div'); |
| 74 | + tmp.textContent = ''; |
| 75 | + // Parse into a temporary element to strip tags |
| 76 | + var parser = new DOMParser(); |
| 77 | + var doc = parser.parseFromString(html, 'text/html'); |
| 78 | + var walker = doc.body.childNodes; |
| 79 | + for (var i = 0; i < walker.length; i++) { |
| 80 | + var node = walker[i]; |
| 81 | + if (node.nodeType === 3) { |
| 82 | + tmp.appendChild(document.createTextNode(node.textContent)); |
| 83 | + } else if (node.nodeType === 1 && node.tagName === 'MARK') { |
| 84 | + var mark = document.createElement('mark'); |
| 85 | + mark.setAttribute('data-pagefind-highlight', ''); |
| 86 | + mark.textContent = node.textContent; |
| 87 | + tmp.appendChild(mark); |
| 88 | + } else { |
| 89 | + tmp.appendChild(document.createTextNode(node.textContent || '')); |
| 90 | + } |
| 91 | + } |
| 92 | + return tmp; |
| 93 | + } |
| 94 | + |
| 95 | + document.addEventListener('keydown', function(e) { |
| 96 | + if ((e.metaKey || e.ctrlKey) && e.key === 'k') { |
| 97 | + e.preventDefault(); |
| 98 | + var modal = getModal(); |
| 99 | + if (!modal) return; |
| 100 | + if (modal.classList.contains('hidden')) openSearch(); |
| 101 | + else closeSearch(); |
| 102 | + } |
| 103 | + if (e.key === 'Escape') closeSearch(); |
| 104 | + }); |
| 105 | + |
| 106 | + document.addEventListener('click', function(e) { |
| 107 | + if (e.target && e.target.hasAttribute('data-search-backdrop')) closeSearch(); |
| 108 | + if (e.target && e.target.closest('[data-search-trigger]')) openSearch(); |
| 109 | + }); |
| 110 | + |
| 111 | + var debounceTimer; |
| 112 | + document.addEventListener('input', function(e) { |
| 113 | + if (!e.target || !e.target.hasAttribute('data-search-input')) return; |
| 114 | + clearTimeout(debounceTimer); |
| 115 | + debounceTimer = setTimeout(function() { |
| 116 | + var input = e.target; |
| 117 | + var results = getResults(); |
| 118 | + if (!results) return; |
| 119 | + |
| 120 | + var query = input.value.trim(); |
| 121 | + if (!query) { |
| 122 | + setResultsText('Type to search...'); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + loadPagefind().then(function(pf) { |
| 127 | + if (!pf) { |
| 128 | + setResultsText('Search unavailable (build required)'); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + pf.search(query).then(function(search) { |
| 133 | + if (search.results.length === 0) { |
| 134 | + setResultsText('No results for "' + query + '"'); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + Promise.all(search.results.slice(0, 8).map(function(r) { return r.data(); })) |
| 139 | + .then(function(items) { |
| 140 | + while (results.firstChild) results.removeChild(results.firstChild); |
| 141 | + items.forEach(function(item) { |
| 142 | + var link = document.createElement('a'); |
| 143 | + link.href = item.url; |
| 144 | + link.className = 'block px-3 py-2 hover:bg-surface-container-high transition-colors group'; |
| 145 | + |
| 146 | + var title = document.createElement('div'); |
| 147 | + title.className = 'font-medium text-on-surface text-sm group-hover:text-primary transition-colors'; |
| 148 | + title.textContent = (item.meta && item.meta.title) || item.url; |
| 149 | + link.appendChild(title); |
| 150 | + |
| 151 | + if (item.excerpt) { |
| 152 | + var excerptEl = sanitizeExcerpt(item.excerpt); |
| 153 | + excerptEl.className = 'text-xs text-on-surface-variant mt-0.5 line-clamp-2'; |
| 154 | + link.appendChild(excerptEl); |
| 155 | + } |
| 156 | + |
| 157 | + link.addEventListener('click', closeSearch); |
| 158 | + results.appendChild(link); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }, 200); |
| 164 | + }); |
| 165 | +})(); |
| 166 | +</script> |
0 commit comments