Reorder
The hooks' API, a prerelease. This is
@wardic/solid/hooks, in@wardic/solid@next: npm'snexttag, built from thesolid-nextbranch. It may still change.
createReorder(list) is a list its visitor drags into a new order, or reorders from the keyboard, as one hook. It gives the list's group, spread on the list's Subject, and list.item(value), a behaviour rune for each item's row. The rows are the app's: plain createWards, or createWards' rows.
import { createReorder, createWards } from "@wardic/solid/hooks"
import { createSignal, For } from "solid-js"
const [items, setItems] = createSignal([{ name: "Linen" }, { name: "Wool" }])
const list = createReorder({ each: items, onReorder: setItems })
const rows = createWards(items, (row) => list.item(row.item), { whileDrag: { scale: 1.03 } })
<ul {...list}>
<For each={rows()}>{(row) => <li {...row}>{row.item.name}</li>}</For>
</ul>The list is the app's: each is its order, and onReorder is handed the new one as an item is dragged past another or moved by the keys. Set the list from it.
The list spreads as it is, a ref that carries its props already, as a ward does: its ref, and its style over overflow-anchor: none. To move the list's own Subject, give a ward of its own the list as a rune beside its other runes, createWard(fade, list), and spread that ward in the list's place.
The list
createReorder takes a ReorderSource:
| Key | |
|---|---|
each | The list, in its order: an accessor. Its items match by identity, or by key. |
onReorder | Handed the new order as an item is dragged past another, or moved by the keys: set the list from it. |
key | What makes two items one, where the app makes its list again (a refetch, a store's copy): an item made again keeps its place, its drag and its keyboard lift. Left out, items match by identity. Read once. Give the rows' createWards the same key. |
axis | The list's direction: "y", a column, "x", a row, or "xy", a grid that wraps. Left out, it's found from the items' boxes. |
keyboard | The keyboard reorder: on unless false. Off, the items have no keys, no tab stop and no instructions. |
announcements | What a screen reader hears at each step of a keyboard reorder, over the English defaults: lift, step, drop, cancel, and move for a move in code, each a function of the item's label, position and the list's count. |
instructions | What each item's aria-describedby reads, over the English default. |
style | The list's style, over overflow-anchor: none. |
initial | false: the rows made as the list mounts have no entrance, each item's behaviour giving it enter: false until the list's first settle, and on a server every one. createWards' rows take their own initial. |
Each option is read where the group reads it, so one given by a getter (a component's props) is followed as it changes; onReorder is read as it is at each new order, and key once.
Each item: list.item(value)
list.item(value) is a behaviour, a rune like any other: give it to the item's row, createWard(list.item(item), ...), or as a createWards row's rune. It gives its row:
layout: true, and adragalong the group's axis, read at each drag's start: a grid's"xy"is a free drag.lifted, true while a keyboard lift holds it. The row is raised over the rest while it's held, and until it lands.- The group's
touch-action: none for a grid's free drag, the other axis's panning for a column or a row. - A tab stop for the keyboard reorder while it's on, described by the group's instructions.
What it does, it does through ward.drag, which another library's reorder could listen to as well: nothing group-level is public.
Its members
What createReorder returns is a ReorderList: the group, with item beside it.
| Member | |
|---|---|
item(value) | The item's behaviour, a rune for its row. |
move(value, toIndex) | Moves value to toIndex, as a button of your own would, and announces it to a screen reader. |
axis() | The list's direction: axis, else the one found from the items' boxes. |
next(name) | A promise of the group's next name after the call. It rejects with an AbortError once the group is gone. |
each(name) | Every name from the call on, as an async iterable, for a for await: each step waits for the next one, so one that comes while a step's work runs is missed. It ends once the group is gone. |
when(name) | Every name, as a memo for an effect, as a ward's when is: pending until the first, so the effect runs at each one. |
keyboard() | Whether the items reorder from the keyboard. |
instructions() | The instructions the items' aria-describedby reads. |
group | The group's state: @wardic/core's Reorder.Group. |
Its events:
| Event | |
|---|---|
"reorderEnd" | Once at the end of a reorder that changed the order: a drag let go, or a keyboard lift dropped, with the order (values), the item that moved (value), and its index from and to. What an app saves. Not for a keyboard cancel, nor for move. |
"holdStart" | As an item is taken in hand, by a drag starting (past its threshold, not at the press) or a keyboard lift, with the item (value) and its index then (from). |
"holdEnd" | Once as an item in hand is let go, however: let go or dropped (how: "drop"), put back with Escape ("cancel"), or unmounted ("unmount"). After "reorderEnd" when it moved. |
To save the order once, at each reorder's end:
import { createReorder } from "@wardic/solid/hooks"
import { createEffect, createSignal } from "solid-js"
const [items, setItems] = createSignal(["Bowls", "Plates", "Cups"])
const list = createReorder({ each: items, onReorder: setItems })
createEffect(list.when("reorderEnd"), (end) => {
void save(end.values)
})Grips: createGrip(row)
A grip is what a user drags a row by, inside the row's Subject. createGrip(row) makes one, as a rune: a press on its Subject starts row's drag, and while one of row's grips is on the page, row drags from its grips alone and hears its keys from them. Where row's behaviour hears keys, each grip is a tab stop, described as the behaviour says. Touch still scrolls the page on the rest of the row. The grip's own motion is a ward of its own:
import { createGrip, createWard } from "@wardic/solid/hooks"
const row = createWard(list.item(value), lift)
const grip = createWard(createGrip(row), gripLook)
<li {...row}><button {...grip}>⋮⋮</button>{value}</li>rowis aDraggingWard: any ward one of whose runes gives adrag, a behaviour's as its function gives it, marked in its type alone. Given a ward that doesn't drag, development says so, and the grip drives nothing.- It goes inside its row's Subject: a press on it reaches the row's own drag, as any press inside the row does. Development says so of a grip outside its row: there, a press on it can't reach the row's drag, and while it's on the page the row can't be dragged.
- It writes the
touch-actionofrow's axis on its Subject, and nothing else: a cursor, aroleor a label are the app's. - It ends with the owner it's made under.
<Reorder.Grip>, from @wardic/solid, is a grip of the <Reorder.Item> it's inside, built on createGrip: a <button type="button"> by default, or the tag as names, its motion props a rune. See Reorder.
A behaviour of your own: ward.drag
A behaviour is a rune that drives its ward's drag: a function rune, createRune((ward) => …), that listens on ward.drag and gives the keys that respond to it, drag, layout and lifted, as any rune gives keys. list.item is one; an app can write its own. Nothing of Effect is in what it's handed, so another library's behaviour can stand where Wardic's Reorder does.
import { createRune } from "@wardic/solid/hooks"
const swipes = createRune((ward) => {
ward.drag.onSample((sample) => {
if (sample.phase === "end") onSwipe(sample.offset.x)
})
return { drag: "x" }
})ward.drag is a WardDrag:
| Member | |
|---|---|
onSample(fn) | Each sample of the ward's drag, as the drag delivers it, after its projection write: its phase ("start", "move", "end"), point, offset, delta and velocity. |
onKey(fn, options?) | Its keys, as core's KeyboardLikeEvent, inside their dispatch. Given onKey, its Subject is a tab stop (tabindex="0", an app's own winning), described by options.describedBy, in a server's render too. Focus a key's handling took is put back. |
onBlur(fn) | Focus leaving it, judged after the task's own work: one a key's handling put back isn't heard. |
onPress(fn) | A press on it, its primary pointer's down. |
measured() | Its box as its projection node measures it: a layout ward's. A Measured. |
Each listener ends with the owner it's made under. drag is on a function rune's ward alone: the ward createWard hands back has none. A rune that listens on a ward nothing gives a drag is told so in development, once.
onKey's options, KeyOptions:
| Option | |
|---|---|
describedBy | An accessor of the ids of what describes the ward to a screen reader, its instructions: its aria-describedby. An app's own ids come before them. |
tabStop | An accessor of whether its Subject is a tab stop now: true when left out. A Reorder list's with its keyboard off is none. |
measured()'s Measured:
| Member | |
|---|---|
box() | Its layout box, its place: undefined before it's measured. |
offset() | How far it's dragged from its place. |
version() | A count that rises each time its box is measured. |
Types
ReorderList<T>: whatcreateReorderreturns: the list's group, a rune as it is, withitem(value).ReorderSource<T>: what it takes, above.HookReorderOf:createReorder's type,<T>(source: ReorderSource<T>) => ReorderList<T>.DraggingWard: a ward that drags, whatcreateGriptakes: any ward one of whose runes gives adrag.WardDrag,KeyOptions,Measured:ward.drag,onKey's options andmeasured()'s box, above.
See also createWard, Runes, createWards and Scopes.