Giter VIP home page Giter VIP logo

react-headless-pagination's Introduction

React Headless Pagination

Do you want to design your own pagination component, but do not want to worry about the logic of pagination? Then this tiny, performant and a11y proof package is for you. react-headless-pagination is ideal if you are working with a utility-based css framework like TailwindCSS. react-headless-pagination offers plenty of customization and is also fully typed.

npm Jest unit tests issues stargazers

Install

yarn add react-headless-pagination

Demo: Unstyled & TailwindCSS

Storybook

Usage

import { Pagination } from "react-headless-pagination";


...
const [page, setPage] = React.useState<number>(0);

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

return (
  <>
    Current page: {page + 1}
    <Pagination
      {...args}
      currentPage={page}
      setCurrentPage={handlePageChange}
      className=""
      truncableText="..."
      truncableClassName=""
    >
      <Pagination.PrevButton className="">Previous</Pagination.PrevButton>

      <nav className="flex justify-center flex-grow">
        <ul className="flex items-center">
          <Pagination.PageButton
            activeClassName=""
            inactiveClassName=""
            className=""
          />
        </ul>
      </nav>

      <Pagination.NextButton className="">Next</Pagination.NextButton>
    </Pagination>
  </>
);

An example of a styled version can be found in stories/PaginationTailwind.stories.tsx.

Pagination props

currentPage

Type: number

The value of current page. Required.

setCurrentPage

Type: (page: number) => void

Callback function once a page is updated. Can be directly used with a setState (see example above).

totalPages

Type: number

The number pages. Required.

edgePageCount

Type: number

The items size of one side of the edge of pagination. Default: 2

middlePagesSiblingCount

Type: number

The items size of one side of the middle of pagination. Default: 2

className

Type: string

Styles for the pagination container.

truncableText

Type: string

Text to render if a one or more pages are truncated.

truncableClassName

Type: string

Styles which can be applied to the TruncableElement.

Pagination.PrevButton props

children

Type: string | React.ReactNode

Content for the previous button.

className

Type: string

Styles for the previous button.

Pagination.NextButton props

children

Type: string | React.ReactNode

Content for the next button.

className

Type: string

Styles for the next button.

Pagination.PageButton props

as

Type: React.ReactElement Default:

Provide a custom React Element, such as a next/link component.

className

Type: string

dataTestIdActive

Type: string

data-testid in case the page button is active.

dataTestIdInactive

Type: string

data-testid in case the page button is active.

activeClassName

Type: string

Styles in case the page button is active.

inactiveClassName

Type: string

Styles in case the page button is inactive.

Authors

Taken inspiration from @makotot/paginated.

Contributing

PR's are always welcomed.

  • Clone

To contribute to this project, start by cloning the repository to your local machine.

  • Installation with Yarn

Before getting started, ensure that you have Yarn installed on your system. If not, you can install it by following the instructions on the official Yarn website.

  • Run storybook: yarn storybook

To visualize and test components, run Storybook using the following command:

  • Build tailwind: yarn watch:css

For styling, the project uses Tailwind CSS. To build Tailwind, run the following command: This command will watch for changes in your CSS files and rebuild Tailwind accordingly. Make sure to run this during development to keep styles up-to-date.

react-headless-pagination's People

Contributors

thijssmudde avatar damsfx avatar arifin4web avatar

Stargazers

Homa Wong avatar nino avatar Hannah Roksanne avatar Shu-Wei Hsu avatar Andrei Luca avatar Max Wesley avatar Eddy Recio avatar Saint Biology avatar Riccardo Zanutta avatar Andy Nguyen avatar Ammar avatar Shervin Chen avatar Zakher Masri avatar Horacio Alexandre Fernandes avatar Emerson Moreira avatar  avatar Troy Whiteley avatar Dương Đức Trọng avatar  avatar Behzad Ali Mohammad Zad avatar Kaito Iwatsuki avatar 舜岳 avatar yar2001 avatar Anver Sadutt avatar Josh Thomas avatar Heru Gunawan avatar Farhienza Haikal avatar Wesley van Lee avatar Adam Kalamondo avatar Daichi Kimura avatar Henry Babbage avatar Nate van der Vis avatar Nick DiMatteo avatar Sophie avatar Chanhee Lee avatar Stefan E-K avatar Laís Lobato avatar Pedro Fialho avatar Anurag  avatar Gleb Skibitsky avatar Yang Mingshan avatar Anthony Bouch avatar  avatar Chu Văn Lộc avatar Stephen Doxsee avatar Hexi avatar Andrejs Agejevs avatar Fabien Franzen avatar riku avatar Shun Kakinoki avatar しまぶー avatar Adi Rizky avatar Stephan van Stekelenburg avatar  avatar John Mayes avatar Selahattin Ünlü avatar Masaya Takeda avatar  avatar Shaikh kaif avatar Umar Muhammad Zakari avatar Muttakin Islam Hasib avatar Mark Stephen Calderon avatar epidemiks avatar Daniel Hamilton avatar Fateh Waharp avatar  avatar

Watchers

 avatar

react-headless-pagination's Issues

expose usePagination hook to be used by consumers

Hi,

Thank you for this wonderful package.

Could you please expose the usePagination hook so that we can use it to programatically go to next/prev pages in whichever customer elements we use it for?

Feature request: Usage with Router

Greetings, thanks for writing up this great library.

There is currently an issue where wrappers are hard-coded. To be truly headless, users should be able to change the wrapper tag to something else. E.g, <Link> component for each page button instead of <span>.

Potential solution is to add custom renderer function for more flexibility.

Thanks.

TS2303: Circular definition of import alias 'IPaginationProps'.

Hi there, seems to be a great lib but I'm facing the following issue during compilation:

node_modules/react-headless-pagination/dist/Pagination/Pagination.d.ts:2:10 - error TS2303: Circular definition of import alias 'IPaginationProps'.

2 import { IPaginationProps, ButtonProps, PageButtonProps } from "./Pagination.d";

But changing the line above by

import { IPaginationProps, ButtonProps, PageButtonProps } from "./Pagination.d.d";

Seems to make everything work. Is it possible for you to fix this issue ? I don't know how to twist the typing generation in order to get this result .. 😅

Support Next.js

Thank you for your great library. This is a feature request.

The following error occurs when used with Next.js.

Error: Hydration failed because the initial UI does not match what was rendered on the server.

I can use useEffect or dynamic import to get around this, but I would prefer to use this library in a SSR environment if possible. I look forward to Next.js support.

Format number with K/Ms

I have a large dataset to display but some numbers are overlapping due to their long length.
Screenshot 2023-03-15 at 14 21 06

Can I have a way to format number so that instead of displaying 40000 => 40k and so on?

[Feature] Add support to render extra props in the PageButtons

Context:

In one of our project we need to add aria-label in the page buttons where it will also need the page number. For example aria-label="Go to page 3". That is why we need to the support to add/render extra props in the PageButtons that is dependent on the page numbers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.