react-json-view icon indicating copy to clipboard operation
react-json-view copied to clipboard

A fix for CopyToClipboard functionality that does not work

Open balex13 opened this issue 1 year ago • 1 comments

Fix CopyToClipboard Functionality

Issue:

The CopyToClipboard functionality does not work in Chrome Version 126.0.6478.127 (Official Build) (arm64), with no error messages provided.

Root Cause:

The execCommand function, which was previously used for copying text to the clipboard, is deprecated and no longer recommended as per MDN documentation. This deprecation affects its reliability and compatibility across browsers.

Solution:

To address this issue, the fix implements a conditional approach:

  • Primary Method: Use the new Clipboard API if it is available. This API is more modern and secure, though not fully supported in all environments.
  • Fallback Method: If the Clipboard API is not supported, the function falls back to using execCommand to maintain compatibility with older browsers.

Changes:

  • Added a check for Clipboard API availability.
  • Implemented the Clipboard API for copying text when supported.
  • Retained execCommand as a fallback to ensure broad compatibility.

This approach ensures that the CopyToClipboard functionality works reliably across a range of browsers and versions, improving user experience and adhering to current web standards.

balex13 avatar Jul 13 '24 00:07 balex13

The execCommand("copy") works again in Chrome Version 127.0.6533.100 (Official Build) (64-bit).

Nevertheless, it's deprecated and not working on Firefox already So it would be great to get this merged 🙏

mbtools avatar Aug 09 '24 17:08 mbtools

can be closed in favor of #37

mbtools avatar Sep 11 '24 20:09 mbtools