Skip to content

Commit

Permalink
Merge pull request #36 from Ralf-Pauli/feat/scroll-to-top
Browse files Browse the repository at this point in the history
feat: scroll to top button
  • Loading branch information
m-krebs authored Oct 4, 2024
2 parents 46c8ae3 + ae14b2b commit a971ae7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lib/components/ScrollToTop.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
import { onMount } from "svelte";
import { fade } from "svelte/transition";
import { ArrowUp } from "lucide-svelte";
let showButton = false;
function scrollToTop() {
window.scrollTo({ top: 0, behavior: "smooth" });
}
function handleScroll() {
const scrollY = window.scrollY ?? document.documentElement.scrollTop;
showButton = scrollY > 100;
}
onMount(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
});
</script>
{#if showButton}
<button
on:click={scrollToTop}
transition:fade
class="fixed bottom-5 right-5 bg-primary text-primary-foreground rounded-full p-3 shadow-lg hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2"
aria-label="Scroll to top">
<ArrowUp class="h-6 w-6" />
</button>
{/if}
3 changes: 3 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import Navbar from "$components/navbar/Navbar.svelte";
import Footer from "$components/footer/Footer.svelte";
import ScrollToTop from "$components/ScrollToTop.svelte";
export let data: LayoutData;
</script>

<div class="h-screen px-3">
<Navbar />
<slot />
<Footer />
<ScrollToTop />
</div>

0 comments on commit a971ae7

Please sign in to comment.