Play an icon's animation when its button is hovered
By default, an icon plays its motion when the icon itself is hovered or receives keyboard focus. In a button, link or card, the icon is a small part of the target, so it reads better when the whole control starts the motion.
Mark the control, then the icon
Add data-kubai-trigger to the element that should start the motion, and trigger="parent" to the icon. The icon then listens to the closest ancestor that carries data-kubai-trigger. If there is none, it listens to its direct parent.
import { Trash } from "kubai-icons-react";
export function DeleteButton() {
return (
<button data-kubai-trigger className="btn">
<Trash size={18} trigger="parent" />
Delete
</button>
);
}The same works in Vue:
<script setup>
import { Trash } from "kubai-icons-vue";
</script>
<template>
<button data-kubai-trigger class="btn">
<Trash :size="18" trigger="parent" />
Delete
</button>
</template>In Svelte:
<script>
import { Trash } from "kubai-icons-svelte";
</script>
<button data-kubai-trigger class="btn">
<Trash size={18} trigger="parent" />
Delete
</button>In Solid:
import { Trash } from "kubai-icons-solid";
export function DeleteButton() {
return (
<button data-kubai-trigger class="btn">
<Trash size={18} trigger="parent" />
Delete
</button>
);
}And with the web component:
<a href="/settings" data-kubai-trigger>
<kubai-icon name="settings" size="18" trigger="parent"></kubai-icon>
Settings
</a>Cards & list rows
The marked element can be as large as you like: a card, a table row or a menu item. Every icon inside it with trigger="parent" plays together.
import { CalendarCheck, Mail } from "kubai-icons-react";
export function InviteCard() {
return (
<a href="/invites/42" data-kubai-trigger className="card">
<CalendarCheck size={28} trigger="parent" />
<p>Team review, Friday 10:00</p>
<Mail size={18} trigger="parent" />
</a>
);
}Plain JavaScript
With createIcon, pass trigger: "parent" and add the icon to the page in the same task that creates it, because the icon looks for its trigger element right after that task:
import { createIcon, Download } from "kubai-icons";
const button = document.querySelector("#export");
button.setAttribute("data-kubai-trigger", "");
button.prepend(createIcon(Download, { size: 18, trigger: "parent" }));To bind an icon that you add later, use animate and bindTrigger. See JavaScript.
Start the motion from your own state
For a motion that follows something other than hover, such as an open panel or a drag over a drop zone, set trigger="none" and drive the hover prop. true plays the motion and false reverses it.
import { useState } from "react";
import { Folder } from "kubai-icons-react";
export function DropZone() {
const [over, setOver] = useState(false);
return (
<div
onDragEnter={() => setOver(true)}
onDragLeave={() => setOver(false)}
onDrop={() => setOver(false)}
>
<Folder size={40} trigger="none" hover={over} />
Drop files here
</div>
);
}Keyboard, touch & toggles
- Keyboard focus on the marked element plays the motion, as hover does. Focus left behind by a mouse click does not.
- On touch screens, a tap plays the motion forward and back.
- Toggle icons, such as
Bookmark, switch state when the marked element is clicked. UseactiveandonActiveChange(orv-model:activein Vue andbind:activein Svelte) to keep that state in your app. - When
prefers-reduced-motionis set, icons move straight to their end pose.
Questions
Why doesn't the icon move when I hover it inside my button?
Some component libraries set pointer-events: none on icons inside buttons, so the icon never receives hover. Add data-kubai-trigger to the button and trigger="parent" to the icon, and the button starts the motion instead.
What happens if no element has data-kubai-trigger?
With trigger="parent", the icon listens to its direct parent element.
Can one hover play several icons?
Yes. Every icon with trigger="parent" inside the same marked element plays when that element is hovered or focused.
Can I stop an icon from animating at all?
Yes. Set trigger="none" and leave out the hover prop. The icon then stays in its rest pose.