docs(examples): use useClipboard instead of navigator.clipboard

This commit is contained in:
Benjamin Canac
2025-06-18 14:16:54 +02:00
parent abfd0ede03
commit 04f12adc5b
5 changed files with 17 additions and 14 deletions

View File

@@ -1,15 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { useClipboard } from '@vueuse/core'
const value = ref('npx nuxt module add ui') const value = ref('npx nuxt module add ui')
const copied = ref(false)
function copy() { const { copy, copied } = useClipboard()
navigator.clipboard.writeText(value.value)
copied.value = true
setTimeout(() => {
copied.value = false
}, 2000)
}
</script> </script>
<template> <template>
@@ -25,7 +19,7 @@ function copy() {
size="sm" size="sm"
:icon="copied ? 'i-lucide-copy-check' : 'i-lucide-copy'" :icon="copied ? 'i-lucide-copy-check' : 'i-lucide-copy'"
aria-label="Copy to clipboard" aria-label="Copy to clipboard"
@click="copy" @click="copy(value)"
/> />
</UTooltip> </UTooltip>
</template> </template>

View File

@@ -2,6 +2,7 @@
import { h, resolveComponent } from 'vue' import { h, resolveComponent } from 'vue'
import { upperFirst } from 'scule' import { upperFirst } from 'scule'
import type { TableColumn } from '@nuxt/ui' import type { TableColumn } from '@nuxt/ui'
import { useClipboard } from '@vueuse/core'
const UButton = resolveComponent('UButton') const UButton = resolveComponent('UButton')
const UCheckbox = resolveComponent('UCheckbox') const UCheckbox = resolveComponent('UCheckbox')
@@ -9,6 +10,7 @@ const UBadge = resolveComponent('UBadge')
const UDropdownMenu = resolveComponent('UDropdownMenu') const UDropdownMenu = resolveComponent('UDropdownMenu')
const toast = useToast() const toast = useToast()
const { copy } = useClipboard()
type Payment = { type Payment = {
id: string id: string
@@ -220,7 +222,7 @@ const columns: TableColumn<Payment>[] = [{
}, { }, {
label: 'Copy payment ID', label: 'Copy payment ID',
onSelect() { onSelect() {
navigator.clipboard.writeText(row.original.id) copy(row.original.id)
toast.add({ toast.add({
title: 'Payment ID copied to clipboard!', title: 'Payment ID copied to clipboard!',

View File

@@ -2,12 +2,14 @@
import { h, resolveComponent } from 'vue' import { h, resolveComponent } from 'vue'
import type { TableColumn } from '@nuxt/ui' import type { TableColumn } from '@nuxt/ui'
import type { Row } from '@tanstack/vue-table' import type { Row } from '@tanstack/vue-table'
import { useClipboard } from '@vueuse/core'
const UButton = resolveComponent('UButton') const UButton = resolveComponent('UButton')
const UBadge = resolveComponent('UBadge') const UBadge = resolveComponent('UBadge')
const UDropdownMenu = resolveComponent('UDropdownMenu') const UDropdownMenu = resolveComponent('UDropdownMenu')
const toast = useToast() const toast = useToast()
const { copy } = useClipboard()
type Payment = { type Payment = {
id: string id: string
@@ -119,7 +121,7 @@ function getRowItems(row: Row<Payment>) {
}, { }, {
label: 'Copy payment ID', label: 'Copy payment ID',
onSelect() { onSelect() {
navigator.clipboard.writeText(row.original.id) copy(row.original.id)
toast.add({ toast.add({
title: 'Payment ID copied to clipboard!', title: 'Payment ID copied to clipboard!',

View File

@@ -1,5 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import type { TableColumn, DropdownMenuItem } from '@nuxt/ui' import type { TableColumn, DropdownMenuItem } from '@nuxt/ui'
import { useClipboard } from '@vueuse/core'
interface User { interface User {
id: number id: number
@@ -10,6 +11,7 @@ interface User {
} }
const toast = useToast() const toast = useToast()
const { copy } = useClipboard()
const data = ref<User[]>([{ const data = ref<User[]>([{
id: 1, id: 1,
@@ -71,7 +73,8 @@ function getDropdownActions(user: User): DropdownMenuItem[][] {
label: 'Copy user Id', label: 'Copy user Id',
icon: 'i-lucide-copy', icon: 'i-lucide-copy',
onSelect: () => { onSelect: () => {
navigator.clipboard.writeText(user.id.toString()) copy(user.id.toString())
toast.add({ toast.add({
title: 'User ID copied to clipboard!', title: 'User ID copied to clipboard!',
color: 'success', color: 'success',

View File

@@ -3,6 +3,7 @@ import { h, resolveComponent } from 'vue'
import { upperFirst } from 'scule' import { upperFirst } from 'scule'
import type { TableColumn, TableRow } from '@nuxt/ui' import type { TableColumn, TableRow } from '@nuxt/ui'
import { getPaginationRowModel } from '@tanstack/vue-table' import { getPaginationRowModel } from '@tanstack/vue-table'
import { useClipboard } from '@vueuse/core'
const UButton = resolveComponent('UButton') const UButton = resolveComponent('UButton')
const UCheckbox = resolveComponent('UCheckbox') const UCheckbox = resolveComponent('UCheckbox')
@@ -10,6 +11,7 @@ const UBadge = resolveComponent('UBadge')
const UDropdownMenu = resolveComponent('UDropdownMenu') const UDropdownMenu = resolveComponent('UDropdownMenu')
const toast = useToast() const toast = useToast()
const { copy } = useClipboard()
type Payment = { type Payment = {
id: string id: string
@@ -231,7 +233,7 @@ const columns: TableColumn<Payment>[] = [{
}, { }, {
label: 'Copy payment ID', label: 'Copy payment ID',
onSelect() { onSelect() {
navigator.clipboard.writeText(row.original.id) copy(row.original.id)
toast.add({ toast.add({
title: 'Payment ID copied to clipboard!', title: 'Payment ID copied to clipboard!',