On this page

createAnimate

createAnimate animates elements that you do not render with motion.*: nodes that a chart library creates, or elements that you pick by selector.

tsx
import { createAnimate } from "@risinghq/solid-motion";

function Toolbar() {
  const [scope, animate] = createAnimate();

  return (
    <div ref={scope}>
      <button
        onClick={() =>
          animate(".dot", {
            scale: [1, 1.5, 1],
            transition: { duration: 0.4 },
          })
        }
      >
        Pulse
      </button>
      <span class="dot" />
    </div>
  );
}

createAnimate() returns [scope, animate].

  • scope is a ref. Put it on the root element. scope.current is that element, or undefined before it mounts.
  • animate(target, definition) animates the target and returns the playback controls.

target is an element, an array of elements, or a CSS selector. A selector is searched inside scope.current. Before the scope mounts, a selector matches nothing.

definition is a target as in animate={...}: values, an optional transition and an optional transitionEnd.

Controls

The return value of animate has stop(), pause(), play(), complete(), cancel(), time, speed, duration, state and finished. It is also awaitable.

tsx
await animate(".dot", { opacity: 0 });

finished also settles when you call stop(). A call that you make before the first frame runs, such as pause(), is applied once the animation starts.

Numbers and motion values

animate(from, to, options) animates a plain number. Nothing is rendered: read each frame in onUpdate. to can also be a list of keyframes.

tsx
const [count, setCount] = createSignal(0);

animate(0, 100, { duration: 1, onUpdate: setCount });

animate(value, to, options) animates a MotionValue, so everything bound to it follows without a signal in between. A later call on the same value takes over, and the earlier one settles.

tsx
const progress = createMotionValue(0);

animate(progress, 1, { type: "spring", stiffness: 120 });

Neither form needs scope.

Sequences

Pass a list of [target, definition] steps to run them one after another. Each step starts when the one before it ends.

tsx
await animate([
  [".title", { opacity: 1 }],
  [".body", { opacity: 1 }],
]);

A step takes a third item: its transition, plus at to move its start.

atThe step starts
0.50.5 s after the sequence starts
"+0.5"0.5 s after the previous step ends
"-0.2"0.2 s before the previous step ends
"<"with the previous step
"<0.1"0.1 s after the previous step starts
"intro"at the label intro
"intro+1"1 s after the label intro
tsx
animate([
  [".card", { y: 0 }, { duration: 0.4 }],
  [".shadow", { opacity: 1 }, { at: "<" }],
  "details",
  [".title", { opacity: 1 }, { at: "-0.2" }],
  [".body", { opacity: 1 }, { at: "details+0.1" }],
]);

A string on its own marks a label at the current time. { name, at } places a label at a given time. { sequence, at } nests a sequence and moves it as one step. A MotionValue can be a step too: [progress, 1, { duration: 0.5 }].

The second argument of a sequence takes duration, delay, repeat and the other playback options for the whole sequence, and defaultTransition for each step. A step without its own timing lasts 0.3 s with easeOut.

A spring in a step becomes an easing curve, and transition.path does not apply inside a sequence.

Rules

  • Animations stop when the owner of createAnimate is cleaned up.
  • MotionConfig applies: its transition is the default, skipAnimations jumps to the target, and reducedMotion makes movement jump.
  • If a motion.* element and animate target the same property of one element, the later call takes over, and the earlier one settles.