aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-09-12 17:06:44 +0300
committerValentin Popov <valentin@popov.link>2024-09-12 17:06:44 +0300
commit0b57b888caf8817ff4992c59ed40ed29bee34fd4 (patch)
tree37ea0f6c9a50f5be21e5d8e5c5c844fb498c98f4 /src
parent4ba339984d239180a526a5ae8ffbb558f1b5642a (diff)
downloadpopov.link-0b57b888caf8817ff4992c59ed40ed29bee34fd4.tar.xz
popov.link-0b57b888caf8817ff4992c59ed40ed29bee34fd4.zip
Add PostPagination component for blog post navigation
Diffstat (limited to 'src')
-rw-r--r--src/components/PostPagination.astro47
-rw-r--r--src/pages/blog/[...slug].astro17
2 files changed, 61 insertions, 3 deletions
diff --git a/src/components/PostPagination.astro b/src/components/PostPagination.astro
new file mode 100644
index 0000000..6ae6bef
--- /dev/null
+++ b/src/components/PostPagination.astro
@@ -0,0 +1,47 @@
+---
+const { prevPost, nextPost } = Astro.props;
+---
+
+<style lang="scss">
+ .pagination {
+ overflow: hidden;
+ padding: 5rem 0;
+ width: 100%;
+ }
+
+ @media (width <=684px) {
+ .pagination {
+ padding: 2rem 0;
+ }
+ }
+
+ .prev,
+ .next {
+ max-width: 40%;
+ }
+
+ .prev {
+ float: left;
+ }
+
+ .next {
+ float: right;
+ }
+</style>
+
+<div class="pagination">
+ {
+ prevPost && (
+ <span class="prev">
+ <a href={`/blog/${prevPost.slug}`}>&lt; {prevPost.data.title}</a>
+ </span>
+ )
+ }
+ {
+ nextPost && (
+ <span class="next">
+ <a href={`/blog/${nextPost.slug}`}>{nextPost.data.title} &gt;</a>
+ </span>
+ )
+ }
+</div>
diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro
index 4ecb1e1..9262a66 100644
--- a/src/pages/blog/[...slug].astro
+++ b/src/pages/blog/[...slug].astro
@@ -2,17 +2,24 @@
import { type CollectionEntry, getCollection } from "astro:content";
import Comments from "../../components/Comments.astro";
import Layout from "../../layouts/PageLayout.astro";
+import Pagination from "../../components/PostPagination.astro";
export async function getStaticPaths() {
const posts = await getCollection("blog");
- return posts.map((post) => ({
+ const total = posts.length;
+
+ return posts.map((post, index) => ({
params: { slug: post.slug },
- props: post,
+ props: {
+ post,
+ prevPost: index + 1 === total ? null : posts[index + 1],
+ nextPost: index === 0 ? null : posts[index - 1],
+ },
}));
}
type Props = CollectionEntry<"blog">;
-const post = Astro.props;
+const { post, prevPost, nextPost } = Astro.props;
const { Content, remarkPluginFrontmatter } = await post.render();
---
@@ -41,6 +48,10 @@ const { Content, remarkPluginFrontmatter } = await post.render();
</section>
<section>
+ <Pagination prevPost={prevPost} nextPost={nextPost} />
+ </section>
+
+ <section>
<Comments />
</section>
</article>