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

fun-search-bar

v1.1.0

Published

The `SearchBar` class provides a customizable search bar component for web applications. Users can integrate this class to create a search functionality with various customization options.

Downloads

5

Readme

SearchBar Class

The SearchBar class provides a customizable search bar component for web applications. Users can integrate this class to create a search functionality with various customization options.

Constructor

constructor(container, options, callback)
  • container: The DOM element to which the search bar will be appended.
  • options: An object containing configuration options for the search bar.
    • url: The URL from which to fetch data.
    • filter: An array of items to be filtered (optional).
    • styles: Custom CSS styles for the search bar (optional).
      • searchbarSize: Size of the search bar (default: "40px").
      • inputSize: Size of the input field (default: "360px").
      • backgroundIconColor: Background color of the search icon (default: "#F0F8FF").
      • iconColor: Color of the search icon (default: "black").
      • clearIconColor: Color of the clear icon (default: "#999").
      • iconSize: Size of the search icon (default: "20px").
      • position: Specifies the type of positioning method used for the search bar (default: "relative"). Other possible values include "absolute", "fixed", "sticky", and "static".
      • right: Controls the right offset of the search bar. It can be used to adjust the position of the search bar within its container (default: "0px").
    • filterProperty: The property of each item to filter on.
    • triggerWhen: Event trigger for updating search results (click, keyup, or keydown) default value: "click".
  • callback: A function to be called whenever the search results are updated.

Methods

createSearchBar()

Creates and appends the search bar HTML elements to the specified container. Also handles user interactions such as clicking the search icon or typing in the input field.

getResults()

Returns the filtered search results.

Usage

CDN

import  SearchBar  from  "https://unpkg.com/[email protected]/index.js";

function Search() {
    const searchBar = new SearchBar(container, {
      url: "example.com/data",
      filter: "products",
      filterProperty: "title"
    }, () => {
      const results = searchBar.getResults();
      console.log(results);
    });
    searchBar.createSearchBar();
}
Search();

NPM

npm i fun-search-bar
<body>
<div  class="search-bar"></div>
</body>
<script  type="module">
import  SearchBar  from  "./index.js";

const  container  =  document.querySelector(".search-bar");
const  url  =  "./items.json";  

const  searchBar  =  new  SearchBar(
container,
{
url:  url,
filter:  "products.items",
filterProperty:  "name",
},
() => {
const  results  =  searchBar.getResults();
console.log(results);
}
);

searchBar.createSearchBar();

</script>

This function demonstrates how to use the SearchBar class to create a search bar, fetch data asynchronously, and update search results based on user input. Customize the options object according to your requirements, including the styles object for customizing the appearance of the search bar.

Note:

  • filter vs filterProperty: It's important to distinguish between the filter and filterProperty options:
    • filter: This refers to an array of items that are intended to be filtered during the search operation. These items could be fetched from an external data source or provided directly by the user. If the items to be filtered are not available or if the search functionality is not based on a predefined list of items, the filter option should not be provided.
    • filterProperty: This specifies the property of each item in the filter array that will be used for filtering. For example, if the filter array contains a list of products, the filterProperty might be set to "name" to filter the products based on their names.