--- meta: title: Animation description: Animate elements declaratively with nearly 100 baked-in presets, or roll your own with custom keyframes. layout: component --- To animate an element, wrap it in `` and set an animation `name`. The animation will not start until you add the `play` attribute. Refer to the [properties table](#properties) for a list of all animation options. ```html:preview
``` ```jsx:react import { SlAnimation } from '@shoelace-style/shoelace/dist/react'; const css = ` .animation-overview .box { display: inline-block; width: 100px; height: 100px; background-color: var(--sl-color-primary-600); margin: 1.5rem; } `; const App = () => ( <>
); ``` :::tip The animation will only be applied to the first child element found in ``. ::: ## Examples ### Animations & Easings This example demonstrates all of the baked-in animations and easings. Animations are based on those found in the popular [Animate.css](https://animate.style/) library. ```html:preview
``` ### Using Intersection Observer Use an [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) to control the animation when an element enters or exits the viewport. For example, scroll the box below in and out of your screen. The animation stops when the box exits the viewport and restarts each time it enters the viewport. ```html:preview
``` ```jsx:react import { useEffect, useRef, useState } from 'react'; import { SlAnimation } from '@shoelace-style/shoelace/dist/react'; const css = ` .animation-scroll { height: calc(100vh + 100px); } .animation-scroll .box { display: inline-block; width: 100px; height: 100px; background-color: var(--sl-color-primary-600); } `; const App = () => { const animation = useRef(null); const box = useRef(null); useEffect(() => { const observer = new IntersectionObserver(entries => { if (entries[0].isIntersecting) { animation.current.play = true; } else { animation.current.play = false; animation.current.currentTime = 0; } }); if (box.current) { observer.observe(box.current); } }, [box]); return ( <>
); }; ``` ### Custom Keyframe Formats Supply your own [keyframe formats](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats) to build custom animations. ```html:preview
``` ```jsx:react import { SlAnimation } from '@shoelace-style/shoelace/dist/react'; const css = ` .animation-keyframes .box { width: 100px; height: 100px; background-color: var(--sl-color-primary-600); } `; const App = () => ( <>
); ``` ### Playing Animations on Demand Animations won't play until you apply the `play` attribute. You can omit it initially, then apply it on demand such as after a user interaction. In this example, the button will animate once every time the button is clicked. ```html:preview
Click me
``` ```jsx:react import { useState } from 'react'; import { SlAnimation, SlButton } from '@shoelace-style/shoelace/dist/react'; const App = () => { const [play, setPlay] = useState(false); return (
setPlay(false)}> setPlay(true)}> Click me
); }; ```