Skip to content

feat(ui): consistent copy-to-clipboard feedback across the app - #3648

Open
officialasishkumar wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
officialasishkumar:feat/copy-feedback-hook
Open

officialasishkumar wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
officialasishkumar:feat/copy-feedback-hook

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Summary

Adds the reusable copy-to-clipboard behaviour from #1937 and moves every copy action in the app onto it. Each copy button now shows the same feedback: a checkmark that pops in and a "Copied!" label for 2 seconds. Failures that were silent now show an error. Before this, nine places had their own clipboard code with different labels ("Copied!", "Copied", "✓ Copied!", icon-only), and some only logged to the console on failure or did nothing at all.

Closes #1937


Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that changes existing behavior)
  • 📝 Documentation update
  • ♻️ Refactor / code cleanup (no functional change)
  • ⚡ Performance improvement
  • 🔒 Security fix
  • 🧪 Tests only

What Changed

  • src/hooks/useCopyToClipboard.ts (new)
    • copyToClipboard(text) uses the Clipboard API where it exists. It falls back to a hidden textarea and execCommand("copy") where the API is missing (plain-HTTP/LAN previews, older browsers), then gives focus back to the button.
    • useCopyToClipboard() returns copy(text, key?), which resolves to true/false so each caller keeps its own error message, and isCopied(key?).
    • The key lets a list mark only the clicked item (goals, resume sections). Copying again restarts the 2 s timer instead of being cut short by the old one, and the timer is cleared on unmount.
  • src/components/ui/copy-feedback.tsx (new): CopyFeedback renders the button contents. A copy icon turns into a checkmark with a short pop animation (motion-safe, so it's off for reduced motion), with an optional label in --success. Props cover icon-only buttons, icon sizes, and buttons on an accent fill that keep their own text colour.
  • tailwind.config.ts: the copy-pop keyframe and animation.
  • Call sites moved onto the pair:
    • CopyLinkButton, used for the profile link and the duel link.
    • GoalTracker: goal share link.
    • Settings: public profile URL.
    • useStreakTracker / StreakTracker: streak stats.
    • WeeklySummaryCard: AI summary. This one also fixes an unhandled promise rejection when the copy failed.
    • BadgeSection: badge markdown.
    • WebhookManager: new webhook secret.
    • ExportPanel / ResumePreview: resume text and sections.
    • Existing success toasts are unchanged. Failures that were silent or console-only now show an error toast.
  • Tests
    • test/useCopyToClipboard.test.tsx covers the Clipboard API path, the fallback (selected value, cleanup, focus restore), the rejected and unsupported cases, the copied window and its reset, per-key state, restarting the timer, a custom delay, and cleanup on unmount and after an unmount mid-copy.
    • test/components/CopyFeedback.test.tsx covers the icon/label states and the props.
  • docs/architecture.md: a short paragraph pointing new copy actions at the hook and component.

How to Test

  1. pnpm vitest run test/useCopyToClipboard.test.tsx test/components/CopyFeedback.test.tsx test/StreakTracker.test.ts
  2. Click the copy buttons on a public profile (share section and badge markdown), a duel page, the dashboard streak card, a public goal, Settings → profile URL, and a new webhook secret.
  3. Click one twice in quick succession, and on the resume page copy one section and then another.
  4. Open the app over a LAN IP on plain HTTP, where navigator.clipboard is undefined, and copy again.

Expected result: Each button shows the checkmark and "Copied!" (icon-only buttons show just the checkmark) for 2 seconds, measured from the latest click. Only the clicked goal or section is marked. Copying still works over plain HTTP, and when copying fails an error toast appears instead of nothing.


Screenshots / Recordings

Every changed button now looks like this: a copy icon next to the existing label at rest, and a checkmark with "Copied!" in the theme's success colour for two seconds after a click.


Checklist

  • Linked the related issue above
  • Self-reviewed my own diff
  • No unnecessary console.log, debug code, or commented-out blocks
  • npm run lint passes locally
  • No TypeScript errors (npm run type-check)
  • Added or updated tests where applicable
  • Updated documentation / comments if behavior changed

Accessibility (UI changes only)

  • Keyboard navigation works correctly (no new interactive elements; the fallback path returns focus to the button)
  • Color contrast meets WCAG AA standard (uses the existing --success theme colour, as the summary card already did)
  • ARIA labels / roles added where needed (icons are aria-hidden, existing labels are kept, and failures are announced through the toast)
  • Tested on mobile / responsive layout

Additional Context

Nine copy actions each had their own clipboard code and their own idea of
feedback: some swapped the label to "Copied!", some to "Copied", one to
"✓ Copied!", some showed a checkmark and some only text. Failures were
handled differently too. The badge, resume and webhook-secret buttons
only logged to the console or stayed silent, and the weekly AI summary
left a rejected promise unhandled. None of the reset timers were cleared
on unmount, and clicking twice reset the label early because the first
timer still fired.

Add one shared implementation and move every copy action onto it:

- useCopyToClipboard (src/hooks) writes through the Clipboard API and
  falls back to a hidden textarea where the API is missing (non-secure
  origins, older browsers), giving focus back to the button afterwards.
  copy() resolves to true or false so each caller shows its own error.
  It keeps a single "copied" key with an optional per-item key for lists
  (goals, resume sections), restarts the 2 s timer on a repeat copy, and
  clears it on unmount.
- CopyFeedback (src/components/ui) renders the button contents: a copy
  icon that pops into a checkmark (motion-safe, so reduced motion turns
  it off) with an optional "Copied!" label in the success colour.
  Icon-only and accent-filled buttons have props for their layout.
- Profile link, duel link, goal share link, settings profile URL, streak
  stats, weekly AI summary, badge markdown, webhook secret, resume text
  and resume sections all use the pair. Failures that were silent now
  show an error toast, and existing success toasts are unchanged.

Tests cover the clipboard fallback and its failure modes, the copied
window and its reset, per-key state, restarting the timer, cleanup on
unmount, and the CopyFeedback states. docs/architecture.md points new
copy actions at the hook and component.

Closes Priyanshu-byte-coder#1937
@github-actions github-actions Bot added type:design GSSoC type bonus: UI/design (+10 pts) type:docs GSSoC type bonus: documentation (+5 pts) type:feature GSSoC type bonus: new feature type:testing GSSoC type bonus: tests (+10 pts) labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:design GSSoC type bonus: UI/design (+10 pts) type:docs GSSoC type bonus: documentation (+5 pts) type:feature GSSoC type bonus: new feature type:testing GSSoC type bonus: tests (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add "Copy Success" Visual Feedback Animation to All Copy Actions

1 participant