Skip to content

Commit

Permalink
basic animation when a choice is made
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmcadam committed Aug 2, 2024
1 parent 635d412 commit 3f24b76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
--brand: 181 69% 14%;
--brand-600: 166 12% 48%;
--brand-300: 167 11% 69%;
--brand-100: 129 18% 92%;
}
}
35 changes: 25 additions & 10 deletions src/components/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,41 @@ export function Quiz() {
const isAccepted = questions.every((q) => q.status === "complete");

function handleOptionClick(option: Option) {
// Update the current question with its response and "complete" status
// and set the next question as "upcoming"
setQuestions((prevQuestions) =>
// Update the current question with its response and "complete" status
// and set the next question as "upcoming"
prevQuestions.map((q, i) => {
if (i === currentIndex) {
return {
...q,
response: option,
status: "complete",
};
} else if (i === currentIndex + 1 && q.status === "upcoming") {
return {
...q,
status: "current",
};
}

return q;
}),
);

if (currentIndex < questions.length - 1) {
setCurrentIndex(currentIndex + 1);
}
// Add a small delay to show the selected option before moving to the next question
setTimeout(() => {
setQuestions((prevQuestions) =>
prevQuestions.map((q, i) => {
if (i === currentIndex + 1 && q.status === "upcoming") {
return {
...q,
status: "current",
};
}

return q;
}),
);

if (currentIndex < questions.length - 1) {
setCurrentIndex(currentIndex + 1);
}
}, 1000);
}

// If the number of options is a multiple of 3, use 3 columns, otherwise 2
Expand Down Expand Up @@ -117,6 +129,9 @@ export function Quiz() {
variant="outline"
className={cn("mt-2 h-auto flex flex-col relative", {
"ring-2 ring-primary": isOptionSelected,
// TODO: only apply this after clicking the option because otherwise, when navigating back to a question,
// the selected option will blink.
"animate-blink": isOptionSelected,
})}
>
{isOptionSelected ? (
Expand Down
6 changes: 6 additions & 0 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const config = {
DEFAULT: "hsl(var(--brand))",
600: "hsl(var(--brand-600))",
300: "hsl(var(--brand-300))",
100: "hsl(var(--brand-100))",
},
},
borderRadius: {
Expand All @@ -73,10 +74,15 @@ const config = {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: "0" },
},
blink: {
"0%, 50%, 100%": { backgroundColor: "transparent" },
"25%, 75%": { backgroundColor: "hsl(var(--brand-100))" },
},
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
blink: "blink 0.6s cubic-bezier(0.4, 0, 0.2, 1) both",
},
fontFamily: {
sans: ["var(--font-sans)", ...fontFamily.sans],
Expand Down

0 comments on commit 3f24b76

Please sign in to comment.