useCopyToClipboard

Copy text to the clipboard with a temporary copied state.

Copies text and flips a copied flag for a moment afterwards, so a button can confirm itself without you tracking the timer.

Installation

React Native removed Clipboard from core, so this hook needs expo-clipboard. It is an optional peer dependency — apps that never copy are not asked to install it.

npx expo install expo-clipboard

Usage

import { useCopyToClipboard, Button } from 'panelui-native';

function CopyLink({ url }: { url: string }) {
  const { copy, copied } = useCopyToClipboard();

  return (
    <Button onPress={() => copy(url)}>
      {copied ? 'Copied' : 'Copy link'}
    </Button>
  );
}

Custom timeout

const { copy, copied } = useCopyToClipboard({ timeout: 5000 });

Pass 0 to keep copied set until the next copy.

Callback

const { copy } = useCopyToClipboard({
  onCopy: () => toast.show('Link copied'),
});

API Reference

Options

OptionTypeDefaultDescription
timeoutnumber2000Milliseconds before copied flips back. 0 keeps it set.
onCopy() => voidCalled after a successful copy.

Returns

ValueTypeDescription
copy(value: string) => Promise<void>Writes to the clipboard and sets copied.
copiedbooleanWhether a copy happened within the timeout.

copy rejects with an install message if expo-clipboard is missing. The module is imported lazily, so that surfaces on first use rather than at import time.

On this page