# React Native CopyButton

> A React Native copy-to-clipboard button for iOS, Android and the web: copies a value, shows a check for a moment, announces 'Copied', and works with any clipboard library on native. No dependencies.

# CopyButton

Copies a value, then shows a check for a moment: an API key, an invite code, a link. On the web it uses the browser clipboard. React Native has no clipboard of its own, so on iOS and Android pass `onCopy` from your clipboard library; Patine stays free of native code.

```tsx
import { CopyButton } from 'patine-ui';
import * as Clipboard from 'expo-clipboard';

<CopyButton value={apiKey} onCopy={Clipboard.setStringAsync} />
```

```tsx live
import { Box, CopyButton, HStack, Text } from 'patine-ui';

export default function Example() {
  return (
    <Box border radius="lg" padding="md" gap="sm" style={{ maxWidth: 420 }}>
      <Text weight="semibold">API key</Text>
      <HStack justify="space-between" style={{ backgroundColor: 'rgba(0,0,0,0.04)', borderRadius: 8, paddingStart: 12 }}>
        <Text style={{ fontFamily: 'monospace' }}>sk_live_51Hx…9fQ2</Text>
        <CopyButton value="sk_live_51Hx7Lk2eZvKYlo2C9fQ2" accessibilityLabel="Copy API key" />
      </HStack>
      <CopyButton value="https://patine.dev/invite/7Q2K" label="Copy invite link" variant="outline" />
    </Box>
  );
}
```

## Example: invite a friend

A referral screen: the code and the link each have their own copy button, and `onCopy` counts the copies so the screen can thank you.

```tsx live
import { useState } from 'react';
import { Box, CopyButton, HStack, Text, VStack } from 'patine-ui';

const CODE = 'MAYA-20';
const LINK = 'https://shop.example.com/r/maya-20';

const Row = ({ label, value, onCopy }: { label: string; value: string; onCopy: () => void }) => (
  <VStack gap="xs">
    <Text size="sm" color="textSecondary">{label}</Text>
    <HStack justify="space-between" align="center" style={{ backgroundColor: 'rgba(0,0,0,0.04)', borderRadius: 8, paddingStart: 12 }}>
      <Text numberOfLines={1} style={{ flex: 1, fontFamily: 'monospace' }}>{value}</Text>
      <CopyButton value={value} onCopy={onCopy} accessibilityLabel={`Copy ${label.toLowerCase()}`} />
    </HStack>
  </VStack>
);

export default function Example() {
  const [copies, setCopies] = useState(0);
  const copied = () => setCopies((n) => n + 1);
  return (
    <Box border radius="lg" padding="lg" gap="md" style={{ width: '100%', maxWidth: 420 }}>
      <VStack gap="xs">
        <Text weight="semibold">Give €20, get €20</Text>
        <Text color="textSecondary">Friends get €20 off their first order over €60. You get €20 when it ships.</Text>
      </VStack>
      <Row label="Your code" value={CODE} onCopy={copied} />
      <Row label="Your link" value={LINK} onCopy={copied} />
      <CopyButton value={`Use my code ${CODE} for €20 off: ${LINK}`} label="Copy a message to send" variant="outline" onCopy={copied} />
      {copies ? <Text size="sm" color="success">Copied. Paste it anywhere to share.</Text> : null}
    </Box>
  );
}
```

## Accessibility

A `button` named "Copy" (or `accessibilityLabel`, or its `label`), then "Copied" for `timeout` ms, announced politely.

## Props

Also takes Button's props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | The text to copy. |
| `onCopy` | `(value: string) => unknown` | browser clipboard | Copies it; pass your clipboard library's on native. |
| `label` | `string` | — | A word beside the icon. Default: icon only. |
| `copiedLabel` | `string` | `'Copied'` | What it says after copying. |
| `timeout` | `number` | `2000` | How long the check shows, in ms. |
| `variant`, `size` | as Button | `'ghost'` | The button's look. |
