preact-missing-hooks
Version:
A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.
37 lines (36 loc) • 1.3 kB
TypeScript
export interface UseClipboardOptions {
/** Duration in ms to keep `copied` true before resetting. Default: 2000 */
resetDelay?: number;
}
export interface UseClipboardReturn {
/** Copy text to the clipboard. Returns true on success. */
copy: (text: string) => Promise<boolean>;
/** Read text from the clipboard. Returns empty string if denied or unavailable. */
paste: () => Promise<string>;
/** Whether the last copy operation succeeded (resets after resetDelay) */
copied: boolean;
/** Error from the last failed operation, or null */
error: Error | null;
/** Manually reset copied and error state */
reset: () => void;
}
/**
* A Preact hook for reading and writing the clipboard. Uses the async
* Clipboard API when available (requires secure context and user gesture).
*
* @param options - Optional configuration (e.g., resetDelay for copied state)
* @returns Object with copy, paste, copied, error, and reset
*
* @example
* ```tsx
* function CopyButton() {
* const { copy, copied, error } = useClipboard();
* return (
* <button onClick={() => copy('Hello!')}>
* {copied ? 'Copied!' : 'Copy'}
* </button>
* );
* }
* ```
*/
export declare function useClipboard(options?: UseClipboardOptions): UseClipboardReturn;