Calendar

A range picker that drops open like fabric.

Installation

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

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

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

export type DateRange = { start: Date; end: Date };

/** A quick pick in the panel (the Date picker's Today / Tomorrow / Next week). */
export type CalendarPreset = { label: string; /** days from today */ days: number };

type CommonProps = {
  /** What the trigger says while no date is picked (single mode). */
components/fabricui/calendar.css
/*
 * Calendar — the date / range picker that drops open like fabric. Shared by
 * the Calendar (range) and Date picker (single date) pages; calendar.js.
 */

/* ---------- Dropdown shell ---------- */
.cal:where(.fui-calendar, .fui-calendar *) {
  position: relative;
  width: 299px;
  height: 38px; /* the slot keeps its closed height; the panel overlays below */
}
.cal__panel:where(.fui-calendar, .fui-calendar *) {
  position: absolute;
  inset: 0 0 auto 0;
  z-index: 10;
}
lib/fabricui/calendar-engine.js
/*
 * calendar-engine.js — the calendar that drops open like fabric (calendar.css).
 * Needs cloth-drop.js. Shared by the Calendar and Date picker pages.
 *
 *   new CalendarDropdown(root, { start, end, onChange })            // a range
 *   new CalendarDropdown(root, { mode: "single", start, onChange })  // one date
 *
 * Range: first click sets the start, second click sets the end and closes.
 * Clicking a day before the start restarts the range from that day.
 * Closing mid-pick (Esc / outside click) restores the last full range.
 *
 * Single: a click picks the date and closes. `start` may be left out (no
 * date yet: the trigger shows `placeholder`). Buttons in the panel with
 * data-preset="<days from today>" pick that day (Today 0, Tomorrow 1, …).
 *
 * (Fabric Kit: components/fabricui/calendar.tsx renders the markup and starts it.)

Usage

Usage
"use client";

import * as React from "react";
import { Calendar, type DateRange } from "@/components/fabricui/calendar";

export function TripDates() {
  const [range, setRange] = React.useState<DateRange>(() => {
    const t = new Date();
    return { start: t, end: new Date(t.getFullYear(), t.getMonth(), t.getDate() + 4) };
  });
  return <Calendar value={range} onValueChange={setRange} />;
}

Props

PropTypeDefault
mode

range: the first click sets the start, the second the end, then it closes. single: one click picks a date.

"range" | "single""range"
value

The range ({ start, end }), or in single mode the date, if you control it.

DateRange | Date | null—
defaultValue

The starting value when you don't. Range: today if left out. Single: no date.

DateRange | Date | null—
onValueChange

Called with the new range once its end is picked, or with the picked date.

(value: DateRange | Date) => void—
placeholder

What the trigger says while no date is picked (single mode).

string"Pick a date"
presets

Quick picks above the month (days from today), as in the Date picker.

{ label: string; days: number }[]—
panelLabel

The panel's accessible name.

string"Choose dates" / "Choose a date"
aria-labelledby

Ids of the field's label; the trigger reads as the label followed by the date.

string—
className

Added to the root.

string—