-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #941 from tszhong0411/pack-14-add-pagination
Add pagination
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tszhong0411/ui': patch | ||
--- | ||
|
||
add pagination component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Pagination | ||
description: Pagination with page navigation, next and previous links. | ||
--- | ||
|
||
<ComponentPreview name='pagination/pagination' /> | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { | ||
Pagination, | ||
PaginationContent, | ||
PaginationEllipsis, | ||
PaginationItem, | ||
PaginationLink, | ||
PaginationNext, | ||
PaginationPrevious | ||
} from '@/components/ui/pagination' | ||
``` | ||
|
||
```tsx | ||
<Pagination> | ||
<PaginationContent> | ||
<PaginationItem> | ||
<PaginationPrevious href='#' /> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationLink href='#'>1</PaginationLink> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationEllipsis /> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationNext href='#' /> | ||
</PaginationItem> | ||
</PaginationContent> | ||
</Pagination> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Pagination, | ||
PaginationContent, | ||
PaginationEllipsis, | ||
PaginationItem, | ||
PaginationLink, | ||
PaginationNext, | ||
PaginationPrevious | ||
} from '@tszhong0411/ui' | ||
|
||
const PaginationDemo = () => { | ||
return ( | ||
<Pagination> | ||
<PaginationContent> | ||
<PaginationItem> | ||
<PaginationPrevious href='#' /> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationLink href='#'>1</PaginationLink> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationLink href='#' isActive> | ||
2 | ||
</PaginationLink> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationLink href='#'>3</PaginationLink> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationEllipsis /> | ||
</PaginationItem> | ||
<PaginationItem> | ||
<PaginationNext href='#' /> | ||
</PaginationItem> | ||
</PaginationContent> | ||
</Pagination> | ||
) | ||
} | ||
|
||
export default PaginationDemo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { cn } from '@tszhong0411/utils' | ||
import { ChevronLeftIcon, ChevronRightIcon, MoreHorizontalIcon } from 'lucide-react' | ||
import Link from 'next/link' | ||
|
||
import { type ButtonProps, buttonVariants } from './button' | ||
|
||
type PaginationProps = React.ComponentProps<'nav'> | ||
|
||
export const Pagination = (props: PaginationProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<nav | ||
role='navigation' | ||
aria-label='pagination' | ||
className={cn('mx-auto flex w-full justify-center', className)} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
type PaginationContentProps = React.ComponentProps<'ul'> | ||
|
||
export const PaginationContent = (props: PaginationContentProps) => { | ||
const { className, ...rest } = props | ||
|
||
return <ul className={cn('flex flex-row items-center gap-1', className)} {...rest} /> | ||
} | ||
|
||
type PaginationItemProps = React.ComponentProps<'li'> | ||
|
||
export const PaginationItem = (props: PaginationItemProps) => { | ||
const { className, ...rest } = props | ||
|
||
return <li className={cn(className)} {...rest} /> | ||
} | ||
|
||
type PaginationLinkProps = { | ||
isActive?: boolean | ||
} & Pick<ButtonProps, 'size'> & | ||
React.ComponentProps<typeof Link> | ||
|
||
export const PaginationLink = (props: PaginationLinkProps) => { | ||
const { className, isActive, size = 'icon', ...rest } = props | ||
|
||
return ( | ||
// eslint-disable-next-line jsx-a11y/anchor-has-content -- it's a component | ||
<Link | ||
aria-current={isActive ? 'page' : undefined} | ||
className={cn( | ||
buttonVariants({ | ||
variant: isActive ? 'outline' : 'ghost', | ||
size | ||
}), | ||
className | ||
)} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
type PaginationPreviousProps = React.ComponentProps<typeof PaginationLink> | ||
|
||
export const PaginationPrevious = (props: PaginationPreviousProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<PaginationLink | ||
aria-label='Go to previous page' | ||
size='default' | ||
className={cn('gap-1 pl-2.5', className)} | ||
{...rest} | ||
> | ||
<ChevronLeftIcon className='size-4' /> | ||
<span>Previous</span> | ||
</PaginationLink> | ||
) | ||
} | ||
|
||
type PaginationNextProps = React.ComponentProps<typeof PaginationLink> | ||
|
||
export const PaginationNext = (props: PaginationNextProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<PaginationLink | ||
aria-label='Go to next page' | ||
size='default' | ||
className={cn('gap-1 pr-2.5', className)} | ||
{...rest} | ||
> | ||
<span>Next</span> | ||
<ChevronRightIcon className='size-4' /> | ||
</PaginationLink> | ||
) | ||
} | ||
|
||
type PaginationEllipsisProps = React.ComponentProps<'span'> | ||
|
||
export const PaginationEllipsis = (props: PaginationEllipsisProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<span | ||
aria-hidden | ||
className={cn('flex size-9 items-center justify-center', className)} | ||
{...rest} | ||
> | ||
<MoreHorizontalIcon className='size-4' /> | ||
<span className='sr-only'>More pages</span> | ||
</span> | ||
) | ||
} |