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

@exmg/exmg-searchbar

v8.0.0

Published

An element to search the needle in a haystack.

Downloads

3

Readme

exmg-searchbar

A customizable search bar element.

This search bar uses mwc-textfield for the input element. More information about styling and the input element itself can be found on the link below: https://github.com/material-components/material-components-web-components/tree/master/packages/textfield

Usage

Import this element to your project with npm i @exmg/exmg-searchbar.

Then add the snippet below into your code.

<exmg-searchbar></exmg-searchbar>

Default Filtering

ExmgSearchBar has it's own filtering methods in it which you can also override. To achieve default filtering, take the example of html code below:

<exmg-searchbar .data="${dataArray}" .filterKeys="${filterKeys}" suggestion-label-key="url"> </exmg-searchbar>

When you pass the data array into data property along with filterKeys, exmg-search bar automatically filters data and shows suggestions.

filterKeys property has to contain specific key names from the type of each item in data.

For example; if you have [{username:"exmachina",name: "Ex Machina"},{username:"livery",name:"Livery"}] as the data array, you can pass ['username'] to filter out username values of data. Or pass ['username', 'name'] to filter out values of both keys.

To determine which value of items passed into data should be shown as the label of each suggestion, use suggestion-label-key just like filterKeys but the only difference is, it accepts a single key as a simple string, not an array.

Custom Suggestions and Filtering

By extending ExmgSearchBar, you can override any of three functions of your selection. These functions are:

  • filter(data: any[], filterKeys: string[], query: string): ExmgSearchSuggestion[]: TemplateResult
  • renderSuggestions(suggestions: ExmgSearchSuggestion[])
  • renderSuggestionsLoading(): TemplateResult

filter expects array of ExmgSearchSuggestion objects. By implementing your own filtering method, you might not need suggestion-label-key since you can place any string value into text property of ExmgSearchSuggestion.

Please check ExmgSearchBar class in order to see how these three functions are working. After that you can extend ExmgSearchBar class and override those functions to create your fancy filtering methods or suggestions.

Showing Suggestions Manually

If you are handling filtering logic manually on the parent, you can follow this way to show suggestions manually. To show suggestons, pass the suggestion data to suggestions property of exmg-searchbar. It accepts array of ExmgSearchSuggestion type of elements into suggestions.

Follow this document for available properties, events, methods and styling.

Styling

| Variable | Default | Description | | ---------------------------------------------------- | ------------------- | ------------------------------------------------ | | --exmg-searchbar-error-color | #b00020 | Error color of search bar | | --exmg-searchbar-hint-color | rgba(0, 0, 0, 0.6) | Hint text color of search bar | | --exmg-searchbar-primary-color | #0071dc | Primary theme color of search bar | | --exmg-searchbar-text-color | rgba(0, 0, 0, 0.87) | Text color of search bar | | --exmg-searchbar-suggestions-spinner-color | #0071dc | Suggestions loading indicator color | | --exmg-searchbar-suggestions-spinner-width | 3px | Suggestions loading indicator spinner width | | --exmg-searchbar-suggestions-max-visible-suggestions | 5 | Max visible suggestions before showing scrollbar | | --exmg-searchbar-suggestions-min-height | 40px | Suggestion item row height | | --exmg-searchbar-suggestions-text-color | rgba(0, 0, 0, 0.6) | Suggestion item text color | | --exmg-searchbar-suggestions-background-color | #ffffff | Suggestions list background color | | --exmg-searchbar-suggestions-z-index | 1 | z-index of suggestions list | | --exmg-searchbar-width | 100% | Width of search bar |

Properties

| Name | Type | Default | Description | | ----------------------- | ---------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | | filterKeys | string[] | undefined | Set of which keys of data should be checked to filter data by default. | | data | any[] | undefined | The data in search bar to filter. | | keepFocus | boolean | false | If true, keeps focus on search bar after query-submit is fired | | keepSuggestionsOnSelect | boolean | false | Option to whether keep suggestions visible or not on suggestion selection | | submitKeys | string[] | ['ENTER'] | Determines which keys should trigger submitting search query | | notifyOnQueryChange | boolean | true | If true, dispatches query-change event with the current query on every change of search input. | | searchQuery | string | '' | Current query of search bar | | suggestionLabelKey | string | undefined | Defines which property of each data item should be placed into suggestions' labels. | | submitOnKeyPress | boolean | true | If true, dispatches query-submit event with the current query upon receiving key press event with specific keys passed to keys string array | | suggestions | ExmgSearchSuggestion[] | [] | List of suggestions to be passed into and displayed | | suggestionsLoading | boolean | false | If true and there are no suggestions passed to element, a loading indicator should be shownbar |

Events

@query-change

Fired on query change.

Payload: {value: string}

@query-submit

Fired on query submit.

Payload: {value: string}

@suggestion-select

Fired on suggestion selection.

This event is also fired when there is only one suggestion and user submits query.

Payload: {value: any, index: number}

Methods

clearSuggestions()

Clears suggestions.

hideSuggestionsLoading()

Hides the loading indicator.

search()

Fires query-submit event with passed query into search bar.

setQuery(query: string)

Sets query.

setSuggestions(suggestions: ExmgSearchSuggestion[])

Sets suggestions.

showSuggestionsLoading()

Shows the loading indicator if there are no suggestions available.