Skip to content

fix(dynamic-images): re-measure layers once the editor canvas is shown - #1828

Merged
sung17 merged 1 commit into
mainfrom
fix/dynamic-image-layer-position
Nov 30, 2026
Merged

sung17 merged 1 commit into
mainfrom
fix/dynamic-image-layer-position

Conversation

@sung17

@sung17 sung17 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Problem

After creating a dynamic image, the app navigates to its Edit page. There, every layer was drawn away from where it was placed. After a reload (F5) the layers were back in the right place. The saved document was always correct; only the editor's drawing was off.

Cause

The Create → Edit client-side navigation mounts the editor while the new page is still inside a display: none ancestor. react-rnd measures each layer's offset from its parent once, at mount, from on-screen positions. Hidden elements report 0, so the offset becomes -x, and each layer is drawn at twice its saved x/y.

Reproduced in Chrome: a text layer saved at (224, 144) was drawn at (448, 288) after the navigation. It was still there 2 s later, and back at (224, 144) after a reload. Instrumenting the first getBoundingClientRect on the Edit page showed the hidden ancestor.

Fix

  • New useShownLayoutVersion(ref) (components/use-shown-layout-version.ts). A ResizeObserver watches the canvas, and a counter goes up whenever the canvas turns from hidden to shown.
  • The layer <Rnd>s are keyed on ${element.id}:${layoutVersion}, so they remount and measure again against the real layout. A canvas that is visible from the start never remounts. The saved data is not touched.

Verification

  • Browser run (Create → Edit): layer at (224, 144) on Create, after the navigation, and after a reload. Dragging it on Edit right after the navigation, then Save, then reload gives the same (274, 174) every time.
  • New unit test components/__tests__/use-shown-layout-version.test.tsx: hidden → shown, visible from start, hidden again → shown again, disconnect on unmount.
  • pnpm lint and builder check-types pass. dynamic-image-form-locked.test.tsx still passes.

🤖 Generated with Claude Code

After Create, the client-side navigation to the Edit page mounted the
canvas while the new page was still hidden (display: none). react-rnd
measures each layer's offset from on-screen positions once, at mount, so it
read 0 and stored an offset of -x: every layer was drawn at twice its saved
x/y until the page was reloaded. The saved document was always correct.

useShownLayoutVersion watches the canvas with a ResizeObserver and goes up
when it turns from hidden to shown; the layers are keyed on it, so they
remount and measure against the real layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working as expected label Nov 30, 2026
@sung17
sung17 merged commit 853c21d into main Nov 30, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant