DatePicker
Data inputA date typed segment by segment or picked from a calendar in a popover: <DatePicker.Root> (bind:value a DateValue, bind:open, minValue, maxValue, isDateUnavailable, granularity, name) holds a DatePicker.Label (which names it; use it instead of Field.Label), a DatePicker.Input (its segments and a button that opens the calendar) and a DatePicker.Content (the popover, which draws a DatePicker.Calendar). The calendar's parts are named like Calendar's for custom layouts. Inside a Field, its description and error describe it.
import { DatePicker } from '@limerdev/zest'; DateField. A period: use DateRangePicker.Examples
Basic
Type the date, or open the calendar with the button.
When the invoice is due.
<script lang="ts">
import { DatePicker, Field, type DateValue } from '@limerdev/zest';
let due: DateValue | undefined = $state();
</script>
<Field.Root class="w-auto">
<DatePicker.Root bind:value={due}>
<DatePicker.Label>Due date</DatePicker.Label>
<DatePicker.Input />
<DatePicker.Content />
</DatePicker.Root>
<Field.Description>
{due ? `Due ${due.toString()}` : 'When the invoice is due.'}
</Field.Description>
</Field.Root> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
DatePicker.Root
| Prop | Type | Default | Description |
|---|---|---|---|
| calendarLabel | string | – | This label is exclusively used for accessibility, remaining hidden from the page. It's read by screen readers when the calendar is opened. The current month and year are automatically appended to the label, so you only need to provide the base label. For instance: - 'Date of birth' will be read as 'Date of birth, January 2021' if the current month is January 2021. - 'Appointment date' will be read as 'Appointment date, January 2021' if the current month is January 2021. - 'Booking date' will be read as 'Booking date, January 2021' if the current month is January 2021. |
| closeOnDateSelect | boolean | true | Whether to close the popover when a date is selected. |
| disabled | boolean | false | If true, the date field will be disabled and users will not be able to interact with it. This also disables the hidden input element if the date field is used in a form. |
| disableDaysOutsideMonth | boolean | false | Whether to disable the selection of days outside the current month. By default, days outside the current month are rendered to fill the calendar grid, but they are not selectable. Setting this prop to true will disable this behavior. |
| errorMessageId | string | – | The id of the element which contains the error messages for the date field when the date is invalid. |
| fixedWeeks | boolean | false | Display 6 weeks per month, regardless the month's number of weeks. This is useful for displaying a consistent calendar, where the size of the calendar doesn't change month to month. To display 6 weeks per month, you will need to render out the previous and next month's dates in the calendar as well. |
| granularity | "day" | "hour" | "minute" | "second" | 'day' | The granularity of the date field. This determines which segments will be includes in the segments array used to build the date field. By default, when a CalendarDate value is used, the granularity will default to 'day', and when a CalendarDateTime or ZonedDateTime value is used, the granularity will default to 'minute'. Granularity is only used for visual purposes, and does not impact the value of the date field. You can have the same value synced between multiple date fields with different granularities and they will all contain the same value. |
| hideTimeZone | boolean | false; | Whether or not to hide the timeZoneName segment from the date field. |
| hourCycle | 12 | 24 | the locale's default time format | The format to use for displaying the time in the input. If using a 12 hour clock, ensure you also include the dayPeriod segment in your input to ensure the user can select AM/PM. |
| initialFocus | boolean | – | Whether to focus a date when the picker is first opened. |
| isDateDisabled | DateMatcher | – | A function that returns true if the given date is disabled, which makes the date unable to be selected in the calendar. |
| isDateUnavailable | DateMatcher | – | A function that returns true if the given date is unavailable, where if selected, the date field will be marked as invalid. |
| locale | string | 'en' | The locale to use for formatting the date field. |
| maxValue | DateValue | – | The maximum acceptable date. When provided, the date field will be marked as invalid if the user enters a date after this date. |
| minValue | DateValue | – | The minimum acceptable date. When provided, the date field will be marked as invalid if the user enters a date before this date. |
| monthFormat | "numeric" | "long" | "short" | "narrow" | "2-digit" | ((month: number) => string) | "long" | The format of the month names in the calendar. |
| numberOfMonths | number | 1 | Determines the number of months to display on the calendar simultaneously. For navigation between months, refer to the pagedNavigation prop. |
| onInvalid | DateOnInvalid | – | A callback fired when the date field's value is invalid. Use this to display an error message to the user. |
| onOpenChange | OnChangeFn<boolean> | – | A callback function called when the open state changes. |
| onOpenChangeComplete | OnChangeFn<boolean> | – | A callback function called when the open state changes complete. |
| onPlaceholderChange | OnChangeFn<DateValue> | – | A callback function called when the placeholder value changes. |
| onValueChange | OnChangeFn<DateValue | undefined> | – | A callback function called when the value changes. |
| open bindable | boolean | false | The open state of the date picker popover. |
| pagedNavigation | boolean | false | Applicable only when numberOfMonths is greater than 1. Controls whether to use paged navigation for the next and previous buttons in the date picker. With paged navigation set to true, clicking the next/prev buttons changes all months in view. When set to false, it shifts the view by a single month. For example, with pagedNavigation set to true and 2 months displayed (January and February), clicking the next button changes the view to March and April. If pagedNavigation is false, the view shifts to February and March. |
| placeholder bindable | DateValue | – | The placeholder value of the date field. This determines the format and what date the field starts at when it is empty. |
| preventDeselect | boolean | false | Whether or not users can deselect a date once selected without selecting another date. |
| readonly | boolean | false | If true, the date field will be readonly, and users will not be able to edit the values of any of the individual segments. |
| readonlySegments | ("month" | "year" | "day" | "hour" | "minute" | "second" | "dayPeriod")[] | – | An array of segment names that should be readonly. If provided, only the segments not in this array will be editable. |
| required | boolean | false | Whether or not the hidden input of the date field requires a value to be submitted. |
| validate | DateValidator | – | A function that returns a string or array of strings as validation errors if the date is invalid, or nothing if the date is valid |
| value bindable | DateValue | – | The value of the date picker. |
| weekdayFormat | "long" | "short" | "narrow" | 'short' | How the string representation of the weekdays provided via the weekdays state store should be formatted. md - "long": "Sunday", "Monday", "Tuesday", etc. - "short": "Sun", "Mon", "Tue", etc. - "narrow": "S", "M", "T", etc. |
| weekStartsOn | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 (Sunday) | The day of the week to start the calendar on, which must be a number between 0 and 6, where 0 is Sunday and 6 is Saturday. |
| yearFormat | "numeric" | "2-digit" | ((year: number) => string) | "numeric" | The format of the year names in the calendar. |
DatePicker.Label
No props of its own beyond the common ones.
DatePicker.Input
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | – | The name to use for the hidden input element of the date field, which is used to submit the ISO string value of the date field to a server. If not provided, the hidden input element will not be rendered. |
DatePicker.Segment
| Prop | Type | Default | Description |
|---|---|---|---|
| part required | "month" | "year" | "day" | "hour" | "minute" | "second" | "dayPeriod" | "timeZoneName" | "literal" | – |
DatePicker.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
| closeDelay | number | 300 | How long to wait before closing the popover after hover ends (ms). Only applies when openOnHover is true. |
| label | string | the Config's `strings.openCalendar`, else 'Open calendar' | The button's name for screen readers. |
| openDelay | number | 700 | How long to wait before opening the popover on hover (ms). Only applies when openOnHover is true. |
| openOnHover | boolean | false | Whether the popover should open when the trigger is hovered. |
DatePicker.Content
| Prop | Type | Default | Description |
|---|---|---|---|
| align | "start" | "center" | "end" | 'start' | The preferred alignment of the anchor to render against when open. This may change when collisions occur. |
| alignOffset | number | – | An offset in pixels from the "start" or "end" alignment options. |
| arrowPadding | number | – | This describes the padding between the arrow and the edges of the floating element. If your floating element has border-radius, this will prevent it from overflowing the corners. |
| avoidCollisions | boolean | true | When true, overrides the side and align options to prevent collisions with the boundary edges. |
| collisionBoundary | Arrayable<Boundary> | – | A boundary element or array of elements to check for collisions against. |
| collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 | The amount in pixels of virtual padding around the viewport edges to check for overflow which will cause a collision. |
| customAnchor | string | HTMLElement | Measurable | null | – | Use an element other than the trigger to anchor the content to. If provided, the content will be anchored to the provided element instead of the trigger. You can pass a selector string or an HTMLElement. |
| dir | "ltr" | "rtl" | – | The text direction of the content. |
| escapeKeydownBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Escape behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to its parent component that has an escape keydown handler. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press. |
| forceMount | boolean | – | Whether to force mount the component. |
| hideWhenDetached | boolean | – | |
| interactOutsideBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Interact outside behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to the parent element. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an outside interaction. |
| onCloseAutoFocus | EventCallback | – | Event handler called when auto-focusing on close. Can be prevented. |
| onEscapeKeydown | ((e: KeyboardEvent) => void) | – | Callback fired when escape is pressed. |
| onFocusOutside | ((event: FocusEvent) => void) | – | Callback fired when focus leaves the dismissible layer. |
| onInteractOutside | InteractOutsideEventHandler | – | Callback fired when an outside interaction event completes, which is either a pointerup, mouseup, or touchend event, depending on the user's input device. |
| onOpenAutoFocus | EventCallback | – | Event handler called when auto-focusing on open. Can be prevented. |
| portalProps | PortalProps | – | Props for the portal the content renders in: to (the element to render in, the body by default) and disabled (render in place). |
| preventOverflowTextSelection | boolean | true | Passing true will prevent the overflow of text selection outside the element, provided the element is the top layer. |
| preventScroll | boolean | true | Whether to prevent scrolling the body when the content is open. Whether to prevent body scrolling when the content is open. |
| side | "top" | "right" | "bottom" | "left" | – | The preferred side of the anchor to render against when open. Will be reversed when collisions occur. |
| sideOffset | number | 4 | The distance in pixels from the anchor to the floating element. |
| sticky | "partial" | "always" | – | |
| strategy | "absolute" | "fixed" | – | The positioning strategy to use for the floating element. |
| trapFocus | boolean | false | Whether focus is trapped within the focus scope. |
| updatePositionStrategy | "always" | "optimized" | "optimized" | "optimized" will only update the position when necessary, while "always" will update the position on each animation frame, which is useful if the floating content is following something like a mouse cursor. |
DatePicker.Portal
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Disable portalling and render the component inline |
| to | PortalTarget | document.body | Where to portal the content to. |
DatePicker.Calendar
No props of its own beyond the common ones.
DatePicker.Header
No props of its own beyond the common ones.
DatePicker.Heading
No props of its own beyond the common ones.
DatePicker.PrevButton
No props of its own beyond the common ones.
DatePicker.NextButton
No props of its own beyond the common ones.
DatePicker.Months
No props of its own beyond the common ones.
DatePicker.Grid
No props of its own beyond the common ones.
DatePicker.GridHead
No props of its own beyond the common ones.
DatePicker.GridBody
No props of its own beyond the common ones.
DatePicker.GridRow
No props of its own beyond the common ones.
DatePicker.HeadCell
No props of its own beyond the common ones.
DatePicker.Cell
| Prop | Type | Default | Description |
|---|---|---|---|
| date required | DateValue | – | The date value of the cell. |
| month required | DateValue | – | The month DateValue that this cell is being rendered in. |
DatePicker.Day
No props of its own beyond the common ones.