Skip to content

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

Description

@sravanth-space

Describe the bug

With isVirtualized, every re-render of EuiCodeBlock replaces the inner <code> element and all rows inside it with new DOM nodes. When the content updates often (for example streaming logs) this shows as a brief, intermittent flicker, and a text selection inside the block is lost within a second.

Cause: EuiCodeBlockVirtualized creates VirtualizedOuterElement and VirtualizedInnerElement inside useMemo. The inner element depends on [codeProps, label], and both change identity on every render of EuiCodeBlock:

  • codeProps is a useMemo that lists rest as a dependency, and rest is a new object on every render.
  • codeBlockLabelElement is a new JSX element on every render.

react-window therefore receives a new innerElementType each time and React unmounts and remounts the whole inner tree. The outer <pre> is not affected.

Impact and severity

Visual only: a short flicker when content of a virtualized code block updates frequently, plus lost text selection. No data loss. In our case it is the live log panel, where it is uncomfortable to look at. Workaround: do not use isVirtualized, which is not practical for large content.

Environment and versions

Minimum reproducible sandbox

https://codesandbox.io/p/sandbox/p3q9h5
To Reproduce

  1. Open the sandbox.
  2. Wait about 10 seconds while a line is appended every second.
  3. The text above the code block reports the <code> element replaced about once per second (expected: 0).

Expected behavior

The <code> element is reused across re-renders and only changed rows are updated.

Screenshots (Optional)

Image

Additional context (Optional)

Proposed fix: make both element types stable module-level components and pass preProps, codeProps and label to them through context.

Activity

  1. added
    ⚠️ needs validation
    on Oct 27, 2026
  2. added
    ⚠️ needs validation
    and removed
    ⚠️ needs validation
    on Nov 18, 2026
  3. sravanth-space commented on Nov 18, 2026

    @sravanth-space
    ContributorAuthor

    Hi @weronikaolejniczak, thanks for pointing me to the contributing guidelines. I missed them and opened #10252 before agreeing on a direction here. Let's discuss the approach here, as you suggested.

    EuiCodeBlockVirtualized creates innerElementType in useMemo with codeProps and label as dependencies. Both change identity on every render (codeProps depends on the rest props object, and label is a new JSX element), so react-window gets a new element type each time and React remounts the inner <code> tree. With frequent content updates this shows as a flicker and drops any text selection. In the repro, the <code> element was replaced 13 times in about 12 seconds.

    Proposed fix: make both element types stable module-level components and pass preProps, codeProps and label to them through context. Props would still update the rendered elements, but nothing would remount. EuiCodeBlock keeps the same props, exports and types. Only the internals of EuiCodeBlockVirtualized change, so consumers don't have to change anything.

    Would you be open to this approach? If so, I'd be happy to contribute the fix once the issue is labelled "help wanted". If you'd prefer a different approach, or to handle it in-house, that works for me too.

  4. eokoneyo commented on Nov 24, 2026

    @eokoneyo
    Contributor

    Hi @sravanth-space, thanks for the detailed writeup — the diagnosis is correct, and stabilising the element types via context is indeed the right fix for react-window v1. Worth noting it's also the pattern EUI already uses for the same problem in data_grid_body_virtualized.tsx, which cites the same upstream issue. Worth mirroring that file's memo(forwardRef(...)) + displayName while you're in here.

    One thing I noticed that I'd implore you to address whilst fixing this, because I see your original proposed PR rewrites the exact lines it lives on:

    Both new components spread the EUI props last:

    <code style={logicalStyles(style)} {...props} ref={ref} {...codeProps} />

    codeProps is { className, css, 'data-code-language', ...rest }, typed as EuiCodeSharedProps which is infact CommonProps & HTMLAttributes<HTMLElement>, so style is a public, typed prop on EuiCodeBlock that ends up in rest and overwrites react-window's inner style. That style is the only thing establishing the list's scroll range — react-window sets height: estimatedTotalSize on the inner element, and the rows themselves are position: absolute.

    Rendering 200 rows at overflowHeight={100} on main:

    without a consumer style: <code style="block-size: 3600px; inline-size: 100%;">
    with a style={{ padding: 0 }}: <code style="padding: 0px;">

    The computed 3600px is gone, so the <pre>'s scrollable extent collapses to roughly the last rendered row and rows 9–200 become unreachable — the list stops virtualising in any useful sense. It only affects the virtualized path; non-virtualized just puts the style on <code> harmlessly.

    This is pre-existing on main rather than something you introduced, but since you're rewriting these lines it'd be good to fix here. Spreading EUI's props first resolves it:

    <code {...codeProps} style={logicalStyles(style)} {...props} ref={ref} />

    The outer <pre> has the same ordering issue. It's only latent today (preProps is a closed set with no style), but preProps.className does silently drop any className react-window passes through, so it's worth reordering both for consistency.

  5. added
    help wanted
    and removed
    ⚠️ needs validation
    on Nov 24, 2026
  6. github-actions commented on Nov 24, 2026

    @github-actions

    👋 Thank you for your suggestion or request! While the EUI team agrees that it's valid, it's unlikely that we will prioritize this issue on our roadmap. We'll leave the issue open if you or anyone else in the community wants to implement it by contributing to EUI. If not, this issue will auto close in one year.

  7. sravanth-space commented on Nov 24, 2026

    @sravanth-space
    ContributorAuthor

    Thanks @eokoneyo, that's helpful. I'll mirror the memo(forwardRef(...)) + displayName pattern from data_grid_body_virtualized.tsx and reorder the spreads on both elements so react-window's style wins. I'll add a test for the consumer style case and open a PR shortly.

  8. sravanth-space commented on Nov 24, 2026

    @sravanth-space
    ContributorAuthor

    @eokoneyo PR is up (#10390) with your inner-element ordering: a consumer style is no longer applied to the virtualized <code>, so padding: 0 or pointerEvents are ignored there. Is that acceptable as-is, or would you prefer merging non-conflicting styles (and/or a breaking change label)?

  9. eokoneyo commented on Nov 24, 2026

    @eokoneyo
    Contributor

    @eokoneyo PR is up (#10390) with your inner-element ordering: a consumer style is no longer applied to the virtualized <code>, so padding: 0 or pointerEvents are ignored there. Is that acceptable as-is, or would you prefer merging non-conflicting styles (and/or a breaking change label)?

    Hello @sravanth-space thanks for the contribution, I'll take a look at your PR; that been said we should aim to merge non-conflicting styles.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions