createWard

The hooks' API, a prerelease. This is @wardic/solid/hooks, in @wardic/solid@next: npm's next tag, built from the solid-next branch. It may still change.

createWard(...runes) is one Subject's motion, made of runes: spread it on its Subject, the element it moves. Nothing is set up first, no provider and no list of Features: the page's Environment is the DOM's, and each ward brings the Features its own config uses.

import { createWard } from "@wardic/solid/hooks"

function Card() {
  const card = createWard({ initial: { opacity: 0 }, animate: { opacity: 1 }, whileHover: { y: -4 } })
  return <article {...card} />
}

Each rune is a config, or one made once with createRune, and they join into the one config the ward reads, a later rune's key over an earlier one's. Each is checked as a ward on the page takes it, a function rune as it is and a config key by key: a misspelt key, or one only a Stage's ward takes, is an error at the call that names it. See Runes.

In place of a spread, a rune's subject gives the ward its Subject: an element another renders, or an accessor of it. A rune that names its Subject's tag types the ward's spread for that element.

The main entry's ward(config) is left as it is: this createWard is a new function beside it.

Where a ward is made

A ward is made where its Subject is drawn: in a component's body, or a row's callback, beside the markup it's spread on. A ward is one Subject, so a hook that handed one out would fix which Subjects there are, and the line that says what moves would sit away from what it moves. So an app's hooks hold state and give runes: runes that close over that state, functions that make one for each Subject, and createRuneWith values. Never a ward.

A follower is written out where it's made, beside its leader. follow makes it take its leader's variant names for every value it doesn't set itself:

import { createSignal } from "solid-js"
import { createRune, createWard } from "@wardic/solid/hooks"

const turns = createRune({ variants: { open: { rotate: 180 }, closed: { rotate: 0 } } })

function Disclosure() {
  // `setState("open")` opens the panel, and turns the chevron with it.
  const [state, setState] = createSignal<"open" | "closed">("closed")
  const panel = createWard({ initial: "closed", animate: state })
  const chevron = createWard({ follow: panel }, turns)
  …
}

For a ward for each item of a list, see createWards; for wards with their Features given at once and the page's settings, makeWard.

Members

A ward createWard makes is a HookWard: a ward, with its reads, whether it's shown, its exits and "ready".

Member
hovering(), pressing(), dragging(), viewing(), focusing()Its gestures, as state. See Reads.
animating()Whether an animation of its own is on its way: isPending(() => card.settled()).
shown()Whether it's shown: its show, false from the moment it starts to leave. Always true for a ward with no show.
mounted()Whether its Subject belongs on the page: true while it's shown, and until its exit has landed. Always true for a ward with no show.
exiting()Whether it's leaving: true from the moment it starts to leave until it has gone, or is shown again. Always false for a ward no hook shows.
hold()Holds the leave under way until the release it hands back is called. See Holding a leave.
next(name), each(name), when(name)Its events: a ward's own (Ward events), its exits, and "ready".
read(key), atom(key), with(props), settled(), tapped()A ward's own: see Wards.

Reads

A ward's gestures and its motion, as state, on the ward itself:

import { createWard } from "@wardic/solid/hooks"

const card = createWard({ whileHover: { y: -4 } })
<article {...card}>{card.hovering() ? "Over" : "Away"}</article>
Readtrue while
hovering()The pointer is over its Subject. A touch never hovers.
pressing()A press is held on its Subject, a pointer's or the keyboard's, until its tap, its cancel, or a drag taking it over.
dragging()A drag is under way, from its start to its release.
viewing()Its Subject is in view, by its own viewport.
focusing()Its Subject has the focus.
animating()An animation of its own is on its way.

Each gesture's read is what that gesture's sensor reads given the ward (createHover(card) and the rest), reached from the ward. Nothing is made until one is first read: then it makes one signal, listens to the ward's own events, and asks for its gesture's Feature, so a ward reads hovering() with no whileHover. The accessor is kept, so const over = card.hovering is the same one read anywhere. On a server each is false.

Shown and leaving

A rune's show, an accessor, shows and hides the ward. Shown, it animates to animate from initial. Not shown, it animates to its exit (its initial, with none; exit: false leaves at once), then sets display: none. mounted() stays true until that exit has landed, and its followers', for a <Show> that removes the Subject instead:

import { createSignal, Show } from "solid-js"
import { createWard } from "@wardic/solid/hooks"

function Notice() {
  const [open, setOpen] = createSignal(true)
  const notice = createWard({ show: open, initial: { opacity: 0, y: 12 }, animate: { opacity: 1, y: 0 }, exit: { opacity: 0 } })
  return (
    <Show when={notice.mounted()}>
      <p {...notice} onClick={() => setOpen(false)}>
        Saved
      </p>
    </Show>
  )
}

shown() is for what a leaving ward's own content should do meanwhile. A rune's pop takes a show ward out of the flow as it starts to leave, so what's after it takes its place at once: see the page's keys.

A presence takes a ward itself: createPresence(ward, value) has the one ward leave and come back, on a new Subject for each value.

Exits

A ward a hook shows (by show, as a list's row, or a createPresence's) hears its own exits, beside its other events. It's made where its Subject's owner is, and outlives the Subject.

  • "exitStart": it started to leave.
  • "exitComplete": it has gone, its followers landed and the layoutId twins inside it flown home. An exit it turned round from doesn't complete; one with nowhere to leave to starts and completes at once.
import { createEffect, createSignal } from "solid-js"
import { createPresence, createWard } from "@wardic/solid/hooks"

const [open, setOpen] = createSignal(false)
const frame = createWard({ initial: "closed", animate: "open" })
const showing = createPresence(frame, open)
createEffect(frame.when("exitComplete"), () => {
  trigger.focus()
})

A ward made inside what a presence component takes away never hears its own: it ends as its exit starts. Those are the boundary's to tell. A Stage's ward isn't told them in this prerelease.

Holding a leave

hold() keeps the ward on the page, its exit landed or not, until the release it hands back is called; then it goes, and its exitComplete is told. It's for what isn't Wardic's to run: a save, a CSS transition, another library's exit (Motion's usePresence's safeToRemove).

import { createEffect, createSignal } from "solid-js"
import { createWard } from "@wardic/solid/hooks"

const [open, setOpen] = createSignal(true)
const card = createWard({ show: open, exit: { opacity: 0 } })
createEffect(card.when("exitStart"), () => {
  const release = card.hold()
  saveDraft().then(release)
})

A hold is the leave's it's taken in: one taken while the ward isn't leaving holds nothing, and once it's shown again, a release from before does nothing. Releasing twice is releasing once.

Features it brings

A ward asks for the Features its own config uses, read from its config as it's made. A gesture it only listens to (card.when("pan")) is asked for as the listener is made, and a read as it's first read.

FeatureAsked for by
hoverwhileHover; hovering(); a hoverStart, hover or hoverEnd listener
presswhileTap; pressing(); a pressStart, tap or tapCancel listener
focuswhileFocus; focusing()
inViewwhileInView; viewing(); a viewportEnter or viewportLeave listener
pana panSessionStart, panStart, pan or panEnd listener
dragdrag that's on: true, "x" or "y"
layoutlayout, layoutId, layoutScroll or layoutRoot
colorsA colour only that Feature reads (oklch(), oklab(), lab(), lch(), hwb(), a color() not in sRGB) in a target, style or a variant

Each Feature is a module of its own, fetched the first time a ward on the page asks for it and built as it arrives: the ward's hover, its drag or its layout starts from then on. A ward made before one has arrived does nothing of that Feature's until it has. A page ships the code of what it uses, with nothing to list. A server render has them all at once, and loads nothing.

Ready

"ready" is told once every Feature its own config uses is on the page, so its gestures, its drag and its layout work from then on:

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

const piece = createWard({ drag: true })
createEffect(piece.when("ready"), () => {
  setHint("Drag a piece to move it.")
})

It happens once, so it's kept, where a ward's other events are heard from the call on: asked for after it, next("ready") is resolved already, and an effect over when("ready") runs at once. A ward that asks for no Feature is ready as it's made. A load that fails is reported, and counts. Nothing is told on a server, and in this prerelease it isn't told for a createReorder's rows or on a Stage.

A page whose wards shouldn't wait gives its Features at once, in its own bundle, to a makeWard.

Types

  • HookWard: a ward made of runes: a ward, with mounted, shown, exiting, hold, its exits, "ready", and its reads.
  • HookWardOf: createWard's type, a makeWard's too: each rune checked as a ward on the page takes it. Its spread is typed for the element a rune's tag names, and a ward one of whose runes gives drag is one that drags by its type, which a grip (createGrip) takes.
  • WardReads: the six reads, by name.
  • ExitEvents: what a ward's exits resolve to, by name: exitStart and exitComplete, both undefined, a presence's two. ExitEvent, a name.
  • AutoFeature: a Feature a ward can bring for itself: "hover", "press", "focus", "inView", "pan", "drag", "layout" or "colors".