-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathnuxt-icon.vue
59 lines (50 loc) · 1.19 KB
/
nuxt-icon.vue
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
52
53
54
55
56
57
58
59
<template>
<span
class="nuxt-icon"
:class="{ 'nuxt-icon--fill': !filled, 'nuxt-icon--stroke': hasStroke && !filled }"
v-html="icon"
/>
</template>
<script setup lang="ts">
import { ref, watchEffect } from '#imports'
const props = withDefaults(defineProps<{
name: string;
filled?: boolean
}>(), { filled: false })
const icon = ref<string | Record<string, any>>('')
let hasStroke = false
async function getIcon () {
try {
const iconsImport = import.meta.glob('assets/icons/**/**.svg', {
eager: false,
query: '?raw',
import: 'default'
})
const rawIcon = await iconsImport[`/assets/icons/${props.name}.svg`]()
if (rawIcon.includes('stroke')) { hasStroke = true }
icon.value = rawIcon
} catch {
console.error(
`[nuxt-icons] Icon '${props.name}' doesn't exist in 'assets/icons'`
)
}
}
await getIcon()
watchEffect(getIcon)
</script>
<style>
.nuxt-icon svg {
width: 1em;
height: 1em;
margin-bottom: 0.125em;
vertical-align: middle;
}
.nuxt-icon.nuxt-icon--fill,
.nuxt-icon.nuxt-icon--fill * {
fill: currentColor !important;
}
.nuxt-icon.nuxt-icon--stroke,
.nuxt-icon.nuxt-icon--stroke *{
stroke: currentColor !important;
}
</style>