API reference

State store

Create calendar state outside Root, and change it from anywhere in your app.

Root creates a store for its state automatically. Create one yourself when code outside Root needs to change the calendar, or when the state should survive Root unmounting. Everything here is exported from @moltenship/calendar-solid. The store has no framework in it.

createCalendarStore

store.tsx
import { createCalendarStore, createStateSelector } from "@moltenship/calendar-solid";

import { seed, weekRules } from "./app";
import { Calendar, type View } from "./calendar";

export const store = createCalendarStore<View>({
  defaultView: "list",
  defaultAnchorDate: Temporal.PlainDate.from("2026-10-08"),
  defaultTimeZone: "Europe/Berlin",
});

store.actions.setAnchorDate(Temporal.PlainDate.from("2026-10-15"));
console.log(store.getSnapshot().anchorDate.toString()); // 2026-10-15

Prop

Type

The type arguments are <TView, TSelection, TFocus, TResourceId>. The store has three members:

  • actions: setView, setAnchorDate, setTimeZone, setSelection, setFocus, and setExpandedResourceIds. Each takes the new value and, optionally, { event?, reason?, source? } (CalendarCommandOptions), which a mounted Root passes on to its change handler.
  • getSnapshot(): the current state as one frozen object. It's a new object after each change.
  • subscribe(listener): calls listener after each change and returns an unsubscribe function.

createStateSelector(store, select, options?) returns an accessor of one value from the store, so any component can read it, inside Root or not. It updates only when that value changes, by options.isEqual when you pass one:

store.tsx
export function AnchorDate() {
  const anchorDate = createStateSelector(store, (state) => state.anchorDate, {
    isEqual: (a, b) => a.equals(b),
  });
  return <time>{anchorDate().toString()}</time>;
}

Sharing a store with Root

Pass the store to Root with the store prop:

store.tsx
function JumpToHolidays() {
  return (
    <button
      type="button"
      onClick={() => store.actions.setAnchorDate(Temporal.PlainDate.from("2026-12-24"))}
    >
      Holidays
    </button>
  );
}

export function Page() {
  return (
    <>
      <JumpToHolidays />
      <Calendar.Root
        store={store}
        events={seed}
        defaultAnchorDate={Temporal.PlainDate.from("2026-10-08")}
        defaultTimeZone="Europe/Berlin"
        locale="en-US"
        weekRules={weekRules}
      >
        <Calendar.ActiveView />
      </Calendar.Root>
    </>
  );
}
  • Root shows the store's values. TypeScript still asks for defaultAnchorDate and defaultTimeZone, but Root ignores them when it has a store.
  • store.actions go through the mounted Root: controlled props stay controlled, and their change handlers run. If several mounted roots share the store, the most recently mounted one handles them. A handler hears such a call with reason: "imperative" and source: "programmatic", and can stop it with cancel().
  • An agent's changes go through the same store. The store has no agent API of its own: agents reach the calendar through the mounted Root.
  • getSnapshot() returns the values the store holds. A value you control with a prop lives in your own state, not in the store.
  • Unmounting Root leaves the store as it was.

On this page