Primitives
The framework-free layer under the React and Solid integrations, the calendar as a ui-sdk primitive, and the types of its agent actions.
@moltenship/calendar-primitives is the layer between the engine and a framework. It holds what
Root does, with no React or Solid in it: the state store, the runtime that turns events and state
into what a view draws, and the calendar as a ui-sdk primitive, which is what
an agent operates.
Apps don't need it: your framework's package exports the parts an app uses. Install it to write an integration for another framework, or to name the agent types below.
Install
pnpm add @moltenship/calendar-primitives @ui-sdk/core@ui-sdk/core is a peer dependency, from 0.1.0-beta.16 on within the 0.1 line. The package
imports no UI framework. It needs a runtime with Temporal, as the rest of the library does.
Where it sits
@moltenship/calendar-react @moltenship/calendar-solid thin: render parts, bind state
└──────────────┬──────────────┘
@moltenship/calendar-primitives runtime, store, primitive, agent actions
┌──────────────┴──────────────┐
@moltenship/calendar-core @ui-sdk/core the engine · parts and the agent bridgeThe engine doesn't import ui-sdk, and ui-sdk's own date components aren't used. Dates, time zones, recurrence, layouts, transactions and policies are the calendar's own.
What it exports
| Export | What it is |
|---|---|
createCalendarStore(options) | The store of the six state values. The same one your framework's package exports |
createCalendarRuntime(config) | What Root runs: event snapshots, pending changes, the active view's projection, the visible range, diagnostics |
createCalendarPrimitive({ views }) | The calendar as a ui-sdk primitive: the six values as controlled keys, slots for the parts, and the agent actions |
calendarAgentActions | The fifteen action names an agent can call |
calendarStateSlices | The six state value names |
resolveViewOptions(view, overrides) | A view's options: its defaultOptions with a root's viewOptions over them |
createDraftStore(), createKeyboardSessionStore() | The stores behind gesture previews and keyboard grabs |
createAnnouncerChannel(getEnvironment, getResources) | The channel a live region reads announcements from |
createClockTicker(clock, intervalMs), systemClock, clockOf | The clock a root samples for today and the current time |
eventLabel, agentEventLabel, visibleEventsOf | How an agent's items are made: an event's label, the default label of a described event, the events in view |
describeAgentEvent, describeAgentPlacement, eventsInRange | An event and a placement as an agent reads them, and the events listEvents returns for a range |
findEvent, readAgentInput | An event by the ID an agent sent, and an action's one input object checked against its keys |
parseInstant, parsePlainDate, parsePlainDateTime, parseTimeZone | How an agent's JSON values are read, with the errors the reference lists |
It also exports the equality helpers the integrations compare props with (sameInstant,
samePlainDate, sameRange, sameWeekRules and others), createMemoCell and createStableCell,
and the state types: CalendarPublicState, CalendarActions, CalendarStore,
CalendarStateChangeDetail, CalendarChangeReason, CalendarChangeSource and
CalendarCommandOptions.
The primitive
createCalendarPrimitive defines the calendar for ui-sdk, once per createCalendar call. An
integration renders it with its framework's ui-sdk package, @ui-sdk/react or @ui-sdk/solid.
Its slots are the calendar's structural parts:
| Slot | The part |
|---|---|
root | Calendar.Frame |
toolbar | Calendar.Toolbar |
title | Calendar.Title |
previous-trigger | Calendar.PreviousTrigger |
next-trigger | Calendar.NextTrigger |
today-trigger | Calendar.TodayTrigger |
view-switcher | Calendar.ViewSwitcher |
viewport | Calendar.Viewport |
Events and cells aren't slots. A view renders them as ordinary elements with the data attributes the pointer layer reads, so a calendar with a thousand events doesn't make a thousand ui-sdk parts.
The six state values are always controlled keys of the primitive. The integration merges what the app controls with what the calendar store holds, and passes the result in. ui-sdk's own store is a copy of that for the parts and for agents, never the owner of a value. That keeps what State promises: actions in one handler build on each other, and dates compare by value.
Agent types
The primitive's api is what an agent calls. Its TypeScript types are exported for code that
wraps the bridge:
import {
calendarAgentActions,
type CalendarAgentApi,
type CalendarAgentChange,
type CalendarAgentEvent,
type CalendarAgentItem,
} from "@moltenship/calendar-primitives";
// The fifteen action names. A snapshot lists the ones a calendar takes, in alphabetical order.
export const actionNames: readonly (keyof CalendarAgentApi)[] = calendarAgentActions;
// One entry of a calendar's `items`: `{ value, label }`.
export type VisibleEvent = CalendarAgentItem;
// What `listEvents` returns for an event, and what an event action returns.
export type ListedEvent = CalendarAgentEvent;
export type ChangeResult = CalendarAgentChange;CalendarAgentApi has one method per action. Each takes nothing or one object, which is the one
argument of an agent's step:
| Method | Input | Returns |
|---|---|---|
describeCalendar() | none | CalendarAgentDescription |
listEvents(input?) | CalendarAgentRange: { start?, end? } | readonly CalendarAgentEvent[] |
getChange(input) | { changeId } | CalendarAgentChange |
navigateNext(), navigatePrevious(), navigateToday() | none | nothing |
goToDate(input) | { date } | nothing |
setView(input) | { view } | nothing |
setTimeZone(input) | { timeZone } | nothing |
selectEvent(input) | { eventId }, or { eventId: null } to clear | nothing |
createEvent(input) | CalendarAgentCreateInput: { start, end, title?, resourceIds?, fields? } | CalendarAgentChange |
updateEvent(input) | CalendarAgentUpdateInput: { eventId, title?, start?, end?, resourceIds?, fields? } | CalendarAgentChange |
moveEvent(input) | CalendarAgentMoveInput: { eventId, start, end?, resourceIds? } | CalendarAgentChange |
resizeEvent(input) | CalendarAgentResizeInput: { eventId, start } or { eventId, end } | CalendarAgentChange |
deleteEvent(input) | CalendarAgentEventRef: { eventId } | CalendarAgentChange |
The five event methods are optional in the type, because a calendar has them only when it can
take them: createEvent, updateEvent, moveEvent and resizeEvent when its root has
onEventChange, and deleteEvent when the root also has allowAgentDelete. A read-only
calendar's snapshot doesn't list them.
Dates and times are ISO strings in both directions. CalendarAgentChange is
{ changeId, cause, eventId, proposed, status, reason? }, with status one of "saved",
"saving", "validating" and "rejected".
The types say what a well-formed call sends. A model can send anything, so the calendar checks the input when the step runs, and fails the step with a message that names the action's input. The agent reference has every input, result and error.
An app doesn't call these methods. An agent calls them by name through the bridge, where your
allow decides first.
Interaction
The keyboard, roving focus, drafts, the history binding and the announcer are written once, here. An integration binds each in a few lines, with three shapes:
- Stores have
subscribeandgetSnapshot, andgetServerSnapshotwhen the server reads something else. React reads them withuseSyncExternalStore, Solid with a store selector. - Element listeners take the element and a getter for the latest options, and return a
cleanup, as
attachCalendarPointer(element, () => options)does. The getter is read whenever an event arrives, so new options never re-attach anything. - Queries are memoized functions an integration calls while it renders, as often as it renders.
Most take the root's interaction runtime, model.interaction from runtime.derive, or the few
fields of it they use.
| Export | What it is |
|---|---|
createCalendarViewRuntime(getView) | One rendered active view: getView reads the latest view, and roving is its roving focus store. Keep it on the root's focus with roving.update(focus, calendarViewEntry(view)) after each render |
calendarViewEntry(view) | Where focus enters a view, from its spatial navigation |
focusRovingTarget(interaction, target, days, event) | What a cell or event does when it takes DOM focus itself: it becomes the calendar's focus |
calendarFocusKey(target) | A key for a focus target, which changes exactly when the target does |
createCalendarKeyboard(interaction, viewRuntime, getOptions, getResources) | A view's keyboard: keyDown(event, wrapper?) and focusOut(event, region) for the element around its cells. wrapper is the framework's own event, when it wraps DOM events as React does. Bind keydown on that element itself (addEventListener from a ref in Solid): a handler delegated to the document stops Escape too late |
interaction.draft | A store of the change being drafted: the keyboard's session while there is one, else the pointer's draft |
createBusyResourcesQuery(), createDraftBusyQuery() | Queries for the resources busy in a range, and for one resource during the draft |
bindCalendarHistory(interaction, history, getOptions) | An event history bound to a root, as a store of { undo, redo, canUndo, canRedo, … } |
attachCalendarHistoryShortcuts(interaction, getConfig) | The undo and redo keys, a keydown listener on the document for keys inside scope; returns a cleanup |
createAnnouncerLiveText() | A live region's text as a store. Pass its speak to interaction.announcements.mount and render the snapshot |
subscribeAnchorDateRequests(interaction, listener) | Hears every date the root is asked to show, with the range the active view shows for it |
createResourceExpansionCommands(interaction, resourceId) | expand, collapse and toggle for one resource of a tree |
createResourceStatusClock(source, compute) | A store of a status that depends on the time, such as whether a resource is open now, sampled on source's clock once a minute while subscribed, and null on the server. Only the clock is read: when source.now is fixed, compute at it yourself |
A view that unmounts mid-gesture leaves nothing grabbed when its integration calls
interaction.keyboard.set(null) on unmount. Its React binding, useCalendarKeyboard, is this:
const keyboard = useMemo(
() =>
createCalendarKeyboard(
interaction,
view,
() => latest.current.options,
() => latest.current.resources,
),
[interaction, view],
);
useEffect(() => () => interaction.keyboard.set(null), [interaction.keyboard]);
return {
onBlur: (event) => keyboard.focusOut(event.nativeEvent, event.currentTarget),
onKeyDown: (event) => keyboard.keyDown(event.nativeEvent, event),
};Writing an integration
@moltenship/calendar-solid is the model to read: it binds everything React's does, in less code. An integration
does four things:
- Creates the runtime and the primitive in its factory, as
createCalendardoes. - In its
Root, connects the runtime to a store (runtime.connect), merges controlled state, and callsruntime.derive(inputs)in the framework's reactive idiom.deriveis synchronous and memoized per root. - Renders the primitive's slots as parts through the framework's ui-sdk package.
- Passes the model to custom views and hooks through a context of its own.
The interaction below is shared from here as stores and plain functions. The pointer layer is
@moltenship/calendar-dnd, which imports no framework either: bind attachCalendarPointer as
useCalendarPointer does in both integrations.
There's no guide beyond this page yet, and the runtime's types change as the Solid integration grows. If you plan an integration, open an issue first.