createBatch

Animates many elements on the same keys as one Batch: each member's values, speeds and targets are kept together, and every frame steps them all in one run. Changing a thousand members' targets costs about a twentieth of changing them one at a time (measured), and each member turns toward its new target at the speed it already has.

import { createBatch, stagger } from "@wardic/solid" // or "@wardic/solid/v1"

const grid = createBatch({
  count: 100,
  animate: (i) => ({ x: (order()[i] % 10) * 48, y: Math.floor(order()[i] / 10) * 48 }),
  transition: { type: "spring", visualDuration: 0.4, bounce: 0.2, delay: stagger(0.005) },
})
<For each={cells}>{(i) => <div class="cell" {...grid.at(i)} />}</For>

Call it in a component under a WardicProvider: the component's cleanup stops it.

Server rendering

On a server a Batch animates nothing, and each spread member's element renders with its start, so a hydrated page doesn't jump:

  • With initial, its values, as the DOM draws them (transform: translateY(20px); opacity: 0), over the element's own style. Hydrated, the member animates from there to its targets.
  • With initial: false, or, keyed, under an <AnimatePresence initial={false}>'s first render, its targets. A server-rendered grid that should arrive in place takes initial: false.
  • With neither, nothing: it starts at each key's identity and animates to its targets once hydrated, as a Wardic element with no initial does.

Give the element's own style to its handle, grid.at(i, { style }): on a server the handle writes the start into style, and a style beside the spread would take its place, or be replaced by it. A keyed Batch reads its list once on the server; a handle for a key the list doesn't have renders no start. A handle given to ref alone, not spread, renders nothing of the Batch's. An SVG member's translate is right from the server; a rotate or scale turns about the SVG's origin until hydration, which turns it about its own centre.

Its entrance from the first paint

By default a Batch's entrance plays once the page's JavaScript has run. Give it appear: true and the server plays it from the first paint, as a Wardic element's entrance before the JavaScript: one CSS animation in the page's head for all its members, each member's own start and targets written on its element as custom properties, and its stagger() its own delay. As the page hydrates, the Batch takes each member over where its CSS has got to, at the speed it has there. With the JavaScript off, each member plays to its targets and stays there.

const grid = createBatch({
  appear: true,
  count: 1000,
  initial: { opacity: 0, scale: 0.8 },
  animate: (i) => ({ x: (i % 40) * 24, y: Math.floor(i / 40) * 24, opacity: 1, scale: 1 }),
  transition: { type: "spring", visualDuration: 0.4, delay: stagger(0.001) },
})
  • What plays: springs and tweens with a named ease. A Batch whose ease is a function waits for the JavaScript, as does a member whose own style sets an animation, which it keeps.
  • A Wardic element as a member with an entrance of its own: under a Batch with appear, the Batch's plays from the first paint and the element's waits for the JavaScript; without appear, the element's plays from the first paint, as any element's.
  • Its cost: a few KB of CSS for each set of transitions its members run, and on each member its animation and each value of its own that its members don't all share: about 85 bytes for a tile of that grid, before compression.
  • Reduced motion and skipAnimations apply as to an element's entrance, and the provider's appear={false} turns it off with the rest. With appear="explain", the server says why a Batch that asked waits.
  • Streamed content: a Batch under a Suspense (Loading on Solid 2) that arrives after the shell plays when a Wardic element in the same chunk does: that element brings the chunk's CSS. With none, its members wait for the JavaScript.
  • On SVG, give a member that turns or scales transform-box: fill-box; transform-origin: center in its own style, so it turns about its own centre from the first paint.
  • Thousands of members: taking them over reads every animation on the page once, which Chromium sorts by where each element sits, and ends that many CSS animations at once, which Chromium draws again. In Chromium, a grid of 1,000 members in one parent drops two frames as it hydrates, and one of 5,000 holds the page about 0.4 s. Rows of 50, each its own parent, cut the read from 170 ms to 6 ms at 5,000. For a Batch that large, leave appear off, or give it to the members on screen first.

Its config

  • count: how many members it has, read once.
  • animate(i): member i's targets. What it reads is tracked: when it changes, every member turns toward its new targets in one change. A member whose targets didn't change is left alone.
  • initial: where each member starts, values or a function of its index. Without it, a member starts at each key's identity (x: 0, scale: 1, opacity: 1) and animates to its targets as it mounts, as a Wardic element with no initial does. With initial: false, it starts at its targets.
  • transition: a Wardic element's: a spring or a tween, each key's own under its name, else the nearest WardicConfig's, else each key's Default transition. Reduced motion and skipAnimations apply as on any animation. Its delay is seconds, or stagger(), which counts only the members a change moves: delete one item of a long list and the few that shift start at once.
  • appear: true plays its entrance from the first paint of a server-rendered page (above). Off by default.

Its keys are the transforms and opacity (WardicBatchKey), as numbers: px for a move, degrees for a turn, unitless for a scale. They're the ones its first targets and starts give: a key a later animate gives is an error.

Keyed, for a list

For a list whose items come and go, give it the list, each, and each item's key: a property's name, or a function of the item.

const list = createBatch({
  each: () => todos(),
  key: "id",
  initial: { opacity: 0, y: -12 },
  animate: (todo, i) => ({ y: i * 48, opacity: 1 }),
  transition: { type: "spring", visualDuration: 0.3, delay: stagger(0.02) },
})
<For each={todos()}>{(todo) => <li {...list.member(todo.id)}>{todo.title}</li>}</For>
  • animate(item, i) and initial(item, i) take the item and its index in the list now. A change of the list, or of its order, turns every member once: reordered, each one heads for its new place at the speed it has.
  • A new item's member enters as its element arrives, from initial, and moves with the others that arrive in the same task. An item that leaves the list takes its member with it at once, unless it's under an <AnimatePresence>.
  • Under <AnimatePresence>, a leaving member plays its exit, its element kept until it lands, each one as its own does. exit is values with an optional transition, or (item, i, custom) => values, custom the presence's. Its delay: stagger() counts the members leaving together. An item back while it exits takes over where it was, at its speed, its old element gone at once. initial={false} on the presence starts its first render's members at their targets.
const list = createBatch({ each: () => todos(), key: "id", initial: { opacity: 0 }, animate: (_, i) => ({ y: i * 48, opacity: 1 }), exit: { opacity: 0, x: -24, transition: { duration: 0.2, delay: stagger(0.03) } } })
<AnimatePresence>
  <For each={todos()}>{(todo) => <li {...list.member(todo.id)}>{todo.title}</li>}</For>
</AnimatePresence>
  • list.member(key) is its handle, given { ref, style } as at(i)'s is. A handle for a key the list doesn't have yet is fine: its element enters once the list has it. An element whose key the list still doesn't have at the end of its task is reported, naming the key, and enters if the list gets it later.
  • Its keys are its first members' targets' and starts', and its exit's, so a list that starts empty makes its Batch when its first items come. Give keys: ["x", "y", "opacity"] to fix them up front, for a list whose later items use a key its first ones don't.
  • Two items with one key, and an item with no key, are errors naming them.

Its members

grid.at(i) is member i's handle: spread it onto its element. Give the element's own ref and style beside it, grid.at(i, { ref, style }): its style is kept, without the properties the Batch draws (transform and opacity).

A member hovers and presses (below), but has no variants, drag or layout of its own: for variants or a drag, make its element a Wardic element (one that can't have layout, below). An element inside an <svg> turns about its own centre, as an HTML one does.

Hover and press

whileHover and whileTap move a member while the pointer is over it, or while it's pressed, as a Wardic element's do. Each takes values with an optional transition, or a function of the member's index (keyed: its item and index), read as the gesture starts:

const grid = createBatch({
  count: 100,
  animate: (i) => ({ x: colX(i), y: rowY(i), scale: 1 }),
  whileHover: { scale: 1.08, transition: { duration: 0.15 } },
  whileTap: (i) => ({ scale: 0.95, y: rowY(i) + 2 }),
})
  • Each member's layers: whileTap over whileHover over animate. A gesture moves its member into its values on its own transition, else the Batch's, and back on the Batch's: a press that ends while the member's still hovered goes back to whileHover's values on its transition, as on a Wardic element. A key only a gesture gives goes back to where it was. A gesture never staggers, and a leaving member's exit isn't interrupted by one.
  • A change during a gesture moves the keys the gesture doesn't hold. A tile shuffled mid-hover heads for its new cell, still lifted, and drops back to its new scale as the hover ends.
  • Keys: an object's keys join the Batch's. A function's must be keys the Batch already has, else the hover or press is reported, naming the key.
  • Keyboard: with whileTap or a tap event, a member's element takes tabindex="0" unless it's focusable or has a tabindex already, and Enter and Space press it while it has focus.
  • Touch never hovers, as on a Wardic element.

Its events: on Solid 2, grid.next("tap") and grid.when("hoverStart"), with hoverEnd, pressStart and tapCancel, each with the member's element, i, item and point (WardicBatchPointInfo; a press's WardicBatchPressInfo has its input too). On Solid 1, onHoverStart, onHoverEnd, onTapStart, onTap and onTapCancel in its config, each (event, { point, i }), with item keyed, and a press's input:

import { createBatch } from "@wardic/solid"
import { createEffect } from "solid-js"

const grid = createBatch({ count: 100, animate: (i) => ({ x: colX(i), y: rowY(i) }), whileTap: { scale: 0.95 } })
createEffect(grid.when("tap"), ({ i }) => select(i))

A Batch listens for hover and presses from one source each, however many members it has. It starts once it has whileHover or whileTap, a gesture callback, or on Solid 2 a first next or when of a gesture's event. Its gesture code loads then, so an app whose Batches have no gestures ships none of it.

Its landings

On Solid 2, grid.settled() is an async value: pending while any member moves, then the landing, { moved }, how many members set off. grid.next("settled") is a promise of the next one, and grid.when("settled") every one, for an effect:

import { createBatch } from "@wardic/solid"
import { isPending } from "solid-js"

<p>{isPending(() => grid.settled()) ? "Moving…" : "Ready"}</p>

On Solid 1, onAnimationStart is called as members set off from rest, and onAnimationComplete(landing) once every one is at rest again.

A Wardic element as a member

A member's element can be a Wardic element, for a drag or a variant of its own: <w.div {...grid.at(i)} drag whileTap={{ scale: 0.95 }} />. The latest driver takes a key:

  • As its leg starts on a transform key, or as its drag starts, it takes the member's whole transform from the Batch, from where the member is and how fast it's moving. opacity goes on its own. A key it would leave where the Batch draws it stays the Batch's.
  • The Batch takes it back as its last leg on it ends, or its drag's release comes to rest, or at the Batch's own next change that moves it, and turns it toward its targets from there. A drag in progress keeps the transform through a change, and so does a transform key the Batch doesn't draw, such as a hover's rotate on an { x, y } Batch, until it's back at rest. To keep a dropped tile where it lands, move its target on drop.
  • A member's Wardic element can't have layout or layoutId, and an element can be a member of one Batch: either is an error.

Animating it from code

createAnimate's animate takes a Batch, or one member's handle, and returns the same playback controls it returns for an element: await it, pause it, seek it, change its speed, complete, cancel or stop it.

const [scope, animate] = createAnimate()
const shuffle = () => animate(grid, { x: (i) => colX(order[i]), y: (i) => rowY(order[i]) }, { type: "spring", delay: stagger(0.002) })
animate(grid, { opacity: [0, 1] })              // two stops: every member from 0, to 1
animate(grid, { y: (i) => [-2, rowY(i)] })      // two stops for each member
animate(grid.at(42), { scale: 1.4 })            // one member

Each key takes a number for every member, two stops (its first and its target), or a function of the member's index and the Batch's count that gives one or two. Its keys are the Batch's. A later change of a member's key, by the config's animate or another call, takes it: that member goes the later change's way, and the call it left doesn't finish, as a Wardic element's doesn't. A call that moves nothing finishes at once.

A member's element moves its member too. animate(tile, { x: 40, backgroundColor: "tomato" }) moves x in the Batch, as animate(grid.at(i), { x: 40 }) does, and backgroundColor as on any element, under one set of controls. A selector or a list of members moves them as one call over them, in the list's order, and its stagger() counts the members that move. Through an element, a Batch's key takes a number or two stops. A transform the Batch draws without having it as a key, such as rotate on a Batch of x and y, is an error: the Batch draws the element's whole transform.

A Batch can be a sequence's Segment too, [grid, { x: (i) => colX(order[i]) }, options]. Each member runs through its own stops in the Batch, and the sequence's controls drive it with the rest. There, a key takes a number, null, stops, or a function that gives them, and a Segment's stagger() counts its members.

Coming from Motion

Motion has no Batch: its animate(elements, …) runs each element on its own, as createAnimate does. A Batch is for the collections where that cost shows: hundreds or thousands of like elements moving at once.

Types

  • WardicBatchConfig: its config. WardicBatchValues: a member's values. WardicBatchTransition: its transition, its delay a stagger() too.
  • WardicKeyedBatchConfig: the keyed form's config. WardicBatchExit: its exit as values. WardicKeyedBatch: what the keyed form returns, its handles by key.
  • WardicBatch: what it returns. WardicBatchMember: a member's handle. WardicBatchMemberProps: the element's own props a handle takes.
  • WardicBatchLanding: a landing, { moved }. WardicBatchEvents: (Solid 2) its events, for next and when.
  • WardicBatchSubject: what animate takes for a Batch: a Batch, by index or keyed, or a member's handle. WardicBatchTarget: its targets there. WardicBatchStop: one stop, a number or a function of a member's index and the count, that gives a number.