All files useClipboardApi.ts

66.66% Statements 10/15
0% Branches 0/1
100% Functions 2/2
66.66% Lines 10/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x       1x 3x   3x 1x         1x 1x 1x 1x               3x    
import { useState } from 'react';
 
import type { CopiedValue, CopyFn } from './types';
 
export default function useClipboardApi(): [CopiedValue, CopyFn] {
  const [copiedValue, setCopiedValue] = useState<CopiedValue>(null);
 
  const copy: CopyFn = async (value) => {
    Iif (!navigator?.clipboard) {
      console.warn('Clipboard not supported.');
      return false;
    }
 
    try {
      await navigator.clipboard.writeText(value);
      setCopiedValue(value);
      return true;
    } catch (err) {
      console.error('Failed to Copy', err);
      setCopiedValue(null);
      return false;
    }
  };
 
  return [copiedValue, copy];
}