Skip to content

CopyButton component not copying text to the clipboard if rendered inside a modal dialog element #11109

Open
@nickonometry

Description

@nickonometry

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

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:
Image

Reproducible demo

https://codesandbox.io/p/devbox/frosty-hodgkin-7p6wwf

Steps to reproduce

  1. Test the copy to clipboard functionality on the codeblock that is on the homepage of the reproducible demo, confirm it works fine.
  2. Click the open dialog button and click the copy to clipboard button from within the dialog. It doesn't copy anything

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

  • Public source code:
  • Public site URL:
  • Docusaurus version used:
  • Environment name and version (e.g. Chrome 89, Node.js 16.4):
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS):

Self-service

  • I'd be willing to fix this bug myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn error in the Docusaurus core causing instability or issues with its executionstatus: needs triageThis issue has not been triaged by maintainers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions