Pagination
Bounded page navigation with controlled state and consumer-composed buttons.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add paginationSource files
Expand a file to read the source the installer copies into your app.
components/ui/pagination/Pagination.tsx
import * as React from 'react'
import { Pressable, View, type ViewProps } from 'react-native'
import { Button, type ButtonProps } from '@/components/ui/button'
import { Text, type TextProps } from '@/components/ui/text'
import { cn } from '@/lib/utils'
export interface PaginationProps extends ViewProps {
page?: number
defaultPage?: number
pageCount?: number
onPageChange?: (page: number) => void
disabled?: boolean
className?: string
}
interface PageContext {
page: number
count: number
disabled: boolean
go: (page: number) => void
}
const Context = React.createContext<PageContext | null>(null)
function usePage() {
const context = React.useContext(Context)
if (!context) throw new Error('Pagination parts must be inside Pagination')
return context
}
function clamp(page: number, count: number) {
return count ? Math.max(1, Math.min(count, Number.isFinite(page) ? Math.trunc(page) : 1)) : 0
}
export const Pagination = React.forwardRef<React.ComponentRef<typeof View>, PaginationProps>(
({ page, defaultPage = 1, pageCount = 1, onPageChange, disabled = false, className, ...props }, ref) => {
const count = Number.isFinite(pageCount) ? Math.max(0, Math.trunc(pageCount)) : 0
const [internal, setInternal] = React.useState(() => clamp(defaultPage, count))
const current = clamp(page ?? internal, count)
React.useEffect(() => {
if (page === undefined) setInternal((value) => clamp(value, count))
}, [count, page])
return (
<Context.Provider
value={{
page: current,
count,
disabled,
go: (next) => {
if (disabled || !count) return
const target = clamp(next, count)
if (target === current) return
if (page === undefined) setInternal(target)
onPageChange?.(target)
},
}}
>
<View
{...props}
ref={ref}
className={cn('flex-row flex-wrap items-center justify-center gap-[8px]', className)}
/>
</Context.Provider>
)
},
)
Pagination.displayName = 'Pagination'
export const PaginationPrevious = React.forwardRef<React.ComponentRef<typeof Pressable>, ButtonProps>(
({ children = 'Previous', disabled, onPress, ...props }, ref) => {
const page = usePage()
const isDisabled = page.disabled || !!disabled || page.page <= 1
return (
<Button
variant="outline"
{...props}
ref={ref}
disabled={isDisabled}
onPress={(event) => {
if (isDisabled) return
onPress?.(event)
if (!event.isDefaultPrevented()) page.go(page.page - 1)
}}
>
{children}
</Button>
)
},
)
PaginationPrevious.displayName = 'PaginationPrevious'
export const PaginationNext = React.forwardRef<React.ComponentRef<typeof Pressable>, ButtonProps>(
({ children = 'Next', disabled, onPress, ...props }, ref) => {
const page = usePage()
const isDisabled = page.disabled || !!disabled || page.page >= page.count || !page.count
return (
<Button
variant="outline"
{...props}
ref={ref}
disabled={isDisabled}
onPress={(event) => {
if (isDisabled) return
onPress?.(event)
if (!event.isDefaultPrevented()) page.go(page.page + 1)
}}
>
{children}
</Button>
)
},
)
PaginationNext.displayName = 'PaginationNext'
export interface PaginationPageProps extends ButtonProps {
page: number
}
export const PaginationPage = React.forwardRef<React.ComponentRef<typeof Pressable>, PaginationPageProps>(
({ page: destination, children, disabled, onPress, accessibilityState, ...props }, ref) => {
const page = usePage()
const selected = destination === page.page
const isDisabled =
page.disabled || !!disabled || !Number.isInteger(destination) || destination < 1 || destination > page.count
return (
<Button
variant={selected ? 'default' : 'outline'}
{...props}
ref={ref}
accessibilityLabel={props.accessibilityLabel ?? 'Page ' + destination}
accessibilityState={{ ...accessibilityState, selected }}
disabled={isDisabled}
onPress={(event) => {
if (isDisabled) return
onPress?.(event)
if (!event.isDefaultPrevented()) page.go(destination)
}}
>
{children ?? String(destination)}
</Button>
)
},
)
PaginationPage.displayName = 'PaginationPage'
export const PaginationStatus = React.forwardRef<React.ComponentRef<typeof Text>, TextProps>(
({ children, ...props }, ref) => {
const page = usePage()
return (
<Text {...props} ref={ref}>
{children ?? (page.count ? 'Page ' + page.page + ' of ' + page.count : 'No pages')}
</Text>
)
},
)
PaginationStatus.displayName = 'PaginationStatus'
components/ui/pagination/index.ts
export * from './Pagination'