From 548b7f31f9c0b4412e77709e5f0491e866597bd4 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 3 Sep 2023 11:49:00 +0100 Subject: [PATCH] Fix confetti does not show up properly --- src/components/Confetti.tsx | 17 ++++++++++------- src/components/Questions/QuestionsList.tsx | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/components/Confetti.tsx b/src/components/Confetti.tsx index 0064e510c..79c517cb9 100644 --- a/src/components/Confetti.tsx +++ b/src/components/Confetti.tsx @@ -20,12 +20,7 @@ export function Confetti(props: ConfettiProps) { undefined | ConfettiPosition >(); - useEffect(() => { - if (!element) { - setConfettiPos(undefined); - return; - } - + function populateConfettiPosition(element: HTMLElement) { const elRect = element.getBoundingClientRect(); // set confetti position, keeping in mind the scroll values @@ -35,6 +30,15 @@ export function Confetti(props: ConfettiProps) { w: elRect?.width || 0, h: elRect?.height || 0, }); + } + + useEffect(() => { + if (!element) { + setConfettiPos(undefined); + return; + } + + populateConfettiPosition(element); }, [element]); if (!confettiPos) { @@ -47,7 +51,6 @@ export function Confetti(props: ConfettiProps) { numberOfPieces={40} recycle={false} onConfettiComplete={(confettiInstance) => { - confettiInstance?.reset(); setConfettiPos(undefined); onDone(); }} diff --git a/src/components/Questions/QuestionsList.tsx b/src/components/Questions/QuestionsList.tsx index 61ef525f7..3d615c0c2 100644 --- a/src/components/Questions/QuestionsList.tsx +++ b/src/components/Questions/QuestionsList.tsx @@ -36,6 +36,17 @@ export function QuestionsList(props: QuestionsListProps) { const alreadyKnowRef = useRef(null); const didNotKnowRef = useRef(null); + function showConfetti(el: HTMLElement | null) { + // If confetti is already showing, remove that first + if (confettiEl) { + setConfettiEl(null); + } + + window.setTimeout(() => { + setConfettiEl(el); + }, 0); + } + async function fetchUserProgress(): Promise< UserQuestionProgress | undefined > { @@ -146,7 +157,7 @@ export function QuestionsList(props: QuestionsListProps) { disabled={isLoading || isUpdatingStatus} ref={alreadyKnowRef} onClick={(e) => { - setConfettiEl(alreadyKnowRef.current); + showConfetti(alreadyKnowRef.current); updateQuestionStatus('know', currQuestion.id).finally(() => null); }} className="flex flex-1 items-center rounded-xl border border-gray-300 bg-white py-3 px-4 text-black transition-colors hover:border-black hover:bg-black hover:text-white disabled:pointer-events-none disabled:opacity-50" @@ -157,7 +168,7 @@ export function QuestionsList(props: QuestionsListProps) {