useCopyToClipboard.ts 914 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useClipboard, whenever } from '@vueuse/core'
  3. import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
  4. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  5. export const useCopyToClipboard = () => {
  6. const { copy, copied: copiedToClipboard } = useClipboard()
  7. const { notify } = useNotifications()
  8. const copyToClipboard = (input?: string | null) => {
  9. if (typeof input === 'undefined' || input === null) return
  10. copy(input)
  11. }
  12. whenever(copiedToClipboard, () => {
  13. notify({
  14. id: 'copied-to-clipboard',
  15. type: NotificationTypes.Success,
  16. message: __('Copied.'), // TODO should this not be something given to the composable for a more meaningful message?
  17. })
  18. })
  19. return {
  20. copiedToClipboard,
  21. copyToClipboard,
  22. }
  23. }