Skip to content

Conversation

@e-brokenc0de
Copy link

Adds optional onSuccess and onError callbacks to the copy function while maintaining full backward compatibility with the promise-based API.

New API

const [copiedText, copy] = useCopyToClipboard();

// Using callbacks (recommended for most use cases)
copy(text, {
  onSuccess: ({ text }) => {
    console.log('Copied!', { text });
  },
  onError: ({ error, text }) => {
    console.error('Failed to copy!', { error, text });
  }
});

// Promise-based API still works (backward compatible)
copy(text)
  .then(() => console.log('Copied!'))
  .catch(error => console.error('Failed!', error));

Changes

  • Hook Implementation: Added CopyOptions type with optional onSuccess and onError callbacks
  • Structured Callback Data:
    • onSuccess receives { text: string }
    • onError receives { error: Error, text: string }
  • Demo Updated: Shows both callback and promise patterns side by side
  • Comprehensive Tests: Added 8 new tests covering all callback scenarios
  • Real-world Usage: Refactored command-copy.tsx to use callbacks for better reliability

Benefits

  • Backward Compatible: All existing code continues to work without changes
  • Better Ergonomics: Cleaner syntax for common use cases
  • Improved Reliability: Status updates only on actual success
  • Better Error Context: Both error and text available in error callback
  • Type Safe: Full TypeScript support with proper types

This enhancement provides a more ergonomic API for common use cases while preserving all existing functionality.

Adds optional onSuccess and onError callbacks to the copy function
while maintaining full backward compatibility with the promise-based API.

Changes:
- Added CopyOptions type with optional onSuccess/onError callbacks
- Callbacks receive structured data: {text} for success, {error, text} for failure
- Updated demo to show both callback and promise patterns
- Added 8 comprehensive tests for callback functionality
- Refactored www component to use callback pattern for better reliability

This enhancement provides a more ergonomic API for common use cases
while preserving all existing functionality.
@changeset-bot
Copy link

changeset-bot bot commented Nov 7, 2025

⚠️ No Changeset found

Latest commit: 996d959

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

1 participant