Skip to content

Commit

Permalink
feat: implement back-to-top button in blog post page
Browse files Browse the repository at this point in the history
Implement back-to-top button under tags in PostDetails page. Update html scroll-behavior to smooth.

Closes: #156
  • Loading branch information
satnaing committed Dec 15, 2023
1 parent fb3fa98 commit c526157
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,22 @@ const ogUrl = new URL(
<Content />
</article>

<ul class="tags-container">
<ul class="my-8">
{tags.map(tag => <Tag name={slugifyStr(tag)} />)}
</ul>
<div class="flex justify-end">
<button
id="back-to-top"
class="focus-outline whitespace-nowrap hover:opacity-75"
>
<svg xmlns="http://www.w3.org/2000/svg" class="rotate-90">
<path
d="M13.293 6.293 7.586 12l5.707 5.707 1.414-1.414L10.414 12l4.293-4.293z"
></path>
</svg>
<span>Back to Top</span>
</button>
</div>
</main>
<Footer />
</Layout>
Expand All @@ -66,7 +79,13 @@ const ogUrl = new URL(
.post-title {
@apply text-2xl font-semibold text-skin-accent;
}
.tags-container {
@apply my-8;
}
</style>

<script is:inline>
/* When the user clicks on the "Back to Top" button,
* scroll to the top of the document */
document.querySelector("#back-to-top")?.addEventListener("click", () => {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
});
</script>
3 changes: 3 additions & 0 deletions src/styles/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
--color-card-muted: 138, 51, 2;
--color-border: 171, 75, 8;
}
html {
scroll-behavior: smooth;
}
#sun-svg,
html[data-theme="dark"] #moon-svg {
display: none;
Expand Down

0 comments on commit c526157

Please sign in to comment.