Skip to content

fix: code copy button now copies full code block#550

Open
olga-venger wants to merge 1 commit intopmndrs:mainfrom
olga-venger:fix/code-copy-button
Open

fix: code copy button now copies full code block#550
olga-venger wants to merge 1 commit intopmndrs:mainfrom
olga-venger:fix/code-copy-button

Conversation

@olga-venger
Copy link

Closes #546

Problem

The copy button in code blocks only copies partial content. extractTextFromChildren recursively walks the React element tree using isValidElement, but next-mdx-remote/rsc renders code blocks as React Server Components, where lines are streamed to the client as lazy chunks. isValidElement returns false for these, so the function silently drops everything from that point in the tree.

Fix

Replace extractTextFromChildren with ref.current.textContent on the <pre> element. By the time the copy button is clicked, the browser has already rendered the full RSC output into real DOM text nodes and textContent reads from there directly, regardless of how React internally represented the element tree.
This also removes the dependency on isValidElement, which is a legacy React API not designed for RSC.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy button only copies partial content from code blocks in docs

1 participant