it-tools/src/composable/copy.ts
Corentin THOMASSET 86e964a274
fix(copy): prevent shorthand copy if source is present in useCopy (#559)
* fix(copy): prevent shorthand copy if source is present in useCopy

* refactor(copy): normalized copy usage
2023-08-09 22:07:44 +00:00

20 lines
635 B
TypeScript

import { type MaybeRef, get, useClipboard } from '@vueuse/core';
import { useMessage } from 'naive-ui';
export function useCopy({ source, text = 'Copied to the clipboard' }: { source?: MaybeRef<unknown>; text?: string } = {}) {
const { copy } = useClipboard(source ? { source: computed(() => String(get(source))) } : {});
const message = useMessage();
return {
async copy(content?: string, { notificationMessage }: { notificationMessage?: string } = {}) {
if (source) {
await copy();
}
else {
await copy(content);
}
message.success(notificationMessage ?? text);
},
};
}