Live values
A value that changes as the page is used, a pointer's place or a slider's, goes straight into an element's style. Where Motion needs a motion value, Wardic takes a signal: the element follows it, and nothing re-renders.
const [x, setX] = createSignal(0)
<w.div style={{ x }} />The create* primitives make values from values, each an accessor that style takes too. Here, two springs follow the pointer's place:
import { createSpring } from "@wardic/solid"
import { createWard, w } from "~/lib/wardic.ts"
import { createEffect, createSignal } from "solid-js"
/** A dot that follows the pointer: the pointer sets two signals, and two springs follow them. */
export default function Follow() {
const [x, setX] = createSignal(0)
const [y, setY] = createSignal(0)
const springX = createSpring(x, { stiffness: 300, damping: 20 })
const springY = createSpring(y, { stiffness: 300, damping: 20 })
// A ward that only listens: it hears the pointer over the field, where it comes in and at each move.
const field = createWard()
createEffect(field.when("hover"), ({ point, element }) => {
const box = element.getBoundingClientRect()
setX(point.x - box.left - 20)
setY(point.y - box.top - 20)
})
return (
<div class="sample-field" {...field}>
<w.div class="sample-dot" style={{ x: springX, y: springY }} />
</div>
)
}import { createSpring } from "@wardic/solid/v1"
import { w } from "~/lib/wardic.ts"
import { createSignal } from "solid-js"
/** A dot that follows the pointer: the pointer sets two signals, and two springs follow them. */
export default function Follow() {
const [x, setX] = createSignal(0)
const [y, setY] = createSignal(0)
const springX = createSpring(x, { stiffness: 300, damping: 20 })
const springY = createSpring(y, { stiffness: 300, damping: 20 })
const follow = (event: PointerEvent & { currentTarget: HTMLDivElement }) => {
const box = event.currentTarget.getBoundingClientRect()
setX(event.clientX - box.left - 20)
setY(event.clientY - box.top - 20)
}
return (
<div class="sample-field" onPointerMove={follow}>
<w.div class="sample-dot" style={{ x: springX, y: springY }} />
</div>
)
}import * as Effect from "effect/Effect"
import * as Atom from "effect/reactivity/Atom"
import * as AtomRegistry from "effect/reactivity/AtomRegistry"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import * as Mount from "foldkit/mount"
import { LiveValue, Wardic } from "@wardic/foldkit"
import { wardic } from "~/lib/wardic.ts"
// The Live values the view binds, Atoms at module scope: where the dot sits. The field's Mount feeds them.
const x = Atom.make(0)
const y = Atom.make(0)
const Model = Schema.Struct({})
type Model = typeof Model.Type
const Message = defineMessageUnion({ Following: {} })
type Message = typeof Message.Type
export const init = (): Model => ({})
export const update = (model: Model, message: Message) => Message.match(message, { Following: () => ({ model }) })
/** Each value of `from`, written into `into`, until the Scope ends. */
const feed = <A>(from: Atom.Atom<A>, into: Atom.Writable<A, A>) =>
Effect.gen(function* () {
const registry = yield* AtomRegistry.AtomRegistry
const stop = registry.subscribe(from, (value) => registry.set(into, value), { immediate: true })
yield* Effect.addFinalizer(() => Effect.sync(stop))
})
/**
* The field's Mount: the pointer over it as Live values (`Wardic.pointer`), set within the pointer's own
* events, and two springs that follow it, run on the Handle's services (`Wardic.provide`) while the field
* is on the page. Its Message tells `update` it has started; nothing listens.
*/
const Follow = Mount.define("Follow", {
messages: [Message.Following],
execute: ({ element }) =>
Effect.provide(
Wardic.provide(
Effect.gen(function* () {
const pointer = yield* Wardic.pointer(element)
const springX = yield* LiveValue.spring(Atom.map(pointer.x, (at) => at - 20), { stiffness: 300, damping: 20 })
const springY = yield* LiveValue.spring(Atom.map(pointer.y, (at) => at - 20), { stiffness: 300, damping: 20 })
yield* feed(springX, x)
yield* feed(springY, y)
return Message.Following()
}),
),
wardic.resources,
),
})
/** A dot that follows the pointer: the pointer sets two Atoms, and two springs follow them. */
export const view = (_model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return h.div([h.Class("sample-field"), h.OnMount(Follow())], [w.div([h.Class("sample-dot"), w.Style({ x, y })])])
}import { useMotionValue, useSpring } from "motion/react"
import * as m from "motion/react-m"
import type { PointerEvent } from "react"
/** A dot that follows the pointer: the pointer sets two motion values, and two springs follow them. */
export default function Follow() {
const x = useMotionValue(0)
const y = useMotionValue(0)
const springX = useSpring(x, { stiffness: 300, damping: 20 })
const springY = useSpring(y, { stiffness: 300, damping: 20 })
const follow = (event: PointerEvent<HTMLDivElement>) => {
const box = event.currentTarget.getBoundingClientRect()
x.set(event.clientX - box.left - 20)
y.set(event.clientY - box.top - 20)
}
return (
<div className="sample-field" onPointerMove={follow}>
<m.div className="sample-dot" style={{ x: springX, y: springY }} />
</div>
)
}From one value to another
createSpring(source, options)followssourceon a spring,stiffness,dampingandmass, or avisualDurationandbounce. A change mid-way keeps its speed.jump(value)puts it somewhere at once.createFollowValue(source, transition)follows on any transition: a tween, a spring or an inertia.createTransform(source, input, output)maps ranges: a source atinput[i]givesoutput[i], and blends between them, numbers, units and colours alike.createTransform(source, fn)maps through a function.createVelocity(source)is how fastsourceis changing, in its units a second. A velocity is a value too, socreateVelocity(createVelocity(x))is its acceleration.
const opacity = createTransform(x, [-150, 0, 150], [0, 1, 0])
const background = createTransform(x, [-150, 150], ["#bf616a", "#a3be8c"])From time
createTime()is the milliseconds since it was made, for something that turns forever:createTransform(createTime(), (ms) => (ms / 1000) * 90)is 90° a second.createAnimationFrame((time, delta) => …)calls you every frame, for drawing that isn't an element's style.
Where an element is
A value you give is never written to: it's what the element follows. What Wardic draws, mid-animation, comes from the element's ward, as an accessor of the value it's drawn at, to drive another value from a drag, say:
- On Solid 2,
card.read("x"), fromconst card = createWard({ drag: "x" })spread onto the element. - On Solid 1,
card.value("x"), fromconst card = w.createWard({ drag: "x" })spread onto the element.
import { createTransform, domAnimation, drag } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
// This page drags: its own Wardic instance has `drag` beside `domAnimation`.
const { createWard, w } = makeWard({ features: [domAnimation, drag], elements })
const card = createWard({ drag: "x" })
const opacity = createTransform(card.read("x"), [-150, 0, 150], [0, 1, 0])
<div {...card} />
<w.p style={{ opacity }}>Swipe to dismiss</w.p>In Wardic's frame
createFrameEffect(compute, apply) runs your own code in Wardic's frame, for drawing what isn't an element's style. Its compute reads values, a ward's read("x") among them. Its apply runs after Wardic has written the frame's styles and before the frame is painted. Here, a canvas draws a line between two balls as you drag them:
import { createFrameEffect, domAnimation, drag } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
// This page drags: its own Wardic instance brings `drag`, so pages that don't never load it.
const { createWard } = makeWard({ features: [domAnimation, drag], elements })
/** Two balls to drag, joined by a line a canvas draws in Wardic's frame, from where each ball is drawn. */
export default function FrameEffect() {
let arena!: HTMLDivElement
let canvas!: HTMLCanvasElement
let left!: HTMLElement
let right!: HTMLElement
// Each ball dragged inside the arena; its rune's `ref` hands over its element, for the line's ends.
const dragged = { drag: true, dragConstraints: () => arena, dragElastic: 0.2, whileDrag: { scale: 1.15 } } as const
const a = createWard(dragged, {
ref: (ball) => {
left = ball
},
})
const b = createWard(dragged, {
ref: (ball) => {
right = ball
},
})
const [ax, ay, bx, by] = [a.read("x"), a.read("y"), b.read("x"), b.read("y")]
// After Wardic has moved the balls for the frame, before it's painted: the line is never a frame behind.
createFrameEffect(
() => [ax(), ay(), bx(), by()],
([x1, y1, x2, y2]) => {
canvas.width = arena.clientWidth
canvas.height = arena.clientHeight
const context = canvas.getContext("2d")!
context.strokeStyle = getComputedStyle(canvas).color
context.lineWidth = 3
context.setLineDash([8, 6])
context.beginPath()
context.moveTo(left.offsetLeft + left.offsetWidth / 2 + x1!, left.offsetTop + left.offsetHeight / 2 + y1!)
context.lineTo(right.offsetLeft + right.offsetWidth / 2 + x2!, right.offsetTop + right.offsetHeight / 2 + y2!)
context.stroke()
},
)
return (
<div class="sample-arena" ref={arena}>
<canvas class="sample-canvas" ref={canvas} />
<div class="sample-ball" {...a} />
<div class="sample-ball sample-ball-alt" {...b} />
</div>
)
}Its apply runs again only once what the compute read has changed, and every frame with createTime() read in the compute. On the page, Solid's own createEffect reading the same values also runs before paint. In a scene's loop, which draws right after Wardic's writes, only createFrameEffect lands in the frame being drawn. Solid 2 only: see createFrameEffect.
Where they live
Make them in a component's body, where you'd make a signal: each one lives as long as its owner. On the server, each is its source's value at that moment. Scroll values are live values too.
In the storefront
The storefront's examples that show it, live and with their code:
- A closer look at the glaze: Hover over the vase and a lens follows your pointer, gliding after it and settling where you stop. (on Foldkit)
- Before and after the kiln: Drag the handle across a piece to see it before its glaze and after the kiln; the arrow keys move it too.
- An announcement bar that leans with your scroll: The studio's news runs across the page; scroll, and the bar leans into it and runs faster, backwards as you scroll up.
- Cards that catch the light: Move over a piece and its card tilts toward your pointer, with a soft light under it that follows. (on Foldkit)
- Announcements that take turns: The strip above the shop shows one message at a time, a bar filling under it, and holds while you read it or look away. (on Foldkit)