Fix hand re-animating on every turn #14

Merged
cmoriarty merged 1 commit from hand-deal-anim-fix into main 2026-11-07 11:54:33 -05:00
Owner

Problem

The cards in the hand animated away and back (flew in from above with a stagger) every time it became my turn to play. Confusing and unnecessary — the cards should only animate when they are dealt.

Root cause

The deal-in animation is applied via the hand-card-anim class, which was gated on myTurn:

myTurn ? 'hand-card-anim' : '',

So every time myTurn flipped false→true, React re-added the class to the already-mounted card elements, re-running the deal-in keyframe animation on the whole hand. It re-triggered on every one of my turns within a round.

Fix

Gate the animation on a dealt window instead of myTurn:

  • A dealt state is set true for a brief period (DEAL_ANIM_MS = 1200ms — the longest stagger of 10 cards × 60ms plus the 400ms flight, with slack) after each fresh deal, i.e. when dealKey bumps, then removed.
  • Applied via useLayoutEffect so the class is present before the first paint of the new cards (no flash of un-animated cards).
  • The cards remount on each deal (their key includes dealKey), so the animation runs exactly once per deal.

Why the class is removed after the window (rather than left applied): the per-card stagger delay is a CSS variable (--deal-i) that changes whenever the hand is sorted or dragged. I verified in the browser that changing the delay of an applied deal-in animation re-runs it (the opacity dips back toward the from state and recovers). So a lingering class would re-trigger the deal animation on Sort. With the class removed after the window, sorting only slides the cards to their new positions via the existing dnd-kit transition — the natural "sort" animation.

Verified (real browser, Playwright)

Scenario deal-in animations running
Fresh deal (round 1) 10 (all cards) ✓
New round deal (round 2→3) 8 (all cards) ✓
My turn starts (the bug) 0 ✓
Sort clicked 0 (cards slide via dnd-kit) ✓

Tests

  • Added a regression test: on a freshly dealt hand the cards carry the hand-card-anim class even when it is not the player's turn. Confirmed it fails under the old myTurn gating (0 cards instead of 2) and passes with the fix.
  • 161/161 tests, typecheck clean.

Note: the bot pacing test in session.test.ts is timing-sensitive and flakes occasionally under load; unrelated to this change (no game/session code touched).

## Problem The cards in the hand animated away and back (flew in from above with a stagger) every time it became my turn to play. Confusing and unnecessary — the cards should only animate when they are dealt. ## Root cause The `deal-in` animation is applied via the `hand-card-anim` class, which was gated on `myTurn`: ```js myTurn ? 'hand-card-anim' : '', ``` So every time `myTurn` flipped false→true, React re-added the class to the already-mounted card elements, re-running the `deal-in` keyframe animation on the whole hand. It re-triggered on every one of my turns within a round. ## Fix Gate the animation on a `dealt` window instead of `myTurn`: - A `dealt` state is set true for a brief period (`DEAL_ANIM_MS = 1200ms` — the longest stagger of 10 cards × 60ms plus the 400ms flight, with slack) after each fresh deal, i.e. when `dealKey` bumps, then removed. - Applied via `useLayoutEffect` so the class is present before the first paint of the new cards (no flash of un-animated cards). - The cards remount on each deal (their `key` includes `dealKey`), so the animation runs exactly once per deal. **Why the class is removed after the window (rather than left applied):** the per-card stagger delay is a CSS variable (`--deal-i`) that changes whenever the hand is sorted or dragged. I verified in the browser that changing the delay of an applied `deal-in` animation re-runs it (the opacity dips back toward the `from` state and recovers). So a lingering class would re-trigger the deal animation on Sort. With the class removed after the window, sorting only slides the cards to their new positions via the existing dnd-kit transition — the natural "sort" animation. ## Verified (real browser, Playwright) | Scenario | `deal-in` animations running | |---|---| | Fresh deal (round 1) | 10 (all cards) ✓ | | New round deal (round 2→3) | 8 (all cards) ✓ | | **My turn starts** (the bug) | **0** ✓ | | Sort clicked | 0 (cards slide via dnd-kit) ✓ | ## Tests - Added a regression test: on a freshly dealt hand the cards carry the `hand-card-anim` class even when it is **not** the player's turn. Confirmed it **fails** under the old `myTurn` gating (0 cards instead of 2) and passes with the fix. - 161/161 tests, typecheck clean. Note: the `bot pacing` test in `session.test.ts` is timing-sensitive and flakes occasionally under load; unrelated to this change (no game/session code touched).
The deal-in animation class (hand-card-anim) was gated on myTurn, so the
whole hand re-ran its deal animation — flying in from above with a
stagger — every time the turn came back around to the player. Confusing
and unnecessary: the cards should only animate when they are dealt.

Gate the animation on a 'dealt' window instead: it is applied for a
brief period after each fresh deal (dealKey bumps) and removed again
once the longest possible stagger has played out. This is what makes it
run exactly once per deal.

The class is removed after the window (rather than left applied) because
the per-card stagger delay is a CSS variable that changes when the hand
is sorted or dragged; changing the delay of an applied animation
re-runs it, so a lingering class would re-trigger the deal animation on
sort. With the class removed, sorting only slides the cards to their new
positions via the dnd-kit transition.

Add a regression test: on a freshly dealt hand the cards carry the
deal-animation class even when it is not the player's turn (it failed
under the old myTurn gating).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
cmoriarty/scratch-vllm-fp8!14
No description provided.