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

@dflare/svelte-page-navigation

v1.0.7

Published

A simple Svelte component to handle pagination

Downloads

53

Readme

Svelte Page Navigation

MIT License GitHub release npm version

About The Project

A simple Svelte component for handling pagination, nothing more. Corrections and suggestions are welcome.

paginator-screenshot-1

Built With

Svelte

SvelteKit

The demo app utilizes Pico.css as its styling framework.

The SVG icons used in this project are retrieved from Iconify, specifically the following icons:

  • material-symbols:dark-mode-outline
  • material-symbols:light-mode-outline
  • material-symbols:chevron-left
  • material-symbols:chevron-right
  • material-symbols:first-page
  • material-symbols:last-page
  • material-symbols:more-horiz
  • mdi:github

Getting Started

  1. Install the package.

    npm i @dflare/svelte-page-navigation
  2. Import the Paginator component.

    import {Paginator} from "@dflare/svelte-page-navigation";
  3. Add the Paginator component to the markup and handle the chenge event.

    <Paginator on:change={pageChangeHandler} />

Paginator component

The Paginator component is the main component that implements pagination and provides the following properties:

  • pages: a number that represents the total number of items to be managed. Its default value is 10.
  • currentPage: a number indicating the current page (Zero-based numbering). Its default value is 0.
  • initChunkSize: a number indicating the size of page grouping. If this number is smaller than the total number of pages and the available space is sufficient, then the page navigation buttons will be displayed in groups of size equal to initChunkSize. Its default value is equal to the total number of pages set with the pages property.
  • initShowFirstButton: a boolean that, when set to true, enables displaying a button to directly navigate to the first page. The default value is true.
  • initShowLastButton: a boolean that, when set to true, enables displaying a button to directly navigate to the last page. The default value is true.
  • showCurrentPageLabel: a boolean that, when set to true, enables displaying a label with the current page number. The default value is false.
  • customPaginatorButton: it allows to specify a custom component that represents the button used by Paginator component.

If there is enough available space, the Paginator component will display all the buttons for the pages (based on the values set for the pages and initChunkSize properties). Otherwise, it will automatically reduce the number of visible buttons.

Usage

Basic example

<script>
	import {Paginator} from '@dflare/svelte-page-navigation';
	
	let pages = 20;
	let currentPage = 6; // It can be omitted if you want to use the default value of 0 (Zero-based numbering)
	let initChunkSize = 10;
	let initShowFirstButton = true; // It can be omitted if you want to use the default value of true 
	let initShowLastButton = true; // It can be omitted if you want to use the default value of true
  let showCurrentPageLabel = true; // It can be omitted if you want to use the default value of false

	function pageChangeHandler(event) {
		currentPage = event.detail.page;
	}
</script>

<div>
	Current Page: {currentPage + 1}
</div>

<Paginator 
	pages={pages}
	currentPage={currentPage}
	initChunkSize={initChunkSize}
	initShowFirstButton={initShowFirstButton}
	initShowLastButton={initShowLastButton}
	showCurrentPageLabel={showCurrentPageLabel}
	on:change={pageChangeHandler} />

Result

paginator-screenshot-2

Custom button example

To create a completely custom button, it is sufficient to create a new component that has the structure and properties of the PaginatorButton default component. The custom component should be passed to the Paginator component, which will use it instead of the default one.

App.svelte

<script>
    import {Paginator} from '@dflare/svelte-page-navigation';
    import CustomButton from './CustomButton.svelte';
	
    let pages = 20;
    let currentPage = 6;
    let initChunkSize = 10;

    function pageChangeHandler(event) {
      currentPage = event.detail.page;
    }
</script>

<div>
	Current Page: {currentPage + 1}
</div>

<Paginator
    customPaginatorButton={CustomButton}
    pages={pages}
    currentPage={currentPage}
    initChunkSize={initChunkSize}
    on:change={pageChangeHandler} />

CustomButton.svelte

<script>
    export let title = undefined;
    export let disabled = false;
    export let page = undefined;
    export let active = false;
</script>

<button type="button" disabled={disabled} data-page={page} class:active={active} title={title} on:click>
    <slot></slot>
</button>

<style>
    button {
        display: flex;
        align-items: center;
        justify-content: center; 
        width: 75px;
        height: 75px;
        padding: 0.75rem 1rem;
        border: 0;
        margin: 0;        
        border-radius: 50%;
        text-align: center;
        cursor: pointer;
        background-color: #1095c1;
        color: #f0f0f0;
        word-break: break-all;
        overflow: hidden;
        transition: background-color, opacity 0.15s ease;
    }

    button:hover {
        background-color: #07aee5;
    }

    button:active {
        transform: scale(0.95);
    }

    button[disabled] {
        opacity: 0.5;
        pointer-events: none;
    }

    button.active {
        background-color: #4CAF50;
        font-weight: bold;
    }
</style>

Result

paginator-screenshot-3

License

Distributed under the MIT License. See LICENSE.md for more information.