Elements

Calendar

A calendar picks a date, or with range a range of dates, from months of days laid out in weeks: a due date in a form, the nights of a stay, the days of a report. It shows one month or several side by side, turns the months with Previous and Next, and switches to a view of the year’s months or a page of years to go far. It is the calendar in DatePicker’s popup, on its own in the page.

el-date-picker.tsx
el-date-picker.tsxLive
import { type JSX, mount, signal, Text, View } from "nsh";
import { DatePicker, type DatePickerValue, type DateRangePreset, type HourCycle, type OpenChangeReason } from "nsh/base";
import { desktop, Log, Panel, Segmented, size, Split, ui, wide, sectionSurface, controlSize, clearButtonLook, datePickerLook, DateTrigger } from "./palette";

// Three date pickers in a card: a due date (weekends disabled, presets
// beside the calendar, a clear button), a stay picked as a range over two
// months in a wide window, and a meeting's date and time with the hours
// on the clock the inspector picks. The look is the shared listbox look,
// after shadcn/ui's date picker: the trigger (`DateTrigger`) led by a
// calendar, the value or a muted placeholder, and `datePickerLook` for the
// rest: the popup at the trigger (a sheet on a touch screen), the items
// (`CalendarItem`: the month and year, two-letter weekdays, today on a quiet
// fill, the selected days on `primary`, a range's band), the presets, and
// the time's row, segments and well.

const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

/** A day's ISO date (`"2026-10-28"`), in local time as the calendar's days are. */
const iso = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
const addDays = (d: Date, days: number) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + days);
/** An ISO date's month and day: `Oct 28`. */
const monthDay = (value: string) => `${MONTHS[Number(value.slice(5, 7)) - 1].slice(0, 3)} ${Number(value.slice(8, 10))}`;

/** A time `"HH:MM"` on the picked clock: `14:30`, or `2:30 PM`. */
function clock(time: string, cycle: HourCycle) {
  if (cycle === "h23") return time;
  const hour = Number(time.slice(0, 2));
  return `${hour % 12 === 0 ? 12 : hour % 12}:${time.slice(3, 5)} ${hour < 12 ? "AM" : "PM"}`;
}

/** A trigger's width: set with a pointer, the field's on a touch screen. */
const trigger = desktop ? { width: 260 } : undefined;

/** A label over its control, a quieter line under the label. */
function Field(props: { label: string; detail: string; children: JSX.Element }) {
  return (
    <View style={{ gap: 8 }}>
      <View style={{ gap: 2 }}>
        <Text style={{ fontSize: size.label, fontWeight: "500", color: ui().text }}>{props.label}</Text>
        <Text style={{ fontSize: size.detail, color: ui().muted }}>{props.detail}</Text>
      </View>
      {props.children}
    </View>
  );
}

function App() {
  const due = signal<string | null>(null);
  const stay = signal<DatePickerValue>(null);
  const meeting = signal<string | null>(null);
  const cycle = signal<HourCycle>("h12");
  const dueOpen = signal(false);
  const stayOpen = signal(false);
  const meetingOpen = signal(false);
  const events = signal<string[]>([]);
  const log = (line: string) => events.set([line, ...events()].slice(0, 8));
  const opened = (open: (next: boolean) => void) => ({ open: next }: { open: boolean; reason: OpenChangeReason }) => open(next);
  // The presets from today.
  const today = new Date();
  const monday = addDays(today, ((8 - today.getDay()) % 7) || 7);
  const dues: DateRangePreset[] = [
    { label: "Today", value: iso(today) },
    { label: "Tomorrow", value: iso(addDays(today, 1)) },
    { label: "Next Monday", value: iso(monday) },
  ];
  const stays: DateRangePreset[] = [
    { label: "This week", value: [iso(today), iso(addDays(today, (7 - today.getDay()) % 7))] },
    { label: "Next 7 nights", value: [iso(today), iso(addDays(today, 7))] },
  ];
  const stayTitle = () => {
    const r = stay();
    return Array.isArray(r) && r[0] && r[1] ? `${monthDay(r[0])} – ${monthDay(r[1])}, ${r[1].slice(0, 4)}` : undefined;
  };
  const meetingTitle = () => {
    const v = meeting();
    return v ? `${monthDay(v)}, ${v.slice(0, 4)} · ${clock(v.slice(11, 16), cycle())}` : undefined;
  };
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>date-picker</Text>
      <Text style={{ color: ui().muted }}>
        {desktop ? "Arrows move between days, Return picks; the trigger takes Delete to clear" : "Tap a day; the meeting's time takes digits"}
      </Text>

      <Split
        gap={14}
        inspector={
          <>
            <Panel title="value">
              <Text style={{ fontSize: 13, color: ui().text }}>{`due ${JSON.stringify(due())}`}</Text>
              <Text style={{ fontSize: 13, color: ui().text }}>{`stay ${JSON.stringify(stay())}`}</Text>
              <Text style={{ fontSize: 13, color: ui().text }}>{`meeting ${JSON.stringify(meeting())}`}</Text>
            </Panel>
            <Segmented label="hourCycle" options={["h12", "h23"] as const} value={cycle()} onChange={(v) => cycle.set(v)} />
            <Log title="onChange" lines={events()} minHeight={120} />
          </>
        }
      >
        <View style={{ ...sectionSurface({ padded: true }), gap: 20 }}>
          <Field label="Due" detail="Weekdays only, presets beside the calendar, a clear button">
            <DatePicker
              value={due()}
              onChange={({ value }) => {
                due.set(typeof value === "string" ? value : null);
                log(`Due → ${JSON.stringify(value)}`);
              }}
              onOpenChange={opened((next) => dueOpen.set(next))}
              accessibilityLabel="Due"
              disabledMatcher={[0, 6]}
              presets={dues}
              {...datePickerLook()}
              style={trigger}
              cleanable
              clearButtonAccessibilityLabel="Clear the due date"
              {...clearButtonLook({ over: true })}
              trigger={<DateTrigger title={due() ? `${monthDay(due()!)}, ${due()!.slice(0, 4)}` : "No due date"} placeholder={!due()} open={dueOpen()} clearable={due() !== null} style={trigger} />}
            />
          </Field>
          <Field label="Stay" detail={wide() ? "A range over two months: the first press starts it" : "A range: the first press starts it, the second ends it"}>
            <DatePicker
              range
              value={stay()}
              onChange={({ value }) => {
                stay.set(value);
                log(`Stay → ${JSON.stringify(value)}`);
              }}
              onOpenChange={opened((next) => stayOpen.set(next))}
              accessibilityLabel="Stay"
              numberOfMonths={wide() ? 2 : 1}
              firstDayOfWeek={1}
              presets={stays}
              {...datePickerLook({ months: () => (wide() ? 2 : 1), firstDayOfWeek: 1 })}
              trigger={<DateTrigger title={stayTitle() ?? "Check-in – check-out"} placeholder={!stayTitle()} open={stayOpen()} style={trigger} />}
            />
          </Field>
          <Field label="Meeting" detail={desktop ? "A day, then its time; the day again closes the calendar" : "A tap picks the day; open it again for the time"}>
            <DatePicker
              value={meeting()}
              onChange={({ value }) => {
                meeting.set(typeof value === "string" ? value : null);
                log(`Meeting → ${JSON.stringify(value)}`);
              }}
              onOpenChange={opened((next) => meetingOpen.set(next))}
              accessibilityLabel="Meeting"
              timePrecision="minute"
              hourCycle={cycle()}
              defaultTime="09:00"
              {...datePickerLook()}
              timeLabel={<Text style={{ fontSize: controlSize.label, color: ui().muted }}>Time</Text>}
              timeAccessibilityLabel="Meeting time"
              trigger={<DateTrigger title={meetingTitle() ?? "Not scheduled"} placeholder={!meetingTitle()} open={meetingOpen()} style={trigger} />}
            />
          </Field>
        </View>
      </Split>
    </View>
  );
}

mount(App);

The calendar owns the behavior: the value (the app’s, or its own), the shown month, the views, the days that cannot be picked, the keyboard and what screen readers hear. The look is the app’s: it styles the header, the body, each month, each row and each item, and draws the items’ content (the months’ and weekdays’ names, today’s mark, the range’s band). The grid fills the width it is given, as a table’s columns do: the months share it and the items of a row share the row equally, so a calendar in a phone’s sheet spans the screen and one in a popup sized to its content keeps its items’ widths (flex-none in itemClass or monthClass keeps an item or a month at its own width).

use nsh_base::{CalendarEntry, CalendarItemKind, Date, Matcher, calendar};
use nsh_core::prelude::*;

const MONTHS: [&str; 12] = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December",
];
const WEEKDAYS: [&str; 7] = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];

fn due_date() -> impl IntoElement {
    let due = signal(Date::default());
    calendar()
        .value(move || due.get())
        .on_change(move |next: &Date| due.set(*next))
        .disabled_matcher(Matcher::DayOfWeek(vec![0, 6]))
        .previous_accessibility_label("Previous month")
        .next_accessibility_label("Next month")
        .item_class("rounded-md selected:bg-primary selected:text-primary-foreground disabled:opacity-50")
        .render_item(|item: &CalendarEntry| {
            let label = match item.kind {
                CalendarItemKind::Previous => "‹".to_string(),
                CalendarItemKind::Next => "›".to_string(),
                CalendarItemKind::Weekday => WEEKDAYS[item.value as usize].to_string(),
                CalendarItemKind::MonthToggle | CalendarItemKind::Month => {
                    MONTHS[item.value as usize - 1].to_string()
                }
                _ => item.value.to_string(),
            };
            view()
                .w_8()
                .h_8()
                .items_center()
                .justify_center()
                .child(text(label).text_sm())
        })
}

Calendar comes from nsh/base, the nsh-base crate in Rust. In Rust, calendar() makes one with a setter per prop (.value(..), .range(..), .number_of_months(..)) and a listener per event (.on_change(..)). Its value is a Date (Date::Single, Date::Range, from a chrono::NaiveDate or a pair of them), the days it disables a Matcher.

#Value

The value is an ISO date, "2026-10-28", as an HTML <input type="date">’s; with range it is [start, end], and null is no date. With value, the app owns it: the calendar shows it as selected and reports a pick with onChange({ value }); the app sets value to follow it. Left out, the calendar keeps its own value, from defaultValue at first.

onChange reports complete values only, as gpui-base’s Selected: a pressed day, or a range’s end.

  • Without range, a press on a day picks it.
  • With range, the first press starts the range and the second ends it, the days between them in the range (each selected, its ends included, see Items). A second press on an earlier day starts the range over from it, as does a press once the range is complete. A half range shows until it is complete and only then is reported.

The calendar opens on the month of the value (a range’s start), else today’s.

#Months and views

numberOfMonths shows that many months side by side, from the shown one on; Previous and Next turn them one month at a time. firstDayOfWeek is the day the weeks start on, 0 Sunday (the default) to 6 Saturday; the user’s is localeSettings().firstDayOfWeek % 7 (Regional preferences).

The header holds Previous, two toggles and Next:

  • The day view shows the months’ days, each month a column of its weekdays’ row and its weeks; the days of the months before and after fill the weeks (outside, data-outside).
  • The month toggle (monthToggle) opens the month view: the twelve months, in rows of three. Picking one shows it in the day view.
  • The year toggle (yearToggle) opens the year view: a page of 20 years, in rows of five, Previous and Next turning the page. Picking one shows that year in the day view. yearRange is the years the pages run through, [start, end) (default: 50 years before this one to 50 after).

With several months, the header shows each month’s title instead of the toggles (a monthToggle item that takes no press), as gpui-base’s.

#Disabled dates

disabledMatcher is the days the user cannot pick, gpui-base’s Matcher, in one of four forms:

Form Disables
[0, 6] Those days of the week, 0 Sunday to 6 Saturday.
{ before, after } The days before before and after after (either left out).
{ from, to } The days from from to to, inclusive (either left out is open).
(date) => boolean The days for which the function returns true, given each shown day’s ISO date.

The function is asked once per shown day, in the next JS turn, and not again for a day it answered: turning the months asks only about the days they bring, so its answer is a pure function of the date. Passing another function asks again. A disabled day is disabled:: it takes no press, and Return and Space do not pick it.

#Items

Every press target and label of the calendar is an item: Previous, the toggles, Next, the weekdays, the days, the months and the years. Each is drawn in a part styled by itemClass and itemStyle, whose variants follow its state:

Variant The item
selected: Is selected: the date, every day of a complete range (its ends included, as React Aria’s range calendar), a half range’s start, the shown month or year, the open view’s toggle. Screen readers hear it selected.
disabled: Is unavailable: a disabled date, Previous and Next in the month view or at the ends of the years. The weekdays and the months’ titles are labels, which screen readers hear as column headers and headers: they take no press and keep their own look.
focus: Has the keyboard.
data-range-start:, data-range-end: Is a complete range’s first or last day (a range of one day is both).
data-range-middle: Is a day inside a complete range, not an end.
data-today: Is today.
data-outside: Is a day of the month before or after, filling its weeks.

The data-* variants are Tailwind’s (data-range-start: matches [data-range-start]), as shadcn’s calendar styles its days; with group in itemClass, the content follows them with group-selected:, group-data-range-middle:, and so on. selected: means every day of a range, so a range’s ends take their look over it (later variants win: data-* comes after selected:), and its middle days theirs:

<Calendar
  range
  itemClass="group rounded-md selected:bg-primary selected:text-primary-foreground data-range-middle:rounded-none data-range-middle:bg-accent data-range-middle:text-accent-foreground data-range-start:rounded-r-none data-range-end:rounded-l-none data-range-start:data-range-end:rounded-md"
/>

renderItem draws an item’s content from what it is, a CalendarEntry, never from its state: the drawing is made once and kept while the item is selected, focused or put in a range, so the keys, a pick and the focus draw nothing again, and a turn of the months draws the days it brings once each (a day keeps its drawing by its month and date: with several months, the months still shown keep theirs).

Field Meaning
kind "previous", "monthToggle", "yearToggle", "next", "weekday", "day", "month" or "year".
value Its number: the day of the month, the month (1 to 12), the year, the day of the week (0 Sunday to 6); 0 for Previous and Next.
date A day’s ISO date.
year, month The year and month it belongs to: a day’s (the month whose weeks show it), a month toggle’s or title’s, a month’s of the month view; a year toggle’s and a year’s own year. 0 for Previous, Next and the weekdays (and month for the year toggle and the years).
today The day is today.
outside The day is of the month before or after.

Without renderItem, an item shows its number, and Previous and Next show ‹ and ›, as gpui-base’s; a weekday shows its number too, 0 to 6, since the names of the months and weekdays are the app’s words in the app’s language.

#Props

Prop Rust Type Default Description
value .value(..) CalendarValue none The value, owned by the app.
defaultValue .default_value(..) CalendarValue null The value at first, without value.
range .range(..) boolean false Pick a range: the first press starts it, the second ends it.
numberOfMonths .number_of_months(..) number 1 How many months show side by side.
firstDayOfWeek .first_day_of_week(..) number 0 The day the weeks start on, 0 Sunday to 6 Saturday.
disabledMatcher .disabled_matcher(..) DateMatcher none The dates that cannot be picked.
yearRange .year_range(..) [number, number] 50 years either way The years the year view pages through, [start, end).
previousAccessibilityLabel .previous_accessibility_label(..) string none What screen readers say for Previous.
nextAccessibilityLabel .next_accessibility_label(..) string none What screen readers say for Next.
headerClass, headerStyle .header_class(..), .header_style(..) string, StyleProp<ViewStyle> none The header: Previous, the toggles (or the months’ titles), Next.
bodyClass, bodyStyle .body_class(..), .body_style(..) string, StyleProp<ViewStyle> none The body: the months side by side, or the month or year view’s rows.
monthClass, monthStyle .month_class(..), .month_style(..) string, StyleProp<ViewStyle> none Each month of the day view.
rowClass, rowStyle .row_class(..), .row_style(..) string, StyleProp<ViewStyle> none Each row: the weekdays, a week, a row of months or years.
itemClass, itemStyle .item_class(..), .item_style(..) string, StyleProp<TextStyle> none Each item: selected:, disabled:, focus:; a day’s data-range-start:, data-range-end:, data-range-middle:, data-today:, data-outside: (Items).
renderItem .render_item(..) (item: CalendarEntry) => JSX.Element none An item’s content, drawn by the app from what the item is; its number otherwise.
class, style style methods string, StyleProp<ViewStyle> none The calendar: the header above the body.

#Events

Event Rust Description
onChange .on_change(|value| ..) The user picked a complete value: { value }, a day or a range. Set value to follow it. A value the app passes is never reported.

#Parts

Part Where Styled with
The calendar the root class, style
The header the calendar, first headerClass, headerStyle
The body the calendar, after the header bodyClass, bodyStyle
A month the body (day view) monthClass, monthStyle
A row a month, or the body (month and year views) rowClass, rowStyle
An item the header and the rows itemClass, itemStyle, renderItem (group-selected:, group-data-range-middle:, … inside, under group in itemClass)

#Keyboard

The days are one stop of Tab: it lands on the selected day, else today, else the first shown day, passing over disabled days, which take no focus. Then:

Key Action
Left / Right The day before or after.
Up / Down The same day a week before or after.
Home / End The first or last day of the week.
Page Up / Page Down The same day a month before or after.
Shift+Page Up / Shift+Page Down The same day a year before or after.
Return, Space Picks the day.

In a right-to-left layout the weeks run from the right, so Left is the day after and Right the day before; Up and Down keep the weeks. A key that reaches a disabled day goes on to the next day that is not, in the direction it moves (Home and End back toward the day they left). Moving past the shown months turns them. Previous, the toggles, Next, the months and the years take presses. gpui-base’s days take presses only; the keys are NSH’s, after the WAI-ARIA date picker dialog.

#Accessibility

  • Each day is a button named by its ISO date, selected while it is the date or a day of the range (its ends included, as React Aria’s range calendar), dimmed while disabled.
  • Previous and Next are buttons named by previousAccessibilityLabel and nextAccessibilityLabel; the toggles, months and years are buttons named by what they show.

See Accessibility for labels, roles and announcements in general.