<button id="search-trigger" aria-label="Search (Cmd+K)">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
<span>Search...</span>
<kbd>⌘K</kbd>
</button>
<div id="search-modal" class="hidden">
<div class="modal-content">
<div class="search-header">
<svg class="search-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>
<input type="text" id="search-input" placeholder="Search documentation..." autocomplete="off" />
<div class="search-actions">
<button id="close-search">Esc</button>
</div>
</div>
<div class="search-body">
<ul id="search-results"></ul>
<div id="search-footer">
<span>Powered by <a href="https://pagefind.app/" target="_blank">Pagefind</a></span>
</div>
</div>
</div>
</div>
<script is:inline>
const modal = document.getElementById('search-modal');
const input = document.getElementById('search-input');
const results = document.getElementById('search-results');
const trigger = document.getElementById('search-trigger');
const closeBtn = document.getElementById('close-search');
// Load Pagefind
let pagefind = null;
async function loadPagefind() {
if (pagefind) return;
try {
pagefind = await import('/pagefind/pagefind.js');
pagefind.init();
} catch (e) {
console.warn('Pagefind not found. Search is only available in production builds.', e);
results.innerHTML = '<li class="error-msg">Search is available in production builds.</li>';
}
}
function openSearch() {
modal.classList.remove('hidden');
input.focus();
document.body.style.overflow = 'hidden';
loadPagefind();
}
function closeSearch() {
modal.classList.add('hidden');
input.value = '';
results.innerHTML = '';
document.body.style.overflow = '';
}
// Trigger
trigger.addEventListener('click', openSearch);
closeBtn.addEventListener('click', closeSearch);
// Shortcut
document.addEventListener('keydown', (e) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault();
openSearch();
}
if (e.key === 'Escape') {
closeSearch();
}
});
function formatBreadcrumbs(url) {
// url example: /docs/guides/processing/
const parts = url.split('/').filter(p => p && p !== 'docs');
if (parts.length === 0) return 'Docs';
return 'Docs > ' + parts.map(p => p.charAt(0).toUpperCase() + p.slice(1).replace(/_/g, ' ')).join(' > ');
}
// Search Logic
input.addEventListener('input', async (e) => {
const query = e.target.value.toLowerCase().trim();
if (!query || !pagefind) {
results.innerHTML = '';
return;
}
try {
const search = await pagefind.search(query);
const fiveResults = await Promise.all(search.results.slice(0, 5).map(r => r.data()));
if (fiveResults.length === 0) {
results.innerHTML = `<li class="no-results">No results found for "${query}"</li>`;
return;
}
results.innerHTML = fiveResults.map(item => `
<li>
<a href="${item.url}" class="result-card">
<div class="result-icon">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>
</div>
<div class="result-content">
<div class="result-breadcrumbs">${formatBreadcrumbs(item.url)}</div>
<div class="result-title">${item.meta.title}</div>
<div class="result-desc">${item.excerpt}</div>
</div>
<div class="result-enter">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 10 4 15 9 20"></polyline><path d="M20 4v7a4 4 0 0 1-4 4H4"></path></svg>
</div>
</a>
</li>
`).join('');
} catch (err) {
console.error(err);
}
});
// Close on click outside
modal.addEventListener('click', (e) => {
if (e.target === modal) closeSearch();
});
</script>
<style>
#search-trigger {
display: flex;
align-items: center;
gap: 0.5rem;
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
color: var(--text-muted);
padding: 0.4rem 0.8rem;
border-radius: 6px;
font-size: 0.9rem;
cursor: pointer;
width: 100%;
margin-bottom: 1.5rem;
transition: all 0.2s;
}
#search-trigger:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.2);
}
kbd {
margin-left: auto;
font-size: 0.75rem;
background: rgba(0, 0, 0, 0.3);
padding: 2px 6px;
border-radius: 4px;
color: var(--text-muted);
}
/* Modal */
#search-modal {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.6);
backdrop-filter: blur(8px);
z-index: 1000;
display: flex;
justify-content: center;
padding-top: 10vh;
animation: fadeIn 0.15s ease-out;
}
#search-modal.hidden {
display: none;
}
.modal-content {
width: 100%;
max-width: 650px;
background: #0d1117;
border: 1px solid #30363d;
border-radius: 12px;
box-shadow: 0 40px 80px -20px rgba(0,0,0,0.8);
height: max-content;
max-height: 80vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.search-header {
display: flex;
align-items: center;
padding: 1rem 1.25rem;
border-bottom: 1px solid #30363d;
gap: 0.75rem;
}
.search-icon {
color: var(--text-muted);
}
#search-input {
flex: 1;
background: transparent;
border: none;
font-size: 1.1rem;
color: var(--text-main);
outline: none;
font-family: var(--font-body);
}
#close-search {
background: rgba(255,255,255,0.1);
border: none;
color: var(--text-muted);
cursor: pointer;
font-size: 0.75rem;
padding: 0.2rem 0.5rem;
border-radius: 4px;
}
.search-body {
padding: 1rem;
overflow-y: auto;
}
#search-results {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
#search-results li {
list-style: none;
}
.result-card {
display: flex;
align-items: flex-start;
gap: 1rem;
padding: 1rem;
border-radius: 8px;
color: var(--text-main);
text-decoration: none;
border: 1px solid rgba(255,255,255,0.05);
background: rgba(255,255,255,0.02);
transition: all 0.15s ease;
}
.result-card:hover {
background: rgba(255,255,255,0.05);
border-color: var(--primary);
}
.result-icon {
color: var(--text-muted);
margin-top: 0.1rem;
}
.result-content {
flex: 1;
min-width: 0;
}
.result-breadcrumbs {
font-size: 0.75rem;
color: var(--text-muted);
margin-bottom: 0.25rem;
display: flex;
align-items: center;
}
.result-title {
font-weight: 600;
font-size: 1rem;
margin-bottom: 0.25rem;
color: var(--primary);
}
.result-desc {
font-size: 0.85rem;
color: var(--text-muted);
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
line-height: 1.4;
}
.result-enter {
opacity: 0;
color: var(--text-muted);
transform: translateX(-5px);
transition: all 0.15s ease;
display: flex;
align-items: center;
height: 100%;
}
.result-card:hover .result-enter {
opacity: 1;
transform: translateX(0);
}
.no-results, .error-msg {
padding: 2rem;
text-align: center;
color: var(--text-muted);
font-style: italic;
}
#search-footer {
padding-top: 1rem;
margin-top: 0.5rem;
border-top: 1px solid rgba(255,255,255,0.05);
text-align: right;
font-size: 0.75rem;
color: var(--text-muted);
}
#search-footer a {
color: var(--text-muted);
text-decoration: underline;
}
:global(mark) {
background: rgba(56, 189, 248, 0.2);
color: inherit;
padding: 0;
font-weight: 500;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
</style>