Skip to content

Commit

Permalink
feat(plugins): clipboard (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Feb 2, 2022
1 parent ed499b3 commit 832ffe4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export default defineNuxtModule<ModuleOptions>({
})

addPlugin(resolve(runtimeDir, 'plugins', 'toast.client'))
addPlugin(resolve(runtimeDir, 'plugins', 'clipboard.client'))

addComponentsDir({
path: resolve(runtimeDir, 'components', 'elements'),
Expand Down
33 changes: 33 additions & 0 deletions src/runtime/plugins/clipboard.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { defineNuxtPlugin } from '#app'
import { ClipboardPlugin } from '../types/clipboard'

export default defineNuxtPlugin((nuxtApp) => {
function copy (text: string, success: { title?: string, description?: string } = {}, failure: { title?: string, description?: string } = {}) {
if (!navigator?.clipboard) {
return
}

navigator.clipboard.writeText(text).then(() => {
if (!success.title && !success.description) {
return
}

nuxtApp.$toast.success(success)
}, function (e) {
nuxtApp.$toast.error({
...failure,
description: failure.description || e.message
})
})
}

nuxtApp.provide('clipboard', {
copy
})
})

declare module '#app' {
interface NuxtApp {
$clipboard: ClipboardPlugin
}
}
3 changes: 3 additions & 0 deletions src/runtime/types/clipboard.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ClipboardPlugin {
copy: (text: string, success?: { title?: string, description?: string }, failure?: { title?: string, description?: string }) => void
}

1 comment on commit 832ffe4

@vercel
Copy link

@vercel vercel bot commented on 832ffe4 Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.