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

@moderntribe/faceted-loops

v1.1.0

Published

A react widget to render a loop of products with filters and pagination

Downloads

24

Readme

Faceted Loops

NPM semantic-release semantic-release

React hooks to fetch records with built-in filtering and pagination

Install

npm install --save @moderntribe/faceted-loops

or

yarn add @moderntribe/faceted-loops

Usage

See the example folder for a full working example.

import React, { Component } from 'react'
import { FacetedLoops } from '@moderntribe/faceted-loops'
import App from './App';

const INITIAL_DATA = {
  filters: [
    // ...
  ],
  records: [
    // ...
  ],
  meta: {
    // ...
  }
}

ReactDOM.render(
  <FacetedLoops initialData={INITIAL_DATA}>
    <App />
  </FacetedLoops>
, document.getElementById('root'))

Filters

Initially the widget will load the filters from a JavaScript object provided by backend. Once the filters or pagination are changed, the widget will request the filters (which must* be in the same format) from backend.

Every filter must have a unique ID and it's used to save the status of each filter in the url as query params.

  {
    type: 'search-box',
    id: "search-by-name",
    placeholder: 'Search here...'
  },
  {
    type: 'checkbox',
    id: 'coffee-tables',
    label: 'Coffee Tables',
    group: 'category'
  },
  {
    type: 'checkbox',
    id: 'lounge-chairs',
    label: 'Lounge Chairs',
    group: 'category'
  },

When a user type hello world in that search box, the url will change from tri.be to tri.be?search-by-name=hello%20world

Checkbox

[
  {
    type: 'checkbox',
    id: 'coffee-tables',
    label: 'Coffee tables',
    name: 'category'
    // group: "category"
  },
  {
    type: 'checkbox',
    id: 'chairs',
    label: 'Chairs',
    name: 'category'
    // group: "category"
  },
]

Examples of encoding:

  • If a user doesn't select any: ?category= or nothing
  • If a user select one: ?category=coffee-tables
  • If a user select two: ?category=coffee-tables,chairs

Radio

[
  {
    type: 'radio-button',
    id: 'male',
    label: 'Male',
    name: 'gender'
    // group: "category"
  },
  {
    type: 'radio-button',
    id: 'female',
    label: 'Female',
    name: 'gender'
    // group: "category"
  }
]

Examples of encoding:

  • If a user doesn't select any: ?gender= or nothing
  • If a user select male: ?gender=male

Search box

{
  type: 'search-box',
  id: 'my-search-box',
  placeholder: 'Search by keyword',
  // group: "category"
}

Examples of encoding:

  • If a user types 'Hello world': ?my-search-box=Hello%20world
  • If a user types nothing: ?my-search-box= or nothing

Toggle

{
  type: 'toggle',
  id: 'in-stock',
  label: 'Coffee tables',
  // group: "category"
}

Examples of encoding:

  • If a user toggle on: ?in-stock=1
  • If a user toggle off: ?in-stock=0 or nothing

Records

These are the different items shown.

Initially the widget will load the records from a JavaScript object provided by backend. Once the filters or pagination are changed, the widget will request the records (which must be in the same format) from backend.

To get the new data, the widget uses the endpoint provided and adds the query params that the current route has. Eg:

  • the endpoint is tri.be/wp-json/records
  • the current route is tri.be?search-by-name=hello%20world
  • the widget will get the data from tri.be/wp-json/records?search-by-name=hello%20world

Meta

Extra useful information that is received initially and in each request.

{
  pagination: {
    type: 'scroll', // 'scroll' | 'all' | 'paged'
    page: 1 // the current page number
    per_page: 5, // max number of records per page
    total_pages: 4
    total_results: 16, // number of records in the current page
  },
  groups: [ // extra information of the groups
    {
      id: 'category',
      label: 'Category'
    }
  ],
  endpoint: 'wp-json/my-endpoint', // API endpoint
  context: { // additional query parameters to pass
    extraParam: '1',
  }
}

Caveats

  • The pagination type and the filters must be the same on every render

License

MIT © moderntribe