File size: 2,241 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Box, HStack, Stack, StackProps, Text } from "@chakra-ui/react"
import Link, { LinkProps } from "next/link"
import { LuChevronLeft, LuChevronRight } from "react-icons/lu"

interface PaginationItemProps extends StackProps {
  href: LinkProps["href"]
  external?: boolean | undefined
}

const PaginationItem = (props: PaginationItemProps) => {
  const { children, href, external, ...rest } = props

  return (
    <Box
      flex="1"
      borderWidth="1px"
      focusRing="contain"
      focusRingWidth="2px"
      rounded="md"
      p="4"
      {...rest}
      asChild
    >
      {external ? (
        <a href={href as string} target="_blank" rel="noopener noreferrer">
          {children}
        </a>
      ) : (
        <Link href={href}>{children}</Link>
      )}
    </Box>
  )
}

interface PaginationProps extends StackProps {
  previous?: {
    title: string
    url?: LinkProps["href"]
    external?: boolean | undefined
  } | null
  next?: {
    title: string
    url?: LinkProps["href"]
    external?: boolean | undefined
  } | null
}

export const Pagination = (props: PaginationProps) => {
  const { previous, next, ...rest } = props

  return (
    <HStack {...rest}>
      {previous ? (
        <PaginationItem href={previous.url || "#"} external={previous.external}>
          <Stack gap="1" textAlign="start" textStyle="sm">
            <Text color="fg.muted">Previous</Text>
            <HStack
              display="inline-flex"
              justify="flex-start"
              fontWeight="medium"
            >
              <LuChevronLeft />
              {previous.title}
            </HStack>
          </Stack>
        </PaginationItem>
      ) : (
        <Box flex="1" />
      )}
      {next ? (
        <PaginationItem href={next.url || "#"} external={next.external}>
          <Stack gap="1" textAlign="end" textStyle="sm">
            <Text color="fg.muted">Next</Text>
            <HStack
              display="inline-flex"
              justify="flex-end"
              fontWeight="medium"
            >
              {next.title}
              <LuChevronRight />
            </HStack>
          </Stack>
        </PaginationItem>
      ) : (
        <Box flex="1" />
      )}
    </HStack>
  )
}