Fix Chessground mouse hitbox desync after responsive layout shift#304
Open
Tortue95 wants to merge 1 commit into
Open
Fix Chessground mouse hitbox desync after responsive layout shift#304Tortue95 wants to merge 1 commit into
Tortue95 wants to merge 1 commit into
Conversation
|
@Tortue95 is attempting to deploy a commit to the Maia Platform Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix Chessground mouse hitbox desync after responsive layout shift
Closes #303
Problem
Chessground caches board bounds lazily via a memoized
getBoundingClientRect()call. This cache is used for all mouse-to-square coordinate conversions. It is invalidated bywindow.resizeand re-populated on the next call tobounds()— which happens during piece rendering and on user interactions.After each move, React re-renders several components (fen [most impacted], stats panel, move list). If those updates change the width of a sibling panel, the flex row reflows and the board shifts position.
The same mechanism causes a mismatch at initial page load, before the responsive layout has fully settled.
Fix
Chessground registers a permanent
window.resize → bounds.clear()listener at initialization.Dispatching
window.resizeinvalidate this cache.A single
useEffectkeyed oncurrentNodedispatcheswindow.resize50ms after every position change (mount, player move, opponent response). By the time the user can click, the cache has been cleared and Chessground re-measures from the correct DOM position.Changes
src/components/Board/GameBoard.tsxAdded one
useEffect:The 50ms delay also acts as a debounce: if
currentNodechanges multiple times in quick succession, the cleanup function cancels the pending timeout and only the last change dispatches the event.This workaround ensures the board always has correct click mapping regardless of any unexpected layout shift — even if the board moves when it should not.