-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add a scroll to top button in the website #78
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||||||||
| import React, { useEffect, useState } from 'react'; | ||||||||||||
| import styles from './styles.module.css'; | ||||||||||||
|
|
||||||||||||
| export default function ScrollButton() { | ||||||||||||
| const [showButton, setShowButton] = useState(false); | ||||||||||||
|
|
||||||||||||
| useEffect(() => { | ||||||||||||
| const handleScroll = () => { | ||||||||||||
| if (typeof window !== 'undefined') { | ||||||||||||
| setShowButton(window.scrollY > 150); | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| if (typeof window !== 'undefined') { | ||||||||||||
| window.addEventListener('scroll', handleScroll); | ||||||||||||
| // Initial check | ||||||||||||
| handleScroll(); | ||||||||||||
| return () => window.removeEventListener('scroll', handleScroll); | ||||||||||||
| } | ||||||||||||
|
||||||||||||
| } | |
| } | |
| // Return a no-op cleanup function when window is undefined (e.g., during SSR) | |
| return () => {}; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| .scrollButtonContainer { | ||
| position: fixed; | ||
| bottom: 2rem; | ||
| right: 2rem; | ||
| z-index: 1000; | ||
| } | ||
|
|
||
| .scrollButton { | ||
| width: 3rem; | ||
| height: 3rem; | ||
| border-radius: 0.5rem; | ||
| border: 1px solid #e5e7eb; | ||
| background-color: #ffffff; | ||
| box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); | ||
| cursor: pointer; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| transition: all 0.3s ease; | ||
| } | ||
|
|
||
| .scrollButton:hover { | ||
| background-color: #f9fafb; | ||
| transform: translateY(-0.25rem); | ||
| } | ||
|
|
||
| .scrollIcon { | ||
| width: 1.5rem; | ||
| height: 1.5rem; | ||
| color: #374151; | ||
| } | ||
|
|
||
| /* Dark mode styles */ | ||
| html[data-theme="dark"] .scrollButton { | ||
| background-color: #1f2937; | ||
| border-color: #4b5563; | ||
| } | ||
|
|
||
| html[data-theme="dark"] .scrollButton:hover { | ||
| background-color: #374151; | ||
| } | ||
|
|
||
| html[data-theme="dark"] .scrollIcon { | ||
| color: #d1d5db; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| import Layout from "@theme/Layout"; | ||
| import HomepageHeader from "../components/HomepageHeader"; | ||
| import HomepageContent from "../components/HomepageContent"; | ||
| import ScrollButton from "../components/ScrollButton"; | ||
|
|
||
| export default function Home() { | ||
| return ( | ||
| <Layout title="Home" description="Landing page for schema project"> | ||
| <HomepageHeader /> | ||
| <HomepageContent /> | ||
| <ScrollButton /> | ||
| </Layout> | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scroll event handler is called on every scroll event without throttling or debouncing. This can cause performance issues on lower-end devices or during fast scrolling. Consider using a throttle or debounce mechanism to limit the frequency of state updates.