Morph
Usage
Import morph from @mielui/svelte/attachments/morph and add {@attach morph({ key })} to a visual wrapper. Change key with the displayed value. Duration defaults to 220 milliseconds.
SVG and text
Matching closed SVG shapes interpolate their geometry. Different icon structures use the same fixed-cell rotation, scale, and crossfade as Copy Button. Text changes use a short blur and crossfade.
Composition and accessibility
Use a span inside your existing button, not the button itself. Keep the button's accessible label outside the animation. The visual copy is hidden from assistive technology and cannot receive input. Give text a stable width if surrounding controls should not move.
Reduced-motion preferences skip the animation. The visual layer stays inside its wrapper during scrolling and resizing. Interrupted transitions continue from the current visual state, and unmount restores the original content.
Changing a count
Keep the animated number in a fixed-width wrapper. The surrounding label and button stay in place while the count changes.
API reference
morph takes a MorphOptions object and returns a Svelte attachment for an HTMLElement. A new options object re-attaches it, which morphs from what the element drew last when key changed. It cleans up when the element unmounts.
keyRequiredunknownChange it when the displayed content changes.
durationnumberTransition length in milliseconds.
Default
220
<script lang="ts"> import { Moon02Icon as Moon, Sun03Icon as Sun } from '@hugeicons/core-free-icons'; import { morph } from '@mielui/svelte/attachments/morph'; import { Button } from '@mielui/svelte/components/button'; import HugeiconsIcon from '@mielui/svelte/hugeicons-icon'; let night = $state(false); function toggleAppearance() { night = !night; } </script> <Button variant="outline" onclick={toggleAppearance} aria-label="Switch appearance preview"> <span class="inline-flex size-5" aria-hidden="true" {@attach morph({ key: night })}> <HugeiconsIcon icon={night ? Moon : Sun} size={20} /> </span> <span class="inline-block w-12 text-left" {@attach morph({ key: night })}> {night ? 'Night' : 'Day'} </span> </Button>