Adding animation to Akar should feel similar to any other component, but there are some caveats noted here in regards to exiting animations with JS animation libraries.
The simplest way to animate Primitives is with CSS.
You can use CSS animation to animate both mount and unmount phases. The latter is possible because the Akar will suspend unmount while your animation plays out.
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
.DialogOverlay[data-state='open'],
.DialogContent[data-state='open'] {
animation: fadeIn 300ms ease-out;
}
.DialogOverlay[data-state='closed'],
.DialogContent[data-state='closed'] {
animation: fadeOut 300ms ease-in;
}
Other than using CSS animation, you might prefer to use the native Vue <Transition>. Great news! It should be as easy as wrapping component (that has forceMount prop), and you are done!
<script setup lang="ts">
import { ADialogClose, ADialogContent, ADialogOverlay, ADialogPortal, ADialogRoot, ADialogTrigger, } from 'akar';
</script>
<template>
<ADialogRoot v-model:open="open">
<ADialogTrigger>
Edit profile
</ADialogTrigger>
<ADialogPortal>
<Transition name="fade">
<ADialogOverlay />
</Transition>
<Transition name="fade">
<ADialogContent>
<h1>Hello from inside the ADialog!</h1>
<ADialogClose>Close</ADialogClose>
</ADialogContent>
</Transition>
</ADialogPortal>
</ADialogRoot>
</template>
<style>
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>
Akar also supports implementing animation using Motion Vue. This lightweight, powerful library integrates seamlessly with components and offers extensive flexibility for creating smooth, performant animations.
<script setup lang="ts">
import { ADialogClose, ADialogContent, ADialogOverlay, ADialogPortal, ADialogRoot, ADialogTrigger, } from 'akar';
import { AnimatePresence, Motion } from 'motion-v';
</script>
<template>
<ADialogRoot>
<ADialogTrigger>
Edit profile
</ADialogTrigger>
<ADialogPortal>
<AnimatePresence multiple>
<ADialogOverlay as-child>
<Motion
:initial="{ opacity: 0, scale: 0 }"
:animate="{ opacity: 1, scale: 1 }"
:exit="{ opacity: 0, scale: 0.6 }"
/>
</ADialogOverlay>
<ADialogContent as-child>
<Motion
:initial="{ opacity: 0, top: '0%' }"
:animate="{ opacity: 1, top: '50%' }"
:exit="{ opacity: 0, top: '30%' }"
>
<h1>Hello from inside the Dialog!</h1>
<ADialogClose>Close</ADialogClose>
</Motion>
</ADialogContent>
</AnimatePresence>
</ADialogPortal>
</ADialogRoot>
</template>
When many stateful Primitives are hidden from view, they are actually removed from the DOM. JavaScript animation libraries need control of the unmounting phase, so we provide the forceMount prop on many components to allow consumers to delegate the mounting and unmounting of children based on the animation state determined by those libraries.
For example, if you want to use @vueuse/motion to animate a Dialog, you would do so by conditionally rendering the dialog Overlay and Content parts based on the animation state from one of its composable like useSpring:
<script setup lang="ts">
import { useSpring } from '@vueuse/motion';
import { ADialogClose, ADialogContent, ADialogOverlay, ADialogPortal, ADialogRoot, ADialogTrigger, } from 'akar';
import { reactive, ref, watch } from 'vue';
const stages = {
initial: { opacity: 0, scale: 0, top: 0, },
enter: { opacity: 1, scale: 1, top: 50, },
leave: { opacity: 0, scale: 0.6, top: 30, },
};
const styles = reactive(stages.initial);
const { set } = useSpring(styles, {
damping: 8,
stiffness: 200,
});
const open = ref(false);
watch(open, () => {
if (open.value) {
set(stages.enter);
} else {
set(stages.leave);
}
});
</script>
<template>
<ADialogRoot v-model:open="open">
<ADialogTrigger>
Edit profile
</ADialogTrigger>
<ADialogPortal v-if="styles.opacity !== 0">
<ADialogOverlay
force-mount
:style="{
opacity: styles.opacity,
transform: `scale(${styles.scale})`,
}"
/>
<ADialogContent
force-mount
:style="{
opacity: styles.opacity,
top: `${styles.top}%`,
}"
>
<h1>Hello from inside the ADialog!</h1>
<ADialogClose>Close</ADialogClose>
</ADialogContent>
</ADialogPortal>
</ADialogRoot>
</template>