All files useClipboardApi.ts

57.14% Statements 8/14
75% Branches 3/4
100% Functions 2/2
53.84% Lines 7/13

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 2x   1x   1x 1x                           1x    
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) => {
    if (!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];
}