# DatePicker

Calendar panel to choose a day or, with `range`, a range of days: the title of the visible month between two arrows, above a `Calendar`. Clicking the title shows the months of the year, clicking it again the years; choosing a year goes to its months, choosing a month to its days. `visibleMonth`, `visibleYear` and `view` can be bound to follow or drive the navigation. Values are `Date` objects at local midnight, names come from `locale` through `Intl`, `min` and `max` limit the days, the months, the years and the arrows, and `isDateDisabled` the days. With `months={2}` the day view shows two months side by side, the visible one and the next, with the arrows at the outer sides; the days of the nearby months are hidden there. `footerSnippet` adds content under the calendar, such as Cancel and Apply buttons. It is a `role="group"` named by `aria-label` (the field gives it `role="dialog"`) with the surface of a popup; `DatePickerTextField` shows it in a menu under a field. Its state is exposed as `data-view`, `data-months` and `data-disabled` for app CSS.

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

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

## Examples

### Choose a day

A calendar panel: the arrows change month, the title opens the months and then the years. `bind:selectedDate` holds the chosen day.

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

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

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

<style>
	.example {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

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

### Range

With `range` the first click sets the start and the second the end; hovering previews the range. `min` and `max` also disable the arrows, the months and the years outside them.

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

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

<DatePicker
	range
	bind:selectedDate
	bind:selectedDateTo
	min={new Date(2026, 0, 1)}
	max={new Date(2027, 11, 31)}
	aria-label="Stay"
/>
```

### Views and locale

`view` decides what the panel shows and can be bound: here it starts on the months. `locale` translates names and title and sets the first day of the week.

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

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

<div class="example">
	<DatePicker locale="it" bind:view bind:selectedDate aria-label="Data" />
	<p>View: {view}</p>
</div>

<style>
	.example {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

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

### Two months

`months={2}` shows the visible month and the next side by side, which makes long ranges easier to pick; the arrows move both. `footerSnippet` adds content under the calendar, here the length of the range.

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

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

	let nights = $derived(
		selectedDate && selectedDateTo
			? Math.round((selectedDateTo.getTime() - selectedDate.getTime()) / 86_400_000)
			: 0
	);
</script>

<DatePicker range months={2} bind:selectedDate bind:selectedDateTo aria-label="Stay">
	{#snippet footerSnippet()}
		<p class="nights">{nights ? `${nights} nights` : 'Choose the last night'}</p>
	{/snippet}
</DatePicker>

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

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `view` (bindable) | `'day' \| 'month' \| 'year'` |  | What the panel shows: the days of the visible month, the months of the visible year or the years. Clicking the title goes from days to months to years and back; choosing a year shows its months, choosing a month its days. |
| `months` | `1 \| 2` | `1` | Months shown side by side in the day view: the visible one and, with `2`, the next. Choosing a month in the month view shows it first. |
| `class` | `{ container?: string; header?: string; title?: string; calendar?: string }` | `{}` | Extra classes for each part. |
| `datePickerElement` (bindable) | `HTMLDivElement` |  | The panel element. |
| `locale` | `string` |  | BCP 47 locale of the title, of the names of months and days, of the first day of the week and of the texts of the arrows (`it` and `en` are built in, other languages fall back to English). Defaults to the one set with `setDefaultLocale`, else `en`. |
| `previousMonthLabel` | `string` |  | Accessible name of the left arrow in the day view. Defaults to "Previous month", in the language of `locale`. |
| `nextMonthLabel` | `string` |  | Accessible name of the right arrow in the day view. Defaults to "Next month", in the language of `locale`. |
| `previousYearLabel` | `string` |  | Accessible name of the left arrow in the month view. Defaults to "Previous year", in the language of `locale`. |
| `nextYearLabel` | `string` |  | Accessible name of the right arrow in the month view. Defaults to "Next year", in the language of `locale`. |

### From Calendar

It accepts the props, snippets and events of Calendar (`/components/calendar.md`), listed in the tables "From Calendar". Except `variant`, `calendarElement`.

| 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. |
| `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. |

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 |
| --- | --- | --- |
| `footerSnippet` |  | Content under the calendar, inside the panel, such as Cancel and Apply buttons. |
| `titleSnippet` | `{ title: string; month: number; year: number; view: 'day' \| 'month' \| 'year' }` | Replaces the text of the title: the month and year in the day view, the year in the others. The button around it keeps switching view. With two months it is called for each. |

### From Calendar

| 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

### From Calendar

| 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: `<DatePicker bind:this={ref} />`, then `ref.focus()`.

| Signature | Description |
| --- | --- |
| `focus()` | Moves the focus into the grid of the current view: a day, a month or a year. |

## 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: `<DatePicker --date-picker-background="tomato">`. The `--date-picker-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

It also takes the variables of Calendar (`/components/calendar.md`).

| Per-instance variable | Default |
| --- | --- |
| `--date-picker-gap` | `--date-picker-default-gap`: `12px` |
| `--date-picker-width` | `--date-picker-default-width`: `280px` |
| `--date-picker-padding` | `--date-picker-default-padding`: `12px` |
| `--date-picker-border-width` | `--date-picker-default-border-width`: `var(--global-border-width)` |
| `--date-picker-border-color` | `--date-picker-default-border-color`: `var(--global-color-border-strong)` |
| `--date-picker-border-radius` | `--date-picker-default-border-radius`: `var(--global-radius-lg)` |
| `--date-picker-background` | `--date-picker-default-background`: `var(--global-color-surface-pop)` |
| `--date-picker-box-shadow` | `--date-picker-default-box-shadow`: `var(--global-shadow-lg)` |
| `--date-picker-color` | `--date-picker-default-color`: `var(--global-color-text)` |
| `--date-picker-month-width` | `--date-picker-default-month-width`: `254px` |
| `--date-picker-months-gap` | `--date-picker-default-months-gap`: `24px` |
| `--date-picker-title-border-radius` | `--date-picker-default-title-border-radius`: `var(--global-radius-sm)` |
| `--date-picker-title-color` | `--date-picker-default-title-color`: `var(--global-color-text)` |
| `--date-picker-title-font-family` | `--date-picker-default-title-font-family`: `var(--global-font-family-display)` |
| `--date-picker-title-font-size` | `--date-picker-default-title-font-size`: `16px` |
| `--date-picker-title-font-weight` | `--date-picker-default-title-font-weight`: `600` |
| `--date-picker-title-letter-spacing` | `--date-picker-default-title-letter-spacing`: `-0.02em` |
| `--date-picker-title-hover-background` | `--date-picker-default-title-hover-background`: `var(--global-color-surface-2)` |
| `--date-picker-focus-ring-color` | `--date-picker-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--date-picker-footer-padding-top` | `--date-picker-default-footer-padding-top`: `12px` |
| `--date-picker-footer-border-color` | `--date-picker-default-footer-border-color`: `var(--global-color-border)` |
