CopyButton component not copying text to the clipboard if rendered inside a modal dialog element #11109
Open
3 of 7 tasks
Labels
bug
An error in the Docusaurus core causing instability or issues with its execution
status: needs triage
This issue has not been triaged by maintainers
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clear
oryarn clear
command.rm -rf node_modules yarn.lock package-lock.json
and re-installing packages.Description
I am rendering CodeBlocks within an HTML dialog. The problem is that the copy button is not actually copying the code snippets to the clipboard. The code strings are being passed down into the copy button properly, but the issue occurs when a <textarea> is rendered dynamically via the copy-text-to-clipboard module. Since the page behind the dialog is inert, and the textarea is appended to the body, it can't copy the text successfully.
Screenshot of my app:

Reproducible demo
https://codesandbox.io/p/devbox/frosty-hodgkin-7p6wwf
Steps to reproduce
Expected behavior
I would expect it to copy just fine from within a dialog. I ended up swizzling the code block component and modified the copy function on line 13 of the CopyButton/index.js to:
navigator.clipboard.writeText(code);
That resolves it. I would love for this to be fixed as we like to use things out of the box and keep swizzling to a minimum.
Actual behavior
As described above. The issue is that the CodeBlock component does not work proeprly when rendered in a dialog that's opened with the showModal function. This occurs because the native dialog applies inert to the page, and the current copy to clipboard function in the CopyButton component appends a textarea element to the dom, since the page is inert, accessing the textarea value doesn't work properly
Your environment
Self-service
The text was updated successfully, but these errors were encountered: