-
Notifications
You must be signed in to change notification settings - Fork 110
feat: add scroll to top button with progress indicator #205
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
Open
Swaim-Sahay
wants to merge
2
commits into
AOSSIE-Org:main
Choose a base branch
from
Swaim-Sahay:feature/scroll-to-top
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
β0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,94 @@ | ||
| import { useState, useEffect } from "react"; | ||
| import { ArrowUp } from "lucide-react"; | ||
|
|
||
| export default function ScrollToTop() { | ||
| const [isVisible, setIsVisible] = useState(false); | ||
| const [scrollProgress, setScrollProgress] = useState(0); | ||
|
|
||
| // Show button when page is scrolled down and track scroll progress | ||
| useEffect(() => { | ||
| const toggleVisibility = () => { | ||
| const scrolled = window.scrollY; | ||
| const windowHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight; | ||
| const progress = (scrolled / windowHeight) * 100; | ||
|
|
||
| setScrollProgress(progress); | ||
|
|
||
| if (scrolled > 300) { | ||
| setIsVisible(true); | ||
| } else { | ||
| setIsVisible(false); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener("scroll", toggleVisibility); | ||
| toggleVisibility(); // Initial check | ||
|
|
||
| return () => { | ||
| window.removeEventListener("scroll", toggleVisibility); | ||
| }; | ||
| }, []); | ||
|
|
||
| const scrollToTop = () => { | ||
| window.scrollTo({ | ||
| top: 0, | ||
| behavior: "smooth", | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div | ||
| className={`fixed bottom-8 right-8 z-50 transition-all duration-500 ${ | ||
| isVisible ? "opacity-100 translate-y-0" : "opacity-0 translate-y-16 pointer-events-none" | ||
| }`} | ||
| > | ||
| <button | ||
| onClick={scrollToTop} | ||
| className="group relative p-4 rounded-full bg-gradient-to-br from-purple-600 via-purple-700 to-indigo-600 text-white shadow-2xl hover:shadow-purple-500/50 transition-all duration-300 hover:scale-110 focus:outline-none focus:ring-4 focus:ring-purple-500/50 active:scale-95 backdrop-blur-sm" | ||
| aria-label="Scroll to top" | ||
| style={{ | ||
| boxShadow: `0 10px 40px rgba(147, 51, 234, ${scrollProgress / 200})`, | ||
| }} | ||
| > | ||
| {/* Animated gradient border */} | ||
| <div className="absolute inset-0 rounded-full bg-gradient-to-r from-purple-400 via-pink-500 to-purple-600 opacity-0 group-hover:opacity-100 blur-sm transition-opacity duration-300 -z-10" /> | ||
|
|
||
| {/* Progress ring */} | ||
| <svg className="absolute inset-0 w-full h-full -rotate-90" style={{ filter: 'drop-shadow(0 0 8px rgba(168, 85, 247, 0.4))' }}> | ||
| <circle | ||
| cx="50%" | ||
| cy="50%" | ||
| r="45%" | ||
| fill="none" | ||
| stroke="rgba(255, 255, 255, 0.2)" | ||
| strokeWidth="2" | ||
| /> | ||
| <circle | ||
| cx="50%" | ||
| cy="50%" | ||
| r="45%" | ||
| fill="none" | ||
| stroke="white" | ||
| strokeWidth="2" | ||
| strokeDasharray={`${2 * Math.PI * 20}`} | ||
| strokeDashoffset={`${2 * Math.PI * 20 * (1 - scrollProgress / 100)}`} | ||
| strokeLinecap="round" | ||
| className="transition-all duration-300" | ||
| /> | ||
| </svg> | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| {/* Arrow icon with animation */} | ||
| <ArrowUp className="h-6 w-6 relative z-10 group-hover:animate-bounce" /> | ||
|
|
||
| {/* Pulse effect on hover */} | ||
| <span className="absolute inset-0 rounded-full bg-white opacity-0 group-hover:opacity-20 group-hover:animate-ping" /> | ||
| </button> | ||
|
|
||
| {/* Tooltip */} | ||
| <div className="absolute bottom-full right-0 mb-2 px-3 py-1.5 bg-gray-900 text-white text-sm rounded-lg opacity-0 group-hover:opacity-100 transition-opacity duration-200 whitespace-nowrap pointer-events-none"> | ||
| Back to top | ||
| <div className="absolute top-full right-4 -mt-1 border-4 border-transparent border-t-gray-900" /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.