mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-10-23 04:52:14 +00:00
24 lines
715 B
TypeScript
24 lines
715 B
TypeScript
import type { Accessor } from 'solid-js';
|
|
import { createSignal } from 'solid-js';
|
|
import { toast } from '../../ui/components/sonner';
|
|
|
|
export { useCopy, writeTextToClipboard };
|
|
|
|
function writeTextToClipboard({ text }: { text: string }) {
|
|
return navigator.clipboard.writeText(text);
|
|
}
|
|
|
|
function useCopy(getText: Accessor<string | number>, { toastMessage = 'Copied to clipboard' }: { toastMessage?: string } = {}) {
|
|
const [getIsJustCopied, setIsJustCopied] = createSignal(false);
|
|
|
|
return {
|
|
getIsJustCopied,
|
|
copy: () => {
|
|
writeTextToClipboard({ text: String(getText()) });
|
|
setIsJustCopied(true);
|
|
setTimeout(() => setIsJustCopied(false), 2000);
|
|
toast(toastMessage);
|
|
},
|
|
};
|
|
}
|