How to Create an Astro Search component 🔍🤔

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • Astro-search-component

  • Note: The source code for this project is available on this GitHub repository.

  • tailwindcss-typography

    Beautiful typographic defaults for HTML you don't control.

  • To do this, install the Astro Tailwind integration, and the typography plugin to modify the styles in markdown rendered pages, like the posts.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • Tailwind CSS

    A utility-first CSS framework for rapid UI development.

  • For now, don't worry about the styles. We'll be applying Tailwind CSS later in the tutorial.

  • iconify

    Universal icon framework. One syntax for FontAwesome, Material Design Icons, DashIcons, Feather Icons, EmojiOne, Noto Emoji and many other open source icon sets (over 150 icon sets and 200k icons). SVG framework, React, Vue and Svelte components!

  • I'll also use a search icon from Iconify, one of the largest collections of open-source icons.

  • Fuse

    Lightweight fuzzy-search, in JavaScript

  • import Fuse from 'fuse.js'; import { useState } from 'react'; // Configs fuse.js // https://fusejs.io/api/options.html const options = { keys: ['frontmatter.title', 'frontmatter.description', 'frontmatter.slug'], includeMatches: true, minMatchCharLength: 2, threshold: 0.5, }; function Search({ searchList }) { // User's input const [query, setQuery] = useState(''); const fuse = new Fuse(searchList, options); // Set a limit to the posts: 5 const posts = fuse .search(query) .map((result) => result.item) .slice(0, 5); function handleOnSearch({ target = {} }) { const { value } = target; setQuery(value); } return ( <> Searchlabel> {query.length > 1 && (

    Found {posts.length} {posts.length === 1 ? 'result' : 'results'} for '{query}' p> )}

      {posts && posts.map((post) => (
    • {post.frontmatter.title}a> {post.frontmatter.description} li> ))} ul> ); } export default Search;

  • astro

    The web framework for content-driven websites. ⭐️ Star to support our work!

  • Fortunately, the Astro web framework has an API, where you can fetch the markdown or MDX files you need. After fetching all the data, we'll able to search for a specific query provided by our users.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts