-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProseCode.vue
More file actions
51 lines (46 loc) · 1.21 KB
/
ProseCode.vue
File metadata and controls
51 lines (46 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script setup lang="ts">
interface Props {
code?: string
language?: string | null
filename?: string | null
highlights?: Array<number>
meta?: string | null
class?: string
}
const props = withDefaults(defineProps<Props>(), {
code: '',
language: null,
filename: null,
highlights: () => [],
meta: null,
class: ''
})
const copied = ref(false)
const copyToClipboard = async () => {
if (!props.code) return
try {
await navigator.clipboard.writeText(props.code)
copied.value = true
setTimeout(() => {
copied.value = false
}, 2000)
} catch (err) {
console.error('Failed to copy text: ', err)
}
}
</script>
<template>
<div class="relative group">
<pre :class="$props.class"><slot /></pre>
<button
v-if="code"
@click="copyToClipboard"
class="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity duration-200 bg-base-300 hover:bg-base-200 text-base-content p-2 rounded-md text-sm"
:class="{ 'opacity-100': copied }"
:title="copied ? 'Copied!' : 'Copy to clipboard'"
>
<Icon v-if="!copied" name="feather:copy" size="16" />
<Icon v-else name="feather:check" size="16" />
</button>
</div>
</template>