Skip to content

Commit

Permalink
Merge pull request #149 from FinalDoubleTen/FE-67--Mypage/View
Browse files Browse the repository at this point in the history
Refactor: 탑버튼 재구현(임시)
  • Loading branch information
LeHiHo authored Jan 13, 2024
2 parents 038a9e0 + 1b4426e commit 5af7a8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
62 changes: 36 additions & 26 deletions src/components/DetailSectionTop/DetailTopButton.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { TopIcon } from '@components/common/icons/Icons';

export default function DetailTopButton() {
const [showButton, setShowButton] = useState(true);
export default function DetailTopButton({ parentRef }: any) {
const [isVisible, setIsVisible] = useState<boolean>(false);
const [scrollPosition, setScrollPosition] = useState<number>(0);
const [viewportHeight, setViewportHeight] = useState<number>(0);

const scrollToTop = () => {
window.scroll({
top: 0,
behavior: 'smooth',
});
};
const scrollButtonRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleShowButton = () => {
if (window.scrollY > 400) {
setShowButton(true);
} else {
setShowButton(false);
const handleScroll = () => {
if (scrollButtonRef.current && parentRef.current) {
setViewportHeight(screen.height);

// 부모 요소의 높이보다 적을 때까지 스크롤 허용
if (window.scrollY < parentRef.current.clientHeight - 50) {
// 기기 높이의 절반 이상 스크롤 했을 때
if (window.scrollY >= viewportHeight / 2) {
setIsVisible(true);
setScrollPosition(window.scrollY);
} else {
setIsVisible(false);
}
}
}
};

window.addEventListener('scroll', handleShowButton);
window.addEventListener('scroll', handleScroll);

return () => {
window.removeEventListener('scroll', handleShowButton);
window.removeEventListener('scroll', handleScroll);
};
}, []);
}, [parentRef, scrollPosition, setScrollPosition]);

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

return (
showButton && (
<div
onClick={scrollToTop}
className="scroll__container sticky bottom-3 z-20 flex cursor-pointer items-center justify-end">
<div className="flex h-12 w-12 items-center justify-center rounded-full bg-white shadow-md">
<TopIcon />
</div>
</div>
)
<div
ref={scrollButtonRef}
className={`absolute right-2 flex h-12 w-12 cursor-pointer items-center justify-center rounded-full bg-white shadow-md transition-opacity duration-500 ${
isVisible ? 'opacity-100' : 'opacity-0'
}`}
onClick={scrollToTop}
style={{ top: `${scrollPosition}px` }}>
<TopIcon />
</div>
);
}
15 changes: 10 additions & 5 deletions src/pages/detail/detail.page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { DetailHeader } from '@components/common/header';
import DetailSectionTop from '@components/DetailSectionTop/DetailSectionTop';
import DetailSectionBottom from '@components/DetailSectionBottom/DetailSectionBottom';
// import { DetailTopButton } from '@components/DetailSectionTop';
import { DetailTopButton } from '@components/DetailSectionTop';
import { useRef } from 'react';

const DetailTours = () => {
const parentRef = useRef<HTMLDivElement>(null);

return (
<>
<div>
<DetailHeader />
<DetailSectionTop />
<DetailSectionBottom />
{/* <DetailTopButton /> */}
</>
<div className="relative" ref={parentRef}>
<DetailSectionBottom />
<DetailTopButton parentRef={parentRef} />
</div>
</div>
);
};

Expand Down

0 comments on commit 5af7a8c

Please sign in to comment.