Skip to content

fix: find the animating square by id so custom board ids cannot crash - #241

Open
ryan-opatka wants to merge 1 commit into
Clariity:mainfrom
ryan-opatka:fix/piece-animation-safe-id
Open

ryan-opatka wants to merge 1 commit into
Clariity:mainfrom
ryan-opatka:fix/piece-animation-safe-id

Conversation

@ryan-opatka

Copy link
Copy Markdown

Description

Piece looks up the source square for its move animation with

document.querySelector(`#${id}-square-${sourceSquare}`)

The board id goes into a CSS selector unescaped, so any id that isn't a valid CSS identifier breaks the first animated move:

  • 1-board, board:1, or React's useId() output (e.g. :r0:): querySelector throws SyntaxError: ... is not a valid selector. The error is thrown inside an effect, so React unmounts the tree.
  • a.b: the selector parses as id a + class b-square-e2, matches nothing, and the animation is silently skipped.

This PR uses document.getElementById(${id}-square-${sourceSquare}), the same lookup utils.ts already uses for the same element (the id Square sets on line 98). For ids that are already valid selectors, behavior is unchanged.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

A small Vite page using the published react-chessboard@5.12.4 (React 19.2), run in headless Chrome. For each id it renders the start position, then e4, and records uncaught errors and whether a translate(...) animation style appears.

id 5.12.4 as published with this change
chessboard animates animates
1-board throws, tree unmounted animates
board:1 throws, tree unmounted animates
:r0: throws, tree unmounted animates
a.b no animation animates

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings

🤖 Generated with Claude Code

The piece animation looked up its source square with
document.querySelector(`#${id}-square-...`). Ids that are not valid CSS
selectors (e.g. "1-board", "board:1", or React's useId() output like
":r0:") made querySelector throw a SyntaxError on the first animated move,
unmounting the app. Ids containing "." silently skipped the animation.

Use document.getElementById, as utils.ts already does for the same
`${id}-square-${squareId}` element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 23, 2026

Copy link
Copy Markdown

@ryan-opatka is attempting to deploy a commit to the Ryan Gregory's projects Team on Vercel.

A member of the Team first needs to authorize it.

@ryan-opatka

Copy link
Copy Markdown
Author

Ran the project's checks locally on this branch:

  • pnpm test:static (tsc, eslint, prettier --check): pass
  • pnpm exec commitlint on the commit: pass
  • pnpm build: pass
  • pnpm build-storybook, then clicked through the Show Animations story in Chrome: pieces animate, no page errors

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant