Runes
The hooks' API, a prerelease. This is
@wardic/solid/hooks, in@wardic/solid@next: npm'snexttag, built from thesolid-nextbranch. It may still change.
A ward is made of runes: createWard(...runes), each rune doing one job, a fade, a lift, a spring, a list's place. A rune is a config object, or createRune((ward) => config) for one that listens to the ward it's given to. The ward's config is its runes read together.
import { createRune, createWard } from "@wardic/solid/hooks"
const fadesIn = createRune({ initial: { opacity: 0 }, animate: { opacity: 1 } })
const lifts = createRune({ whileHover: { y: -4 } })
function Card() {
const card = createWard(fadesIn, lifts)
return <article {...card} />
}createRune(config)
Given a config, createRune gives back that config and nothing more, checked where it's written: its keys complete there, a misspelt one is an error there, and a literal needs no as const. It's how a rune kept in a constant is written, since nothing else checks one there. A rune written in a ward's own call is checked by that call, and a plain object is still a rune.
import { createRune } from "@wardic/solid/hooks"
const lift = createRune({ whileHover: { y: -4 } })
const spring = (stiffness: number) => createRune({ transition: { type: "spring", stiffness, damping: 28 } })A rune's type is the keys it has and no others, so one of Wardic's keys alone fits any ward that takes each key it has, a Stage's too. A key a ward doesn't take is an error at the createWard that's given it, which names the key.
A rune is made once, for what it describes: in a module if it depends on nothing, once in a hook if it depends on that hook's state, and by a function, once for each Subject, if it depends on which Subject it's on. Never in JSX or in a spread. Runes are what an app's hooks give, where the ward is the component's.
A rune is fixed once made. Its keys are the ones it has as it's made, and a plain value in it (a target, a transition, its variants) can't change: the object given is frozen, and setting, adding or deleting a key throws. In development the data inside it is frozen too. What changes is an accessor, a function or a getter, read where the ward reads it (animate: () => ({ x: x() })), or a store, whose keys are the ones it has as the rune is made. What depends on the ward itself is a function rune. What's under custom, layoutDependency and subject is the app's own: the key is fixed, and what's inside it isn't touched.
How runes join
A ward's runes join key by key, a later rune winning:
initial,animate,exit, thewhile*targets,transitionandstylemerge their keys when both are objects.variantsmerge by name, then key by key.- Anything else is the later rune's.
- But a
ref: every rune's runs, and none wins. - And
propsjoin name by name onto the ward's own spread, a later rune's winning a name, and a list of ids (aria-describedbyand its five fellows) joining.
A key is best given by one rune. A key several of a ward's runes give is merged across them each time it changes, both its accessors called and their objects merged, where one rune's is read as it is: a ward whose animate two runes give retargets several per cent slower for it.
Function runes
Given a function of its ward, createRune makes a rune that listens to the ward it's given to. The function is a hook's body: it's called once for each ward the rune is given to, as that ward is made and under its owner, and gives back that ward's config. So the rune is written once, and every ward that takes it has its own run.
import { createEffect } from "solid-js"
import { createRune } from "@wardic/solid/hooks"
const dragToClose = (close: () => void) =>
createRune((sheet) => {
createEffect(sheet.when("dragEnd"), (release) => {
if (release.offset.y > 120) close()
})
return { drag: "y", dragSnapToOrigin: true }
})A hook that reads or listens to its own ward gives a function rune, as dragToClose does, rather than making the ward itself.
The ward it's handed is a RuneWard: the ward, by its type too, whose reads and events are the finished ward's own, the two sharing one lifecycle. Its gestures and its motion (ward.hovering()); whether it's shown, on the page and leaving (ward.shown(), ward.mounted(), ward.exiting()); and its exits and its readiness (ward.when("exitComplete"), ward.next("ready")). See createWard's members.
- Read where it's followed: in a value (a
props', a target's accessor), a memo, an effect. One made in the rune's own body runs at once, before the ward is finished, and is still right: from its first read where the ward'sshowis a plain rune's, and once the page has settled where a presence takes the ward later, or itsshowis a function rune's. On a server nothing settles: there, a value read as it's rendered is right. - Read bare in the body, it's read once, and Solid says so, as it does of a signal read at the top of a component's body.
- Its Subject is handed to its
ref: the ward has no read of it.
A function rune's ward alone has drag: what it hears of its ward's drag, its samples, its keys, its blur and its press, and its box as measured. The ward createWard hands back has none. A function rune that drives its ward's drag that way is a behaviour, as a Reorder item's is: see Reorder.
Keys
A rune's keys are Wardic's, and its Environment's: Wardic's own say motion, whatever is moved; each Environment takes those its ward carries and adds its own, the page's here.
Wardic's keys
WardicKeys: what a rune says of motion, whatever it moves. A target's values are its Environment's, by their names: one of these keys fits a page's ward and a Stage's when every value it names is one both animate (x, opacity, scale). What a value means is its author's to know: a page's y is in pixels, a scene's in its own units.
| Key | |
|---|---|
initial, animate, exit, transition | Motion's: where it enters from, where it goes, where it leaves to, and how. |
whileHover, whileTap, whileFocus, whileInView, whileDrag | The gestures' targets. |
variants | Named targets, each a RuneVariant: a target that may carry its followers' timing (followers), or a function of custom. |
custom, inherit | As on Props: the value its function variants are handed, and false to keep a ward with variants out of its parent's names and stagger. |
follow | The ward this one follows: it takes that ward's variant names for every value it doesn't set itself. |
show | An accessor of whether it's shown: it enters from initial when true; when false it leaves by exit, then hides. See Shown and leaving. |
followers | Its followers' timing, for every variant that sets none of its own. See Followers. |
enter | false starts it at its animate, with no entrance, while its initial is still where it enters from later and where it leaves to. |
The ward a ward follows may name its states in place (animate: "open"), through a function (a signal of the names, or () => …, one name or several), or by a function rune's: a hook ward carries its variants whatever it sets. Following one that names no state at all, a ward with variants of its own can never be given one, which development says once for that leader; a ward with none may follow it only to leave with it.
The page's keys
PageKeys: the rest of a ward's config on the page, its style, its layout and layoutId, its viewport, appear, and its drag and how that's set (Props), with the page's own beside them:
| Key | |
|---|---|
subject | The Subject it moves, or an accessor of it: an element another renders, a router's page for its layout, in place of a spread. With none, its Subject comes by its ref, as a spread gives it. |
ref | What this rune does to its Subject beside moving it. See ref. |
props | What this rune sets on its Subject beside moving it: its attributes. See props. |
tag | The tag of its Subject ("circle", "path"). It types this rune's ref and props for that element, and the ward's spread; an SVG element's attribute keys (cx, d, pathLength) render as attributes, on a server and in its first frame. Not the other runes of the call, and a ward whose runes name none is an HTML element's. The last rune to give one wins. |
pop | A show ward popped as it leaves: taken out of the flow where it is (position: absolute, its size kept), so what's after it takes its place at once. true pins it by its top left; a corner's name (a PopCorner) pins it by that corner, for a container that resizes as it goes and keeps another corner still, as a stack in the bottom right does. |
lifted | Whether it's lifted, a behaviour's word, as a keyboard lift is: a layout ward is raised over the rest while it's lifted, and from then until it lands, and its whileDrag is on. |
reducedMotion | How it honours the visitor's reduced-motion setting: "user" follows it, "always" reduces its motion, "never" animates whatever it is. A value or an accessor. |
skipAnimations | Its animations land at their ends on the next frame: for end-to-end and visual-regression tests. A value or an accessor. |
transformPagePoint | Handed each point its drag or pan uses, in the viewport's coordinates, it gives the one the gesture uses. Called at each drag or pan start. |
reducedMotion, skipAnimations and transformPagePoint lay over the config around the ward, read as each of its animations starts; a config scope given to a ward gives them. dragListener and dragControls aren't the hooks' keys: a grip, createGrip(ward), is their way (Reorder).
ref
A rune's ref is a function handed its ward's Subject, for what the rune does to it that isn't motion: listening to it, giving it the focus, measuring it.
import { createRune } from "@wardic/solid/hooks"
const closesOnEscape = (close: () => void) =>
createRune({
ref: (subject) => {
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") close()
}
subject.addEventListener("keydown", onKey)
return () => subject.removeEventListener("keydown", onKey)
},
})- Handed its Subject, and nothing else, once it's on the page: there to be measured and focused. A ward's
refs begin once it has settled. - Its life is the time its Subject is on show (the ward's
mounted()). What it returns undoes it: called once ashowward's or a row's exit has landed, when the ward moves to another Subject, and when the ward ends. Shown again, it runs again. - Every rune's runs, in the order the runes were given, and each one's undoing in the reverse. None replaces another, nor a
refwritten in its Subject's markup. - It isn't tracked and owns nothing, as Solid's isn't. A signal read as it runs is read once and never followed: what's to follow a signal is an effect, made in a function rune's body.
A rune listens in its ref. A listener it adds with addEventListener hears a click, a key or a pointer's event before any handler written in the page's markup does, since Solid hears those from the root. So it takes an event with preventDefault(), which those handlers then see; never with stopPropagation(), which would keep the event from every one of them; and it leaves alone an event that began in a child which should keep it, a field's key say (event.target !== subject).
A ward with no show, whose Subject a <Show> of the app's own takes away, isn't told: Solid tells a ref nothing of a removal, so its undoing waits for the ward's next Subject, or its end. A ward made inside what a <Presence> takes away ends as its exit starts, so its ref is undone then, its Subject still leaving.
props
A rune's props is what it sets on its Subject beside moving it: on the page, its attributes, a role, a tabindex, an aria- state, a data- mark of the rune's own.
import { createRune } from "@wardic/solid/hooks"
// `data-on` is on the Subject while `on()` is true, and off while it's false.
const toggles = (on: () => boolean) => createRune({ props: { role: "switch", tabindex: 0, "data-on": on } })- Every element's attributes, and no others, each typed as Solid types it, with any
data-*. One only some elements have (disabled,href) says what an element is, which its markup says; a rune that names itstagtakes that element's (a link'shref). - Set through the ward's spread, beside its
refandstyle, so they're in a server's markup. A ward given its Subject bysubjectis never spread: the hooks write them on that Subject themselves, and put back what was there as the ward lets go. - A value may follow a signal: an accessor of it, read as the attribute is written.
falseandundefinedleave the attribute off.propsitself is a plain object. - On its Subject for as long as it's rendered, leaving included, where a
refis undone once an exit has landed. - Beside its Subject's own markup, Solid's rule holds: what's written after the spread wins, and what's written before it is replaced.
What's no prop is an error where it's written: a handler (onClick), as a rune listens in its ref; style and ref, a rune's own keys beside props; and class, as a rune marks a state with a data- attribute, which a stylesheet reads as it is.
Followers
followers, on a ward or inside one of its variants, is how a leader times its followers: read into transition's staggerChildren, delayChildren, staggerDirection and when.
| Key | |
|---|---|
stagger | Seconds between one follower's start and the next's. |
delay | Seconds before the first follower starts, or a stagger() function for each one's whole delay. |
from | Which follower starts first: "last" runs them back to front. |
order | Who goes first: "leader-first", "followers-first", or all "together" (the default). |
createRuneWith
createRuneWith(rune, api) is one value that's a rune where a ward is given it, and an API where an app calls it, as a utility of an app's own hands one back:
import { createSignal } from "solid-js"
import { createRuneWith, createWard } from "@wardic/solid/hooks"
const createCarousel = (count: number) => {
const [index, setIndex] = createSignal(0)
return createRuneWith(
{ get animate() { return { x: `${-index() * 100}%` } }, transition: { type: "spring", bounce: 0 } },
{ index, next: () => setIndex((i) => Math.min(i + 1, count - 1)), prev: () => setIndex((i) => Math.max(i - 1, 0)) },
)
}
const carousel = createCarousel(slides.length)
const track = createWard({ drag: "x" }, carousel)
<div {...track}>…</div>
<button onClick={carousel.next}>Next</button>- The rune is a config, checked where it's written as
createRune(config)'s is, or a behaviour (createRune(fn)). A config's keys are the ones it has ascreateRuneWithis called, and a ward reads each one live. A rune given to it is never changed. - The API's members are invisible to a ward and to a spread, and read-only.
- Refused: an API member named like one of the rune's keys, which would hide it from the ward (by its type, and in development); and, in development, a rune that carries an API already, and a bare function, which is no rune (a behaviour is
createRune((ward) => …)).
Types
Rune: a rune of a ward: a config, or a function rune.RuneConfig: a rune's config on the page: Wardic's keys, and the page's own.WardicKeys,PageKeys: Wardic's keys, which say motion whatever is moved, and the page's, above.FunctionRune: a rune made of a function of its ward,createRune(fn).RuneWard: the ward a function rune is handed: aHookWard, with itsdrag.RuneVariant: one variant of a rune: a target that may carry its followers' timing, or a function ofcustom.Followers:followers' timing, above.PopCorner: the corner a popped ward is pinned by:"top-left","top-right","bottom-left"or"bottom-right".RuneWith<R, Api>: whatcreateRuneWith(rune, api)gives back: the rune, and its API beside it.