Skip to content

Commit

Permalink
Feat: top button 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
eunji-0623 committed Jul 20, 2024
1 parent 2a4b9d6 commit 2b7e667
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions components/Button/TopButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Image from 'next/image';
import React, { useState, useEffect } from 'react';

const TopButton = () => {
const [showButton, setShowButton] = useState(false);

const scrollToTop = () => {
window.scroll({
top: 0,
behavior: 'smooth',
});
};
useEffect(() => {
const ShowButtonClick = () => {
if (window.scrollY > 500) {
setShowButton(true);
} else {
setShowButton(false);
}
};
window.addEventListener('scroll', ShowButtonClick);
return () => {
window.removeEventListener('scroll', ShowButtonClick);
};
}, []);
return (
<>
{showButton && (
<div className="fixed right-[30px] bottom-[30px] z-30">
<button
onClick={scrollToTop}
type="button"
className="flex justify-center items-center w-[40px] h-[40px] rounded-full bg-nomad-black border-[3px] border-solid border-var-gray1 dark:bg-var-dark2 dark:border-var-dark3"
>
<Image
src="/icon/chevron_up_white.png"
alt="화살표"
width={26}
height={26}
/>
</button>
</div>
)}
</>
);
};

export default TopButton;
2 changes: 2 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import SidenNavigationMobile from '@/components/SideNavigation/SideNavigationMobile';
import Theme from '@/components/Theme/Theme';
import TopButton from '@/components/Button/TopButton';

declare global {
interface Window {
Expand Down Expand Up @@ -72,6 +73,7 @@ export default function App({ Component, pageProps }: AppProps) {
<Modal />
<SidenNavigationMobile />
<SilentRefresh />
<TopButton />
</QueryClientProvider>
)}
</RecoilRoot>
Expand Down
Binary file added public/icon/chevron_up_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b7e667

Please sign in to comment.