Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] :Adding a move to top icon #631

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/components/MoveToTopButton/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
const { class: customClass = "" } = Astro.props;
---

<a
id="moveToTop"
class={`move-to-top hidden outline outline-1 outline-type-color py-2 px-4 flex flex-nowrap w-fit items-center justify-center rounded-full hover:bg-sidebar-type-color hover:text-bg-color hover:no-underline ${customClass}`}
href="#"
title="Go to top"
>
<span>↑</span>
</a>

<script>
const moveToTopButton = document.getElementById("moveToTop");

// Ensure the element exists before accessing its methods
if (moveToTopButton) {
// Show the button when the user scrolls 100px down from the top
window.onscroll = function () {
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
moveToTopButton.classList.add("visible");
moveToTopButton.classList.remove("hidden");
} else {
moveToTopButton.classList.add("hidden");
moveToTopButton.classList.remove("visible");
}
};

// Scroll smoothly to the top when the button is clicked
moveToTopButton.addEventListener("click", (event) => {
event.preventDefault(); // Prevent default anchor behavior
window.scrollTo({ top: 0, behavior: "smooth" });
});
}
</script>

9 changes: 4 additions & 5 deletions src/layouts/BaseLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "@styles/base.scss";
import type { CollectionEntry } from "astro:content";
import { getCollectionInLocaleWithFallbacks } from "@pages/_utils";
import { removeLocalePrefix } from "@i18n/utils";
import MoveToTopButton from "@components/MoveToTopButton/index.astro";
import "@styles/move-to-top.scss";

interface Props {
title: string;
Expand All @@ -21,7 +23,6 @@ interface Props {
variant?: "root" | "item" | "search" | "homepage";
topic?: PageTopic;
mainContentParentClass?: string;
/* Only needed for the homepage */
homepageConfig?: CollectionEntry<"homepage">;
}

Expand Down Expand Up @@ -62,10 +63,7 @@ const headerTopic = topic
: { name: t(capitalize(fallbackTopic)) as string, url: `/${fallbackTopic}` };
---

<html
class={`${titleClass.toLowerCase()} ${className} scroll-smooth`}
lang={currentLocale}
>
<html class={`${titleClass.toLowerCase()} ${className} scroll-smooth`} lang={currentLocale}>
<head>
<script is:inline>
// Get any theme settings and apply before load
Expand Down Expand Up @@ -124,5 +122,6 @@ const headerTopic = topic
</main>
<Footer />
</div>
<MoveToTopButton />
</body>
</html>
47 changes: 47 additions & 0 deletions styles/move-to-top.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.move-to-top {
position: fixed;
bottom: 4.5rem;
right: 1.5rem;
padding: 0.75rem;
background-color: #000000;
color: #ffffff;
border-radius: 50%;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
opacity: 0;
transform: translateY(20px);
transition: opacity 0.3s ease, transform 0.3s ease;
z-index: 1000;
cursor: pointer;
font-size: 0.875rem;
}

.move-to-top.hidden {
opacity: 0;
pointer-events: none;
}

.move-to-top.visible {
opacity: 1;
transform: translateY(0);
}

.move-to-top:hover {
background-color: #333333;
}

.move-to-top:focus {
outline: none;
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.5);
}
.move-to-top:active {
background-color: #1a1a1a;
}
@media screen and (max-width: 640px) {
.move-to-top {
bottom: 2rem;
right: 1rem;
padding: 0.5rem;
font-size: 0.75rem;
}
}