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

roving-index

v1.0.4

Published

A module for managing roving index (like roving tabindex)

Downloads

13

Readme

Roving Index

If you've made keyboard accessible/navigable JavaScript widgets like an autocomplete or a custom select menu, then you're familiar with the idea of roving tabindex. Roving Index aims to provide a common solution to this problem, assisting with managing roving index, tab or otherwise.

Installation

npm install --save roving-index

Usage

RovingIndex uses the UMD pattern, so it can be used with most module loaders or without one.

import RovingIndex from 'roving-index';

const rovingIndex = new RovingIndex({
  total: 5,
});

rovingIndex.next((oldIndex, newIndex) => {
    console.log(newIndex); // 0
});

API

The RovingIndex constructor accepts a single parameter: An options object that accepts the following properties:

  • cyclic: true by default. Determines if the index cycles or not. For example, if the total is 3 and the current index is 2, incrementing the index will set it to 0. If false, then the index will stop at min and max values.
  • index: The default index (zero-based). This represents the active item and cannot be greater than the total. -1 by default.
  • pausable: false by default. If true, then the index, when cycled, will pause at the index -1.
  • total: The max index. This represents the total number of items, minus one.

Examples

Cyclic Index

The following example demonstrates the default behavior (cyclic index). Cyclic index with cycle through the available indices start at 0 when the final index is reached and starting at the final index when -1 is reached. Note that the index is zero-based.

import RovingIndex from 'roving-index';

const rovingIndex = new RovingIndex({
  total: 3,
});

rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 0
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 1
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 2
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 0
Finite Index

The following example demonstrates the finite index. The finite will stop at the max and min indices.

import RovingIndex from 'roving-index';

const rovingIndex = new RovingIndex({
  cyclic: false, // create a finite index
  total: 3,
});

rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 0
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 1
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 2
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 2
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 2
Pausable Index

The following example demonstrates the pausable index. The pausable index will stop at -1 before cycling.

import RovingIndex from 'roving-index';

const rovingIndex = new RovingIndex({
  pausable: true, // create a pausable index
  total: 3,
});

rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 0
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 1
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 2
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // -1 (I paused!)
rovingIndex.next((oldIndex, newIndex) => { console.log(newIndex); }); // 0

Public Methods

The following code snippets assume an instance of RovingIndex has been created:

import RovingIndex from 'roving-index';

const rovingIndex = new RovingIndex();
rovingIndex.clearIndex()

Clears the index, setting its value to -1.

rovingIndex.getIndex()

Returns the current index.

rovingIndex.getTotal()

Returns the current total.

rovingIndex.getTotal()

Sets the total. The total represents to boundaries (min/max) for the index.

rovingIndex.prev(callback)

Decrements the index, triggering the provided callback afterwards. The callback receives two parameters:

  • oldIndex: the previous index
  • newIndex: the new index
rovingIndex.next(callback)

Increments the index, triggering the provided callback afterwards. The callback receives two parameters:

  • oldIndex: the previous index
  • newIndex: the new index
rovingIndex.setIndex(index, total)

Set the active index, and, optionally, the total.