Astro + Pagefind + Solid = 🤯
Build a search UI for your Astro site using Pagefind and SolidJS
Build a search UI for your Astro site using Pagefind and SolidJS
Astro is a framework for building blazingly fast statically generated sites. It is a great choice when building content-driven websites (this site is built with Astro!). But, when it comes to searching your site, there aren’t many great picks except for Pagefind, a full-text search library written in Rust which helps it reach outstanding performance.
Pagefind is really easy to integrate with any framework, because the only requirement is, well, static site generation! Just run the following command in your terminal (assuming that your generated site files reside in ./dist
):
The above command will generate a pagefind
directory in our dist
folder. This directory contains everything related to Pagefind: binaries (WASM), scripts (JS), indexes. It also contains Pagefind’s modular UI library (pagefind-modular-ui.js
), but we won’t need that today. Only 2 files out of this whole directory are actually in our field of interest: pagefind.js
and pagefind-highlight.js
. The first one is the primary Pagefind library, which we’ll need to implement searching, and the last one is the highlighting library used by Pagefind. For highlighting, skip to Highlighting.
Search UI
Now that we know the basics, it’s time to create the search UI!
Our stack
First, let’s settle on all libraries we will use for building the search UI. I haven’t included Astro in this list because the search UI will be able to be consumed outside of an Astro project.
We will use Solid as our UI framework. I also recommend you to install the following Solid Primitives, as they will greatly simplify the process: refs
, media
, event-listener
and utils
.
Setting up
Now that we know our stack, let’s install everything:
Getting started
Dialog
Most websites implement their search UI as a modal dialog. That’s what we’ll do now.
Animation
You can skip this step if you don’t wish to add animation to your dialog.
Adding a responsive anchor
Now that our search UI is fully working, all that’s left to do is to use it, right? Well, not quite. Remember how we must pass a search anchor to our component? Even though is anchor can be any clickable HTML element, I think it would be good for us to make a responsive search anchor.
We will have our component have 3 different states:
- Search icon button - smallest
- Search button with label - medium
- Search bar - largest
If you feel like the medium state is unnecessary, you can skip it, but I will show you how to make it just to demonstrate the use of adaptive breakpoints.
Since our UI design is based on Material You, it’s a good idea to keep the style consistent and use Material’s official specifications for our anchor components:
Here are the three components:
Search bar
Search icon
Finally, it’s time to unite all those 3 components into one. We’ll call it SearchAnchor
.
Let’s update our index.ts
file to export all new components:
Using the component
All that’s left to do now is just to use our component. Let’s add it to our Header
component:
Highlighting
Now that we have our search working, we can move on to add results highlighting!
The Pagefind Highlighter will add the pagefind-highlight
class to all highlighted elements, so we can style them appropriately.
Then, we just need to insert the Highlight
component into our page’s head
element:
Styling
If you are not happy with the default highlight styles (which are quite nice!), you can give the highlight some shiny new styles.
Here’s a basic example of custom highlight styles:
Going further: automatic indexing
If you want to automatically run Pagefind after building your site, you could write a simple Astro integration.
First, we need to install @types/node
:
Then, add it to your Astro config:
And that’s it! Now Pagefind will run right after your site has been built. Try it out:
../../components/primitives/button