Skip to content

Conversation

@abhichaurasiya2022
Copy link

Description

This PR updates the useCopyToClipboard hook to support copying multiple clipboard formats (plain text, HTML, and Markdown) using the modern ClipboardItem API.

Fixes

Fixes #329

Changes

Core Implementation

  • Enhanced useCopyToClipboard to accept either a string or an object with multiple format properties (plain, html, markdown)
  • Implemented ClipboardItem API support for copying multiple formats simultaneously
  • Maintained backward compatibility - existing usage with plain strings continues to work
  • Added proper fallback handling for browsers that don't support ClipboardItem

Type Definitions

  • Added new ClipboardFormats type to index.d.ts with optional properties for different formats
  • Updated function signature to accept string | ClipboardFormats

Documentation

  • Updated hook documentation in useCopyToClipboard.mdx with:
    • New description highlighting multi-format support
    • Updated API reference table
    • Enhanced examples demonstrating both plain text and rich content copying
    • Added error handling display in the demo

Testing

The implementation includes:

  • Full backward compatibility with existing string-based usage
  • Support for modern browsers with ClipboardItem API
  • Fallback to writeText for simple string copying
  • Legacy fallback using document.execCommand for older browsers

Benefits

  • Enables copying rich content (HTML formatting, Markdown) to clipboard
  • Applications can provide formatted content that preserves styling when pasted
  • No breaking changes - fully backward compatible
  • Better user experience with multiple paste format options

Enhance copyToClipboard function to support multiple formats including plain text, HTML, and Markdown using ClipboardItem API.
Updated the useCopyToClipboard hook to support multiple clipboard formats and improved error handling. Adjusted the return value to include both the last copied text and any errors that occurred.
@abhichaurasiya2022
Copy link
Author

moved to PR #358

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.

Update useCopyToClipboard to handle multiple formats

1 participant