Skip to content

fix(EuiCodeBlock): keep virtualized rows mounted when props change - #10252

Closed
sravanth-space wants to merge 1 commit into
elastic:mainfrom
sravanth-space:code-block-virtualized-stable-elements
Closed

sravanth-space wants to merge 1 commit into
elastic:mainfrom
sravanth-space:code-block-virtualized-stable-elements

Conversation

@sravanth-space

@sravanth-space sravanth-space commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • What: EuiCodeBlockVirtualized no longer remounts the inner <code> element and its rows when EuiCodeBlock re-renders.
    • VirtualizedOuterElement and VirtualizedInnerElement are now stable module-level components.
    • preProps, codeProps and label reach them through context.
    • Adds a unit test that the <code> element is the same node after a re-render with new content.
  • Why: Fixes [EuiCodeBlock] Virtualized code block remounts its rows on every re-render #10251. Both element types were created in useMemo with codeProps and label as dependencies, and both change identity on every render (codeProps depends on the rest props object; label is a new JSX element). react-window therefore received a new innerElementType each time and React remounted the inner tree, which shows as a flicker when content updates often and drops any text selection.
  • How: Props still update the elements through context, so changes to them re-render without remounting. The outer container is unchanged, so scrolling is not affected.

API Changes

No public API changes.

Screenshots

No visual change. The difference is in the DOM: the <code> element is no longer replaced on each content update.

Before After
<code> replaced once per update (13 times in ~12 s in the repro) not replaced (0 in 14 s across 5 updates, measured in a Chrome session on an app with this change applied to 123.1.0)

Impact Assessment

  • 🔴 Breaking changes — None expected.
  • 💅 Visual changes — None expected.
  • 🧪 Test impact — None expected: existing components/code snapshots are unchanged, no HTML structure or class names changed.
  • 🔧 Hard to integrate — No.

Not yet tested in Kibana.

Impact level: 🟢 Low

Release Readiness

  • Documentation: n/a
  • Figma: n/a
  • Migration guide: n/a
  • Adoption plan (new features): n/a, bug fix

QA instructions for reviewer

  • Open the repro, which appends a line to a virtualized EuiCodeBlock every second. With this change the "replaced" counter stays at 0 (on main it grows by about 1 per second).
  • In Storybook, check that a virtualized EuiCodeBlock still scrolls and updates correctly.
  • Confirm keyboard focus and the screen-reader label (code block:) still work on a virtualized block.

Checklist before marking Ready for Review

  • Filled out all sections above
  • QA: Tested light/dark modes, high contrast, mobile, Chrome/Safari/Edge/Firefox, keyboard-only, screen reader
  • QA: Tested in CodeSandbox and Kibana
  • QA: Tested docs changes
  • Tests: Added/updated Jest (Cypress and VRT not needed, no visual change)
  • Changelog: Added changelog entry
  • Breaking changes: Added breaking change label

Reviewer checklist

  • Approved Impact Assessment — Acceptable to merge given the consumer impact.
  • Approved Release Readiness — Docs, Figma, and migration info are sufficient to ship.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

👋 Since this is a community submitted pull request, a Buildkite build has not been started automatically. Would an Elastic organization member please verify the contents of this pull request and kick off a build manually?

@github-actions github-actions Bot added the community contribution (Don't delete - used for automation) label Oct 27, 2026
@github-actions

Copy link
Copy Markdown

We don't accept community contributions for issues not labelled "help wanted". Please read our contribution guidelines.

@sravanth-space
sravanth-space marked this pull request as ready for review November 2, 2026 23:09
Copilot AI balanced review requested due to automatic review settings November 2, 2026 23:09
@sravanth-space
sravanth-space requested a review from a team as a code owner November 2, 2026 23:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the change to stabilize virtualized code blocks across re-renders.

No open issues 🎉

What changed in this PR

Fixes #10251 by keeping virtualized EuiCodeBlock elements stable across re-renders, preventing unnecessary remounts that cause flicker and lost text selection.

Changes:

  • Moves the outer and inner virtualized components to module scope.
  • Passes element props and the screen-reader label through context without changing the public API.
  • Adds a regression test confirming that the <code> DOM node survives content updates.
File Description
packages/​eui/​src/​components/​code/​code_block.test.tsx Tests <code> node identity after a content update.
packages/​eui/​src/​components/​code/​code_block_virtualized.tsx Uses stable component types and context to update props without remounting.

Review effort: Balanced

@weronikaolejniczak

Copy link
Copy Markdown
Contributor

@sravanth-space please read our contributing guidelines. Let's first agree on a direction in the issue and only once we label it "help wanted", we can accept an external contribution. This helps us manage our workload and make sure every change is in accordance with our roadmap.

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

Labels

community contribution (Don't delete - used for automation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[EuiCodeBlock] Virtualized code block remounts its rows on every re-render

3 participants