Skip to content

Conversation

@mcull
Copy link
Owner

@mcull mcull commented Sep 30, 2025

Summary

Fixes #311 - "Copy share link didn't work on mobile"

This PR fixes the "Copy Join Link" button in the Manage Members modal, which was failing on iOS Chrome and other mobile browsers.

Problem

The "Copy Join Link" button was displaying "Copy failed, but here's the link" on mobile devices, particularly iOS Chrome. Users had to manually select and copy the link text, which was a poor experience.

Root Cause

  • navigator.clipboard.writeText() requires a secure context and synchronous user gesture
  • On iOS, this breaks when called after async operations (like the API fetch to generate the invite link)
  • The fallback execCommand('copy') method was also unreliable on mobile browsers
  • iOS Safari/Chrome are particularly restrictive about clipboard access outside direct user interactions

Solution

1. Use Native Share on Mobile Devices

On mobile (iOS/Android), the "Copy Join Link" button now uses the native share API (navigator.share()) instead of clipboard copy:

  • Opens the familiar native share sheet on mobile devices
  • Better UX - users can directly share via Messages, Mail, WhatsApp, etc.
  • More reliable than clipboard API on mobile
  • Automatically falls back to clipboard copy on desktop

2. Improved Clipboard Fallback

For desktop browsers without modern clipboard API:

  • Changed textarea positioning from absolute with -9999px to fixed with opacity: 0
  • Added explicit focus() call before select() for better browser compatibility
  • Use actual textarea.value.length instead of hardcoded 99999

3. Better Error Handling

  • Don't show error message when user cancels the native share dialog (AbortError)
  • Still gracefully degrades by showing the link text if all methods fail

Testing

Mobile (iOS Chrome)

  • ✅ Click "Copy Join Link" → Opens iOS native share sheet
  • ✅ Can share directly to Messages, Mail, Notes, etc.
  • ✅ Canceling share dialog doesn't show error

Desktop (Chrome/Firefox/Safari)

  • ✅ Click "Copy Join Link" → Copies to clipboard
  • ✅ Shows success message
  • ✅ Can paste link into other apps

Fallback

  • ✅ If all methods fail, still shows link text for manual copy

Technical Details

Changes in src/components/ManageMembersModal.tsx:

  1. copyText function (lines 115-155): Improved fallback clipboard implementation
  2. Copy Join Link handler (lines 741-780): Added mobile detection and native share

Mobile Detection:

/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)

Share API Usage:

await navigator.share({
  title: `Join ${collectionName} on StuffLibrary`,
  text: `Here's your invite to ${collectionName}`,
  url: link,
});

Related Issues

Closes #311

🤖 Generated with Claude Code

Fixes #311 - "Copy share link didn't work on mobile"

## Problem
The "Copy Join Link" button in the Manage Members modal was failing
on iOS Chrome due to clipboard API restrictions when called after
async operations (like API fetches).

## Root Cause
- navigator.clipboard.writeText() requires secure context and
  synchronous user gesture, which breaks on iOS after awaiting fetch
- Fallback execCommand('copy') also unreliable on mobile browsers
- iOS particularly restrictive about clipboard access outside direct
  user interactions

## Solution
1. **Use native share on mobile**: Detect mobile devices (iOS/Android)
   and use navigator.share() API instead of clipboard copy
   - Opens native share sheet on mobile (better UX, more reliable)
   - Falls back to clipboard copy on desktop

2. **Improve clipboard fallback**: For desktop browsers without
   clipboard API:
   - Changed textarea positioning from absolute/-9999px to fixed/opacity:0
   - Added focus() call before select() for better compatibility
   - Use textarea.value.length instead of hardcoded 99999

3. **Better error handling**: Don't show error when user cancels
   native share dialog (AbortError)

## Testing
- Tested on iOS Chrome: Now opens native iOS share sheet
- Desktop Chrome: Still copies to clipboard as before
- Graceful degradation if both methods fail (shows link text)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Sep 30, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
stufflibrary Ready Ready Preview Comment Sep 30, 2025 1:41am

@mcull mcull merged commit af88acb into main Sep 30, 2025
4 checks passed
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.

Fix Copy Share Link Functionality on Mobile Devices

2 participants