Sheet

A side panel pulled in like fabric.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/sheet.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/sheet.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Sheet as Engine } from "@/lib/fabricui/sheet-engine";
import { Button } from "@/components/fabricui/button";
import "./sheet.css";

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

/** Why it closed: "save" (the form was submitted), the data-close of the button that closed it, or "dismiss" (Escape, the backdrop, ✕). */
export type SheetCloseReason = "save" | "cancel" | "dismiss" | (string & {});

export type SheetProps = {
  /** The button that opens it (one element, rendered as it is). */
  trigger: React.ReactElement;
components/fabricui/sheet.css
/*
 * sheet.css — Sheet (sheet.js). Moved out of sheet.html.
 */
/*
 * Sheet. A side panel, inset from the frame's edge. It's pulled in from
 * the right like a length of fabric (cloth-drop.js slide(): the leading
 * edge leads, the rest follows a beat behind, a wave runs back across it
 * as it lands) and pushed back out plainly on close.
 */


/* ---------- Backdrop ---------- */
.sheet-backdrop:where(.fui-sheet, .fui-sheet *) {
  position: absolute;
  inset: 0;
  z-index: 20;
lib/fabricui/sheet-engine.js
/*
 * sheet-engine.js — Sheet (sheet.css). From sheet.js.
 */
import { ClothDrop } from "./cloth-drop";

/**
 * Sheet: modal side panel. Opens from its trigger; focus moves into the
 * sheet and stays there (Tab wraps); Escape, the backdrop, ✕ or Cancel
 * close it; focus goes back to the trigger. Pulled in as cloth, pushed out
 * plainly.
 */
export class Sheet {
  /**
   * @param {HTMLElement} root  the .sheet
   * @param {{ trigger: HTMLElement, backdrop: HTMLElement, onClose?: (reason: string) => void }} options
   */

Usage

Usage
import { Sheet } from "@/components/fabricui/sheet";
import { Button } from "@/components/fabricui/button";
import { Field, FieldLabel, Input } from "@/components/fabricui/input";

export function EditProfile() {
  return (
    <Sheet
      trigger={<Button variant="primary">Edit profile</Button>}
      title="Edit profile"
      description="Changes show up for everyone in Acme Studio."
      onSave={data => save(Object.fromEntries(data))}
    >
      <Field><FieldLabel>Name</FieldLabel><Input name="name" defaultValue="Mira Chen" /></Field>
      <Field><FieldLabel>Email</FieldLabel><Input name="email" type="email" /></Field>
    </Sheet>
  );
}

Props

PropTypeDefault
trigger

The button that opens it, rendered as it is.

ReactElement—
title

The heading.

ReactNode—
description

The line under it.

ReactNode—
children

The fields. They sit in a <form>: submitting it saves and closes.

ReactNode—
footer

The buttons at the bottom. data-close="cancel" closes; type="submit" saves. Cancel and "Save changes" if left out.

ReactNode—
onSave

Called with the form's data when it's submitted.

(data: FormData) => void—
onClose

Called when it closes: "save", "cancel" or "dismiss" (Escape, the backdrop, ✕).

(reason) => void—
open

Open or closed, if you control it.

boolean—
onOpenChange

Called when it opens or closes.

(open: boolean) => void—