-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v1][www] Paginate blog index page (#7646)
* v1-pagination * Fix things for Gatsby v1 * `pathContext` instead of `pageContext` * import `navigateTo` instead of `navigate`, `Link` from `gatsby-link` instead of `gatsby`
- Loading branch information
Showing
4 changed files
with
164 additions
and
10 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
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,16 @@ | ||
import React from "react" | ||
import Link from "gatsby-link" | ||
import { colors } from "../../utils/presets" | ||
|
||
const PaginationLink = ({ to, children, ...props }) => { | ||
if (to) { | ||
return ( | ||
<Link to={to} {...props}> | ||
{children} | ||
</Link> | ||
) | ||
} | ||
return <span css={{ color: colors.gray.calm }}>{children}</span> | ||
} | ||
|
||
export default PaginationLink |
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,118 @@ | ||
import React from "react" | ||
import { navigateTo } from "gatsby-link" | ||
import ArrowForwardIcon from "react-icons/lib/md/arrow-forward" | ||
import ArrowBackIcon from "react-icons/lib/md/arrow-back" | ||
import PaginationLink from "./PaginationLink" | ||
import presets, { colors } from "../../utils/presets" | ||
import { options, rhythm } from "../../utils/typography" | ||
|
||
class Pagination extends React.Component { | ||
changePage = e => { | ||
navigateTo(`/blog/${e.target.value}`) | ||
} | ||
render() { | ||
const { numPages, currentPage } = this.props.context | ||
const isFirst = currentPage === 1 | ||
const isLast = currentPage === numPages | ||
const prevPageNum = | ||
currentPage - 1 === 1 ? `` : (currentPage - 1).toString() | ||
const nextPageNum = (currentPage + 1).toString() | ||
const prevPageLink = isFirst ? null : `/blog/${prevPageNum}` | ||
const nextPageLink = isLast ? null : `/blog/${nextPageNum}` | ||
|
||
const prevNextLinkStyles = { | ||
"&&": { | ||
boxShadow: `none`, | ||
borderBottom: 0, | ||
fontFamily: options.headerFontFamily.join(`,`), | ||
fontWeight: `bold`, | ||
color: colors.gatsby, | ||
}, | ||
} | ||
|
||
return ( | ||
<div | ||
css={{ | ||
display: `flex`, | ||
justifyContent: `space-between`, | ||
margin: `${rhythm(1)} 0`, | ||
flexDirection: `column`, | ||
[presets.Tablet]: { | ||
flexDirection: `row`, | ||
}, | ||
}} | ||
> | ||
<div | ||
css={{ | ||
display: `flex`, | ||
margin: `0`, | ||
padding: `0`, | ||
justifyContent: `space-between`, | ||
alignItems: `center`, | ||
marginBottom: rhythm(1 / 2), | ||
[presets.Tablet]: { | ||
width: `15rem`, | ||
marginBottom: 0, | ||
}, | ||
}} | ||
> | ||
<PaginationLink to={prevPageLink} css={prevNextLinkStyles}> | ||
<ArrowBackIcon style={{ verticalAlign: `sub` }} /> | ||
Newer posts | ||
</PaginationLink> | ||
<PaginationLink to={nextPageLink} css={prevNextLinkStyles}> | ||
Older posts | ||
<ArrowForwardIcon style={{ verticalAlign: `sub` }} /> | ||
</PaginationLink> | ||
</div> | ||
<div | ||
css={{ | ||
display: `flex`, | ||
alignItems: `center`, | ||
justifyContent: `flex-end`, | ||
fontFamily: options.headerFontFamily.join(`,`), | ||
}} | ||
> | ||
<span>Showing page </span> | ||
<select | ||
value={currentPage === 1 ? `` : currentPage.toString()} | ||
onChange={this.changePage} | ||
css={{ | ||
appearance: `none`, | ||
border: `none`, | ||
padding: `0.5ch 2ch 0.5ch 0.5ch`, | ||
color: `rebeccapurple`, | ||
fontWeight: `bold`, | ||
}} | ||
> | ||
{Array.from({ length: numPages }, (_, i) => ( | ||
<option | ||
value={`${i === 0 ? `` : i + 1}`} | ||
key={`pagination-number${i + 1}`} | ||
> | ||
{i + 1} | ||
</option> | ||
))} | ||
</select> | ||
<svg | ||
width="10" | ||
height="5" | ||
viewBox="0 0 10 5" | ||
css={{ | ||
position: `relative`, | ||
right: `1.5ch`, | ||
fill: `rebeccapurple`, | ||
pointerEvents: `none`, | ||
}} | ||
> | ||
<path d="M0 0l5 4.998L10 0z" fillRule="evenodd" /> | ||
</svg> | ||
<span>of </span> | ||
<span>{numPages}</span> | ||
</div> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default 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