Skip to main content Design tokens Components Style utilities
Skip to table of contents

    Animation

    Animate elements declaratively with nearly 100 baked‐in presets, or roll your own with custom keyframes. These animations are powered by the Web Animations API.

    To animate an element, wrap it in a <pc-animation> element and set an an animation name. The animation will not start until you add the play attribute. Refer to the properties table for a list of all animation options.

    <div class="animation-overview">
      <pc-animation name="bounce" duration="2000" play>
          <div class="box"></div>
      </pc-animation>
      <pc-animation name="jello" duration="2000" play>
          <div class="box"></div>
      </pc-animation>
      <pc-animation name="heartBeat" duration="2000" play>
          <div class="box"></div>
      </pc-animation>
      <pc-animation name="flip" duration="2000" play>
          <div class="box"></div>
      </pc-animation>
    </div>
    
    <style>
      .animation-overview .box {
          display: inline-block;
          width: 100px;
          height: 100px;
          margin: 1.5rem;
          background-color: var(--pc-color-primary-400);
          border-radius: var(--pc-border-radius-l);
      }
    </style>
    Code

    The animation will only be applied to the first child element found in <pc-animation>.

    Demos

    Animations and easings

    This demo demonstrates all of the baked‐in animations and easings. Animations are based on those found in the popular Animate.css library.

    Intersection Observer

    Use an Intersection Observer 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.

    Custom keyframes

    Supply your own keyframes to build custom animations.

    Playing animations programmatically

    Animations won’t play until you add the play attribute. You can omit it initially, then apply it programmatically such as after a user interaction. In this demo, the button will animate once every time the button is clicked.

    Properties

    NameDescriptionReflectsDefault
    nameThe name of the built‐in animation to use. For custom animations, use the keyframes prop. string"none"
    playPlays the animation. When omitted, the animation will be paused. This attribute will be automatically removed when the animation finishes or gets cancelled. booleanfalse
    delayThe number of milliseconds to delay the start of the animation. number0
    directionDetermines the direction of playback as well as the behaviour when reaching the end of an iteration. Learn more about the CSS <code>animation-direction</code> property. PlaybackDirection"normal"
    durationThe number of milliseconds each iteration of the animation takes to complete. number1000
    easingThe easing function to use for the animation. This can be an easing function name like easeInSine or a custom easing function like cubic-bezier(0.12, 0, 0.39, 0). string"ease-in-out"
    endDelay
    end-delay
    The number of milliseconds to delay after the active period of an animation sequence. number0
    fillSets how the animation applies styles to its target before and after its execution. FillMode"auto"
    iterationsThe number of iterations to run before the animation completes. Infinity
    iterationStart
    iteration-start
    The offset at which to start the animation, usually between 0 (start) and 1 (end). number0
    keyframesThe keyframes to use for the animation. If this is set, the name attribute and property will be ignored. Keyframe[] | undefined-
    playbackRate
    playback-rate
    Sets the animation’s playback rate. The default is 1, which plays the animation at a normal speed. Setting this to 2, for example, will double the animation’s speed. A negative value can be used to reverse the animation. This value can be changed without causing the animation to restart. number1
    currentTimeGets and sets the current animation time. CSSNumberish-
    updateComplete A read‐only promise that resolves when the component has finished updating. -

    Learn more about attributes and properties.

    Slots

    NameDescription
    (default)The element to animate. Avoid slotting in more than one element, as subsequent ones will be ignored. To animate multiple elements, either wrap them in a single container or use multiple <pc-animation> elements.

    Learn more about using slots.

    Methods

    NameDescriptionArguments
    cancel()Clears all keyframes caused by this animation and aborts its playback.-
    finish()Sets the playback time to the end of the animation corresponding to the current playback direction.-

    Learn more about methods.

    Events

    NameDescriptionEvent detail
    pc-cancelEmitted when the animation is cancelled.-
    pc-finishEmitted when the animation finishes.-
    pc‐startEmitted when the animation starts or restarts.-

    Learn more about events.

    Importing

    If you’re using the autoloader or the standard loader, you can ignore this section. If you’re cherry picking, you can use any of the following snippets to import this component.

    ScriptImportBundler

    To import this component from the CDN with a script tag, copy this snippet and paste it in your HTML.

    <script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/animation/animation.js"></script>

    To import this component from the CDN using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/animation/animation.js";

    To import this component with a bundler using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "placer-toolkit/dist/components/animation/animation.js";