Skip to content

Commit 2577b8e

Browse files
committed
feat: ui add to top components
1 parent e7f680f commit 2577b8e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/app/layout.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { Metadata } from 'next'
22
import { Inter } from 'next/font/google'
33
import './globals.css'
44
import type { ReactNode } from 'react'
5-
import { BackgroundBeamsWithCollision } from '@/components/ui/background-beams-with-collision'
5+
import { BackgroundBeamsWithCollision } from '@/components/ui/BeamsWithCollision'
66
import { Header } from '@/components/ui/header'
7+
import { ScrollToTop } from '@/components/ui/ScrollToTop'
78

89
const inter = Inter({ subsets: ['latin'] })
910

@@ -26,6 +27,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
2627
{children}
2728
</div>
2829
</main>
30+
<ScrollToTop />
2931
</body>
3032
</html>
3133
)

src/components/ui/ScrollToTop.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use client'
2+
3+
import { useEffect, useState } from 'react'
4+
import { Button } from './button'
5+
import { ArrowUp } from 'lucide-react'
6+
import { cn } from '@/lib/utils'
7+
8+
export const ScrollToTop = () => {
9+
const [isVisible, setIsVisible] = useState(false)
10+
11+
const handleScroll = () => {
12+
const scrollY = window.scrollY
13+
setIsVisible(scrollY > 200)
14+
}
15+
16+
const handleScrollToTop = () => {
17+
window.scrollTo({
18+
top: 0,
19+
behavior: 'smooth'
20+
})
21+
}
22+
23+
useEffect(() => {
24+
window.addEventListener('scroll', handleScroll)
25+
return () => window.removeEventListener('scroll', handleScroll)
26+
}, [])
27+
28+
return (
29+
<Button
30+
variant="outline"
31+
size="icon"
32+
className={cn(
33+
'fixed bottom-8 right-8 z-50 rounded-full shadow-lg transition-all duration-300',
34+
isVisible ? 'translate-y-0 opacity-100' : 'translate-y-28 opacity-0'
35+
)}
36+
onClick={handleScrollToTop}
37+
aria-label="返回顶部"
38+
>
39+
<ArrowUp className="h-4 w-4" />
40+
</Button>
41+
)
42+
}

0 commit comments

Comments
 (0)