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

svelte-search

v2.1.0

Published

Accessible, customizable Svelte search component

Downloads

3,598

Readme

svelte-search

NPM

Accessible, customizable Svelte search component.

This search component is composed using semantic form and input elements.

See svelte-typeahead for a search component with dropdown results.

Try it in the Svelte REPL.


Installation

Yarn

yarn add -D svelte-search

NPM

npm i -D svelte-search

pnpm

pnpm i -D svelte-search

Styling

This component is unstyled by design. Target the component using the [data-svelte-search] selector.

:global([data-svelte-search] input) {
  width: 100%;
  font-size: 1rem;
  padding: 0.5rem;
  margin: 0.5rem 0;
  border: 1px solid #e0e0e0;
  border-radius: 0.25rem;
}

Usage

Two-way binding

<script>
  import Search from "svelte-search";

  let value = "";
</script>

<Search bind:value />

{#if value}
  <button on:click={() => (value = "")}>Clear "{value}"</button>
{/if}

on:submit

The input element is contained in a form. Use the forwarded submit event to obtain the value when pressing "Enter."

<Search bind:value on:submit={() => console.log("submit", value)} />

Label with placeholder text

$$restProps are forwarded to the input element.

<Search label="My label" placeholder="Placeholder text..." />

Label slot

<Search>
  <span slot="label" style="color: red;">Custom label</span>
</Search>

Visually hidden label

Set hideLabel to true to visually hide the label.

<Search hideLabel label="My label" placeholder="Placeholder text..." />

Focus (declarative)

Use the autofocus prop to declaratively focus the input.

<Search autofocus />

Focus (programmatic)

Bind the ref prop to programmatically focus the input.

<script>
  import Search from "svelte-search";

  let ref = null;
</script>

<Search bind:ref />

<button on:click={() => ref.focus()}>Focus</button>

Debounced input

Use the debounce prop to specify the debounce value in milliseconds.

<script>
  import Search from "svelte-search";

  let events = [];
</script>

<Search
  debounce={800}
  on:type={({ detail: value }) => (events = [...events, value])}
/>

<pre>{JSON.stringify(events, null, 2)}</pre>

API

$$restProps are forwarded to the input element.

Props

| Prop name | Type | Default value | | :----------------------- | :----------------- | :-------------------------------------- | | value | string | "" | | label | string | "Search" | | hideLabel | boolean | false | | debounce | number | 0 | | ref | HTMLInputElement | null | | id | string | "search" + Math.random().toString(36) | | removeFormAriaAttributes | boolean | false | | autofocus | boolean | false |

Forwarded events

  • on:input
  • on:change
  • on:submit
  • on:focus
  • on:blur
  • on:keydown

Dispatched events

  • on:type: fired when the the input value is updated
  • on:clear: fired when clicking the "X" button to clear the input value
<Search
  on:type={(e) => {
    console.log("type", e.detail); // input value
  }}
  on:clear={() => {
    console.log("clear");
  }}
/>

TypeScript

Svelte version 3.31 or greater is required to use this component with TypeScript.

TypeScript definitions are located in the types folder.

Changelog

Changelog

License

MIT