-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor ScrollToTop component and add ScrollToTopButton
The ScrollToTop component in src/components/ScrollToTop/ScrollToTop.tsx has been refactored to improve code readability and maintainability. Additionally, a new ScrollToTopButton component has been added to provide a button for scrolling to the top of the page. This change enhances the user experience and navigation within the application.
- Loading branch information
Showing
5 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,22 @@ | ||
import { useScrollVisibility } from '../hooks/useScrollVisibility'; | ||
|
||
const SCROLL_THRESHOLD = 20; // Percentage | ||
|
||
export const ScrollToTopButton: React.FC = () => { | ||
const isVisible = useScrollVisibility(SCROLL_THRESHOLD); | ||
|
||
const handleScrollToTop = () => { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}; | ||
|
||
return ( | ||
<button | ||
onClick={handleScrollToTop} | ||
className={`fixed bottom-5 right-5 p-2.5 text-lg cursor-pointer z-50 bg-blue-500 text-white rounded transition-opacity duration-300 ${ | ||
isVisible ? 'opacity-100' : 'opacity-0 pointer-events-none' | ||
}`} | ||
> | ||
Top | ||
</button> | ||
); | ||
}; |
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './ScrollToTop'; | ||
export * from './ScrollToTopButton'; |
This file contains 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,24 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useScrollVisibility = (threshold: number) => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
const scrollTop = window.scrollY; | ||
const windowHeight = window.innerHeight; | ||
const documentHeight = document.documentElement.scrollHeight; | ||
const scrolledPercentage = | ||
(scrollTop / (documentHeight - windowHeight)) * 100; | ||
|
||
setIsVisible(scrolledPercentage > threshold); | ||
}; | ||
|
||
window.addEventListener('scroll', handleScroll); | ||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, [threshold]); | ||
|
||
return isVisible; | ||
}; |
This file contains 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