Skip to content

Commit 118ae2e

Browse files
authored
Merge pull request #323 from pharuq411/main
Create DiceAnimation component
2 parents 78c1cc6 + 7f246e1 commit 118ae2e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"use client";
2+
3+
import React from "react";
4+
5+
interface DiceAnimationProps {
6+
value?: number;
7+
isRolling?: boolean;
8+
}
9+
10+
const diceDots: Record<number, number[][]> = {
11+
1: [[1, 1]],
12+
2: [[0, 0], [2, 2]],
13+
3: [[0, 0], [1, 1], [2, 2]],
14+
4: [[0, 0], [0, 2], [2, 0], [2, 2]],
15+
5: [[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]],
16+
6: [[0, 0], [0, 2], [1, 0], [1, 2], [2, 0], [2, 2]],
17+
};
18+
19+
export default function DiceAnimation({ value = 1, isRolling = false }: DiceAnimationProps) {
20+
const clampedValue = Math.max(1, Math.min(6, value));
21+
const dots = diceDots[clampedValue];
22+
23+
return (
24+
<div
25+
role="img"
26+
aria-label={isRolling ? "Rolling dice" : `Dice showing ${clampedValue}`}
27+
aria-live="polite"
28+
className="inline-flex items-center justify-center"
29+
>
30+
<div
31+
className={`relative w-16 h-16 bg-[var(--tycoon-card-bg)] border-2 border-[var(--tycoon-accent)] rounded-lg shadow-lg ${
32+
isRolling ? "animate-roll" : ""
33+
}`}
34+
style={{
35+
perspective: "1000px",
36+
}}
37+
>
38+
<div className="absolute inset-0 grid grid-cols-3 grid-rows-3 gap-1 p-2">
39+
{Array.from({ length: 9 }, (_, i) => {
40+
const row = Math.floor(i / 3);
41+
const col = i % 3;
42+
const hasDot = dots.some(([r, c]) => r === row && c === col);
43+
return (
44+
<div
45+
key={i}
46+
className={`rounded-full transition-all ${
47+
hasDot ? "bg-[var(--tycoon-accent)] scale-100" : "scale-0"
48+
}`}
49+
/>
50+
);
51+
})}
52+
</div>
53+
</div>
54+
<style jsx>{`
55+
@keyframes roll {
56+
0%, 100% { transform: rotate(0deg) scale(1); }
57+
25% { transform: rotate(90deg) scale(1.1); }
58+
50% { transform: rotate(180deg) scale(1); }
59+
75% { transform: rotate(270deg) scale(1.1); }
60+
}
61+
62+
.animate-roll {
63+
animation: roll 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
64+
}
65+
66+
@media (prefers-reduced-motion: reduce) {
67+
.animate-roll {
68+
animation: none;
69+
opacity: 0.7;
70+
}
71+
}
72+
`}</style>
73+
</div>
74+
);
75+
}

0 commit comments

Comments
 (0)