Start on Foldkit
Wardic's binding for Foldkit is @wardic/foldkit: Motion's animations on Foldkit's views, as Effect. It's Effect-typed throughout, so it moves to a new Effect major only in a Wardic major, and it follows semver from 1.2.0. It works in a Foldkit app rendered on the server or not.
It takes Motion's props as attributes on w.* elements, which take Foldkit's attributes too. Where Motion takes callback props, such as onTap, Wardic sends a Message: w.OnTap(Message.Tapped()). On Solid, see Start.
Install
npm install @wardic/foldkit @wardic/dom effect foldkitpnpm add @wardic/foldkit @wardic/dom effect foldkityarn add @wardic/foldkit @wardic/dom effect foldkitbun add @wardic/foldkit @wardic/dom effect foldkiteffect and foldkit are peers: Wardic uses the copies your app has.
The Handle
A Foldkit app has one Handle, made once at module scope with Wardic.make. It takes the environment Wardic runs in, DomLive in a browser, and the app's Features. Its html(h) gives a view the builder w.
// src/lib/wardic.ts
import { DomLive } from "@wardic/dom"
import { Features, Wardic } from "@wardic/foldkit"
export const wardic = Wardic.make({ env: DomLive, features: Features.domAnimation })The first w.* element to mount builds Wardic's services on the Handle's env, shared until the last has left. Put w.Provider() on the topmost w.* element: it names the root the subtree options hang on, and the walk a server render starts from.
import type { Html, HtmlBuilder } from "foldkit/html"
import { wardic } from "~/lib/wardic.ts"
const view = (model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return w.div([w.Provider(), h.Class("app")], [page(model, h)])
}These guides import the Handle as ~/lib/wardic.ts, an alias for src; without one, import it by its path.
Your app's Features
What an element does beyond animating its values, a gesture, layout or a colour space, is a Feature, code an app ships only if it asks for it. A Foldkit app chooses its once, on the Handle: Features.domAnimation is hover, press, focus and in-view; Features.layout, Features.drag and Features.presence are named beside it when a page lays out, drags or plays exits.
export const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.layout, Features.presence] })Its elements take those Features' attributes: one of another Feature is a type error that names it. The Features can be a load too, features: () => import("./features.ts").then((m) => m.domMax), fetched by the first w.* element to mount; an element mounted before it arrives starts its gestures once it has. Shipping less has the rest.
Your first element
A w.* element takes Motion's props as attributes: it animates from w.Initial to w.Animate, and to its w.WhileHover target while the pointer is over it. The demo runs the Solid 2 form; the Foldkit tab is the same view on Foldkit's builder.
import { w } from "~/lib/wardic.ts"
/** A card that rises into place as it mounts, and lifts under the pointer. */
export default function Enter() {
return (
<w.div
class="sample-card"
initial={{ opacity: 0, y: 24 }}
animate={{ opacity: 1, y: 0 }}
whileHover={{ y: -4, scale: 1.02 }}
transition={{ type: "spring", stiffness: 260, damping: 22 }}
>
Hello, Wardic
</w.div>
)
}import { w } from "~/lib/wardic.ts"
/** A card that rises into place as it mounts, and lifts under the pointer. */
export default function Enter() {
return (
<w.div
class="sample-card"
initial={{ opacity: 0, y: 24 }}
animate={{ opacity: 1, y: 0 }}
whileHover={{ y: -4, scale: 1.02 }}
transition={{ type: "spring", stiffness: 260, damping: 22 }}
>
Hello, Wardic
</w.div>
)
}import type { Html, HtmlBuilder } from "foldkit/html"
import { wardic } from "~/lib/wardic.ts"
/** A card that rises into place as it mounts, and lifts under the pointer. */
export const view = <Message,>(h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return w.div(
[
h.Class("sample-card"),
w.Initial({ opacity: 0, y: 24 }),
w.Animate({ opacity: 1, y: 0 }),
w.WhileHover({ y: -4, scale: 1.02 }),
w.Transition({ type: "spring", stiffness: 260, damping: 22 }),
],
["Hello, Wardic"],
)
}import * as m from "motion/react-m"
/** A card that rises into place as it mounts, and lifts under the pointer. */
export default function Enter() {
return (
<m.div
className="sample-card"
initial={{ opacity: 0, y: 24 }}
animate={{ opacity: 1, y: 0 }}
whileHover={{ y: -4, scale: 1.02 }}
transition={{ type: "spring", stiffness: 260, damping: 22 }}
>
Hello, Wardic
</m.div>
)
}The same motion can go on an element w doesn't make, with the attributes bundle: h.div([...w.attributes({ initial, animate, transition })]).
A change is a Message: update returns the next Model, the view renders it, and an element whose w.Animate target changed Retargets in the patch, while one whose target is the same holds. Live values, a spring following the pointer or a scroll's progress, are Atoms the view binds with w.Style({ x: atom }), driven from a Mount on the element that reads them: see the Foldkit tabs of Live values and Scroll.
Rendered on the server
On a server-rendered page, an element arrives in the HTML already at w.Initial, so it never flashes at its target first. Under w.Provider(), its entrance plays from the first paint, before the JavaScript has loaded, and the client takes it over mid-flight when it hydrates. See Server rendering.
Foldkit's server rendering is for makeApplication programs. Build the client and the server from one build with @foldkit/vite-plugin, so both carry one build id; on the server, Foldkit's renderToString renders the program's view, every w.* element at its Starting style; on the client, wardic.hydrate takes Runtime.hydrate's place in the entry: it moves the entrance rules into the head before Foldkit adopts the page, and has the hydration render keep each running animation for the element's ward to take over, with no flicker.
// src/entry.ts, the client
import { wardic } from "~/lib/wardic.ts"
import { app } from "./app.ts"
wardic.hydrate(app)Runtime.hydrate called directly would find a <style> the view lacks and rebuild the Provider element's subtree, and strip each running animation back to its start.
Next
- Animate an element:
initial,animateandtransition, each sample with its Foldkit tab. - Foldkit reference: every module of
@wardic/foldkit, generated from its source. - Coming from Motion: every Motion API, and its name in Wardic.