Skip to content

Commit

Permalink
Add Back to Top button
Browse files Browse the repository at this point in the history
  • Loading branch information
gruz0 committed Nov 13, 2024
1 parent b338808 commit aa32373
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app/ideas/[id]/IdeaAnalysisReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useRouter } from 'next/navigation'
import React, { useEffect, useState } from 'react'
import BackToTopButton from '@/components/BackToTopButton'
import FeedbackForm from '@/components/FeedbackForm'
import HorizontalLine from '@/components/HorizontalLine'
import MessageBox from '@/components/MessageBox'
Expand Down Expand Up @@ -395,6 +396,8 @@ export const IdeaAnalysisReport = ({ data }: Props) => {
Remove Idea
</button>
</div>

<BackToTopButton />
</div>
)
}
34 changes: 34 additions & 0 deletions src/components/BackToTopButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client'
import React, { useEffect, useState } from 'react'

const BackToTopButton = () => {
const [isVisible, setIsVisible] = useState(false)

useEffect(() => {
const handleScroll = () => {
setIsVisible(window.scrollY > 300)
}
window.addEventListener('scroll', handleScroll)
return () => window.removeEventListener('scroll', handleScroll)
}, [])

const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: 'smooth' })
}

return (
<>
{isVisible && (
<button
onClick={scrollToTop}
className="fixed bottom-8 right-8 hidden rounded-lg px-4 py-3 text-2xl text-white shadow-lg transition-colors duration-300 hover:bg-blue-600 focus:outline-none md:bottom-10 md:right-10 md:block"
aria-label="Back to Top"
>
⬆️
</button>
)}
</>
)
}

export default BackToTopButton

0 comments on commit aa32373

Please sign in to comment.