3.0.0 • Published 10 months ago

nextjs-pagination v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Nextjs-Pagination

Nextjs-Pagination is a powerful, customizable, and easy-to-use pagination component built specifically for Next.js projects. Designed with user experience in mind, it provides flexible configuration options so you can adjust the pagination behavior and look-and-feel to suit your needs. Nextjs-Pagination is built with TypeScript, offering type safety and autocompletion support in supported editors. Screenshot 2023-05-25 175609

Features

  • Customizable button color and shape: You can easily modify the color and shape of the pagination buttons. For instance, to make the buttons green and circular, you can pass color="green" and shape="circle" as props.
  • Set maximum number of page buttons to display: You can limit the number of page buttons visible at a time. This is useful for large datasets where you don't want to display too many page buttons. For instance, to display up to 7 page buttons at a time, pass buttonCount={7} as a prop.
  • Option to show or hide 'Next' and 'Prev' buttons: If you want to include 'Next' and 'Prev' buttons for easier navigation, you can do so by passing showNextPrev={true} as a prop.
  • Option to show or hide 'First' and 'Last' buttons: Similarly, to include 'First' and 'Last' buttons for quick navigation to the first and last pages, pass showFirstLast={true} as a prop.
  • Callback function for page changes: You can provide a function to be called whenever the page changes. This is useful for fetching new data based on the current page. Pass this function as the onPageChange prop.
  • Customizable text for 'First', 'Prev', 'Next', 'Last' buttons: You can customize the text of the navigation buttons to suit your needs. They can accept any valid React node, such as text, emojis, symbols, etc. Here are some examples:
    • Text: For instance, firstText="Start", prevText="Backward", nextText="Forward", lastText="End"
    • Emojis: firstText="⏮", prevText="⏪", nextText="⏩", lastText="⏭"
    • Symbols: firstText="⇤", prevText="←", nextText="→", lastText="⇥"
    • Special Symbols: firstText="⟪", prevText="⟨", nextText="⟩", lastText="⟫"
  • Customizable CSS classes for pagination container and buttons: You can add your own CSS classes to the pagination container and buttons for more advanced styling. For example, to add the CSS classes "pagination-container" and "pagination-button" to the pagination container and buttons respectively, pass className="pagination-container" and buttonClassName="pagination-button" as props. Remember to replace these with your own CSS classes.

Installation

Install the package using npm:

TypeScript

npm install nextjs-pagination

JavaScript

npm install nextjs-pagination

Usage

First, import the Pagination component from the nextjs-pagination package then use it in your components. Below are the 3 required props needed for JavaScript and TypeScript components:

TypeScript

import { Pagination } from 'nextjs-pagination';

const TSExample = () => {
  const handlePageChange = (page: number) => {
    setCurrentPage(page);
  };

  return (
    <Pagination
      onPageChange={handlePageChange}
      totalItems={100}
      itemsPerPage={10}
    />
  );
};

JavaScript

import Pagination from 'nextjs-pagination';

const JSExample = () => {
  const handlePageChange = (page) => {
    setCurrentPage(page);
  };

  return (
    <Pagination
      onPageChange={handlePageChange}
      totalItems={100}
      itemsPerPage={10}
    />
  );
};

This will render a pagination component for 100 items with 10 items per page.

API

Below are the props that you can pass to the Pagination component:

PropDescriptionTypeDefaultRequired
totalItemsTotal number of itemsnumber-Yes
itemsPerPageNumber of items per pagenumber-Yes
onPageChangeCallback function called when page changesfunction() => {}Yes
colorButton colorstring'#007bff'No
shapeButton shape ('circle' or 'square')string'square'No
buttonCountMaximum number of page buttonsnumber5No
showNextPrevWhether to show 'Next' and 'Prev' buttonsbooleanfalseNo
showFirstLastWhether to show 'First' and 'Last' buttonsbooleanfalseNo
onSuccessCallback function called when a valid page is selectedfunction() => {}No
onErrorCallback function called when an error occursfunction() => {}No
customStylesCustom styles for the buttons. You can use this to apply CSS-in-JS styles directly to the buttons.React.CSSProperties{}No
firstTextText for the 'First' button. Can be any valid React node, such as text, emojis, symbols, etc.React.ReactNode'First'No
prevTextText for the 'Prev' button. Can be any valid React node, such as text, emojis, symbols, etc.React.ReactNode'Prev'No
nextTextText for the 'Next' button. Can be any valid React node, such as text, emojis, symbols, etc.React.ReactNode'Next'No
lastTextText for the 'Last' button. Can be any valid React node, such as text, emojis, symbols, etc.React.ReactNode'Last'No
classNameCustom CSS class name for the pagination container. You should replace this with your own CSS class name.string''No
buttonClassNameCustom CSS class name for the buttons. You should replace this with your own CSS class name.string''No

File Structure

Examples

Want to see it in action? CLICK HERE! to check our the example on replit.

(please give it a few seconds to start up the server when you load)

TypeScript Example

import { Pagination } from 'nextjs-pagination';

const ExamplePage: React.FC = () => {
  const handlePageChange = (page: number) => {
    setCurrentPage(page);
  };

  return (
    <Pagination
      totalItems={500}
      itemsPerPage={20}
      onPageChange={handlePageChange}
      color="green"
      shape="circle"
      buttonCount={7}
      showNextPrev={true}
      showFirstLast={true}
      onSuccess={(page: number) => console.log("Current page: ", page)}
      onError={(error: Error) => console.error(error)}
      firstText="First"
      lastText="Last"
      prevText="Prev"
      nextText="Next"
      className="custom-pagination-container-class"
      buttonClassName="custom-pagination-button-class"
    />
  );
};

JavaScript Example

import Pagination from 'nextjs-pagination';

const ExamplePage = () => {
  const handlePageChange = (page) => {
    setCurrentPage(page);
  };

  return (
    <Pagination
      totalItems={500}
      itemsPerPage={20}
      onPageChange={handlePageChange}
      color="green"
      shape="circle"
      buttonCount={7}
      showNextPrev={true}
      showFirstLast={true}
      onSuccess={(page) => console.log("Current page: ", page)}
      onError={(error) => console.error(error)}
      firstText="First"
      lastText="Last"
      prevText="Prev"
      nextText="Next"
      className="custom-pagination-container-class"
      buttonClassName="custom-pagination-button-class"
    />
  );
};

Contributing

We welcome contributions! Whether it's a bug report, feature request, or a code contribution, we greatly appreciate all help to improve Nextjs-Pagination. For major changes, please open an issue first to discuss what you would like to change.

License

Nextjs-Pagination is MIT licensed.

3.0.0

10 months ago

2.6.0

10 months ago

2.5.0

12 months ago

2.4.0

12 months ago

2.3.0

12 months ago

2.2.0

12 months ago

2.1.0

12 months ago

2.0.0

12 months ago

1.8.0

12 months ago

1.7.0

12 months ago

1.6.0

12 months ago

1.5.0

12 months ago

1.4.0

12 months ago

1.3.0

12 months ago

1.2.0

12 months ago

1.0.0

12 months ago