# DatePickerTextField

Date field: type the date or choose it in a calendar. The value is `selectedDate`, a `Date` at local midnight; with `range` the field has a start and an end input and `selectedDateTo` holds the end. The format comes from `locale` (`en` is MM/dd/yyyy, `it` dd/MM/yyyy) or from `format`, and it is shown as a chip after the text, which hides itself when the field is too narrow for both. Typing accepts digits only and adds the separators; a whole date that does not exist, is outside `min` and `max` or is disabled by `isDateDisabled` (and an unfinished one when the field loses focus) turns the field to the error state with `invalidText`, and the form refuses to submit it. On desktop, focusing or clicking the field opens a `DatePicker` in a menu; Arrow Down moves the focus into it, Escape closes it and Tab leaves it. On screens up to 1024px wide the calendar button opens it in a bottom drawer instead, and the field stays typable with the numeric keyboard (`mobileDrawer`). Choosing a day closes the calendar (`closeOnSelect`). `months={2}` shows two months side by side in the menu. With `confirm` the calendar works on a draft that only the Apply button at its bottom saves; Cancel, Escape and a click outside discard it. With `name` (and `nameTo`) the value is submitted as `yyyy-MM-dd`. Its state is exposed as `data-state`, `data-disabled`, `data-readonly`, `data-open` and `data-range` for app CSS.

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

Category: composed. Source: `src/lib/components/composed/forms/DatePickerTextField.svelte`.

## Examples

### Type or choose

Type the digits (the separators come by themselves) or open the calendar: focus the field, then Arrow Down moves into the calendar and Escape closes it. Choosing a day closes it. On a phone the calendar button opens a bottom drawer instead.

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

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

<div class="example">
	<DatePickerTextField label="Appointment date" bind:selectedDate />
	<p>Value: {selectedDate ? toISODate(selectedDate) : 'none'}</p>
</div>

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

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

### Range

With `range` the field has a start and an end input, and the calendar picks the start with the first click and the end with the second, then closes.

```svelte
<script lang="ts">
	import { DatePickerTextField } 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>

<div class="example">
	<DatePickerTextField
		label="Report period"
		locale="en-GB"
		range
		clearable
		bind:selectedDate
		bind:selectedDateTo
	/>
</div>

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

### Limits and forms

`min`, `max` and `isDateDisabled` limit the calendar and the typed date: an invalid or unfinished date shows `invalidText` and the form refuses it. With `name` the date is submitted as `yyyy-MM-dd`.

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

	let submitted = $state('');

	function submit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
		event.preventDefault();
		submitted = new URLSearchParams(new FormData(event.currentTarget) as never).toString();
	}
</script>

<form onsubmit={submit}>
	<DatePickerTextField
		label="Booking"
		name="booking"
		locale="en-GB"
		required
		clearable
		min={new Date(2026, 8, 1)}
		max={new Date(2026, 11, 31)}
		isDateDisabled={(date) => date.getDay() === 0}
		hint="September to December, closed on Sundays"
	/>
	<DatePickerTextField label="Birth date" locale="en-GB" state="error" hint="Required for the loyalty card" />
	<Button type="submit">Book</Button>
	<p>{submitted ? `Submitted: ${submitted}` : 'Nothing submitted yet'}</p>
</form>

<style>
	form {
		display: grid;
		justify-items: start;
		gap: 14px;
		width: 340px;
	}

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

### Locale and format

`locale` sets the format, the names in the calendar, the first day of the week and the texts: with `it` the chip reads `gg/mm/aaaa` and the buttons and the error are in Italian (`setDefaultLocale('it')` in the root layout does it for the whole app). Text props such as `invalidText` win over it. `format` sets any order and separator with `dd`, `MM` and `yyyy`.

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

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

<div class="example">
	<DatePickerTextField
		label="Data appuntamento"
		locale="it"
		invalidText="Data non valida"
		bind:selectedDate={italian}
	/>
	<DatePickerTextField label="ISO format" format="yyyy-MM-dd" bind:selectedDate={iso} />
</div>

<style>
	.example {
		display: grid;
		gap: 14px;
		width: 340px;
	}
</style>
```

### Two months and Apply

`months={2}` shows two months side by side in the menu (one in the mobile drawer). With `confirm` the clicks only change a draft: Apply saves it and calls `onchange`, Cancel, Escape or a click outside discard it, and with `clearable` Clear empties the field. Useful when every change starts a new search.

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

	let selectedDate = $state<Date | undefined>(new Date(2026, 8, 7));
	let selectedDateTo = $state<Date | undefined>(new Date(2026, 9, 2));
	let searches = $state(0);
</script>

<div class="example">
	<DatePickerTextField
		label="Invoices issued"
		locale="en-GB"
		range
		months={2}
		confirm
		clearable
		bind:selectedDate
		bind:selectedDateTo
		onchange={() => searches++}
	/>
	<p>Searches run: {searches}</p>
</div>

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

	p {
		margin: 0;
		font-size: 14px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `selectedDate` (bindable) | `Date` |  | The date, at local midnight; with `range`, the start of the range. `undefined` while the field is empty or its text is not a valid date. |
| `selectedDateTo` (bindable) | `Date` |  | With `range`, the end of the range. |
| `range` | `boolean` | `false` | Chooses a range: the field has a start and an end input, and the calendar picks the start with the first click and the end with the second. |
| `open` (bindable) | `boolean` |  | Whether the calendar is open. `undefined` counts as closed. |
| `locale` | `string` |  | BCP 47 locale of the format, of the names in the calendar, of the first day of the week and of the texts (`it` and `en` are built in, other languages fall back to English). Defaults to the one set with `setDefaultLocale`, else `en`. |
| `format` | `string` |  | Format of the typed date, with the tokens `dd`, `MM` and `yyyy` and any separator (`dd/MM/yyyy`, `yyyy-MM-dd`, `dd.MM.yyyy`). Defaults to the one of `locale`. |
| `weekStart` | `0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6` |  | First day of the week in the calendar, from 0 (Sunday) to 6 (Saturday). Defaults to the one of `locale`. |
| `weekdayFormat` | `'narrow' \| 'short' \| 'long'` | `'short'` | Length of the names of the days of the week in the calendar. |
| `showOutsideDays` | `boolean` | `true` | Shows in the calendar the days of the previous and next month that complete the first and last week. |
| `min` | `Date` |  | Earliest date that can be chosen or typed. It also limits the months and years of the calendar. |
| `max` | `Date` |  | Latest date that can be chosen or typed. |
| `isDateDisabled` | `(date: Date) => boolean` |  | Returns `true` for the days that cannot be chosen or typed (weekends, holidays, ...). |
| `label` | `string` |  | Visible label above the field, linked to the input. In the mobile drawer it is the title. |
| `hint` | `string` |  | Text below the field. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the border and the hint and shows an icon. `error` also sets `aria-invalid`. Without it the field still shows the error state for an invalid date. |
| `invalidText` | `string` |  | Message shown below the field, and given to the form, when the typed date is not valid. Defaults to "Enter a valid date", in the language of `locale`. |
| `placeholder` | `string` |  | Placeholder of the (start) input. Without it the format chip tells what to type. |
| `placeholderTo` | `string` |  | With `range`, placeholder of the end input. |
| `disabled` | `boolean` | `false` | Native `disabled`: the field and the calendar cannot be used. |
| `readonly` | `boolean` | `false` | Native `readonly`: the date is shown and can be selected, but not changed. |
| `required` | `boolean` | `false` | Native `required` on the inputs. |
| `id` | `string` |  | `id` of the (start) input. Generated when missing, so the label always points to it. |
| `name` | `string` |  | Name of a hidden input that submits the date as `yyyy-MM-dd` (empty when there is no date). |
| `nameTo` | `string` |  | With `range`, name of the hidden input with the end date. |
| `clearable` | `boolean` | `false` | Shows a button that empties the field. |
| `clearLabel` | `string` |  | Accessible name of the clear button. Defaults to "Clear", in the language of `locale`. |
| `closeOnSelect` | `boolean` | `true` | Closes the calendar when a day is chosen (with `range`, when the end is chosen). Ignored with `confirm`. |
| `months` | `1 \| 2` | `1` | Months shown side by side in the calendar menu: `2` helps to pick long ranges. The mobile drawer always shows one. |
| `confirm` | `boolean` | `false` | The calendar works on a draft: the date (or the range) changes only with the Apply button at its bottom, while Cancel, Escape and a click outside discard the draft. With `clearable` the bottom also has a Clear button, which empties the field and closes the calendar. Typing in the field still changes the date at once. |
| `applyLabel` | `string` |  | Text of the Apply button, with `confirm`. Defaults to "Apply", in the language of `locale`. |
| `cancelLabel` | `string` |  | Text of the Cancel button, with `confirm`. Defaults to "Cancel", in the language of `locale`. |
| `placement` | `'bottom-start' \| 'bottom' \| 'bottom-end' \| 'top-start' \| 'top' \| 'top-end' \| 'left-start' \| 'left' \| 'left-end' \| 'right-start' \| 'right' \| 'right-end'` | `'bottom-start'` | Side of the field where the calendar menu opens. It flips when there is not enough space. |
| `mobileDrawer` | `boolean` | `true` | On screens up to 1024px wide, opens the calendar in a bottom drawer from the calendar button instead of a menu, and focusing the field does not open it. |
| `drawerTitle` | `string` |  | Title of the mobile drawer. Defaults to `label`. |
| `closeLabel` | `string` |  | Accessible name of the close button of the mobile drawer. Defaults to "Close", in the language of `locale`. |
| `openLabel` | `string` |  | Accessible name of the calendar button, and of the calendar when there is no `label`. Defaults to "Choose date", in the language of `locale`. |
| `startLabel` | `string` |  | With `range`, accessible name of the start input, after the label. Defaults to "Start date", in the language of `locale`. |
| `endLabel` | `string` |  | With `range`, accessible name of the end input, after the label. Defaults to "End date", in the language of `locale`. |
| `showFormat` | `boolean` | `true` | Shows the format as a chip after the text, also read by screen readers. The chip hides itself while the field is too narrow for the whole text and the chip. |
| `formatLabel` | `string` |  | Text of the format chip. Defaults to the format in lowercase, with the letters of the language of `locale` (`dd/mm/yyyy`, in Italian `gg/mm/aaaa`). |
| `previousMonthLabel` | `string` |  | Accessible name of the calendar's left arrow in the day view (`DatePicker`). Defaults to "Previous month", in the language of `locale`. |
| `nextMonthLabel` | `string` |  | Accessible name of the calendar's right arrow in the day view. Defaults to "Next month", in the language of `locale`. |
| `previousYearLabel` | `string` |  | Accessible name of the calendar's left arrow in the month view. Defaults to "Previous year", in the language of `locale`. |
| `nextYearLabel` | `string` |  | Accessible name of the calendar's right arrow in the month view. Defaults to "Next year", in the language of `locale`. |
| `input` (bindable) | `HTMLInputElement` |  | The (start) input element. |
| `inputTo` (bindable) | `HTMLInputElement` |  | With `range`, the end input element. |
| `class` | `{ container?: string; label?: string; row?: string; field?: string; input?: string; hint?: string; picker?: string; }` | `{}` | Extra classes for each part. `picker` goes on the `DatePicker`. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content. It stays linked to the input. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the field. |
| `stateIconSnippet` | `{ state: 'error' \| 'success' }` | Replaces the icon shown for the validation state. Render nothing to hide it. |
| `iconSnippet` |  | Replaces the calendar icon. The button around it keeps opening the calendar. |
| `prependSnippet` |  | Content outside the field, on the left. |
| `appendInnerSnippet` |  | Content inside the field, after the format chip and before the clear button. |
| `appendSnippet` |  | Content outside the field, on the right. |
| `clearSnippet` |  | Replaces the icon of the clear button. |
| `formatSnippet` | `{ format: string }` | Replaces the content of the format chip. |
| `daySnippet` | `CalendarDay` | Replaces the number of a day in the calendar (see `Calendar`). |
| `dayAppendSnippet` | `CalendarDay` | Content after the number of a day in the calendar (see `Calendar`). |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { selectedDate: Date \| undefined; selectedDateTo: Date \| undefined }) => void` | Called when the user changes the date: in the calendar, by typing a whole valid date (or making it invalid again) and with the clear button. |

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

| Per-instance variable | Default |
| --- | --- |
| `--date-picker-text-field-border-color` | `--date-picker-text-field-default-border-color`: `var(--global-input-border-color)` |
| `--date-picker-text-field-hover-border-color` | `--date-picker-text-field-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--date-picker-text-field-focus-border-color` | `--date-picker-text-field-default-focus-border-color`: `var(--global-input-focus-border-color)` |
| `--date-picker-text-field-focus-ring-color` | `--date-picker-text-field-default-focus-ring-color`: `var(--global-input-focus-ring-color)` |
| `--date-picker-text-field-hint-color` | `--date-picker-text-field-default-hint-color`: `var(--global-input-hint-color)` |
| `--date-picker-text-field-icon-size` | `--date-picker-text-field-default-icon-size`: `18px` |
| `--date-picker-text-field-gap` | `--date-picker-text-field-default-gap`: `6px` |
| `--date-picker-text-field-width` | `--date-picker-text-field-default-width`: `100%` |
| `--date-picker-text-field-max-width` | `--date-picker-text-field-default-max-width`: `none` |
| `--date-picker-text-field-error-color` | `--date-picker-text-field-default-error-color`: `var(--global-input-error-color)` |
| `--date-picker-text-field-success-color` | `--date-picker-text-field-default-success-color`: `var(--global-input-success-color)` |
| `--date-picker-text-field-label-color` | `--date-picker-text-field-default-label-color`: `var(--global-input-label-color)` |
| `--date-picker-text-field-label-font-size` | `--date-picker-text-field-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--date-picker-text-field-label-font-weight` | `--date-picker-text-field-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--date-picker-text-field-outer-gap` | `--date-picker-text-field-default-outer-gap`: `8px` |
| `--date-picker-text-field-icon-color` | `--date-picker-text-field-default-icon-color`: `var(--global-input-icon-color)` |
| `--date-picker-text-field-inner-gap` | `--date-picker-text-field-default-inner-gap`: `var(--global-input-gap)` |
| `--date-picker-text-field-height` | `--date-picker-text-field-default-height`: `var(--global-input-height)` |
| `--date-picker-text-field-padding` | `--date-picker-text-field-default-padding`: `0 var(--global-input-padding-x)` |
| `--date-picker-text-field-background` | `--date-picker-text-field-default-background`: `var(--global-input-background)` |
| `--date-picker-text-field-border-width` | `--date-picker-text-field-default-border-width`: `var(--global-input-border-width)` |
| `--date-picker-text-field-border-radius` | `--date-picker-text-field-default-border-radius`: `var(--global-input-radius)` |
| `--date-picker-text-field-box-shadow` | `--date-picker-text-field-default-box-shadow`: `var(--global-input-shadow)` |
| `--date-picker-text-field-focus-ring-width` | `--date-picker-text-field-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--date-picker-text-field-color` | `--date-picker-text-field-default-color`: `var(--global-input-color)` |
| `--date-picker-text-field-font-family` | `--date-picker-text-field-default-font-family`: `var(--global-font-family-mono)` |
| `--date-picker-text-field-font-size` | `--date-picker-text-field-default-font-size`: `var(--global-input-font-size)` |
| `--date-picker-text-field-font-weight` | `--date-picker-text-field-default-font-weight`: `400` |
| `--date-picker-text-field-placeholder-color` | `--date-picker-text-field-default-placeholder-color`: `var(--global-input-placeholder-color)` |
| `--date-picker-text-field-separator-color` | `--date-picker-text-field-default-separator-color`: `var(--global-color-text-3)` |
| `--date-picker-text-field-format-padding` | `--date-picker-text-field-default-format-padding`: `2px 6px` |
| `--date-picker-text-field-format-border-color` | `--date-picker-text-field-default-format-border-color`: `var(--global-color-border)` |
| `--date-picker-text-field-format-border-radius` | `--date-picker-text-field-default-format-border-radius`: `var(--global-radius-xs)` |
| `--date-picker-text-field-format-background` | `--date-picker-text-field-default-format-background`: `var(--global-color-surface-2)` |
| `--date-picker-text-field-format-color` | `--date-picker-text-field-default-format-color`: `var(--global-color-text-3)` |
| `--date-picker-text-field-format-font-size` | `--date-picker-text-field-default-format-font-size`: `11px` |
| `--date-picker-text-field-hint-font-size` | `--date-picker-text-field-default-hint-font-size`: `var(--global-input-hint-font-size)` |
| `--date-picker-text-field-disabled-background` | `--date-picker-text-field-default-disabled-background`: `var(--global-input-disabled-background)` |
| `--date-picker-text-field-disabled-opacity` | `--date-picker-text-field-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
