Solid MotionOne

repository·main·Indexed 42 days ago

https://github.com/solidjs-community/solid-motionone

A tiny (5.8kb), performant animation library for SolidJS providing hardware-accelerated animations, springs, and deep integration with Solid's reactivity system. It features a declarative Motion component for HTML/SVG elements, a Presence component for enter/exit animations, and a motion directive for easy element binding. Version 1.0.4.

Tokens
4K
Snippets
18
Records
21
Agent score
53%

What's inside Solid MotionOne

  1. Create an animation with the Motion component

    main

    The Motion component is the primary way to create animatable elements. You can use it as a wrapper for content, or use specific element types like <Motion.div>, <Motion.button>, or the generic <Motion tag="button"> to render specific HTML or SVG elements.

    import {Motion} from "solid-motionone"
    
    // As a wrapper
    function MyComponent() {
      return <Motion>Hello world</Motion>
    }
    
    // As a specific HTML element
    function App() {
      return (
        <Motion.div
          animate={{opacity: [0, 1]}}
          transition={{duration: 1, easing: "ease-in-out"}}
        />
      )
    }
    
    // Using the tag prop
    <Motion tag="button" />
  2. Handle exit animations with Presence

    main

    To animate elements as they are removed from the DOM (e.g., when using <Show>), wrap the conditional logic in a Presence component and use the exit prop on the Motion component. You can also provide a specific transition to the exit prop to override the main component's transition during the exit phase.

    import {createSignal, Show} from "solid-js"
    import {Motion, Presence} from "solid-motionone"
    
    function App() {
      const [isShown, setShow] = createSignal(true)
    
      return (
        <div>
          <Presence exitBeforeEnter>
            <Show when={isShown()}>
              <Motion
                initial={{opacity: 0, scale: 0.6}}
                animate={{opacity: 1, scale: 1}}
                exit={{opacity: 0, scale: 0.6}}
                transition={{duration: 0.3}}
              />
            </Show>
          </Presence>
          <button onClick={() => setShow(p => !p)}>Toggle</button>
        </div>
      )
    }
    
    // Exit with custom transition
    <Presence>
      <Show when={isShown()}>
        <Motion
          animate={{opacity: 1}}
          exit={{opacity: 0, transition: {duration: 0.8}}}
        />
      </Show>
    </Presence>
  3. Use reactive signals for animations

    main

    To make animations react to state changes, pass Solid signals directly into the animate object. Note that animate does not accept accessor functions; instead, place the signal call (e.g., bg()) directly in the object to leverage Solid's reactivity.

    const [bg, setBg] = createSignal("red")
    
    return (
      <Motion.button
        onClick={() => setBg("blue")}
        animate={{backgroundColor: bg()}}
        transition={{duration: 3}}
      >
        Click Me
      </Motion.button>
    )
  4. Create and bind motion state with createMotionState

    main
    The createMotion function internally utilizes createAndBindMotionState to establish the relationship between an element and its animation styles. For end-users, the primary way to interact with these primitives is through motion (the directive) or createMotion (the manual hook).
  5. Use the Motion.tag proxy pattern

    main

    Instead of using a generic component, solid-motionone provides a Motion proxy that lets you specify the HTML or SVG tag directly on the component name. This makes the code more semantic and easier to read.

    Commonly used tags include:

    • <Motion.div />
    • <Motion.span />
    • <Motion.button />
    • <Motion.svg />
    • <Motion.path />
    import { Motion } from 'solid-motionone';
    
    function MyComponent() {
      return (
        <Motion.section initial={{ y: 20 }} animate={{ y: 0 }}>
          <Motion.h1 animate={{ color: 'red' }}>Hello</Motion.h1>
        </Motion.section>
      );
    }
  6. Configure transition options

    main

    Use the transition prop to control the animation behavior. You can apply global transition settings to all properties, or override settings for specific properties by providing an object where keys match the property names in animate.

    // Global transition
    <Motion
      animate={{rotate: 90, backgroundColor: "yellow"}}
      transition={{duration: 1, easing: "ease-in-out"}}
    />
    
    // Per-value override
    <Motion
      animate={{rotate: 90, backgroundColor: "yellow"}}
      transition={{
        duration: 1,
        rotate: {duration: 2},
      }}
    />
  7. Define keyframes and offsets

    main

    You can define a sequence of values for a property by passing an array to the animate object. By default, these keyframes are spaced evenly across the duration. To control the timing of specific keyframes, use the offset property within the transition object for that specific property.

    // Evenly spaced keyframes
    <Motion animate={{x: [0, 100, 50]}} />
    
    // Keyframes with custom progress offsets
    <Motion 
      animate={{x: [0, 100, 50]}} 
      transition={{x: {offset: [0, 0.25, 1]}}} 
    />
  8. Use the motion directive in SolidJS

    main

    The motion directive is available for use within SolidJS elements. It accepts motionone.Options as its configuration, allowing you to apply animations directly via the use:motion syntax.

    // Using the motion directive on a standard element
    <div use:motion={{ animate: { opacity: 1 } }} />
  9. Animate HTML or SVG elements with the Motion component

    main

    The Motion proxy allows you to render animatable HTML or SVG elements by using tag names as properties (e.g., <Motion.div />, <Motion.span />, <Motion.svg />).

    Animation Props

    These props are reactive; changing them will trigger a transition to the new state.

    • animate: The target values to animate to. Accepts values and keyframes compatible with the Motion One animate function.
    • initial: The target values to animate from when the element is first rendered.
    • transition: Configures the type of animation (duration, easing, etc.).
    • exit: The target values to animate to when the element is removed. Note: For exit animations to work, the element must be a direct child of a <Presence> component.

    Interaction Props

    • inView: Animation target for when the element enters the viewport.
    • hover: Animation target for when the element is hovered.
    • press: Animation target for when the element is pressed.
    // Basic animation
    <Motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} />
    
    // Interaction animations
    <Motion.div hover={{ scale: 1.2 }} press={{ scale: 0.9 }} />
  10. Use the Presence component for enter/exit animations

    main

    The Presence component manages the lifecycle of child <Motion> components when they are added to or removed from the DOM (e.g., inside a <Show> or <For> component). It allows you to define initial, animate, and exit states that trigger automatically during these transitions.

    Props

    • initial (boolean, default: true): If set to false, the first animation on all child Motion elements will be disabled the first time Presence is rendered.
    • exitBeforeEnter (boolean, default: false): If true, Presence will wait for the exiting element to finish its exit animation before starting the animation for the next entering element (using out-in mode). If false, animations occur in parallel mode.
    <Presence exitBeforeEnter>
      <Show when={toggle()}>
        <Motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
        />
      </Show>
    </Presence>