1.0.1 â€Ē Published 5 months ago

@aniruddha1806/paginator v1.0.1

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

React Paginator

A powerful, customizable pagination component for React applications with TypeScript support. Features traditional pagination, infinite scroll, animations, and extensive customization options.

Installation

npm install @aniruddha1806/paginator

Features

  • 📄 Traditional pagination with page numbers
  • 🔄 Infinite scroll support with load more functionality
  • 🎭 Animated transitions (slide, zoom effects)
  • ðŸŽŊ Jump to page functionality
  • ðŸŽĻ Extensive styling customization
  • ðŸ“ą Responsive design with ellipsis for large page ranges
  • ðŸ”Ē Item count and range display
  • â™ŋ Accessibility features with ARIA support
  • 🎛ïļ Customizable labels and buttons
  • 📝 TypeScript support with full type definitions
  • ðŸŠķ Zero dependencies and lightweight

Quick Start

Basic Pagination

import Paginator from '@aniruddha1806/paginator';

function App() {
  const [currentPage, setCurrentPage] = useState(1);
  const totalPages = 10;

  return (
    <Paginator
      totalPages={totalPages}
      currentPage={currentPage}
      onPageChange={setCurrentPage}
    />
  );
}

Props

Core Props

PropTypeDefaultDescription
totalPagesnumberrequiredTotal number of pages
currentPagenumberrequiredCurrent active page (1-indexed)
onPageChange(page: number) => voidrequiredPage change callback
maxVisiblePagesnumber5Maximum visible page buttons

Styling Props

PropTypeDefaultDescription
classNamestringundefinedCSS class for container
buttonClassNamestringundefinedCSS class for page buttons
activeButtonClassNamestringundefinedCSS class for active button
disabledButtonClassNamestringundefinedCSS class for disabled buttons
prevNextButtonClassNamestringundefinedCSS class for prev/next buttons

Label Props

PropTypeDefaultDescription
prevLabelReact.ReactNode"Previous"Previous button label
nextLabelReact.ReactNode"Next"Next button label
firstLabelReact.ReactNode"First"First button label
lastLabelReact.ReactNode"Last"Last button label
showFirstLastbooleanfalseShow first/last buttons

Feature Props

PropTypeDefaultDescription
showJumpToPagebooleanfalseShow jump to page input
animateTransitionsbooleanfalseEnable page transition animations
animationType"slide" \| "zoom""slide"Animation type

Infinite Scroll Props

PropTypeDefaultDescription
infiniteScrollbooleanfalseEnable infinite scroll mode
onLoadMore() => voidundefinedLoad more callback
hasMoreItemsbooleantrueWhether more items are available
loadingLabelReact.ReactNode"Loading more..."Loading indicator text
itemsPerBatchnumber10Items loaded per batch
totalItemsnumberundefinedTotal number of items

Examples

Basic Pagination

Simple pagination with default styling:

import { useState } from 'react';
import Paginator from '@aniruddha1806/paginator';

function BasicPaginationExample() {
  const [currentPage, setCurrentPage] = useState(1);
  const [data, setData] = useState([]);
  const itemsPerPage = 10;
  const totalItems = 100;
  const totalPages = Math.ceil(totalItems / itemsPerPage);

  // Simulate data loading
  const loadData = (page) => {
    const start = (page - 1) * itemsPerPage;
    const end = start + itemsPerPage;
    const newData = Array.from({ length: itemsPerPage }, (_, i) => ({
      id: start + i + 1,
      name: `Item ${start + i + 1}`
    }));
    setData(newData);
  };

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

  return (
    <div>
      <div style={{ minHeight: '400px', padding: '20px' }}>
        <h3>Items {((currentPage - 1) * itemsPerPage) + 1} - {Math.min(currentPage * itemsPerPage, totalItems)}</h3>
        <ul>
          {data.map(item => (
            <li key={item.id}>{item.name}</li>
          ))}
        </ul>
      </div>
      
      <Paginator
        totalPages={totalPages}
        currentPage={currentPage}
        onPageChange={handlePageChange}
      />
    </div>
  );
}

Advanced Pagination with All Features

Full-featured pagination with animations and jump functionality:

import { useState } from 'react';
import Paginator from '@aniruddha1806/paginator';

function AdvancedPaginationExample() {
  const [currentPage, setCurrentPage] = useState(1);
  const totalPages = 20;

  return (
    <div style={{ padding: '20px' }}>
      <h3>Advanced Pagination</h3>
      
      <Paginator
        totalPages={totalPages}
        currentPage={currentPage}
        onPageChange={setCurrentPage}
        maxVisiblePages={7}
        showFirstLast={true}
        showJumpToPage={true}
        animateTransitions={true}
        animationType="slide"
        prevLabel="← Prev"
        nextLabel="Next →"
        firstLabel="â‡Ī First"
        lastLabel="Last â‡Ĩ"
      />
      
      <div style={{ 
        marginTop: '20px', 
        padding: '16px', 
        backgroundColor: '#f8f9fa', 
        borderRadius: '8px' 
      }}>
        <p><strong>Current Page:</strong> {currentPage}</p>
        <p><strong>Total Pages:</strong> {totalPages}</p>
      </div>
    </div>
  );
}

Custom Styled Pagination

Apply custom styling with CSS classes:

import Paginator from '@aniruddha1806/paginator';
import './custom-pagination.css'; // Your custom CSS

function CustomStyledExample() {
  const [currentPage, setCurrentPage] = useState(1);

  return (
    <Paginator
      totalPages={15}
      currentPage={currentPage}
      onPageChange={setCurrentPage}
      className="custom-paginator"
      buttonClassName="custom-button"
      activeButtonClassName="custom-active"
      disabledButtonClassName="custom-disabled"
      prevNextButtonClassName="custom-nav-button"
    />
  );
}

CSS file (custom-pagination.css):

.custom-paginator {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.custom-button {
  min-width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 2px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.1);
  color: white;
  font-weight: 600;
  transition: all 0.3s ease;
}

.custom-button:hover {
  background: rgba(255,255,255,0.2);
  transform: translateY(-2px);
}

.custom-active {
  background: rgba(255,255,255,0.9);
  color: #667eea;
  border-color: rgba(255,255,255,0.9);
}

.custom-disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.custom-nav-button {
  padding: 0 16px;
  border-radius: 20px;
}

Infinite Scroll Example

Implement infinite scrolling for large datasets:

import { useState, useEffect } from 'react';
import Paginator from '@aniruddha1806/paginator';

function InfiniteScrollExample() {
  const [items, setItems] = useState([]);
  const [hasMore, setHasMore] = useState(true);
  const [loading, setLoading] = useState(false);

  // Simulate initial data load
  useEffect(() => {
    loadInitialData();
  }, []);

  const loadInitialData = () => {
    const initialItems = Array.from({ length: 20 }, (_, i) => ({
      id: i + 1,
      title: `Item ${i + 1}`,
      description: `Description for item ${i + 1}`
    }));
    setItems(initialItems);
  };

  const loadMore = async () => {
    if (loading) return;
    
    setLoading(true);
    
    // Simulate API call delay
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    const currentLength = items.length;
    const newItems = Array.from({ length: 10 }, (_, i) => ({
      id: currentLength + i + 1,
      title: `Item ${currentLength + i + 1}`,
      description: `Description for item ${currentLength + i + 1}`
    }));
    
    setItems(prev => [...prev, ...newItems]);
    setLoading(false);
    
    // Stop loading more after 100 items
    if (items.length + newItems.length >= 100) {
      setHasMore(false);
    }
  };

  return (
    <div style={{ maxWidth: '600px', margin: '0 auto', padding: '20px' }}>
      <h2>Infinite Scroll Example</h2>
      
      <div style={{ marginBottom: '20px' }}>
        {items.map(item => (
          <div
            key={item.id}
            style={{
              padding: '16px',
              margin: '8px 0',
              border: '1px solid #e9ecef',
              borderRadius: '8px',
              backgroundColor: '#f8f9fa'
            }}
          >
            <h4 style={{ margin: '0 0 8px 0' }}>{item.title}</h4>
            <p style={{ margin: 0, color: '#666' }}>{item.description}</p>
          </div>
        ))}
      </div>
      
      <Paginator
        infiniteScroll={true}
        onLoadMore={loadMore}
        hasMoreItems={hasMore}
        loadingLabel={loading ? "Loading..." : "Load more items"}
        totalPages={0}
        currentPage={1}
        onPageChange={() => {}}
      />
      
      <div style={{ 
        textAlign: 'center', 
        marginTop: '20px', 
        color: '#666',
        fontSize: '14px'
      }}>
        Loaded {items.length} items
      </div>
    </div>
  );
}

TypeScript Usage

The component provides full TypeScript support:

import Paginator, { PaginatorProps } from '@aniruddha1806/paginator';
import { useState, useCallback } from 'react';

interface PaginatedData<T> {
  items: T[];
  totalPages: number;
  currentPage: number;
  totalItems: number;
}

interface User {
  id: number;
  name: string;
  email: string;
}

const PaginatedUserList: React.FC = () => {
  const [paginatedData, setPaginatedData] = useState<PaginatedData<User>>({
    items: [],
    totalPages: 0,
    currentPage: 1,
    totalItems: 0
  });

  const handlePageChange = useCallback((page: number): void => {
    // Fetch data for the new page
    fetchUsersForPage(page);
  }, []);

  const fetchUsersForPage = async (page: number): Promise<void> => {
    try {
      const response = await fetch(`/api/users?page=${page}`);
      const data = await response.json();
      setPaginatedData(data);
    } catch (error) {
      console.error('Error fetching users:', error);
    }
  };

  const paginatorProps: PaginatorProps = {
    totalPages: paginatedData.totalPages,
    currentPage: paginatedData.currentPage,
    onPageChange: handlePageChange,
    maxVisiblePages: 7,
    showJumpToPage: true,
    animateTransitions: true,
    totalItems: paginatedData.totalItems,
    itemsPerBatch: 10
  };

  return (
    <div>
      <div>
        {paginatedData.items.map(user => (
          <div key={user.id}>
            <h3>{user.name}</h3>
            <p>{user.email}</p>
          </div>
        ))}
      </div>
      <Paginator {...paginatorProps} />
    </div>
  );
};
1.0.1

5 months ago

1.0.0

7 months ago