-
Notifications
You must be signed in to change notification settings - Fork 508
/
Pagination.tsx
215 lines (190 loc) · 4.88 KB
/
Pagination.tsx
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React from 'react'
import styled from 'styled-components'
import Box from '../Box'
import {get} from '../constants'
import sx, {SxProp} from '../sx'
import getGlobalFocusStyles from '../_getGlobalFocusStyles'
import {buildComponentData, buildPaginationModel} from './model'
const Page = styled.a`
display: inline-block;
min-width: 32px;
padding: 5px 10px;
font-style: normal;
line-height: 20px;
color: ${get('colors.fg.default')};
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: pointer;
user-select: none;
text-decoration: none;
margin-right: ${get('space.1')};
&:last-child {
margin-right: 0;
}
border: ${get('borderWidths.1')} solid transparent;
border-radius: ${get('radii.2')};
transition: border-color 0.2s cubic-bezier(0.3, 0, 0.5, 1);
&:hover,
&:focus {
text-decoration: none;
border-color: ${get('colors.border.default')};
outline: 0;
transition-duration: 0.1s;
}
${getGlobalFocusStyles(0)};
&:active {
border-color: ${get('colors.border.muted')};
}
&[rel='prev'],
&[rel='next'] {
color: ${get('colors.accent.fg')};
}
&[aria-current],
&[aria-current]:hover {
color: ${get('colors.fg.onEmphasis')};
background-color: ${get('colors.accent.emphasis')};
border-color: transparent;
}
&[aria-disabled],
&[aria-disabled]:hover {
color: ${get('colors.primer.fg.disabled')}; // check
cursor: default;
border-color: transparent;
}
@supports (clip-path: polygon(50% 0, 100% 50%, 50% 100%)) {
&[rel='prev']::before,
&[rel='next']::after {
display: inline-block;
width: 16px;
height: 16px;
vertical-align: text-bottom;
content: '';
background-color: currentColor;
}
// chevron-left
&[rel='prev']::before {
margin-right: ${get('space.1')};
clip-path: polygon(
9.8px 12.8px,
8.7px 12.8px,
4.5px 8.5px,
4.5px 7.5px,
8.7px 3.2px,
9.8px 4.3px,
6.1px 8px,
9.8px 11.7px,
9.8px 12.8px
);
}
// chevron-right
&[rel='next']::after {
margin-left: ${get('space.1')};
clip-path: polygon(
6.2px 3.2px,
7.3px 3.2px,
11.5px 7.5px,
11.5px 8.5px,
7.3px 12.8px,
6.2px 11.7px,
9.9px 8px,
6.2px 4.3px,
6.2px 3.2px
);
}
}
`
type UsePaginationPagesParameters = {
theme?: Record<string, unknown> // set to theme type once /src/theme.js is converted
pageCount: number
currentPage: number
onPageChange: (e: React.MouseEvent, n: number) => void
hrefBuilder: (n: number) => string
marginPageCount: number
showPages?: boolean
surroundingPageCount: number
}
function usePaginationPages({
theme,
pageCount,
currentPage,
onPageChange,
hrefBuilder,
marginPageCount,
showPages,
surroundingPageCount
}: UsePaginationPagesParameters) {
const pageChange = React.useCallback(n => (e: React.MouseEvent) => onPageChange(e, n), [onPageChange])
const model = React.useMemo(() => {
return buildPaginationModel(pageCount, currentPage, !!showPages, marginPageCount, surroundingPageCount)
}, [pageCount, currentPage, showPages, marginPageCount, surroundingPageCount])
const children = React.useMemo(() => {
return model.map(page => {
const {props, key, content} = buildComponentData(page, hrefBuilder, pageChange(page.num))
return (
<Page {...props} key={key} theme={theme}>
{content}
</Page>
)
})
}, [model, hrefBuilder, pageChange, theme])
return children
}
const PaginationContainer = styled.nav<SxProp>`
margin-top: 20px;
margin-bottom: 15px;
text-align: center;
${sx};
`
export type PaginationProps = {
theme?: Record<string, unknown>
pageCount: number
currentPage: number
onPageChange?: (e: React.MouseEvent, n: number) => void
hrefBuilder?: (n: number) => string
marginPageCount: number
showPages?: boolean
surroundingPageCount?: number
}
function Pagination({
theme,
pageCount,
currentPage,
onPageChange = noop,
hrefBuilder = defaultHrefBuilder,
marginPageCount = 1,
showPages = true,
surroundingPageCount = 2,
...rest
}: PaginationProps) {
const pageElements = usePaginationPages({
theme,
pageCount,
currentPage,
onPageChange,
hrefBuilder,
marginPageCount,
showPages,
surroundingPageCount
})
return (
<PaginationContainer aria-label="Pagination" {...rest} theme={theme}>
<Box display="inline-block" theme={theme}>
{pageElements}
</Box>
</PaginationContainer>
)
}
function defaultHrefBuilder(pageNum: number) {
return `#${pageNum}`
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
function noop() {}
Pagination.defaultProps = {
hrefBuilder: defaultHrefBuilder,
marginPageCount: 1,
onPageChange: noop,
showPages: true,
surroundingPageCount: 2
}
export default Pagination