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

nextjs-ssr-paginator

v0.0.9

Published

NextJS 13 Server Side Rendered Paginator

Downloads

240

Readme

Next.js Server Side Rendering Paginator

A pagination component for Next.js applications that supports server-side rendering (SSR).

Installation

Install the package using npm:

npm install nextjs-ssr-paginator

Usage

To use the Paginator component in your Next.js application, follow these steps:

Import the Paginator component:

import Paginator from 'nextjs-ssr-paginator';

Place the Paginator component in your Page:

// page.tsx
import Paginator from "nexjs13-ssr-paginator";
import { getUsers } from "../lib/user.service";

export default async function Page({ searchParams }: { searchParams: { [key: string]: string | string[] | undefined } }) {
    const currentPage = parseInt(searchParams['page'] as string, 10) || 1
    const { data, meta } = await getUsers(currentPage)

    return (
        <div className="p-2">
            <h1 className="mb-2 font-bold">Pagination demo</h1>
            {data.map((user) => (
                <div key={user.id} className="py-2">
                    <div>{user.id} - {user.name} - {user.email}</div>
                </div>
            ))}

            <nav className="pt-6 text-sm">
                <Paginator path="/" params={searchParams} currentPage={currentPage} totalPages={meta.pagination.totalPages}/>
            </nav>
        </div>
    )
}

Use the Pagination Helper on the server:

// lib/user.service.ts
import { paginationHelper, prismaPaginationHelper } from "nextjs-ssr-paginator/pagination_helper"

const perPage = 5;

export async function getData(currentPage: number) {
    const prisma = new PrismaClient();
    const [recordsCount, result] = await prisma.$transaction([
        prisma.user.count({}),
        prisma.user.findMany({
            orderBy: { name: 'asc' },
            ...prismaPaginationHelper(currentPage, perPage)
        }),
    ]);
    return {
        meta: paginationHelper(recordsCount, currentPage, perPage),
        data: result as TUser[]
    }
}

Now, the Paginator will seamlessly integrate into your page:

img.png

See the example

Paginator properties

Here's an example with a comprehensive list of available properties:

// const urlParams = { q: 'image', in: 'actions' } 
<Paginator
    path="/your-route"         // The base path for pagination links
    currentPage={1}            // The current active page number
    totalPages={10}            // The total number of pages
    params={ urlParams }       // (Optional) Url params, will be conwerted to /your-route?q=image&in=actions
    maxVisiblePages={5}        // (Optional) The maximum number of visible pages
    buttonLabelPrevious="Prev" // (Optional) Label for the "Prev" button
    buttonLabelNext="Next"     // (Optional) Label for the "Next" button
    styleClassWrapper="..."    // (Optional) CSS class for paginator wrapper element
    styleClassItem="..."       // (Optional) CSS class for general styling
    styleClassLeft="..."       // (Optional) CSS class for left buttons (e.g., Prev)
    styleClassRight="..."      // (Optional) CSS class for right buttons (e.g., Next)
    styleClassMiddle="..."     // (Optional) CSS class for middle buttons
    styleClassActive="..."     // (Optional) CSS class for active page number
    styleClassInactive="..."   // (Optional) CSS class for inactive page numbers
    styleClassDisabled="..."   // (Optional) CSS class for disabled buttons
/>

Styling

While this component is primarily based on Tailwind CSS, you have the flexibility to customize the styling by redefining your own style classes for different elements.

For instance, to redefine the general styling, left buttons, and right buttons, simply set the new class names for styleClass* props accordingly, and define your own related CSS styles

<Paginator styleClassWrapper="my-paginator" styleClassItem="p-item" otherOptions />

Another way to modify styles is to override existing CSS in your application:

/* global.css */

/* Override pagination styles */
body {
    background: rgb(32,33,36);
    color: rgb(232,234,237);
}

ul.pagination .item {
    @apply border-amber-900;
}
ul.pagination .item-state-disable{
    @apply bg-gray-800 text-gray-500 !important;
}
ul.pagination .item-state-current {
    @apply bg-amber-950 !important;
}
ul.pagination .item-state-other {
    @apply bg-gray-600 text-amber-400 !important
}

Props

  • path: The base path for pagination links.
  • currentPage: The current active page number.
  • totalPages: The total number of pages.
  • params (optional): URL params.
  • maxVisiblePages (optional): The maximum number of visible pages (default: 5).
  • buttonLabelPrevious (optional): Label for the "Previous" button (default: "Prev").
  • buttonLabelNext (optional): Label for the "Next" button (default: "Next").
  • styleClassWrapper (optional) CSS class for paginator wrapper element
  • styleClassItem (optional): CSS class for general styling.
  • styleClassLeft (optional): CSS class for left buttons (e.g., Prev).
  • styleClassRight (optional): CSS class for right buttons (e.g., Next).
  • styleClassMiddle (optional): CSS class for middle buttons.
  • styleClassActive (optional): CSS class for active page number.
  • styleClassInactive (optional): CSS class for inactive page numbers.
  • styleClassDisabled (optional): CSS class for disabled buttons.

License

This package is distributed under the MIT License. See LICENSE for more information.


If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.