MonthViewModel
View-model class that powers the built-in "month" view. Renders a 7-column grid of weeks with multi-day event bars snapped to day cells. Subclass it to customize month rendering (week-number column, fixed row count, alternative cell flags) and re-register the subclass under a new id.
Usage
class MonthViewModel extends ViewModel {
snapToCell: boolean;
getSections(): Section[];
rangeStart(date: Date): Date;
addRange(date: Date, n: number): Date;
setRange(date: Date): [Date, Date];
getRangeLabel(): string;
configure(sections: Record<string, any>): void;
}
| Member | Type | Description |
|---|---|---|
snapToCell | boolean | When true, multi-day events also snap to full day cells. Default: false. |
weekStartDay | number | Inherited from ViewModel; 0-6, default 1 (Monday). |
getSections | () => Section[] | Returns one grid section with a 7-day x-scale and a 5-6 week y-scale. |
rangeStart | (date) => Date | First day of the grid: 1st of the month, then back to that week's weekStartDay. |
addRange | (date, n) => Date | Returns the 1st of the target month (n months ahead/back); grid alignment happens in setRange. |
setRange | (date) => [Date, Date] | Computes endDate as startDate + weekCount * 7, where weekCount is 5 or 6. |
getRangeLabel | () => string | Returns the month label (e.g. "March 2025") via fmt("titleMonthFormat"). |
configure | (sections) => void | Stores per-section overrides supplied via views[].sections; merged into the result of getSections. |
Override points (protected) inherited from ViewModel and used by MonthViewModel:
mapToPrimitive- flags primitives withisMultiDayand snaps single-day events to day cells.sortBeforeLayout- sorts multi-day events before single-day events at the same x position.buildCells- emits aGridCell[]withdate,day,inMonth,today,weekend, plus position fields.
Registration
MonthViewModel is pre-registered as "month" when the package loads. Register a subclass under a different id with registerCalendarView.
Example
import { MonthViewModel, registerCalendarView } from "@svar-ui/svelte-calendar";
class CompactMonthViewModel extends MonthViewModel {
snapToCell = true;
}
registerCalendarView("compact-month", CompactMonthViewModel);
<Calendar
{events}
views={["day", "week", "compact-month"]}
view="compact-month"
date={new Date()}
/>
Related articles
registerCalendarView— register the subclass under a view id.WeekViewModel— sibling base for week-shaped variants.views— list registered ids and pass section overrides.- Month view — what the default class renders.
- Custom views — full subclassing patterns.