From 8b63cec71c00d65a14c32f8935624e9a8ed1c2e6 Mon Sep 17 00:00:00 2001 From: tsmami Date: Wed, 1 Jan 2025 22:29:33 +0900 Subject: [PATCH] Add color="random" option to BalloonProps --- apps/trpfrog.net/package.json | 2 ++ .../app/balloon/_components/Balloon/index.tsx | 17 +++++++++++---- .../src/app/balloon/useBalloonState.ts | 19 ++++++++--------- pnpm-lock.yaml | 21 +++++++++++++++++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/apps/trpfrog.net/package.json b/apps/trpfrog.net/package.json index 25bb5b32..85a79ff1 100644 --- a/apps/trpfrog.net/package.json +++ b/apps/trpfrog.net/package.json @@ -40,6 +40,7 @@ "@trpfrog.net/posts": "workspace:*", "@trpfrog.net/utils": "workspace:*", "@twemoji/api": "^15.1.0", + "@types/seedrandom": "^3.0.8", "@vanilla-extract/css": "^1.16.1", "@vercel/edge-config": "^1.4.0", "@vercel/functions": "^1.5.2", @@ -90,6 +91,7 @@ "remark-math": "^6.0.0", "remark-toc": "^9.0.0", "remark-unwrap-images": "^4.0.1", + "seedrandom": "^3.0.5", "server-only": "^0.0.1", "swr": "^2.2.5", "tailwind-merge": "^2.5.5", diff --git a/apps/trpfrog.net/src/app/balloon/_components/Balloon/index.tsx b/apps/trpfrog.net/src/app/balloon/_components/Balloon/index.tsx index 7d6cd678..ff65f2fd 100644 --- a/apps/trpfrog.net/src/app/balloon/_components/Balloon/index.tsx +++ b/apps/trpfrog.net/src/app/balloon/_components/Balloon/index.tsx @@ -3,6 +3,7 @@ import { useId, useState } from 'react' import { useReward } from 'react-rewards' +import seedrandom from 'seedrandom' import { match } from 'ts-pattern' import * as styles from './index.css.ts' @@ -16,7 +17,7 @@ type BalloonProps = { width: string height: string isBurst: boolean - color: (typeof balloonColors)[number] + color: (typeof balloonColors)[number] | 'random' onBurst?: () => void } @@ -57,6 +58,13 @@ const colors: Record<(typeof balloonColors)[number], string[]> = { export const Balloon = (props: BalloonProps) => { const balloonId = useId() const { playSound } = useBalloonSound() + const [seed] = useState(balloonId + Date.now().toString()) + + const color = + props.color === 'random' + ? balloonColors[Math.floor(seedrandom(seed)() * balloonColors.length)] + : props.color + const { reward } = useReward(balloonId, 'confetti', { zIndex: 100, startVelocity: 8, @@ -65,10 +73,10 @@ export const Balloon = (props: BalloonProps) => { elementSize: 6, spread: 50, position: 'absolute', - colors: colors[props.color], + colors: colors[color], }) - const ariaLabel = match(props) + const ariaLabel = match({ isBurst: props.isBurst, color }) .with({ isBurst: true }, () => '割れた風船') .with({ color: 'blue' }, () => '青い風船') .with({ color: 'green' }, () => '緑の風船') @@ -77,6 +85,7 @@ export const Balloon = (props: BalloonProps) => { return (