Fix confetti does not show up properly

pull/4448/head
Kamran Ahmed 1 year ago
parent 2e18d5a563
commit 548b7f31f9
  1. 17
      src/components/Confetti.tsx
  2. 15
      src/components/Questions/QuestionsList.tsx

@ -20,12 +20,7 @@ export function Confetti(props: ConfettiProps) {
undefined | ConfettiPosition undefined | ConfettiPosition
>(); >();
useEffect(() => { function populateConfettiPosition(element: HTMLElement) {
if (!element) {
setConfettiPos(undefined);
return;
}
const elRect = element.getBoundingClientRect(); const elRect = element.getBoundingClientRect();
// set confetti position, keeping in mind the scroll values // set confetti position, keeping in mind the scroll values
@ -35,6 +30,15 @@ export function Confetti(props: ConfettiProps) {
w: elRect?.width || 0, w: elRect?.width || 0,
h: elRect?.height || 0, h: elRect?.height || 0,
}); });
}
useEffect(() => {
if (!element) {
setConfettiPos(undefined);
return;
}
populateConfettiPosition(element);
}, [element]); }, [element]);
if (!confettiPos) { if (!confettiPos) {
@ -47,7 +51,6 @@ export function Confetti(props: ConfettiProps) {
numberOfPieces={40} numberOfPieces={40}
recycle={false} recycle={false}
onConfettiComplete={(confettiInstance) => { onConfettiComplete={(confettiInstance) => {
confettiInstance?.reset();
setConfettiPos(undefined); setConfettiPos(undefined);
onDone(); onDone();
}} }}

@ -36,6 +36,17 @@ export function QuestionsList(props: QuestionsListProps) {
const alreadyKnowRef = useRef<HTMLButtonElement>(null); const alreadyKnowRef = useRef<HTMLButtonElement>(null);
const didNotKnowRef = useRef<HTMLButtonElement>(null); const didNotKnowRef = useRef<HTMLButtonElement>(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< async function fetchUserProgress(): Promise<
UserQuestionProgress | undefined UserQuestionProgress | undefined
> { > {
@ -146,7 +157,7 @@ export function QuestionsList(props: QuestionsListProps) {
disabled={isLoading || isUpdatingStatus} disabled={isLoading || isUpdatingStatus}
ref={alreadyKnowRef} ref={alreadyKnowRef}
onClick={(e) => { onClick={(e) => {
setConfettiEl(alreadyKnowRef.current); showConfetti(alreadyKnowRef.current);
updateQuestionStatus('know', currQuestion.id).finally(() => null); 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" 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) {
<button <button
ref={didNotKnowRef} ref={didNotKnowRef}
onClick={() => { onClick={() => {
setConfettiEl(didNotKnowRef.current); showConfetti(didNotKnowRef.current);
updateQuestionStatus('dontKnow', currQuestion.id).finally( updateQuestionStatus('dontKnow', currQuestion.id).finally(
() => null () => null
); );

Loading…
Cancel
Save