Hold to delete
Hold to confirm: a cloth is drawn over the row, or across the button.
Installation
Terminal
npx shadcn@latest add https://fabrickit.org/r/hold-to-delete.jsonTerminal
pnpm dlx shadcn@latest add https://fabrickit.org/r/hold-to-delete.jsonTerminal
yarn shadcn@latest add https://fabrickit.org/r/hold-to-delete.jsonTerminal
bunx --bun shadcn@latest add https://fabrickit.org/r/hold-to-delete.jsonThis 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.
Step 1: Add the theme (once)
Every colour, radius and curve the components use, as --fabric-* variables. Save it as fabric.css next to your global CSS and import it there. If you've added another Fabric Kit component before, skip this.globals.css@import "./fabric.css";fabric.css/* * fabric.css — Fabric Kit's theme: every colour, radius, ring and curve the * components use, as --fabric-* variables (prefixed, so they never clash * with your own shadcn theme). Installed by `shadcn add @fabrickit/theme` * (it lands in your global CSS) — or import this file once, globally. * Dark by default. For light, put data-theme="light" on <html> (or on any * element: the tokens follow it down the tree; data-theme="dark" turns back). */ :root, [data-theme="dark"] { --fabric-page: #111111; --fabric-surface: #191919; /* triggers, recessed tiles */ --fabric-panel: #202020; /* open panels */ --fabric-raised: #2a2a2a; /* hover, range band */ --fabric-raised-hi: #343434; /* hover inside a band */ --fabric-preview: #252525; /* soft preview fills */ --fabric-text: #e0e0e0;Step 2: Copy the shared engines
Plain JavaScript that does the motion, shared between components. Create each file at the same path in your project; if a file is already there from another component, keep it.lib/fabricui/mount.js/* * mount.js — runs a Fabric Kit engine for a React component and cleans up * after it. The engines are plain JavaScript that wire themselves to the page * (listeners on the document, resize observers, a few elements of their own); * mount() records everything an engine attaches and changes while it's being * set up, and dispose() takes it all back, so a component can come and go (or * be set up twice in a row, as React's Strict Mode does) without leaving * anything behind. * * const m = mount(() => new Engine(root, options)); * m.instance; // the engine * m.dispose(); // on unmount: listeners and observers off, the DOM as it was, then instance.destroy() * // (and destroy() on anything it owns that registered itself, like each ClothDrop) */ export function mount(create) { const ac = new AbortController();lib/fabricui/cloth-sheet.js/* * cloth-sheet.js — a small sheet of sheer cloth, simulated and drawn on a * canvas. Shared by Hold to delete (the list row's cloth) and the Hold * button (its red fabric fill). * * const cloth = new ClothSheet(canvas, { tint, opacity }); * cloth.resize(W, H); // canvas size, css px * const S = cloth.build(box, offX); // a sheet laid over box {x,y,w,h}, shifted offX * cloth.step(dt, pins); // advance; pins: [[particle, x, y, z, weight], …] * cloth.draw({ alpha, clip, round }); // clip: a rounded box {x,y,w,h,r} it stays inside, or null; * // round: the sheet's own corner radius * cloth.clear(); * * The sheet is position-based cloth (stretch, shear and bend constraints) * lying on the surface under gravity, gripping it a little. It's drawn as * the library's sheer cloth: a fine knit you can see through (denser seenlib/fabricui/dither.js/* * dither.js — Fabric Kit's dither, in one place. Every dithered thing (cloth * fold shade, shimmer, weaves, chart fills) uses this 4×4 ordered (Bayer) * pattern, so dots line up and read as one texture across components. * * Dither.B4 the 4×4 matrix, 0–15 * Dither.T4 its thresholds, centred: (v + 0.5) / 16 * Dither.at(x, y) the threshold for cell (x, y) * Dither.GLSL the same pattern for shaders: bayer4(cell) → 0–1 */ const B4 = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5]; const T4 = B4.map(v => v / 16 + 1 / 32); export const Dither = { B4, T4,Step 3: Copy the Hold to delete files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/hold-to-delete.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { HoldDelete } from "@/lib/fabricui/holddelete"; import { HoldButton as HoldButtonEngine } from "@/lib/fabricui/holdbutton"; import "./hold-to-delete.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type HoldToDeleteItem = { /** The row's name (also used in "Deleted …" and the button's label). */ name: string; /** The dim line under the name. */ meta?: React.ReactNode; /** An icon (an `<svg className="icon">`) for the row's tile. */components/fabricui/hold-to-delete.css/* * Hold to delete — a list whose rows are deleted by holding their trash * button; a cloth is drawn over the row as you hold (holddelete.js). * * <div class="hd-card"> positioned; holds the cloth canvas * <ul class="hd-list"> * <li class="hd-row"> * <div class="hd-item"> icon, name, meta, <button class="hd-hold">… </div> * <div class="hd-gone"> Deleted … <button class="hd-undo">Undo</button> </div> */ .hd-card:where(.fui-hold-to-delete, .fui-hold-to-delete *) { position: relative; width: 400px; padding: 6px; background: var(--fabric-surface); border-radius: var(--fabric-radius);lib/fabricui/holddelete.js/* * holddelete.js — hold to delete, with cloth (see holddelete.css). * * new HoldDelete(card, { onDelete: row => …, onUndo: row => … }); * * Each row has a hold button (.hd-hold). Hold it and a sheer cloth is drawn * across the row from its right edge, pulled by its leading hem, as far as * you've held: the hold *is* the cloth. Let go early and it's drawn back off * the way it came. Hold to the end and the cloth lies over the row, gathers * it up (the row fading inside it) and is lifted away; the list closes up * after a few seconds, with an Undo in the row's place until then. * * The cloth is a ClothSheet (cloth-sheet.js): a small simulated sheet drawn * as the library's sheer knit, kept inside the row's rounded shape while it's * drawn over it. One canvas per card, one row at a time. *lib/fabricui/holdbutton.js/* * holdbutton.js — a "Hold to delete" button that a red cloth is drawn across. * Needs cloth-sheet.js. Styles in holddelete.css. * * <button class="hb" type="button" data-label="Hold to delete" data-done="Deleted"></button> * new HoldButton(el, { onConfirm: () => … }); * * Hold it and a red knit cloth is pulled in from the left by its leading * edge, as far as you've held. Let go early and it's drawn back out the way * it came. Hold to the end and the label turns to the done text, the button * gives a little squash, and after a moment the cloth slides on out to the * right and the button resets. The label is drawn twice, the second copy dark * and cut to where the cloth lies, so the text changes colour under it. * * Keyboard: hold Space or Enter. Reduced motion (or no WebGL): a plain fill. */Step 4: Check the import paths
The files import each other through the @/ alias (the shadcn default, there in a new Next.js app). If your project doesn't have it, add it to tsconfig.json, or change the imports to relative paths.tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }Step 5: Use it
Import it from @/components/fabricui/hold-to-delete. The Usage example below shows it in a page.
Code
components/fabricui/hold-to-delete.tsx
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { HoldDelete } from "@/lib/fabricui/holddelete";
import { HoldButton as HoldButtonEngine } from "@/lib/fabricui/holdbutton";
import "./hold-to-delete.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type HoldToDeleteItem = {
/** The row's name (also used in "Deleted …" and the button's label). */
name: string;
/** The dim line under the name. */
meta?: React.ReactNode;
/** An icon (an `<svg className="icon">`) for the row's tile. */components/fabricui/hold-to-delete.css
/*
* Hold to delete — a list whose rows are deleted by holding their trash
* button; a cloth is drawn over the row as you hold (holddelete.js).
*
* <div class="hd-card"> positioned; holds the cloth canvas
* <ul class="hd-list">
* <li class="hd-row">
* <div class="hd-item"> icon, name, meta, <button class="hd-hold">… </div>
* <div class="hd-gone"> Deleted … <button class="hd-undo">Undo</button> </div>
*/
.hd-card:where(.fui-hold-to-delete, .fui-hold-to-delete *) {
position: relative;
width: 400px;
padding: 6px;
background: var(--fabric-surface);
border-radius: var(--fabric-radius);lib/fabricui/holddelete.js
/*
* holddelete.js — hold to delete, with cloth (see holddelete.css).
*
* new HoldDelete(card, { onDelete: row => …, onUndo: row => … });
*
* Each row has a hold button (.hd-hold). Hold it and a sheer cloth is drawn
* across the row from its right edge, pulled by its leading hem, as far as
* you've held: the hold *is* the cloth. Let go early and it's drawn back off
* the way it came. Hold to the end and the cloth lies over the row, gathers
* it up (the row fading inside it) and is lifted away; the list closes up
* after a few seconds, with an Undo in the row's place until then.
*
* The cloth is a ClothSheet (cloth-sheet.js): a small simulated sheet drawn
* as the library's sheer knit, kept inside the row's rounded shape while it's
* drawn over it. One canvas per card, one row at a time.
*lib/fabricui/holdbutton.js
/*
* holdbutton.js — a "Hold to delete" button that a red cloth is drawn across.
* Needs cloth-sheet.js. Styles in holddelete.css.
*
* <button class="hb" type="button" data-label="Hold to delete" data-done="Deleted"></button>
* new HoldButton(el, { onConfirm: () => … });
*
* Hold it and a red knit cloth is pulled in from the left by its leading
* edge, as far as you've held. Let go early and it's drawn back out the way
* it came. Hold to the end and the label turns to the done text, the button
* gives a little squash, and after a moment the cloth slides on out to the
* right and the button resets. The label is drawn twice, the second copy dark
* and cut to where the cloth lies, so the text changes colour under it.
*
* Keyboard: hold Space or Enter. Reduced motion (or no WebGL): a plain fill.
*/Usage
Usage
import { HoldToDelete, HoldButton } from "@/components/fabricui/hold-to-delete";
const FILES = [
{ name: "Q4 report.pdf", meta: "2.4 MB · Edited 2h ago" },
{ name: "Brand assets.zip", meta: "18 MB · Edited yesterday" },
];
export function Files() {
return <HoldToDelete items={FILES} onDelete={file => removeFile(file.name)} onUndo={file => restoreFile(file.name)} />;
}
export function DeleteAccount() {
return <HoldButton label="Hold to delete" doneLabel="Deleted" onConfirm={deleteAccount} />;
}Props
| Prop | Type | Default |
|---|---|---|
| items The rows. Read when the list mounts; give it a new key to start over with other rows. | { name: string; meta?: ReactNode; icon?: ReactNode }[] | — |
| onDelete Called when a row's hold completes. The row shows "Deleted … Undo" until it closes up. | (item, index) => void | — |
| onUndo Called when a deleted row's Undo is pressed. | (item, index) => void | — |
| onEmpty Called once the last row has closed up. | () => void | — |
| emptyText Shown when every row is gone. | ReactNode | "All files deleted" |
| ref restore() puts every original row back, one after another. | { restore: () => void } | — |
| HoldButton A single button that fills with cloth while held. Props: label ("Hold to delete"), doneLabel ("Deleted"), onConfirm, and any <button> prop. | component | — |