Sortable list

Pick a row up and drag it. It moves like fabric.

  1. Write the announcementMon
  2. Record the demo videoTue
  3. Update the pricing pageWed
  4. Prepare the press kitThu
  5. Schedule the social postsFri
  6. Brief the support teamFri

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/sortable.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/sortable.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { ClothSort } from "@/lib/fabricui/sortable-engine";
import "./sortable.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

export type SortableItem = {
  /** The row's text (also used in its grip's label). */
  title: string;
  /** The dim note at the row's end. */
  meta?: React.ReactNode;
};
components/fabricui/sortable.css
/*
 * Sortable list — drag to reorder; the picked-up row is cloth (sortable.js).
 *
 *   <div class="so-wrap">                      positioned: holds the cloth canvas
 *     <ol class="so-list">
 *       <li class="so-row">
 *         <button class="so-grip" aria-label="Move …"><svg class="icon">…</svg></button>
 *         <span class="so-title">…</span><span class="so-meta">…</span>
 *
 * Rows are 48px; their corners follow the roundness rule (40% of the height).
 */
.so-wrap:where(.fui-sortable, .fui-sortable *) { position: relative; }
.so-canvas:where(.fui-sortable, .fui-sortable *) { position: absolute; pointer-events: none; z-index: 2; }

.so-list:where(.fui-sortable, .fui-sortable *) {
  list-style: none;
lib/fabricui/sortable-engine.js
/*
 * sortable-engine.js — a drag-to-reorder list whose picked-up row is cloth
 * (see sortable.css). Needs cloth-drop.js (for the embedded font).
 *
 *   const list = new ClothSort(listEl, { onChange: order => … });
 *
 * Press a row and it lifts off the list as a piece of fabric, pinched where
 * you hold it. Drag it and it follows your hand, the far end trailing and
 * swinging with your speed, bending and rucking up when you change
 * direction. The other rows slide aside (plain motion). Let go and it lands
 * in its new place, flattens out and settles, and hands back to the real row.
 *
 * The cloth is a small simulated sheet (stretch, shear and bend constraints)
 * held to its own shape by a pull that's strong where you pinch it and weak
 * at the far end; that's what makes the end lag and swing. It's drawn in
 * WebGL with the row's real rendering as its picture (HTML + CSS through an

Usage

Usage
import { SortableList } from "@/components/fabricui/sortable";

const TASKS = [
  { title: "Write the announcement", meta: "Mon" },
  { title: "Record the demo video", meta: "Tue" },
  { title: "Update the pricing page", meta: "Wed" },
];

export function Launch() {
  return <SortableList items={TASKS} onOrderChange={items => save(items)} />;
}

Props

PropTypeDefault
items

The rows, in their starting order. Read when the list mounts; from then on it keeps its own order.

{ title: string; meta?: ReactNode }[]—
onOrderChange

Called with the items in their new order after a row is moved (by dragging its grip, or with the arrow keys).

(items) => void—
className

Added to the list.

string—