Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 무한 캐러셀 관련 버그 수정 #66

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/comment/autoScrollCarousel/useAutoCarousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useMountDragEvent from "@/common/useMountDragEvent";

const FRICTION_RATE = 0.1;
const MOMENTUM_THRESHOLD = 0.6;
const MOMENTUM_RATE = 0.15;
const MOMENTUM_RATE = 0.3;

function useAutoCarousel(speed = 1) {
const childRef = useRef(null);
Expand All @@ -14,15 +14,14 @@ function useAutoCarousel(speed = 1) {
const dragging = useRef(false);
const prevDragState = useRef({ x: 0, mouseX: 0, prevMouseX: 0 });
const momentum = useRef(speed);
const raf = useRef(null);

useEffect(() => {
if (isControlled) return;

let progress = true;
timestamp.current = performance.now();
function animate(time) {
const animate = useCallback(
(time) => {
if (childRef.current === null) return;

const width = childRef.current.clientWidth;

// 마우스 뗐을 때 관성 재계산
const baseSpeed = isHovered ? 0 : speed;
momentum.current -= (momentum.current - baseSpeed) * FRICTION_RATE;
Expand All @@ -35,20 +34,26 @@ function useAutoCarousel(speed = 1) {
const interval = performance.now() - timestamp.current;
setPosition((position) => {
const newPos = position + finalSpeed * interval;
return newPos % childRef.current.clientWidth;
return newPos % width;
});

// 타임스탬프 저장
timestamp.current = time;
if (progress) requestAnimationFrame(animate);
}

requestAnimationFrame(animate);
},
[isHovered, speed],
);

useEffect(() => {
timestamp.current = performance.now();
function realAnimate(time) {
animate(time);
raf.current = requestAnimationFrame(realAnimate);
}
raf.current = requestAnimationFrame(realAnimate);
return () => {
progress = false;
cancelAnimationFrame(raf.current);
};
}, [isControlled, isHovered, speed]);
}, [isControlled, animate]);

// 드래그 도중 함수
const onDrag = useCallback(({ x: mouseX }) => {
Expand Down Expand Up @@ -83,11 +88,9 @@ function useAutoCarousel(speed = 1) {
ref: childRef,
eventListener: {
onMouseEnter() {
setIsControlled(true);
setIsHovered(true);
},
onMouseLeave() {
setIsControlled(false);
setIsHovered(false);
},
onPointerDown(e) {
Expand Down
3 changes: 2 additions & 1 deletion src/comment/commentCarousel/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMemo } from "react";
import Suspense from "@/common/Suspense.jsx";
import ErrorBoundary from "@/common/ErrorBoundary.jsx";
import { fetchResource } from "@/common/fetchServer.js";
Expand All @@ -6,7 +7,7 @@ import CommentCarouselSkeleton from "./CommentCarouselSkeleton.jsx";
import CommentCarouselError from "./CommentCarouselError.jsx";

function CommentCarouselView() {
const resource = fetchResource("/api/v1/comment");
const resource = useMemo(() => fetchResource("/api/v1/comment"), []);
return (
<ErrorBoundary fallback={<CommentCarouselError />}>
<Suspense fallback={<CommentCarouselSkeleton />}>
Expand Down