Animated icons in Vue & Nuxt
kubai-icons-vue provides each icon as a Vue component. It requires Vue 3.3 or later and works in Nuxt with server rendering: the SVG renders on the server, and the hover motion is added on the client.
Install
npm install kubai-icons-vueUse an icon in a component
Import icons by name in <script setup>. The same code works in a Vite app and in a Nuxt page or component:
<script setup>
import { Folder, BellOff } from "kubai-icons-vue";
</script>
<template>
<Folder :size="28" color="#6366F1" />
<BellOff variant="soft" title="Muted" />
</template>The props are size, color, variant, badgeColor, trigger, hover, active, defaultActive and title. Without a title, an icon is hidden from screen readers. Classes and other attributes pass through to the <svg>.
Register icons globally in Nuxt
Nuxt auto-imports components from your own components/ folder, not from packages. Import icons where you use them, or register the ones you use often in a plugin:
// plugins/kubai-icons.ts
import { Folder, Inbox, Settings } from "kubai-icons-vue";
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.component("FolderIcon", Folder);
nuxtApp.vueApp.component("InboxIcon", Inbox);
nuxtApp.vueApp.component("SettingsIcon", Settings);
});Register only the icons you need. Each icon is its own module, so the bundle carries only those.
Play the motion when a link is hovered
Add data-kubai-trigger to the link and trigger="parent" to the icon. NuxtLink passes the attribute on to its <a>:
<script setup>
import { Settings } from "kubai-icons-vue";
</script>
<template>
<NuxtLink to="/settings" data-kubai-trigger class="nav-link">
<Settings :size="18" trigger="parent" />
Settings
</NuxtLink>
</template>More patterns are in Play an icon's animation when its button is hovered.
Toggle icons with v-model
Toggle icons, such as Bookmark, switch between two states when clicked or tapped. Bind the state with v-model:active:
<script setup>
import { ref } from "vue";
import { Bookmark } from "kubai-icons-vue";
const saved = ref(false);
</script>
<template>
<button data-kubai-trigger :aria-pressed="saved">
<Bookmark v-model:active="saved" trigger="parent" />
{{ saved ? "Saved" : "Save" }}
</button>
</template>Icons chosen at runtime
When the icon name comes from data, use DynamicIcon. It loads each icon as a separate chunk. It renders an empty placeholder on the server and loads the icon in the browser, so prefer named imports for icons that are always shown.
<script setup>
import { DynamicIcon } from "kubai-icons-vue/dynamic";
defineProps({ name: String });
</script>
<template>
<DynamicIcon :name="name" />
</template>The soft variant for a whole page
The variant is inherited. Bind VARIANTS.soft to a root element, and every icon inside uses it unless it sets its own variant:
<script setup>
import { VARIANTS } from "kubai-icons-vue";
</script>
<template>
<div :style="VARIANTS.soft">
<NuxtPage />
</div>
</template>Faster dev server
A dev server may load the package index, which references every icon. Import each icon from its own file to avoid that:
import Folder from "kubai-icons-vue/icons/folder";Questions
Do the icons work with Nuxt server rendering?
Yes. The SVG markup renders on the server, so icons are in the first HTML. Motion is added on the client after hydration. DynamicIcon is the exception: it renders an empty placeholder on the server.
Which Vue version do I need?
Vue 3.3 or later. Current Nuxt releases already include a newer Vue.
How do I change an icon's color?
Set the color prop, or set the CSS color on the icon or any parent. Every tone of the icon is calculated from that one color.
Can I control the motion from my own state?
Yes. Set trigger="none" and bind the hover prop: true plays the motion and false reverses it.