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

headless-pagination

v0.1.3

Published

Vanilla JS headless pagination library

Downloads

877

Readme

Headless Pagination

Installation

$ npm i -S headless-pagination

Usage

constructor

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 1000, // required
  initialPage: 1, // optional (default shown), page to show at start
  perPage: 24, // optional (default shown), how many items you're showing per page
  maxLinks: 7, // optional (default shown), max number of pagination links to show
});

totalPages()

Returns the total number of pages, takes into account a final page without necessarily having max items.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  perPage: 24,
});

// would be 5 because 100 / 24 = 4.166...
console.log(paginator.totalPages());

hasNext()

Whether or not there is a next page to navigate to. Useful for displaying/disabling arrow based navigation.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 1,
});

// true
console.log(paginator.hasNext());

hasPrevious()

Whether or not there is a previous page to navigate to. Useful for displaying/disabling arrow based navigation.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 1,
});

// false, we're on page 1
console.log(paginator.hasPrevious());

from()

Starting item we're currently displaying based on page offset. Useful for showing a message like Showing 24 to 50 results of 100.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 2,
  perPage: 25,
});

// 26
console.log(paginator.from());

from()

Ending item we're currently displaying based on page offset. Useful for showing a message like Showing 24 to 50 results of 100.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 2,
  perPage: 25,
});

// 50
console.log(paginator.to());

onNext()

Increments the active page. Will not go further than the max page.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 50,
  initialPage: 1,
  perPage: 25,
});

paginator.onNext();
// 2
console.log(paginator.page);

paginator.onNext();
// 2 - doesn't go up past final page
console.log(paginator.page);

onPrevious()

Decrements the active page. Will not go lower than the first page.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 50,
  initialPage: 1,
  perPage: 25,
});

// 1
console.log(paginator.page);

paginator.onPrevious();

// 1
console.log(paginator.page);

setPage(newPage: number)

Updates the page to the provided value

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 50,
  initialPage: 1,
  perPage: 25,
});

// 1
console.log(paginator.page);

paginator.setPage(2);

// 2
console.log(paginator.page);

links()

Returns an array of what links to display to the user.

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 1,
  maxLinks: 7,
  perPage: 10,
});

// notice how there are only 7 links!
expect(paginator.links()).toEqual([
  { label: 1, active: true, disabled: false },
  { label: 2, active: false, disabled: false },
  { label: 3, active: false, disabled: false },
  { label: 4, active: false, disabled: false },
  { label: 5, active: false, disabled: false },
  { label: '...', active: false, disabled: true },
  { label: 10, active: false, disabled: false },
]);

As you navigate through the pages, the structure of the links may change by introducing multiple ... items in order to respect the maxLinks config:

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 1,
  maxLinks: 7,
  perPage: 10,
});

paginator.setPage(5);

expect(paginator.links()).toEqual([
  { label: 1, active: false, disabled: false },
  { label: '...', active: false, disabled: true },
  { label: 4, active: false, disabled: false },
  { label: 5, active: true, disabled: false },
  { label: 6, active: false, disabled: false },
  { label: '...', active: false, disabled: true },
  { label: 10, active: false, disabled: false },
]);

And finally as you get closer to the last page, the links will expand out on the right:

import Paginator from 'headless-pagination';

const paginator = new Paginator({
  totalItems: 100,
  initialPage: 1,
  maxLinks: 7,
  perPage: 10,
});

paginator.setPage(8);

expect(paginator.links()).toEqual([
  { label: 1, active: false, disabled: false },
  { label: '...', active: false, disabled: true },
  { label: 6, active: false, disabled: false },
  { label: 7, active: false, disabled: false },
  { label: 8, active: true, disabled: false },
  { label: 9, active: false, disabled: false },
  { label: 10, active: false, disabled: false },
]);