# Calendar

Grid of the days of one month, to choose a day or, with `range`, a range of days. It has no header: `visibleMonth` and `visibleYear` decide the month, and `DatePicker` adds the title and the arrows around it. Values are `Date` objects at local midnight. Names of months and days and the first day of the week come from `locale` through `Intl`; `weekStart` overrides the latter. `min`, `max` and `isDateDisabled` decide which days can be chosen. It follows the ARIA grid pattern: the grid is a single Tab stop, the arrows move by day and week, Home and End go to the ends of the week, Page Up and Page Down change month (with Shift, year) and Enter or Space choose. Today has a dot, the days of the nearby months are faded and choosing one shows its month. While a range is being chosen, hovering or focusing a day previews it. The `grid` variant has large bordered cells for an agenda, with events in `dayAppendSnippet`. Screen readers read the full date of every day. Its state is exposed as `data-variant` and `data-disabled` on the grid, and as `data-today`, `data-selected`, `data-outside`, `data-disabled` and `data-range` (`start`, `middle`, `end`, `single`) on the days, for app CSS.

```ts
import { Calendar } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/dates/Calendar.svelte`.

## Examples

### Choose a day

`bind:selectedDate` holds the chosen day, a `Date` at local midnight. Click a day, or focus the grid and use the arrow keys, Home, End, Page Up and Page Down, then Enter.

```svelte
<script lang="ts">
	import { Calendar } from '@likable-hair/svelte';

	let selectedDate = $state<Date | undefined>(new Date(2026, 8, 24));
</script>

<div class="example">
	<Calendar bind:selectedDate aria-label="Appointment day" />
	<p>{selectedDate ? selectedDate.toDateString() : 'No day chosen'}</p>
</div>

<style>
	.example {
		display: grid;
		gap: 12px;
		width: 280px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### Range and limits

With `range` the first click sets `selectedDate` and the second `selectedDateTo`; hovering previews the range. `min`, `max` and `isDateDisabled` (here: Sundays, the salon is closed) decide which days can be chosen.

```svelte
<script lang="ts">
	import { Calendar } from '@likable-hair/svelte';

	let selectedDate = $state<Date | undefined>(new Date(2026, 8, 8));
	let selectedDateTo = $state<Date | undefined>(new Date(2026, 8, 19));

	const format = new Intl.DateTimeFormat('en', { day: 'numeric', month: 'short' });
	const label = $derived(
		selectedDate && selectedDateTo
			? `${format.format(selectedDate)} – ${format.format(selectedDateTo)}`
			: selectedDate
				? `From ${format.format(selectedDate)}, choose the last day`
				: 'Choose the first day'
	);
</script>

<div class="example">
	<Calendar
		range
		bind:selectedDate
		bind:selectedDateTo
		min={new Date(2026, 8, 3)}
		max={new Date(2026, 10, 30)}
		isDateDisabled={(date) => date.getDay() === 0}
		aria-label="Holiday"
	/>
	<p>{label}</p>
</div>

<style>
	.example {
		display: grid;
		gap: 12px;
		width: 280px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### Agenda

`variant="grid"` draws large bordered cells and `dayAppendSnippet` puts the events of each day under its number. The title and the buttons are markup of the app that changes `visibleMonth` and `visibleYear`.

```svelte
<script lang="ts">
	import { mdiChevronLeft, mdiChevronRight } from '@mdi/js';
	import { Button, Calendar, toISODate } from '@likable-hair/svelte';

	type Tone = 'primary' | 'accent' | 'warning';

	const events: Record<string, { title: string; tone: Tone }[]> = {
		'2026-09-03': [{ title: 'Staff meeting', tone: 'primary' }],
		'2026-09-08': [{ title: 'Balayage course', tone: 'accent' }],
		'2026-09-11': [{ title: 'Inventory', tone: 'warning' }],
		'2026-09-17': [
			{ title: 'Bridal trial', tone: 'primary' },
			{ title: 'New colors', tone: 'accent' }
		],
		'2026-09-24': [
			{ title: '09:30 Luca R.', tone: 'primary' },
			{ title: '11:00 Giulia M.', tone: 'accent' }
		],
		'2026-09-29': [{ title: 'Closed', tone: 'warning' }]
	};

	let visibleMonth = $state(8);
	let visibleYear = $state(2026);
	let selectedDate = $state<Date>();

	const title = $derived(
		new Intl.DateTimeFormat('en', { month: 'long', year: 'numeric' }).format(
			new Date(visibleYear, visibleMonth, 1)
		)
	);

	function move(step: number) {
		const date = new Date(visibleYear, visibleMonth + step, 1);
		visibleMonth = date.getMonth();
		visibleYear = date.getFullYear();
	}

	function goToday() {
		const today = new Date();
		visibleMonth = today.getMonth();
		visibleYear = today.getFullYear();
	}
</script>

<div class="agenda">
	<header>
		<h3>{title}</h3>
		<Button variant="secondary" size="sm" onclick={goToday}>Today</Button>
		<span class="arrows">
			<Button
				buttonType="icon"
				variant="secondary"
				size="sm"
				icon={mdiChevronLeft}
				aria-label="Previous month"
				onclick={() => move(-1)}
			/>
			<Button
				buttonType="icon"
				variant="secondary"
				size="sm"
				icon={mdiChevronRight}
				aria-label="Next month"
				onclick={() => move(1)}
			/>
		</span>
	</header>
	<Calendar variant="grid" bind:visibleMonth bind:visibleYear bind:selectedDate aria-label={title}>
		{#snippet dayAppendSnippet({ date, outside })}
			{#if !outside}
				{#each events[toISODate(date)] ?? [] as event (event.title)}
					<span class="event" data-tone={event.tone}>{event.title}</span>
				{/each}
			{/if}
		{/snippet}
	</Calendar>
</div>

<style>
	.agenda {
		display: grid;
		gap: 14px;
	}

	header {
		display: flex;
		align-items: center;
		gap: 12px;
	}

	h3 {
		margin: 0;
		font-family: var(--global-font-family-display);
		font-size: 22px;
		letter-spacing: -0.03em;
	}

	.arrows {
		display: flex;
		gap: 6px;
		margin-inline-start: auto;
	}

	.event {
		--_tone: var(--global-color-primary);
		overflow: hidden;
		padding: 3px 6px;
		border-radius: var(--global-radius-xs);
		background: var(--global-color-primary-soft);
		color: var(--global-color-text);
		font-size: 11px;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.event::before {
		content: '';
		display: inline-block;
		width: 6px;
		height: 6px;
		margin-inline-end: 6px;
		border-radius: 2px;
		background: var(--_tone);
		vertical-align: 1px;
	}

	.event[data-tone='accent'] {
		--_tone: var(--global-color-accent);
		background: var(--global-color-accent-soft);
	}

	.event[data-tone='warning'] {
		--_tone: var(--global-color-warning);
		background: var(--global-color-warning-soft);
	}
</style>
```

### Locale and marks

`locale` sets the names and the first day of the week through `Intl` (Italian weeks start on Monday). `dayAppendSnippet` adds a mark in the corner of the days with appointments.

```svelte
<script lang="ts">
	import { Calendar, toISODate } from '@likable-hair/svelte';

	const busy = new Set(['2026-09-03', '2026-09-11', '2026-09-17', '2026-09-29']);

	let selectedDate = $state<Date | undefined>(new Date(2026, 8, 24));
</script>

<div class="example">
	<Calendar locale="it" weekdayFormat="narrow" bind:selectedDate aria-label="Giorno">
		{#snippet dayAppendSnippet({ date, outside })}
			{#if !outside && busy.has(toISODate(date))}
				<span class="mark"></span>
			{/if}
		{/snippet}
	</Calendar>
</div>

<style>
	.mark {
		width: 5px;
		height: 5px;
		border-radius: 50%;
		background: var(--global-color-warning);
	}

	.example {
		width: 280px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `selectedDate` (bindable) | `Date` |  | The chosen day; with `range`, the first day of the range. Days are local midnights. |
| `selectedDateTo` (bindable) | `Date` |  | With `range`, the last day of the range. |
| `range` | `boolean` | `false` | Chooses a range of days: the first click sets `selectedDate`, the second `selectedDateTo` (they swap when the second day is earlier, and the same day twice is a one-day range), the third starts a new range. |
| `visibleMonth` (bindable) | `number` |  | Visible month, 0 to 11. Defaults to the month of `selectedDate`, or the current one; it follows `selectedDate` when that changes to another month. Bind it to drive or follow the navigation. |
| `visibleYear` (bindable) | `number` |  | Visible year. |
| `locale` | `string` |  | BCP 47 locale of the names of months and days and of the first day of the week. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `weekStart` | `0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6` |  | First day of the week, from 0 (Sunday) to 6 (Saturday). Defaults to the one of `locale`. |
| `min` | `Date` |  | Earliest day that can be chosen. |
| `max` | `Date` |  | Latest day that can be chosen. |
| `isDateDisabled` | `(date: Date) => boolean` |  | Returns `true` for the days that cannot be chosen (weekends, holidays, ...). |
| `disabled` | `boolean` | `false` | Shows the days without letting anyone choose them; the grid leaves the Tab order. |
| `showOutsideDays` | `boolean` | `true` | Shows the days of the previous and next month that complete the first and last week. |
| `showWeekdays` | `boolean` | `true` | Shows the row with the names of the days of the week. |
| `weekdayFormat` | `'narrow' \| 'short' \| 'long'` | `'short'` | Length of the names of the days of the week. Screen readers always read the full name. |
| `fillOpenRange` | `boolean` | `false` | With `range` and only one end chosen, highlights every day after the start (or before the end). |
| `previewDate` (bindable) | `Date` |  | With `range`, the day the preview reaches while the range is being chosen: the day under the pointer or with the focus. Bind the same variable on two calendars to share the preview between them. |
| `followSelection` | `boolean` | `true` | Shows the month of `selectedDate` or `selectedDateTo` when they change to a month that is not visible. Turn it off when the parent decides the month, as with two calendars side by side. |
| `variant` | `'compact' \| 'grid'` | `'compact'` | `compact` is the grid of a date picker; `grid` has large bordered cells for an agenda, with room for events in `dayAppendSnippet`. |
| `class` | `{ container?: string; weekday?: string; day?: string }` | `{}` | Extra classes for each part. |
| `calendarElement` (bindable) | `HTMLDivElement` |  | The grid element. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `daySnippet` | `CalendarDay` | Replaces the number of a day. It receives a `CalendarDay`: `date`, `outside`, `selected`, `inRange`, `today`, `disabled`. The cell around it keeps click, keyboard and state, and screen readers keep reading the full date. |
| `dayAppendSnippet` | `CalendarDay` | Content after the number of a day: events in the `grid` variant, a mark in the top corner in the `compact` one. |
| `weekdaySnippet` | `{ label: string; name: string; day: number }` | Replaces the name of a day of the week. `day` goes from 0 (Sunday) to 6, `name` is the full name. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `ondayClick` | `(event: { date: Date; outside: boolean; nativeEvent: MouseEvent \| KeyboardEvent; }) => void` | Called when a day that can be chosen is clicked or chosen with Enter or Space, after the selection changed. `outside` is `true` for the days of the nearby months. |
| `onchange` | `(event: { selectedDate: Date \| undefined; selectedDateTo: Date \| undefined }) => void` | Called when the user changes the selection. |

## Methods

Call them on the instance: `<Calendar bind:this={ref} />`, then `ref.focus()`.

| Signature | Description |
| --- | --- |
| `focus()` | Moves the focus into the grid: to the last focused day, else the chosen one, today or the first of the month. |

## Types

Exported by the package: `import type { CalendarDay } from '@likable-hair/svelte';`

### CalendarDay

A day of the grid, as the day snippets receive it.

`interface CalendarDay`

| Name | Type | Description |
| --- | --- | --- |
| `date` (required) | `Date` | The day, at local midnight. |
| `outside` (required) | `boolean` | The day belongs to the month before or after the visible one. |
| `selected` (required) | `boolean` | The day is the chosen one, or an end of the chosen range. |
| `inRange` (required) | `boolean` | The day is inside the chosen range, ends excluded. |
| `today` (required) | `boolean` | The day is today. |
| `disabled` (required) | `boolean` | The day cannot be chosen (`min`, `max`, `isDateDisabled`). |

## CSS variables

For a single instance, pass them as style props: `<Calendar --calendar-color="tomato">`. The `--calendar-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--calendar-day-border-radius` | `--calendar-default-day-border-radius`: `var(--global-radius-sm)` |
| `--calendar-range-background` | `--calendar-default-range-background`: `var(--global-color-primary-soft)` |
| `--calendar-duration` | `--calendar-default-duration`: `var(--global-duration)` |
| `--calendar-easing` | `--calendar-default-easing`: `var(--global-ease)` |
| `--calendar-width` | `--calendar-default-width`: `100%` |
| `--calendar-color` | `--calendar-default-color`: `var(--global-color-text)` |
| `--calendar-font-size` | `--calendar-default-font-size`: `13px` |
| `--calendar-row-gap` | `--calendar-default-row-gap`: `2px` |
| `--calendar-weekday-padding` | `--calendar-default-weekday-padding`: `6px 0`<br>`--calendar-default-grid-weekday-padding`: `10px` |
| `--calendar-weekday-color` | `--calendar-default-weekday-color`: `var(--global-color-text-3)` |
| `--calendar-weekday-font-size` | `--calendar-default-weekday-font-size`: `10.5px` |
| `--calendar-weekday-font-weight` | `--calendar-default-weekday-font-weight`: `600` |
| `--calendar-weekday-letter-spacing` | `--calendar-default-weekday-letter-spacing`: `0.08em` |
| `--calendar-weekday-text-transform` | `--calendar-default-weekday-text-transform`: `uppercase` |
| `--calendar-day-height` | `--calendar-default-day-height`: `32px` |
| `--calendar-day-width` | `--calendar-default-day-width`: `100%` |
| `--calendar-day-background` | `--calendar-default-day-background`: `transparent` |
| `--calendar-day-hover-background` | `--calendar-default-day-hover-background`: `var(--global-color-surface-3)` |
| `--calendar-focus-ring-color` | `--calendar-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--calendar-today-color` | `--calendar-default-today-color`: `inherit` |
| `--calendar-today-background` | `--calendar-default-today-background`: `transparent` |
| `--calendar-range-preview-background` | `--calendar-default-range-preview-background`: `color-mix(in srgb, var(--global-color-primary) 8%, transparent)` |
| `--calendar-range-color` | `--calendar-default-range-color`: `var(--global-color-text)` |
| `--calendar-selected-day-background` | `--calendar-default-selected-day-background`: `var(--global-fill-primary)` |
| `--calendar-selected-day-color` | `--calendar-default-selected-day-color`: `var(--global-color-on-primary)` |
| `--calendar-selected-day-box-shadow` | `--calendar-default-selected-day-box-shadow`: `var(--global-shadow-fill)` |
| `--calendar-selected-day-font-weight` | `--calendar-default-selected-day-font-weight`: `600` |
| `--calendar-outside-day-opacity` | `--calendar-default-outside-day-opacity`: `0.5` |
| `--calendar-disabled-day-opacity` | `--calendar-default-disabled-day-opacity`: `0.35` |
| `--calendar-today-dot-size` | `--calendar-default-today-dot-size`: `4px` |
| `--calendar-today-dot-color` | `--calendar-default-today-dot-color`: `var(--global-color-accent)` |
| `--calendar-grid-border-color` | `--calendar-default-grid-border-color`: `var(--global-color-border)` |
| `--calendar-grid-border-radius` | `--calendar-default-grid-border-radius`: `var(--global-radius-lg)` |
| `--calendar-grid-background` | `--calendar-default-grid-background`: `var(--global-color-surface-solid)` |
| `--calendar-grid-cell-min-height` | `--calendar-default-grid-cell-min-height`: `64px` |
| `--calendar-grid-cell-padding` | `--calendar-default-grid-cell-padding`: `6px` |
| `--calendar-grid-day-size` | `--calendar-default-grid-day-size`: `24px` |
| `--calendar-grid-day-font-size` | `--calendar-default-grid-day-font-size`: `12px` |
| `--calendar-grid-today-background` | `--calendar-default-grid-today-background`: `var(--global-fill-primary)` |
| `--calendar-grid-today-color` | `--calendar-default-grid-today-color`: `var(--global-color-on-primary)` |
