Hold to delete

Hold to confirm: a cloth is drawn over the row, or across the button.

  • Q4 report.pdf2.4 MB · Edited 2h ago
    Deleted Q4 report.pdf
  • Moodboard.png6.1 MB · Edited yesterday
    Deleted Moodboard.png
  • Brand assets.zip48 MB · Edited Mon
    Deleted Brand assets.zip
  • Meeting notes.txt12 KB · Edited Nov 24
    Deleted Meeting notes.txt

All files deleted

Let go early to keep it

Let go early to cancel

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/hold-to-delete.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/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

PropTypeDefault
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—