Sequences as actions
createAnimate's animate returns controls that are a promise of the animation's landing, so a Solid 2 action can yield them. Each step waits for the one before it to land:
actions/solid2.tsx
import { createAnimate } from "@wardic/solid"
import { action, createOptimistic, createSignal, Show } from "solid-js"
/** A box that opens in two steps, an action yielding each animation: the lid swings aside, then the gem rises. */
export default function Actions() {
const [scope, animate] = createAnimate()
const [open, setOpen] = createSignal(false)
// Shown at once, and put back as the action ends: what the button says while it runs.
const [busy, setBusy] = createOptimistic(false)
const toggle = action(function* () {
setBusy(true)
const opening = !open()
// Held with the action: the label below changes once its last animation has landed.
setOpen(opening)
if (opening) {
yield animate(".sample-lid", { x: -56, y: -10, rotate: -24 }, { type: "spring", bounce: 0.4, visualDuration: 0.4 })
yield animate(".sample-gem", { y: -44, opacity: 1 }, { duration: 0.35 })
return
}
yield animate(".sample-gem", { y: 0, opacity: 0 }, { duration: 0.2 })
yield animate(".sample-lid", { x: 0, y: 0, rotate: 0 }, { type: "spring", bounce: 0, visualDuration: 0.3 })
})
return (
<div class="sample-stack" ref={scope}>
<div class="sample-chest">
<div class="sample-gem" />
<div class="sample-lid" />
<div class="sample-chest-base" />
</div>
<button class="sample-button" type="button" disabled={busy()} onClick={() => toggle()}>
<Show when={busy()} fallback={<Show when={open()} fallback="Open the box">Close the box</Show>}>
Moving…
</Show>
</button>
<p class="sample-note">The box is {open() ? "open" : "closed"}.</p>
</div>
)
}What the action holds
An action is a transition: Solid holds the writes it makes until it's done.
- A write the action makes (
setOpen(opening)) shows once its last animation has landed, however early it's made. The box's label changes as the gem settles. - An optimistic flag,
createOptimistic(false)set totrueas the action starts, shows at once and goes back as the action ends: what the button says while it runs.isPendingdoesn't report it, since it reports only whataffects()declares. - Each animation starts as
animateis called and plays on its own. The action waits only where it yields.
Steps or a sequence
A sequence, animate([...]), lays steps out in time and plays them as one animation: it can overlap them (at: "-0.1"), and seek and pause them all. An action's steps run one after another with your own code between them, so a step can wait for a server too:
import { createAnimate } from "@wardic/solid"
import { action, createOptimistic } from "solid-js"
declare const saveDraft: (text: string) => Promise<void>
export function Editor() {
const [scope, animate] = createAnimate()
const [saving, setSaving] = createOptimistic(false)
const save = action(function* (text: string) {
setSaving(true)
yield animate(".draft", { opacity: 0.5 })
yield saveDraft(text)
yield animate(".draft", { opacity: 1 })
})
return (
<form ref={scope} onSubmit={(event) => (event.preventDefault(), save("…"))}>
<textarea class="draft" />
<button disabled={saving()}>Save</button>
</form>
)
}Solid 1 has no actions: an async function awaits each step the same way, with signals of its own for the busy state.
In the storefront
The storefront's examples that show it, live and with their code:
- Checkout, step by step: Place the order: the cart's lines leave, the store takes the order, then its parcel arrives. Set the store to fail the next request, and the lines come back.