Copy button
Copy text, generate a share link or reveal a masked value, with pending and outcome feedback on the control.
npx @swipeui/cli add copy-buttonWhen to use it
- Copying codes, links, wallet addresses or invite text with one tap.
- Code snippets and API keys in developer or settings screens.
- Referral or promo codes people paste elsewhere.
- Anywhere the result of copying should be confirmed visibly.
When not to use it
- Sharing to other apps. Use the system share sheet.
- Copying sensitive secrets without the user asking; copy only on tap.
- Long passages of text; let people select text instead.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add copy-button- Adds
components/swipeui/ui/copy-button.tsx,components/swipeui/ui/use-copy-button.tsand 14 shared helpers. - Installs expo-clipboard, react-native-reanimated, uniwind, react-native-worklets, clsx, tailwind-merge, expo-haptics, class-variance-authority, react-native-svg, react-native-gesture-handler, react-native-safe-area-context. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { CopyButton } from '@/components/swipeui/ui/copy-button';
<CopyButton value={inviteUrl} accessibilityLabel="Copy invitation link" />;The appearances preview compares a labelled button, a compact icon action and a value field. Share link creates a value before copying it. Secrets keeps a fake invitation masked until revealed. Failure lets you fail the next attempt, then retry. The app uses these same specimens.
Settings
| Prop | Default | Behavior |
|---|---|---|
value | required | A string or stable function returning a string or promise. Invoked only on press. |
appearance | button | button shows an icon and label; icon speaks its label; field shows a mono value with middle truncation. |
labels | see below | Localize action, pending, outcome, toast details and reveal labels. The control fits the current label. |
resetAfter | 1800 | Milliseconds before success/error returns to idle. |
minPending | 450 | Minimum pending visibility in milliseconds, including async work. Content timing. |
mask | false | Replace a field value with eight bullets; hidden accessible text never contains the value. |
revealable | true | Offer a labelled eye toggle when the field is masked. |
clearClipboardAfter | 0 | Opt-in delay in milliseconds. Reads once and clears only if the clipboard still contains our latest value. |
iconMorph | true | Morph copy to check; false swaps endpoints with motion disabled. |
effects | true | Draw the success check once. Reduce Motion skips effects. |
labelMorph | true | Animate changed labels without ellipsis while resizing; false updates text in place. Width follows the current label unless explicitly reserved. |
reserveLabelWidth | false | Opt into the old widest-label reservation. Default labelled buttons spring between current text widths. |
fieldDivider | true | Hairline between reveal and copy when the eye control is present. |
toastDuration | 1800 | Toast lifetime in milliseconds; zero persists. |
shakeOnError | true | One short failure shake; Reduce Motion removes travel. |
toast | false | Enqueue a localized success/error toast. Requires a surrounding ToastProvider and ToastHost. |
haptics | true | Global switch or { copy, error }, each defaulting to true. No duplicate press haptic. |
onCopy | undefined | Called once after the latest successful copy. |
onError | undefined | Called once after generation or clipboard failure. Retry remains available. |
variant | outline | Shared Button styles: solid, tinted, outline, ghost, plain; 1.x aliases remain supported. Field actions use ghost inside a neutral tinted chip. |
tone | Button default | Button tone; success/error temporarily use success/destructive feedback. |
size | md | sm, md, lg; small icon controls retain a 44pt touch target. |
shape | preset | Preset control corners or pill, with inset corners on field actions. |
disabled | false | Prevent copy and reveal; pending also prevents repeated copy presses. |
motion | inherited | Override the shared motion feel. Reduce Motion keeps short fades. |
accessibilityLabel | Copy | Describe the idle action. Pending and outcomes use localized labels. |
| Button material props | Button defaults | pressEffect, highlight, glow, disabledStyle, palette and block follow Button. |
| View/style props | undefined | className, style, textClassName, testID and supported native props pass through. Field root owns its class/style. |
labels defaults: copy: 'Copy', pending: 'Copying', success: 'Copied', error: "Couldn't copy", reveal: 'Show value', conceal: 'Hide value', valuePlaceholder: 'Generated on copy', hiddenValue: 'Hidden value', successDescription: 'Text copied to your clipboard.', errorDescription: 'Please try copying again.'.
Masked or generated values
<CopyButton appearance="field" value={invitationCode} mask clearClipboardAfter={4000} />
<CopyButton value={createShareLink} labels={{ copy: 'Copy share link', pending: 'Creating link' }} />Keep a generator stable with useCallback. Changing value invalidates the previous request; an old generator cannot write after replacement or unmount. Clipboard writes already handed to the platform cannot be cancelled. Nothing is copied on mount.
Clearing is best effort while mounted. Unmount cancels it, and changing clipboard contents prevents it. A read and write cannot be atomic, and clearing is not secure erasure. Web clipboard permissions and user activation restrictions apply; an asynchronously generated value may require another attempt on browsers that restrict activation. Leave clearing off when clipboard reads are inappropriate.
In appearances, enable Toast confirmation, then toggle Toast at top to compare both safe-area edges. A toast has a leading icon, title and description with an icon-only close button, and dismisses after 1.8 seconds or on close. Configure ToastHost placement="top" or "bottom" in your app; use haptics={false} on that host when CopyButton already owns the outcome haptic.
Feedback appears on the control, with one spoken announcement. When toast is enabled, the toast owns that announcement. Reduce Motion removes the shake and geometric icon travel, and skips draw-on effects.
API reference
Behavior and gestures
- Press to copy supplied text or invoke a value generator. No clipboard work happens on mount.
- The pending spinner holds the control busy, then copy morphs to a drawn check and the label confirms the result.
- A failure shakes once, uses a destructive tone and offers another press to retry. Each feedback feature can be disabled.
- Masked fields reveal only on request. Optional clearing reads the clipboard and clears it only if the value still matches our latest write.
- The masked field separates reveal and copy with an optional hairline. Toast confirmations include a description and expire after 1800ms by default.
Accessibility
- The button exposes busy during pending and announces a single localized outcome. Optional toast owns its own announcement.
- Icon and field appearances retain spoken action labels; a hidden field exposes a generic label, never its secret.
- Use accessibilityLabel to explain what is being copied. Controls have at least a 44-point touch target.
- Reduce Motion removes shake and icon travel, retaining short fades and readable outcome labels. Native screen reader checks remain pending.
Motion
- Uses the
press,morph,toggleandambientmotion roles, so it follows your app's feel. Passmotionto tune this one instance. - Composes Button press feedback and pending ButtonSpinner, plus IconMorph copy-check geometry and draw-on.
- Failure shake uses the press timing role; label changes use morph entry and exit roles.
- Under Reduce Motion, outcomes remain visible without geometric travel or effects.
Performance
- Request tokens ignore outdated generated values and outcomes after replacement or unmount. Repeated pending presses are ignored.
- All timers are cancelled on unmount. Clipboard clearing is best effort while mounted; it is disabled by default and reads only when opted in.
- Clipboard equality checks cannot make a platform read/write atomic. Do not treat clipboard clearing as secure erasure.
Notes for AI
- Pass a stable value generator to preserve an operation across parent rerenders. It runs only on press.
- Every optional feedback has its own prop. Set haptics globally or separately for copy and error.
- toast requires the surrounding ToastProvider and ToastHost. Browser clipboard permission and user activation restrictions still apply.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.