diff --git a/src/App.tsx b/src/App.tsx index 478da36d..6c564956 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,6 +4,7 @@ import { BrowserRouter } from 'react-router-dom'; import { Routes } from './routes/Routes'; import { SessionProvider } from './contexts'; import { Toaster } from './components/ui/toaster'; +import { ScrollToTopButton } from './components/ScrollToTopButton'; const App = () => { return ( @@ -12,6 +13,7 @@ const App = () => { + diff --git a/src/components/ScrollToTopButton/ScrollToTopButton.tsx b/src/components/ScrollToTopButton/ScrollToTopButton.tsx new file mode 100644 index 00000000..d568d7ef --- /dev/null +++ b/src/components/ScrollToTopButton/ScrollToTopButton.tsx @@ -0,0 +1,44 @@ +import { useState, forwardRef } from 'react'; +import { IScrollToTopButton } from './types'; +import { cn } from '@/lib/utils'; + +const ScrollToTopButton = forwardRef( + ({ className = '', ...rest }, ref) => { + const [isVisible, setIsVisible] = useState(false); + + const toggleVisibility = () => { + const scrolled = document.documentElement.scrollTop; + if (scrolled > 300) { + setIsVisible(true); + } else if (scrolled <= 300) { + setIsVisible(false); + } + }; + + const scrollToTop = () => { + window.scrollTo({ + top: 0, + behavior: 'smooth', + }); + }; + + window.addEventListener('scroll', toggleVisibility); + + return ( + + ); + } +); + +export { ScrollToTopButton }; diff --git a/src/components/ScrollToTopButton/index.ts b/src/components/ScrollToTopButton/index.ts new file mode 100644 index 00000000..d8380d51 --- /dev/null +++ b/src/components/ScrollToTopButton/index.ts @@ -0,0 +1,3 @@ +import { ScrollToTopButton } from './ScrollToTopButton'; + +export { ScrollToTopButton }; diff --git a/src/components/ScrollToTopButton/types.ts b/src/components/ScrollToTopButton/types.ts new file mode 100644 index 00000000..6389149c --- /dev/null +++ b/src/components/ScrollToTopButton/types.ts @@ -0,0 +1,6 @@ +// components/ScrollToTopButton/types.ts + +export interface IScrollToTopButton + extends React.ComponentPropsWithoutRef<'button'> { + className?: string; +} diff --git a/src/global.css b/src/global.css index 80c42b80..a230855c 100644 --- a/src/global.css +++ b/src/global.css @@ -88,6 +88,5 @@ #root { max-width: 100vw; - overflow-x: hidden; } }