Skip to content

Commit

Permalink
feat(search): improvements to the search
Browse files Browse the repository at this point in the history
- Add aria-lable to the search input
- Style the search input and reset buttons
  • Loading branch information
jmiguelv committed Nov 4, 2024
1 parent c1276df commit 4181095
Showing 1 changed file with 60 additions and 31 deletions.
91 changes: 60 additions & 31 deletions src/routes/search/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,51 @@
onMount(() => {
if (browser) {
pagefind = new PagefindUI({
element: '#search',
autofocus: true,
bundlePath: `${base}/pagefind/`,
showEmptyFilters: false,
processResult: (result: any) => {
if (dev && result.url) {
result.url = result.url.replace(/\.html$/, '');
}
// remove footer images from results
if (result.meta.image && result.meta.image.indexOf('assets/footer') >= 0) {
result.meta.image = undefined;
}
return result;
initializeSearch();
}
});
function initializeSearch() {
if (pagefind) {
pagefind.destroy();
}
pagefind = new PagefindUI({
element: '#search',
autofocus: true,
bundlePath: `${base}/pagefind/`,
pageSize: 10,
processResult: (result: any) => {
if (dev && result.url) {
result.url = result.url.replace(/\.html$/, '');
}
});
const searchInput = document.querySelector('#search input') as HTMLInputElement;
if (searchInput) {
searchInput.addEventListener('input', (event: Event) => {
const target = event.target as HTMLInputElement;
const url = new URL(window.location.href);
// remove footer images from results
if (result.meta.image && result.meta.image.indexOf('assets/footer') >= 0) {
result.meta.image = undefined;
}
return result;
},
showEmptyFilters: false
});
setTimeout(() => {
url.searchParams.set('q', target.value);
pushState(url, '');
}, 500);
});
}
const searchInput = document.querySelector('#search input') as HTMLInputElement;
if (searchInput) {
searchInput.setAttribute('aria-label', `Search the site`);
searchInput.addEventListener('input', (event: Event) => {
const target = event.target as HTMLInputElement;
const url = new URL(window.location.href);
triggerSearch();
setTimeout(() => {
url.searchParams.set('q', target.value);
pushState(url, '');
}, 500);
});
}
});
triggerSearch();
}
function triggerSearch() {
const currentUrl = new URL(window.location.href);
Expand All @@ -54,7 +64,9 @@
}
afterNavigate(() => {
triggerSearch();
if (browser) {
initializeSearch();
}
});
</script>

Expand All @@ -71,6 +83,23 @@
</article>

<style>
:global(.pagefind-ui__search-input) {
border-radius: unset !important;
}
:global(.pagefind-ui__search-clear) {
background-color: var(--surface-4) !important;
border-radius: unset !important;
color: var(--text-1) !important;
font-size: var(--font-size-2) !important;
padding-inline: var(--size-4) !important;
&:hover {
background-color: var(--surface-3) !important;
border-bottom: unset !important;
}
}
:global(.pagefind-ui__result-excerpt mark) {
background-color: color-mix(in srgb, var(--yellow) 70%, white);
border-radius: var(--size-1);
Expand Down

0 comments on commit 4181095

Please sign in to comment.