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] 캐스퍼 쇼케이스 화면 로딩 오래 걸리는 오류 수정 #97

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
5 changes: 4 additions & 1 deletion client/src/features/CasperCustom/CasperFlipCard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { memo } from "react";
import { motion } from "framer-motion";
import { CASPER_CARD_SIZE, CASPER_SIZE_OPTION } from "@/constants/CasperCustom/casper";
import { CasperCardType } from "../CasperShowCase/TransitionCasperCards";
Expand All @@ -10,7 +11,7 @@ interface CasperFlipCardProps {
isFlipped: boolean;
}

export default function CasperFlipCard({ size, card, isFlipped }: CasperFlipCardProps) {
function CasperFlipCard({ size, card, isFlipped }: CasperFlipCardProps) {
return (
<div style={{ perspective: "1000px" }}>
<motion.div
Expand Down Expand Up @@ -54,3 +55,5 @@ export default function CasperFlipCard({ size, card, isFlipped }: CasperFlipCard
</div>
);
}

export default memo(CasperFlipCard);
35 changes: 24 additions & 11 deletions client/src/features/CasperShowCase/TransitionCasperCards.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useRef, useState } from "react";
import { motion, useAnimation } from "framer-motion";
import { CASPER_SIZE_OPTION } from "@/constants/CasperCustom/casper";
import { CASPER_CARD_SIZE, CASPER_SIZE_OPTION } from "@/constants/CasperCustom/casper";
import { CARD_TRANSITION } from "@/constants/CasperShowCase/showCase";
import useLazyLoading from "@/hooks/useLazyLoading";
import { SelectedCasperIdxType } from "@/types/casperCustom";
import CasperFlipCard from "../CasperCustom/CasperFlipCard";

Expand All @@ -28,7 +29,7 @@ export default function TransitionCasperCards({
gap,
isEndCard,
}: TransitionCasperCardsProps) {
const containerRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLUListElement>(null);
const transitionControls = useAnimation();

const [x, setX] = useState<number>(initialX);
Expand All @@ -55,6 +56,7 @@ export default function TransitionCasperCards({

const renderCardItem = (cardItem: CasperCardType, id: string) => {
const [isFlipped, setIsFlipped] = useState<boolean>(false);
const { isInView, cardRef } = useLazyLoading<HTMLLIElement>();

const handleMouseEnter = () => {
stopAnimation();
Expand All @@ -67,18 +69,29 @@ export default function TransitionCasperCards({
};

return (
<div key={id} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}>
<CasperFlipCard
card={cardItem}
size={CASPER_SIZE_OPTION.SM}
isFlipped={isFlipped}
/>
</div>
<li
ref={cardRef}
key={id}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
style={{
width: CASPER_CARD_SIZE[CASPER_SIZE_OPTION.SM].CARD_WIDTH,
height: CASPER_CARD_SIZE[CASPER_SIZE_OPTION.SM].CARD_HEIGHT,
}}
>
{isInView && (
<CasperFlipCard
card={cardItem}
size={CASPER_SIZE_OPTION.SM}
isFlipped={isFlipped}
/>
)}
</li>
);
};

return (
<motion.div
<motion.ul
ref={containerRef}
className="flex"
animate={transitionControls}
Expand All @@ -91,6 +104,6 @@ export default function TransitionCasperCards({
>
{cardList.map((card) => renderCardItem(card, card.id))}
{cardList.map((card) => renderCardItem(card, `${card.id}-clone`))}
</motion.div>
</motion.ul>
);
}
30 changes: 30 additions & 0 deletions client/src/hooks/useLazyLoading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useRef, useState } from "react";

export default function useLazyLoading<T extends HTMLElement>() {
const [isInView, setIsInView] = useState<boolean>(false);
const cardRef = useRef<T>(null);

useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsInView(true);
observer.disconnect();
}
},
{ threshold: 0, root: document.body }
);

if (cardRef.current) {
observer.observe(cardRef.current);
}

return () => {
if (cardRef.current) {
observer.unobserve(cardRef.current);
}
};
}, [cardRef]);

return { isInView, cardRef };
}
Loading