npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelter-search-ui

v0.0.7

Published

A Svelte and Tailwind library that provides components for building search functionalities, including `SearchDialog`, `SearchItem`, and `SearchGroup`.

Downloads

488

Readme

Svelter Search UI

A Svelte and Tailwind library that provides components for building search functionalities, including SearchDialog, SearchItem, and SearchGroup.

Svelter Search UI Preview

Table of Contents

Requirements

Before using this library, ensure you have the following installed in your project:

  • Svelte - A component framework that compiles code to tiny, framework-less vanilla JS.
  • Tailwind CSS - A utility-first CSS framework for rapid UI development.
  • @tailwindcss/forms - A plugin that provides a basic reset for form styles.

Installation

Install the library and its peer dependencies using npm:

npm install svelter-search-ui tailwindcss @tailwindcss/forms

Note: Make sure you have Svelte set up in your project. If not, you can create a new Svelte project

Update Tailwind's Content Paths

In the consuming project's tailwind.config.js (or tailwind.config.cjs), you need to include your library's components in the content array:

// tailwind.config.js
module.exports = {
  content: [
    './src/**/*.{html,js,svelte,ts}',
    './node_modules/svelter-search-ui/**/*.{html,js,svelte,ts}',
  ],
  theme: {
    extend: {},
  },
  plugins: [
    require('@tailwindcss/forms'),
  ],
};

Usage

Import the components into your Svelte application:

<script>
  import { SearchDialog, SearchItem, SearchGroup } from 'svelter-search-ui';
</script>

SearchDialog

SearchDialog is the main component that renders the search interface.

Props

  • show (boolean, default: false): Controls the visibility of the search dialog.
  • placeholder (string, default: "Search..."): Placeholder text for the search input.
  • resultCount (number, default: 0): The number of search results found.
  • isLoading (boolean, default: false): Indicates if the search results are loading.
  • showRecent (boolean, default: true): Determines if recent search items should be displayed when there's no input.
  • noRecentItemsText (string, default: "No recent searches to show"): Text to display when there are no recent searches.
  • recentItemsGroupName (string, default: "Recent Searches"): The name for the recent items group.

Events

  • search: Dispatched when the search input changes (after a debounce of 350ms). The event detail contains { value }.

Slots

  • recent-results: Slot to customize the recent search results section.
  • no-recent-results: Slot to customize the content when there are no recent searches.
  • info: Slot to provide additional information or help content.
  • not-found: Slot to customize the content when no search results are found.
  • search-results: Slot to display the search results.
  • footer: Slot to customize the footer content.

Example

<script>
  import { SearchDialog } from 'svelter-search-ui';

  let showDialog = true;

  function handleSearch(event) {
    const searchValue = event.detail.value;
    // Perform search logic here
  }
</script>

<SearchDialog
  bind:show={showDialog}
  placeholder="Type to search..."
  on:search={handleSearch}
>
  <!-- Customize slots if needed -->
</SearchDialog>

SearchItem

SearchItem represents an individual search result or suggestion.

Props

  • title (string): The title of the search item.
  • url (string, default: "#"): The URL associated with the item.
  • target (string, optional): Specifies where to open the linked document ("_blank", "_self", etc.).
  • subtitle (string, optional): A brief description or subtitle.
  • isRecentItem (boolean, default: false): Indicates if the item is a recent search.

Events

  • select: Dispatched when the search item is selected.

Slots

  • media: Slot to customize the media/icon displayed alongside the item.

Example

<SearchItem
  title="Item Title"
  subtitle="Item subtitle"
  url="/item-url"
  target="_blank"
  on:select={() => {
    // Handle item selection
  }}
>
  <!-- Customize the media slot if needed -->
  <div slot="media">
    <!-- Custom icon or image -->
  </div>
</SearchItem>

SearchGroup

SearchGroup groups multiple SearchItem components under a common category.

Props

  • name (string): The name of the group.
  • resultCount (number, optional): The number of items in the group.

Slots

  • Default Slot: Place your SearchItem components here.

Example

<SearchGroup name="Group Name" resultCount={2}>
  <SearchItem title="Item 1" />
  <SearchItem title="Item 2" />
</SearchGroup>

Example

Here's a basic example of how to use the components together:

<script>
  import { SearchDialog, SearchItem, SearchGroup } from 'svelter-search-ui';
  let showDialog = true;

  let searchResults = [];
  let isLoading = false;

  function handleSearch(event) {
    const searchValue = event.detail.value;
    isLoading = true;

    // Simulate async search operation
    setTimeout(() => {
      searchResults = [
        { title: 'Apple', subtitle: 'A sweet red fruit' },
        { title: 'Banana', subtitle: 'A long yellow fruit' },
      ];
      isLoading = false;
    }, 1000);
  }
</script>

<SearchDialog
  bind:show={showDialog}
  isLoading={isLoading}
  resultCount={searchResults.length}
  on:search={handleSearch}
>
  <ul slot="search-results">
    <SearchGroup name="Fruits" resultCount={searchResults.length}>
      {#each searchResults as result}
        <SearchItem
          title={result.title}
          subtitle={result.subtitle}
          on:select={() => (showDialog = false)}
        />
      {/each}
    </SearchGroup>
  </ul>
</SearchDialog>

License

This project is licensed under the MIT License.