Skip to content

[docs-infra] Fix code-block copy button broken on direct page load#48653

Merged
brijeshb42 merged 2 commits into
mui:masterfrom
brijeshb42:fix-hydration
Jun 15, 2026
Merged

[docs-infra] Fix code-block copy button broken on direct page load#48653
brijeshb42 merged 2 commits into
mui:masterfrom
brijeshb42:fix-hydration

Conversation

@brijeshb42

Copy link
Copy Markdown
Contributor

Closes #48629

Introduced in #48261, which re-rendered the App component due to state update of versions after fetch.

Cause

Markdown code blocks are static HTML injected via
dangerouslySetInnerHTML. InitCodeCopy wired each button imperatively with addEventListener, but React replaces those DOM nodes on a re-render after hydration — orphaning the listener. The effect only re-ran on router.pathname change, so a same-page load never re-wired the button.

Fix

  • Replace per-node wiring with a single delegated click listener on document, so copy survives React recreating the nodes (no per-node references to go stale, and router.pathname dependency dropped).
  • Resolve the /Ctrl shortcut hint via CSS off a data-mac attribute set once on <html>, instead of mutating each button's text — also immune to node replacement.
  • Hover/focus tracking for keyboard copy now uses event delegation as well.

@brijeshb42 brijeshb42 requested review from a team June 10, 2026 13:22
@brijeshb42 brijeshb42 added the scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305). label Jun 10, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Jun 10, 2026

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-48653--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@Janpot

Janpot commented Jun 10, 2026

Copy link
Copy Markdown
Member

Is this related?

@brijeshb42

Copy link
Copy Markdown
Contributor Author

Yeah. Did not see this one. Need to update the mui-triage skill to look for duplicates in PRs as well.

Closes mui#48629

### Cause
Markdown code blocks are static HTML injected via
`dangerouslySetInnerHTML`. `InitCodeCopy` wired each button imperatively
with `addEventListener`, but React replaces those DOM nodes on a
re-render after hydration — orphaning the listener. The effect only
re-ran on `router.pathname` change, so a same-page load never re-wired
the button.

### Fix
- Replace per-node wiring with a single delegated `click` listener on
`document`, so copy survives React recreating the nodes (no per-node
references to go stale, and `router.pathname` dependency dropped).
- Resolve the `⌘`/`Ctrl` shortcut hint via CSS off a `data-mac`
attribute set once on `<html>`, instead of mutating each button's text —
also immune to node replacement.
- Hover/focus tracking for keyboard copy now uses event delegation as
well.

### Verified
Click copy, keyboard `⌘C`, and the shortcut hint all work on direct load
and after navigation; the React tabbed code blocks are unaffected
(delegated handler ignores them).
}, 2000);
try {
if (pre?.textContent) {
await clipboardCopy(pre.textContent);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't the "copied" indicator logic go after this call? It shouldn't show "copied" when copy fails, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right. Fixed the existing bug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But now there's no feedback on failure ofcourse. Not sure when it would fail though.

@brijeshb42 brijeshb42 Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What feedback would be apt for failure ? IMO, its better to have no feedback than to show Copied even when nothing was copied.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess we could show an error.


return undefined;
}, [rootNode, router.pathname]);
// Track the hovered/focused code block so `CodeCopyProvider` can copy it on Ctrl/⌘+C.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it common to have Ctrl+C copy the whole content of a focused element? My expectation is that it copies my selection, and if I haven't selected anything it doesn't copy anything at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My assumption would be that explicit copy button in the UI would be for the whole block where it is. If I am selecting something, I'd also copy it with keyboard/mouse directly.
Do you have any example where it happens this way ? I saw 2-3 sites where copy button always copies the whole text regardless of selection (radix/shadcn etc).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes copy button behavior makes sense, but mui.com also copies the whole content if my cursor is in there with nothing selected and I press cmd+c. it even seems to do it when I simply hover the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On which page/link exactly ? Do you have a recording ? Seems a bug to me, especially copy just by hovering

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I mean hover+ctrl+c. Actually the behavior looks like:

  • when I hover regular text, not in a codeblock and de cmd+c => nothing changes in my clipboard
  • when I hover a code block and do the same => clipboard gets cleared

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wait no, that's on the demos. Sry. On the regular codeblocks, when I hover+cm+c it copies the content.

Screen.Recording.2026-06-12.at.12.51.31.mov

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Its in this PR's docs as well, behaviour hasnt been updated in any way. Though I think its going way out of the way to copy stuff.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, just flagging as we could clean this up now that we're here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the copy on hover+cmd+c functionality

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Update: I'll probably do this as a followup PR based on what the conclusion is.

Also fix the blur so the hover item disappears
@brijeshb42 brijeshb42 merged commit f8c1816 into mui:master Jun 15, 2026
17 of 18 checks passed
@brijeshb42 brijeshb42 deleted the fix-hydration branch June 15, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[docs] Copy button in Roboto font section fails after direct page load

2 participants