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

react-navigable

v1.0.1

Published

Common behavior component for navigating a list of flat items with goto, next and prev capability, therefore useful to help create components such as Paginator, Carousel or Table etc.

Downloads

7

Readme

React Navigable

Common behavior component for navigating a list of flat items with goto, next and prev capability, therefore useful to help create components such as Paginator, Carousel or Table etc.

Usage

  <Navigable 
    items={items}
    activeItem={activeItem}
    onSelectItem={onSelectItem}
    circular={false}
    backward={false}
    locked={false}
  >
    {(    
      items, activeItem,       
      goto, prev, next,
      prevIndex, canPrev,
      nextIndex, canNext,
    ) => {
      <div></div>
    }}
  </Navigable>

The render prop passes items and activeItem through to the child component, along with other utility functions, such as

  • goto(item), jump to one item
  • prev(), go to the previous item
  • next(), go to the next item

Depending if circular, backward, or locked flag is set, the list can behave like going circular, backward or nowhere.

Examples

Paginator

Assuming items is array of numbers [1,2,3,4,5]. The render prop can manage

  • click to each page
  • feel for active page
  • click to the previous button
  • disable previous button
  ({ items, activeItem, canPrev, prev, goto }) => (
    <button 
      onClick={e => { prev() }}
      disabled: !canPrev
    >Prev</button>
    <ul>
      {items.map(i => (
        <li 
          key={i}
          onClick={e => { goto(i) }}
          className={(activeItem === i) && 'active'}
        >{i}</li>
      ))}
    </ul>
  )

This component can be further used for Carousel and Table referred as PaginatorComponent.

Carousel

Assume the slide content can be rendered by ItemComponent, the render prop can be responsible for

  • display for each slide
  • click to the next button
  • disable next button
  • pagination using the above example
  {({ items, activeItem, canNext, next, goto, ...all }) => (
    <div>
      <div className='carousel slide fade'>
        <div className="carousel-inner">
          {items.map((_, i) => (
            <ItemComponent 
              key={i}
              index={i} 
              slide={true}
              active={i === activeItem}
              className='carousel-item'
            />
          ))}
        </div>
        <button
          action="next"
          className: "carousel-control-next",
          onClick: e => { next() }
          disabled: !canNext
        />             
      </div>
      {<PaginatorComponent {...all} />}
    </div>
  )

Table

Assume the main table area can be rendered by TbodyPage, the render prop can be responsible for

  • display for current visible table content
  • pagination using the above example
  {({ activeItem, ...all }) => (
    <TableComponent>
      <TbodyPage 
        page={activeItem}
      />
      {<PaginatorComponent {...all} />}
    </TableComponent>
  )}
</Navigable>

State management

The navigation state isn't self contained, instead is controlled via the prop activeItem and the callback onSelectItem, similar to an input value and onChange.

The state management isn't included, but can be implemented quickly via your own version, such as hooks.

  const [activeItem, setActiveItem] = useState(items[0])

  <Navigable 
    items={items} 
    activeItem={activeItem}
    onSelectItem={setActiveItem}
  >
    ...
  </Navigable>

Once a state management is applied, the above code (or component) becomes self contained.