createWards
The hooks' API, from
@wardic/solid/hooks: a prerelease, on npm'snexttag, built from thesolid-nextbranch. It may still change.
A ward for each item of a list. The wards are made by the hook, not by the <For> that renders them, so a ward outlives its item's place on the page: an item that leaves the list stays in what createWards returns, at its index, until its ward's exit has landed, and one that comes back first carries on from where it is. Like createWard, it needs nothing set up.
import { createWards } from "@wardic/solid/hooks"
function Posts(props: { posts: ReadonlyArray<{ title: string }> }) {
const rows = createWards(() => props.posts, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } })
return <For each={rows()}>{(row) => <li {...row}>{row.item.title}</li>}</For>
}createWards(list, ...runes) is made where the list is drawn, as a ward is, from runes made once.
Rows
Each row's ward is createWard({ show }, ...runes), its show whether its item is there: it enters from its initial, and leaves by its exit, as any show ward does. Spread it on the row's Subject. Beside a ward's own members, it has:
| Member | |
|---|---|
item | The row's item: the latest under its key, and its last once it has left the list. Reactive. |
index() | Its place among the rows rendered, leaving ones counted. |
- A function among the runes is called for each row, with its
itemandindex, and gives that row's rune. - A row that pops (
popin its runes) is measured before the change that takes it out reaches the page, with every other row leaving then, so each is pinned where it was however many leave together. Seepopin Runes. - Its runes are checked as a ward's are: a key its ward doesn't take is an error that names it, in a rune and in what a row's function gives, and so is a value its ward doesn't animate.
import { createRune, createWards } from "@wardic/solid/hooks"
const fades = createRune({ initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } })
const toasts = createWards(() => props.toasts, fades, { pop: "bottom-right" }, (row) => ({ transition: { delay: row.index() * 0.05 } }))The list
The first argument is the list: an accessor, or an object that says how its items match.
| Key | |
|---|---|
each | The list, an accessor. |
key | What an item is matched by, when the list's items are made again (a refetch). Left out, the item itself, as <For> matches. |
initial | false: the rows of the first list have no entrance. What comes later enters from its initial. |
wait | true: while a row is leaving, a new item waits off the page, and comes once the last has gone: a set swapped for another, the old rows leaving before the new ones enter. Left out, a new item enters at once, beside the rows leaving. |
affectsLayout | false: a layout ward beside the list moves at once as its rows come, pop and go, where it would move into the space by animation. Left out, or true, it animates. |
import { createWards } from "@wardic/solid/hooks"
const rows = createWards({ each: () => props.todos, key: (todo: { id: string }) => todo.id, initial: false }, fades)One value
{ value } in place of each: the list of what's on the page for one value, the value now and the one it took the place of, until that one has gone. Both are there at once, as a list's rows are. A value of undefined, null or false is nothing on the page. It takes key, initial: false and affectsLayout as a list does.
import { createWards } from "@wardic/solid/hooks"
const heroes = createWards({ value: piece, key: (shown: { id: string }) => shown.id, initial: false }, slides, { pop: true })Where the new value should wait for the last one to go, one thing at a time, it's createPresence(ward, value).
Leaving
What createWards returns is an accessor of its rows, with the list's own exits beside it, in a ward's words for its own:
| Member | |
|---|---|
next(name) | A promise of the next name, from the call on. |
when(name) | Every name, as a memo for an effect: pending until the first. |
"exitStart": something starts to leave, with nothing leaving before."exitComplete": the last of what was leaving has gone, rows that left together counted as one leave.
A row hears its own beside it, through its ward or a function rune.
import { createWards } from "@wardic/solid/hooks"
const rows = createWards(items, fade)
createEffect(rows.when("exitComplete"), () => restoreFocus())Types
Row<T>: what a row's function is handed: itsitem, and itsindex().RowWard<T>: one row's ward: a ward, with its row'sitemandindex().RowRune<T>: a rune for every row, or a function of each row that gives its rune, before any check: what a hook of an app's own takes to pass on to a list.WardsSource<T>: a list, with how its items match:{ each, key, initial, wait }.WardsValue<T>: one value, as the list of what's on the page for it:{ value, key, initial }.AffectsLayout:{ affectsLayout }, which a list, a value and a presence take beside their own keys.Leaving:nextandwhen, the exits of a list or of a presence.HookWardsOf:createWards's type, its rows' runes checked as a ward's are on the page. AmakeWard'screateWardsis one.