forked from torrust/torrust-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge torrust#180: Add link to previous and next posts under each blo…
…g post acdbeab Add link to previous and next posts under each blog post (Graeme Byrne) Pull request description: * Add new component called `PrevNextPost.svelte` to `singletons` component folder. * This component is placed in the `+layout.svelte` file of `(blog-article)` route.`currentPost` and `allPosts` are passed as props to the component. * The component is then responsible for displaying navigation links to the next and previous blog posts based on the currently viewed post. * This enhances user experience by providing easy navigation between posts. * The component uses TypeScript for type safety and reactive statements to dynamically update the displayed links. * It also includes appropriate styling for a clean and visually appealing layout. ACKs for top commit: josecelano: ACK acdbeab Tree-SHA512: 3ee4b14064837a40ae5ab9280790a28704836742af77499866456c530d7cc1d0c07a885421afcefd534e6169fb32606242149fa232b06f145d90837541844122
- Loading branch information
Showing
3 changed files
with
105 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<script lang="ts"> | ||
import type { BlogPost } from '$lib/utils/types'; | ||
import Icon from '@iconify/svelte'; | ||
export let currentPost: BlogPost; | ||
export let allPosts: BlogPost[]; | ||
let prevPost: BlogPost | null = null; | ||
let nextPost: BlogPost | null = null; | ||
$: { | ||
if (currentPost && allPosts.length) { | ||
allPosts.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()); | ||
const currentIndex = allPosts.findIndex((post) => post.slug === currentPost.slug); | ||
if (currentIndex !== -1) { | ||
prevPost = currentIndex > 0 ? allPosts[currentIndex - 1] : null; | ||
nextPost = currentIndex < allPosts.length - 1 ? allPosts[currentIndex + 1] : null; | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<div class="container"> | ||
<div class="nextPost"> | ||
{#if nextPost} | ||
<a href="/{nextPost.slug}">{nextPost.title}</a> | ||
<div class="arrow arrowNext"> | ||
<Icon icon="material-symbols:arrow-left" width="44" height="44" style="color: #650000" /> | ||
<p>Next Post</p> | ||
</div> | ||
{:else} | ||
<h3 class="inactive">You're up to date. More to come soon!</h3> | ||
{/if} | ||
</div> | ||
|
||
<div class="previousPost"> | ||
{#if prevPost} | ||
<a href="/{prevPost.slug}">{prevPost.title}</a> | ||
<div class="arrow arrowPrevious"> | ||
<p>Previous Post</p> | ||
<Icon icon="material-symbols:arrow-right" width="44" height="44" style="color: #650000" /> | ||
</div> | ||
{:else} | ||
<h3 class="inactive">You are reading our first post.</h3> | ||
{/if} | ||
</div> | ||
</div> | ||
|
||
<style lang="scss"> | ||
.container { | ||
display: flex; | ||
justify-content: space-between; | ||
border-top: 1px solid #979797; | ||
border-bottom: 1px solid #979797; | ||
} | ||
.nextPost, | ||
.previousPost { | ||
flex: 1; | ||
padding-top: 1.5rem; | ||
padding-bottom: 1.5rem; | ||
} | ||
.nextPost { | ||
border-right: 1px solid #979797; | ||
} | ||
.previousPost { | ||
text-align: right; | ||
} | ||
.arrow { | ||
display: flex; | ||
align-items: center; | ||
} | ||
.arrowNext { | ||
justify-content: flex-start; | ||
} | ||
.arrowPrevious { | ||
justify-content: flex-end; | ||
} | ||
.inactive { | ||
color: gray; | ||
} | ||
</style> |
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