mirror of
https://github.com/kremalicious/blog.git
synced 2025-02-14 21:10:25 +01:00
prev/next post navigation
This commit is contained in:
parent
64e6a32ce8
commit
9ae5ff2367
@ -25,8 +25,16 @@ exports.createPages = async ({ graphql, actions }) => {
|
|||||||
|
|
||||||
const result = await graphql(`
|
const result = await graphql(`
|
||||||
{
|
{
|
||||||
posts: allMarkdownRemark {
|
posts: allMarkdownRemark(sort: { order: DESC, fields: [fields___date] }) {
|
||||||
edges {
|
edges {
|
||||||
|
next {
|
||||||
|
fields {
|
||||||
|
slug
|
||||||
|
}
|
||||||
|
frontmatter {
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
node {
|
node {
|
||||||
fields {
|
fields {
|
||||||
slug
|
slug
|
||||||
@ -35,6 +43,14 @@ exports.createPages = async ({ graphql, actions }) => {
|
|||||||
tags
|
tags
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
previous {
|
||||||
|
fields {
|
||||||
|
slug
|
||||||
|
}
|
||||||
|
frontmatter {
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +82,7 @@ exports.onPostBuild = async ({ graphql }) => {
|
|||||||
// JSON Feed query
|
// JSON Feed query
|
||||||
const result = await graphql(`
|
const result = await graphql(`
|
||||||
{
|
{
|
||||||
allMarkdownRemark(sort: { order: DESC, fields: [fields___date] }) {
|
allMarkdownRemark {
|
||||||
edges {
|
edges {
|
||||||
node {
|
node {
|
||||||
html
|
html
|
||||||
|
@ -32,7 +32,15 @@ exports.generatePostPages = (createPage, posts) => {
|
|||||||
path: `${post.node.fields.slug}`,
|
path: `${post.node.fields.slug}`,
|
||||||
component: postTemplate,
|
component: postTemplate,
|
||||||
context: {
|
context: {
|
||||||
slug: post.node.fields.slug
|
slug: post.node.fields.slug,
|
||||||
|
prev: post.previous && {
|
||||||
|
title: post.previous.frontmatter.title,
|
||||||
|
slug: post.previous.fields.slug
|
||||||
|
},
|
||||||
|
next: post.next && {
|
||||||
|
title: post.next.frontmatter.title,
|
||||||
|
slug: post.next.fields.slug
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { Link } from 'gatsby'
|
import { Link } from 'gatsby'
|
||||||
import styles from './Pagination.module.scss'
|
import styles from './Pagination.module.scss'
|
||||||
|
import shortid from 'shortid'
|
||||||
|
|
||||||
const PageNumber = ({
|
const PageNumber = ({
|
||||||
i,
|
i,
|
||||||
@ -65,7 +66,12 @@ export default function Pagination({
|
|||||||
<div>{!isFirst && <PrevNext prevPagePath={prevPagePath} />}</div>
|
<div>{!isFirst && <PrevNext prevPagePath={prevPagePath} />}</div>
|
||||||
<div>
|
<div>
|
||||||
{Array.from({ length: numPages }, (_, i) => (
|
{Array.from({ length: numPages }, (_, i) => (
|
||||||
<PageNumber i={i} slug={slug} current={currentPageNumber === i + 1} />
|
<PageNumber
|
||||||
|
key={shortid.generate()}
|
||||||
|
i={i}
|
||||||
|
slug={slug}
|
||||||
|
current={currentPageNumber === i + 1}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
<div>{!isLast && <PrevNext nextPagePath={nextPagePath} />}</div>
|
<div>{!isLast && <PrevNext nextPagePath={nextPagePath} />}</div>
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
|
|
||||||
.title {
|
.title {
|
||||||
font-size: $font-size-h3;
|
font-size: $font-size-h3;
|
||||||
margin-bottom: $spacer * $line-height;
|
margin-bottom: $spacer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.relatedPosts {
|
.relatedPosts {
|
||||||
margin-top: -($spacer * 2);
|
margin-top: -($spacer * 2);
|
||||||
margin-bottom: $spacer;
|
|
||||||
|
|
||||||
@media (min-width: $screen-md) {
|
@media (min-width: $screen-md) {
|
||||||
@include breakoutviewport;
|
@include breakoutviewport;
|
||||||
@ -65,7 +64,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.button {
|
.button {
|
||||||
margin: auto;
|
font-size: $font-size-mini;
|
||||||
display: block;
|
display: inline-block;
|
||||||
margin-top: $spacer * 2;
|
color: $brand-grey-light;
|
||||||
|
text-transform: uppercase;
|
||||||
|
margin-left: $spacer / 2;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,12 @@ export default function RelatedPosts({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<aside className={styles.relatedPosts}>
|
<aside className={styles.relatedPosts}>
|
||||||
<h1 className={styles.title}>Related {photos ? 'Photos' : 'Posts'}</h1>
|
<h1 className={styles.title}>
|
||||||
|
Related {photos ? 'Photos' : 'Posts'}{' '}
|
||||||
|
<button className={styles.button} onClick={refreshPosts}>
|
||||||
|
Refresh
|
||||||
|
</button>
|
||||||
|
</h1>
|
||||||
<ul>
|
<ul>
|
||||||
{filteredPosts
|
{filteredPosts
|
||||||
.sort(() => 0.5 - Math.random())
|
.sort(() => 0.5 - Math.random())
|
||||||
@ -96,9 +101,6 @@ export default function RelatedPosts({
|
|||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
<button className={`${styles.button} btn`} onClick={refreshPosts}>
|
|
||||||
Refresh Related {photos ? 'Photos' : 'Posts'}
|
|
||||||
</button>
|
|
||||||
</aside>
|
</aside>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -176,7 +176,8 @@
|
|||||||
border-top: 2px solid transparent;
|
border-top: 2px solid transparent;
|
||||||
border-bottom: 2px solid transparent;
|
border-bottom: 2px solid transparent;
|
||||||
border-radius: $border-radius;
|
border-radius: $border-radius;
|
||||||
box-shadow: 0 1px 3px rgba($brand-grey, 0.2);
|
box-shadow: 0 3px 5px rgba($brand-grey, 0.2),
|
||||||
|
0 5px 16px rgba($brand-grey, 0.2);
|
||||||
|
|
||||||
@media (min-width: $screen-sm) {
|
@media (min-width: $screen-sm) {
|
||||||
border: 2px solid transparent;
|
border: 2px solid transparent;
|
||||||
|
@ -160,6 +160,7 @@ h6 {
|
|||||||
margin-bottom: $spacer;
|
margin-bottom: $spacer;
|
||||||
-webkit-font-smoothing: antialiased;
|
-webkit-font-smoothing: antialiased;
|
||||||
-moz-osx-font-smoothing: grayscale;
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
transition: color 0.2s $easing;
|
||||||
|
|
||||||
// stylelint-disable no-descending-specificity
|
// stylelint-disable no-descending-specificity
|
||||||
&,
|
&,
|
||||||
@ -172,6 +173,13 @@ h6 {
|
|||||||
.dark & a {
|
.dark & a {
|
||||||
color: $color-headings--dark;
|
color: $color-headings--dark;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a:hover &,
|
||||||
|
a:focus &,
|
||||||
|
.dark a:hover &,
|
||||||
|
.dark a:focus & {
|
||||||
|
color: $link-color;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Responsive Media
|
// Responsive Media
|
||||||
|
@ -5,13 +5,8 @@
|
|||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
|
|
||||||
.hentry__title {
|
.hentry__title {
|
||||||
color: $color-headings;
|
|
||||||
margin-top: 0;
|
margin-top: 0;
|
||||||
margin-bottom: $spacer;
|
margin-bottom: $spacer;
|
||||||
|
|
||||||
:global(.dark) & {
|
|
||||||
color: $color-headings--dark;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.hentry__title__link {
|
.hentry__title__link {
|
||||||
|
56
src/templates/Post/PrevNext.module.scss
Normal file
56
src/templates/Post/PrevNext.module.scss
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
@import 'variables';
|
||||||
|
@import 'mixins';
|
||||||
|
|
||||||
|
.prevnext {
|
||||||
|
@include breakoutviewport;
|
||||||
|
@include divider-top;
|
||||||
|
|
||||||
|
margin-top: $spacer * 4;
|
||||||
|
padding-top: $spacer * 2;
|
||||||
|
display: grid;
|
||||||
|
gap: $spacer;
|
||||||
|
grid-template-columns: 1fr 1fr;
|
||||||
|
|
||||||
|
svg {
|
||||||
|
fill: $brand-grey-light;
|
||||||
|
margin-bottom: -0.1rem;
|
||||||
|
position: absolute;
|
||||||
|
left: 0.1rem;
|
||||||
|
top: $spacer * 1.1;
|
||||||
|
}
|
||||||
|
|
||||||
|
> div {
|
||||||
|
position: relative;
|
||||||
|
|
||||||
|
&:first-child {
|
||||||
|
padding-left: $spacer;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:last-child {
|
||||||
|
padding-right: $spacer;
|
||||||
|
text-align: right;
|
||||||
|
|
||||||
|
svg {
|
||||||
|
right: 0.1rem;
|
||||||
|
left: auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
font-size: $font-size-large;
|
||||||
|
margin: 0;
|
||||||
|
color: $brand-grey-light !important;
|
||||||
|
|
||||||
|
a:hover & {
|
||||||
|
color: $link-color !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.label {
|
||||||
|
margin-bottom: $spacer / 4;
|
||||||
|
font-size: $font-size-small;
|
||||||
|
color: $brand-grey-light;
|
||||||
|
opacity: 0.65;
|
||||||
|
}
|
40
src/templates/Post/PrevNext.tsx
Normal file
40
src/templates/Post/PrevNext.tsx
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import { Link } from 'gatsby'
|
||||||
|
import styles from './PrevNext.module.scss'
|
||||||
|
import { ReactComponent as Right } from '../../images/chevron-right.svg'
|
||||||
|
import { ReactComponent as Left } from '../../images/chevron-left.svg'
|
||||||
|
|
||||||
|
interface Node {
|
||||||
|
title: string
|
||||||
|
slug: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PrevNextProps {
|
||||||
|
prev: Node
|
||||||
|
next: Node
|
||||||
|
}
|
||||||
|
|
||||||
|
const PrevNext = ({ prev, next }: PrevNextProps) => (
|
||||||
|
<nav className={styles.prevnext}>
|
||||||
|
<div>
|
||||||
|
{prev && (
|
||||||
|
<Link to={prev.slug}>
|
||||||
|
<Left />
|
||||||
|
<p className={styles.label}>Older</p>
|
||||||
|
<h3 className={styles.title}>{prev.title}</h3>
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{next && (
|
||||||
|
<Link to={next.slug}>
|
||||||
|
<p className={styles.label}>Newer</p>
|
||||||
|
<h3 className={styles.title}>{next.title}</h3>
|
||||||
|
<Right />
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
)
|
||||||
|
|
||||||
|
export default PrevNext
|
@ -12,15 +12,21 @@ import PostContent from './PostContent'
|
|||||||
import PostActions from './PostActions'
|
import PostActions from './PostActions'
|
||||||
import PostLinkActions from './PostLinkActions'
|
import PostLinkActions from './PostLinkActions'
|
||||||
import PostMeta from './PostMeta'
|
import PostMeta from './PostMeta'
|
||||||
|
import PrevNext from './PrevNext'
|
||||||
import styles from './index.module.scss'
|
import styles from './index.module.scss'
|
||||||
import { Image } from '../../components/atoms/Image'
|
import { Image } from '../../components/atoms/Image'
|
||||||
|
|
||||||
export default function Post({
|
export default function Post({
|
||||||
data,
|
data,
|
||||||
location
|
location,
|
||||||
|
pageContext: { next, prev }
|
||||||
}: {
|
}: {
|
||||||
data: { post: PostMetadata }
|
data: { post: PostMetadata }
|
||||||
location: Location
|
location: Location
|
||||||
|
pageContext: {
|
||||||
|
next: { title: string; slug: string }
|
||||||
|
prev: { title: string; slug: string }
|
||||||
|
}
|
||||||
}) {
|
}) {
|
||||||
const { post } = data
|
const { post } = data
|
||||||
const { title, image, type, linkurl, style, tags } = post.frontmatter
|
const { title, image, type, linkurl, style, tags } = post.frontmatter
|
||||||
@ -63,6 +69,8 @@ export default function Post({
|
|||||||
{(type === 'post' || type === 'photo') && (
|
{(type === 'post' || type === 'photo') && (
|
||||||
<RelatedPosts photos={type === 'photo'} tags={tags} />
|
<RelatedPosts photos={type === 'photo'} tags={tags} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
<PrevNext prev={prev} next={next} />
|
||||||
</Layout>
|
</Layout>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user