Git Product home page Git Product logo

svelte-fuzzy's Introduction

svelte-fuzzy

NPM

Fuse.js binding for fuzzy text match highlighting.


Installation

Yarn

yarn add -D svelte-fuzzy

NPM

npm i -D svelte-fuzzy

Usage

<script>
  import Fuzzy from "svelte-fuzzy";

  let query = "old";

  // Fuse.js options
  let options = { keys: ["title"] };

  // Fuse.js data
  let data = [
    {
      title: "Frankenstein; Or, The Modern Prometheus",
      author: "Mary Wollstonecraft Shelley",
    },
    {
      title: "A Christmas Carol in Prose; Being a Ghost Story of Christmas",
      author: "Charles Dickens",
    },
    { title: "Pride and Prejudice", author: "Jane Austen" },
    { title: "The Scarlet Letter", author: "Nathaniel Hawthorne" },
    { title: "Alice's Adventures in Wonderland", author: "Lewis Carroll" },
  ];

  let formatted = [];
</script>

<input bind:value={query} />

<br />

<button on:click={() => (query = "carol")}>Search "carol"</button>
<button on:click={() => (query = "")}>Clear</button>

<Fuzzy {query} {data} {options} bind:formatted />

{#each formatted as item}
  {#each item as line}
    <li>
      {#each line as { matches, text }}
        {#if matches}
          <mark>{text}</mark>
        {:else}
          {text}
        {/if}
      {/each}
    </li>
  {/each}
{/each}

Highlighter

You can use the Highlighter component for fuzzy text highlighting. Matching characters are wrapped in a mark element.

<script>
  import { Highlighter } from "svelte-fuzzy";
</script>

{#each formatted as item}
  {#each item as line}
    <li>
      <Highlighter {line} />
    </li>
  {/each}
{/each}

API

Props

Fuzzy

Prop name Value
query string (default: "")
data FuzzyData (default: [])
options FuzzyOptions
result FuzzyResult (default: [])
formatted FuzzyFormattedResult (default: [])

Highlighter

Prop name Value
line HighlighterLine (default: [])

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.