feat: implement back-to-top button in blog post page

Implement back-to-top button under tags in PostDetails page. Update html scroll-behavior to smooth.

Closes: #156
This commit is contained in:
satnaing
2023-12-14 11:20:03 +06:30
committed by Sat Naing
parent fb3fa98936
commit c526157118
2 changed files with 26 additions and 4 deletions
+23 -4
View File
@@ -52,9 +52,22 @@ const ogUrl = new URL(
<Content /> <Content />
</article> </article>
<ul class="tags-container"> <ul class="my-8">
{tags.map(tag => <Tag name={slugifyStr(tag)} />)} {tags.map(tag => <Tag name={slugifyStr(tag)} />)}
</ul> </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> </main>
<Footer /> <Footer />
</Layout> </Layout>
@@ -66,7 +79,13 @@ const ogUrl = new URL(
.post-title { .post-title {
@apply text-2xl font-semibold text-skin-accent; @apply text-2xl font-semibold text-skin-accent;
} }
.tags-container {
@apply my-8;
}
</style> </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
View File
@@ -20,6 +20,9 @@
--color-card-muted: 138, 51, 2; --color-card-muted: 138, 51, 2;
--color-border: 171, 75, 8; --color-border: 171, 75, 8;
} }
html {
scroll-behavior: smooth;
}
#sun-svg, #sun-svg,
html[data-theme="dark"] #moon-svg { html[data-theme="dark"] #moon-svg {
display: none; display: none;