Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useClipboard): UseClipboard component #3359

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ export * from '../core/useTimestamp/component'
export * from '../core/useVirtualList/component'
export * from '../core/useWindowFocus/component'
export * from '../core/useWindowSize/component'
export * from '../core/useClipboard/component'
22 changes: 22 additions & 0 deletions packages/core/useClipboard/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineComponent, reactive } from 'vue-demi'
import { useClipboard } from '@vueuse/core'
import type { UseClipboardOptions } from '@vueuse/core'

interface UseClipboardProps<Source = string> extends UseClipboardOptions<Source> {}

export const UseClipboard = /*#__PURE__*/ defineComponent<UseClipboardProps>({

Check failure on line 7 in packages/core/useClipboard/component.ts

View workflow job for this annotation

GitHub Actions / lint

Expected exception block, space or tab after '/*' in comment

Check failure on line 7 in packages/core/useClipboard/component.ts

View workflow job for this annotation

GitHub Actions / lint

Expected space or tab before '*/' in comment
name: 'UseClipboard',
props: [
'source',
'read',
'navigator',
'copiedDuring',
'legacy',
] as unknown as undefined,

setup(props, { slots }) {
const data = reactive(useClipboard(props))

return () => slots.default?.(data)
},
})
10 changes: 10 additions & 0 deletions packages/core/useClipboard/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ const { text, copy, copied, isSupported } = useClipboard({ source })
```

Set `legacy: true` to keep the ability to copy if [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) is not available. It will handle copy with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) as fallback.

## Component Usage

```html
<UseClipboard source="copy me" v-slot="{ copy, copied }">
<button @click="copy()">
{{ copied ? 'Copied' : 'Copy' }}
</button>
</UseClipboard>
```