|
| 1 | +import * as React from "react"; |
| 2 | +import { ChevronLeft, ChevronRight, MoreHorizontal } from "lucide-react"; |
| 3 | + |
| 4 | +import { cn } from "@/lib/utils"; |
| 5 | +import { ButtonProps, buttonVariants } from "@/components/ui/button"; |
| 6 | + |
| 7 | +const Pagination = ({ className, ...props }: React.ComponentProps<"nav">) => ( |
| 8 | + <nav |
| 9 | + role="navigation" |
| 10 | + aria-label="pagination" |
| 11 | + className={cn("mx-auto flex w-full justify-center", className)} |
| 12 | + {...props} |
| 13 | + /> |
| 14 | +); |
| 15 | +Pagination.displayName = "Pagination"; |
| 16 | + |
| 17 | +const PaginationContent = React.forwardRef< |
| 18 | + HTMLUListElement, |
| 19 | + React.ComponentProps<"ul"> |
| 20 | +>(({ className, ...props }, ref) => ( |
| 21 | + <ul |
| 22 | + ref={ref} |
| 23 | + className={cn("flex flex-row items-center gap-1", className)} |
| 24 | + {...props} |
| 25 | + /> |
| 26 | +)); |
| 27 | +PaginationContent.displayName = "PaginationContent"; |
| 28 | + |
| 29 | +const PaginationItem = React.forwardRef< |
| 30 | + HTMLLIElement, |
| 31 | + React.ComponentProps<"li"> |
| 32 | +>(({ className, ...props }, ref) => ( |
| 33 | + <li ref={ref} className={cn("", className)} {...props} /> |
| 34 | +)); |
| 35 | +PaginationItem.displayName = "PaginationItem"; |
| 36 | + |
| 37 | +type PaginationLinkProps = { |
| 38 | + isActive?: boolean; |
| 39 | + onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void; |
| 40 | +} & Pick<ButtonProps, "size"> & |
| 41 | + Omit<React.ComponentProps<"button">, "onClick">; |
| 42 | + |
| 43 | +const PaginationLink = ({ |
| 44 | + className, |
| 45 | + isActive, |
| 46 | + size = "icon", |
| 47 | + onClick, |
| 48 | + ...props |
| 49 | +}: PaginationLinkProps) => ( |
| 50 | + <button |
| 51 | + type="button" |
| 52 | + aria-current={isActive ? "page" : undefined} |
| 53 | + className={cn( |
| 54 | + buttonVariants({ |
| 55 | + variant: isActive ? "secondary" : "ghost", |
| 56 | + size, |
| 57 | + }), |
| 58 | + className, |
| 59 | + )} |
| 60 | + onClick={onClick} |
| 61 | + {...props} |
| 62 | + /> |
| 63 | +); |
| 64 | +PaginationLink.displayName = "PaginationLink"; |
| 65 | + |
| 66 | +const PaginationPrevious = ({ |
| 67 | + className, |
| 68 | + onClick, |
| 69 | + ...props |
| 70 | +}: React.ComponentProps<typeof PaginationLink>) => ( |
| 71 | + <PaginationLink |
| 72 | + aria-label="Go to previous page" |
| 73 | + size="default" |
| 74 | + className={cn("gap-1 pl-2.5", className)} |
| 75 | + onClick={onClick} |
| 76 | + {...props} |
| 77 | + > |
| 78 | + <ChevronLeft className="h-4 w-4" /> |
| 79 | + <span>Previous</span> |
| 80 | + </PaginationLink> |
| 81 | +); |
| 82 | +PaginationPrevious.displayName = "PaginationPrevious"; |
| 83 | + |
| 84 | +const PaginationNext = ({ |
| 85 | + className, |
| 86 | + onClick, |
| 87 | + ...props |
| 88 | +}: React.ComponentProps<typeof PaginationLink>) => ( |
| 89 | + <PaginationLink |
| 90 | + aria-label="Go to next page" |
| 91 | + size="default" |
| 92 | + className={cn("gap-1 pr-2.5", className)} |
| 93 | + onClick={onClick} |
| 94 | + {...props} |
| 95 | + > |
| 96 | + <span>Next</span> |
| 97 | + <ChevronRight className="h-4 w-4" /> |
| 98 | + </PaginationLink> |
| 99 | +); |
| 100 | +PaginationNext.displayName = "PaginationNext"; |
| 101 | + |
| 102 | +const PaginationEllipsis = ({ |
| 103 | + className, |
| 104 | + ...props |
| 105 | +}: React.ComponentProps<"span">) => ( |
| 106 | + <span |
| 107 | + aria-hidden |
| 108 | + className={cn("flex h-9 w-9 items-center justify-center", className)} |
| 109 | + {...props} |
| 110 | + > |
| 111 | + <MoreHorizontal className="h-4 w-4" /> |
| 112 | + <span className="sr-only">More pages</span> |
| 113 | + </span> |
| 114 | +); |
| 115 | +PaginationEllipsis.displayName = "PaginationEllipsis"; |
| 116 | + |
| 117 | +export { |
| 118 | + Pagination, |
| 119 | + PaginationContent, |
| 120 | + PaginationEllipsis, |
| 121 | + PaginationItem, |
| 122 | + PaginationLink, |
| 123 | + PaginationNext, |
| 124 | + PaginationPrevious, |
| 125 | +}; |
0 commit comments