Reorder
<Reorder.Group> and <Reorder.Item> make a list the reader puts in order by dragging. Your app owns the order: the group takes it as values, and onReorder hands each new order back, as an item crosses another.
import { Reorder } from "@wardic/solid"
import { createSignal, For } from "solid-js"
/** A list to put in order: drag a row, or focus it and lift it with Space, then use the arrows. */
export default function Shelf() {
const [rows, setRows] = createSignal(["Bowls", "Plates", "Cups", "Jugs"])
return (
// An item brings its own drag and layout: no Feature to add.
<Reorder.Group class="sample-list sample-reorder" values={rows()} onReorder={setRows}>
<For each={rows()}>
{(row) => (
<Reorder.Item class="sample-row" value={row}>
{row}
</Reorder.Item>
)}
</For>
</Reorder.Group>
)
}import { Reorder } from "@wardic/solid/v1"
import { createSignal, For } from "solid-js"
/** A list to put in order: drag a row, or focus it and lift it with Space, then use the arrows. */
export default function Shelf() {
const [rows, setRows] = createSignal(["Bowls", "Plates", "Cups", "Jugs"])
return (
// An item brings its own drag and layout: no Feature to add.
<Reorder.Group class="sample-list sample-reorder" values={rows()} onReorder={setRows}>
<For each={rows()}>
{(row) => (
<Reorder.Item class="sample-row" value={row}>
{row}
</Reorder.Item>
)}
</For>
</Reorder.Group>
)
}import { DomLive } from "@wardic/dom"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { Features, Wardic } from "@wardic/foldkit"
// An item is dragged within its layout: the Handle names `drag` and `layout` among its Features.
const wardic = Wardic.make({ env: DomLive, features: [Features.drag, Features.layout] })
const Model = Schema.Struct({ rows: Schema.Array(Schema.String) })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Reorder: { rows: Schema.Array(Schema.String) } })
type Message = typeof Message.Type
export const init = (): Model => ({ rows: ["Bowls", "Plates", "Cups", "Jugs"] })
export const update = (_model: Model, message: Message) => Message.match(message, { Reorder: ({ rows }) => ({ model: { rows } }) })
/** A list to put in order: drag a row, or focus it and lift it with Space, then use the arrows. The order lives in the Model. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return w.ul(
[h.Class("sample-list sample-reorder"), w.ReorderGroup({ values: model.rows, onReorder: (rows) => Message.Reorder({ rows }) })],
model.rows.map((row) => w.keyed("li")(row, [h.Class("sample-row"), w.ReorderItem(row)], [row])),
)
}import { Reorder } from "motion/react"
import { useState } from "react"
/** A list to put in order, by dragging a row. */
export default function Shelf() {
const [rows, setRows] = useState(["Bowls", "Plates", "Cups", "Jugs"])
return (
<Reorder.Group axis="y" className="sample-list sample-reorder" values={rows} onReorder={setRows}>
{rows.map((row) => (
<Reorder.Item key={row} className="sample-row" value={row}>
{row}
</Reorder.Item>
))}
</Reorder.Group>
)
}From the keyboard
Every group has a keyboard reorder built in, which Motion's doesn't. Each item is a tab stop. Space or Enter lifts the focused item, the arrows move it one place a press (Home and End to the ends), Space or Enter drops it, and Escape puts it back. Each step is read out to a screen reader, from one live region the page shares.
The words are English by default. The group's announcements replaces any of them, and instructions the instructions read when an item takes focus. keyboard={false} turns it off.
Saving the order
onReorder is called at every step, as items cross. To save the order once, say to a server, use the moment a reorder ends with the order changed:
- On Solid 2, the group's
reorderEndevent, from the group a function asgroupRefis handed. - On Solid 1,
onReorderEnd={(end) => save(end.values)}on the group.
values can be a store's array too, on either group. Set it from onReorder as the store takes an array: on Solid 2, which takes it in place, setItems(() => values) for a root store, or setState((draft) => { draft.items.splice(0, draft.items.length, ...values) }) for a field; on Solid 1, setItems(values) for a root store, or setState("items", values) for a field. A signal holding the array is the cheaper of the two: the group reads every item at each step, and a store reads each one through its proxy.
Its props
axisis"y"(a column),"x"(a row) or"xy"(a grid that wraps). Left out, the group finds it from its items' boxes, as Motion's does; give it when you know it.aspicks the elements: the group is auland each item anliby default.- An item takes any motion prop but
dragandlayoutId, which it sets itself:whileDragfor the item in hand, say. Itslayoutistrueor"position".
The Features
An item brings its own drag, layout and gestures, so a list works under any provider.
Thousands of items: Reorder.BatchGroup
<Reorder.BatchGroup> and <Reorder.BatchItem> reorder a long list as one Batch. The group gives each item the place its index gives, from its size and the sizes of the items before it, so nothing is read from the page as an item crosses another: a list of thousands mounts and drags at a Batch's cost. Switch the demo to a grid to see one whose columns come from its width, or to rows of text, each as tall as its text.
import { Reorder } from "@wardic/solid"
import { createSignal, For } from "solid-js"
const ROWS: ReadonlyArray<string> = Array.from({ length: 1000 }, (_, i) => `Row ${i + 1}`)
/** A thousand rows to put in order, one Batch for them all: drag a row, or focus it and lift it with Space, then use the arrows. */
export default function LongList() {
const [rows, setRows] = createSignal(ROWS)
return (
// The list's scroll container: a row held near its edge scrolls it.
<div class="sample-long-list">
{/* A row's place comes from its index: one size and gap for every row, nothing measured. */}
<Reorder.BatchGroup class="sample-batch-list" values={rows()} onReorder={setRows} itemSize={{ width: 240, height: 36 }} gap={6}>
<For each={rows()}>
{(row) => (
<Reorder.BatchItem class="sample-batch-row" value={row}>
{row}
</Reorder.BatchItem>
)}
</For>
</Reorder.BatchGroup>
</div>
)
}import { Reorder } from "@wardic/solid/v1"
import { createSignal, For } from "solid-js"
const ROWS: ReadonlyArray<string> = Array.from({ length: 1000 }, (_, i) => `Row ${i + 1}`)
/** A thousand rows to put in order, one Batch for them all: drag a row, or focus it and lift it with Space, then use the arrows. */
export default function LongList() {
const [rows, setRows] = createSignal(ROWS)
return (
// The list's scroll container: a row held near its edge scrolls it.
<div class="sample-long-list">
{/* A row's place comes from its index: one size and gap for every row, nothing measured. */}
<Reorder.BatchGroup class="sample-batch-list" values={rows()} onReorder={setRows} itemSize={{ width: 240, height: 36 }} gap={6}>
<For each={rows()}>
{(row) => (
<Reorder.BatchItem class="sample-batch-row" value={row}>
{row}
</Reorder.BatchItem>
)}
</For>
</Reorder.BatchGroup>
</div>
)
}import { Reorder } from "motion/react"
import { useState } from "react"
const ROWS = Array.from({ length: 1000 }, (_, i) => `Row ${i + 1}`)
/** A thousand rows to put in order, by dragging a row. */
export default function LongList() {
const [rows, setRows] = useState(ROWS)
return (
// The list's scroll container: a row held near its edge scrolls it.
<div className="sample-long-list">
{/* Each row is its own element with a drag and a layout animation, all measured as a row crosses another. */}
<Reorder.Group axis="y" className="sample-batch-list sample-batch-motion" values={rows} onReorder={setRows}>
{rows.map((row) => (
<Reorder.Item key={row} className="sample-batch-row" value={row}>
{row}
</Reorder.Item>
))}
</Reorder.Group>
</div>
)
}-
Its shape is a few numbers.
axisis"y", a column (the default),"x", a row, or"xy", a grid, andgapis the space between places. -
Its items' sizes come one of three ways:
itemSize={{ width, height }}, every item one size. A grid'scolumnsis how many items a row holds: left out, as many as the group's width holds, read again as it's resized.itemSizea function of an item and its index, giving each item its own size, as a clip track below does. It's called for every item at each change of the list, and tracked: a size it reads that changes moves the items after it.itemSizeleft out: each item's size is read from the page, its own content's, as the demo's rows of text are. Until the sizes are read, on a server too, the items are laid out in flow, and then each is placed where flow had it, so nothing moves. A size that changes later, from an image loading or text wrapping again, moves the items after it. Space them withgap: a margin counts in flow but not in a place.
In a column a size can leave out
width, and each item is as wide as the group's content box. With sizes from a function or the page, a grid's rows break where the next item doesn't fit its width, each item at its row's top. -
It keeps what
Reorder.Groupgives:valuesandonReorder, the keyboard reorder and its announcements, auto-scroll near its scroll container's edges, the events at a reorder's end, right to left, and a server render with every item at its place. The DOM follows the order, so Tab and a screen reader read the list as it's shown. -
The motion is the group's props.
whileDrag,whileHoverandwhileTapare values, or a function of an item and its index;initialis those, orfalse, its keys moving to their identity as an item enters (initial: { opacity: 0 }fades it in), andappearplays that entrance from a server-rendered page's first paint;transitionis the items' moves to their places; andexit, for items leaving under a<Presence>(Solid 1's<AnimatePresence>) around the<For>, is values, or a function of an item, its last index and the presence'scustom. An item is a member of the group's Batch, not a Wardic element, and takes itsvalue,asand its element's own props. -
What it gives up: an item has no layout animation, variants or
layoutIdof its own. Its keys are a Batch's, the transforms andopacity(a Batch's limits). For items that need more, useReorder.Group. -
Motion's
Reorderhas no such mode: its tab is aReorder.Groupof 1,000Reorder.Items, each with its own drag and layout animation.
A clip track gives each clip its own width, a function of the clip, so nothing is read from the page and a server places every clip:
const [clips, setClips] = createSignal([
{ name: "Intro", seconds: 4 },
{ name: "Verse", seconds: 12 },
{ name: "Chorus", seconds: 8 },
])
<Reorder.BatchGroup values={clips()} onReorder={setClips} axis="x" itemSize={(clip) => ({ width: clip.seconds * 40, height: 56 })} gap={4}>
<For each={clips()}>{(clip) => <Reorder.BatchItem value={clip}>{clip.name}</Reorder.BatchItem>}</For>
</Reorder.BatchGroup>Every prop is on its reference.
In the storefront
The storefront's examples that show it, live and with their code:
- A shortlist, in your order: Drag a piece up or down the list, or move it with the keyboard: the others make room, and each row shows its place.
- Side by side, in the order you compare them: Three pieces in a row: drag one past another to compare them your way.
- A shelf arranged in a grid: Eight pieces on a shelf: drag one anywhere, and the rest shuffle along the rows to make room.
- A wishlist you rearrange by hand: Drag a piece up or down and the others make way; let go and the new order is saved. On Solid 2 the list is shared, and other people's moves glide in live. (on Foldkit)