Skip to content

Optimize expanded image preview rerenders#2876

Draft
cursor[bot] wants to merge 4 commits into
mainfrom
cursor/component-performance-optimization-7678
Draft

Optimize expanded image preview rerenders#2876
cursor[bot] wants to merge 4 commits into
mainfrom
cursor/component-performance-optimization-7678

Conversation

@cursor
Copy link
Copy Markdown
Contributor

@cursor cursor Bot commented May 30, 2026

What Changed

  • Removed the ExpandedImageDialog prop-mirroring Effect that copied preview into local state.
  • Split the dialog into a keyed outer reset boundary and an inner component that owns only local image navigation offset state.
  • Moved the necessary keydown browser subscription into a named useExpandedImageKeyboardShortcuts hook using useEffectEvent.

Why

React Doctor flagged the old implementation as mirrored prop state via Effect. That caused an extra stale render whenever the parent selected a different preview. Keying the inner dialog resets navigation state from the selected preview without an Effect, while keeping arrow-key navigation local to the dialog.

UI Changes

React Scan recordings are included in this PR:

  • Before: docs/perf/expanded-image-dialog-react-scan-before.webm
  • After: docs/perf/expanded-image-dialog-react-scan-after.webm

The same interaction, two parent preview switches, went from 5 total dialog subtree commits before to 3 total commits after. Metadata is in:

  • docs/perf/expanded-image-dialog-react-scan-before.json
  • docs/perf/expanded-image-dialog-react-scan-after.json

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Verification:

  • npx -y react-doctor@latest . --project @t3tools/web --diff main --verbose
  • bun fmt
  • bun lint
  • bun typecheck
Open in Web View Automation 

Note

Reduce rerenders in ExpandedImageDialog by keying content on preview state

  • Replaces the memoized ExpandedImageDialog with a thin wrapper that renders a keyed ExpandedImageDialogContent child, forcing a remount when preview.index, image count, or image sources change via a new getPreviewResetKey helper.
  • Simplifies internal navigation state from a cloned preview object to a single indexOffset integer, deriving currentIndex as (preview.index + indexOffset) % images.length.
  • Extracts keyboard handling into a useExpandedImageKeyboardShortcuts hook using useEffectEvent for a stable listener.
  • Behavioral Change: the content component now remounts on preview changes rather than re-rendering in place, and React.memo/useCallback are removed.

Macroscope summarized 64fcb1a.

cursoragent and others added 4 commits May 30, 2026 16:11
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. labels May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant