Skip to content
Likable Aurora @likable-hair/svelte 5

YearPickerTextField

Year field: type the year or choose it in a scrolling grid of years. The value is selectedYear, a number. Typing accepts four digits; a year outside min and max (and an unfinished one when the field loses focus) turns the field to the error state with invalidText, and the form refuses it. On desktop, focusing or clicking the field opens a YearSelector 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 (mobileDrawer). Choosing a year closes the menu (closeOnSelect). Its state is exposed as data-state, data-disabled, data-readonly and data-open for app CSS.

import { YearPickerTextField } from '@likable-hair/svelte';

src/lib/components/composed/forms/YearPickerTextField.svelte · Markdown for LLMs

Type or choose a year

Type four digits or pick the year in the grid, which opens on the chosen year. min and max limit both.

From 1990 to 2030
Code
<script lang="ts">
	import { YearPickerTextField } from '@likable-hair/svelte';

	let selectedYear = $state<number | undefined>(2026);
</script>

<div class="example">
	<YearPickerTextField
		label="Model year"
		min={1990}
		max={2030}
		clearable
		hint="From 1990 to 2030"
		bind:selectedYear
	/>
</div>

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

Props

NameTypeDefaultDescription
selectedYear bindable numberThe year. undefined while the field is empty or its text is not a valid year.
open bindable booleanWhether the menu of years is open. undefined counts as closed.
min number1900First year that can be chosen or typed.
max number2100Last year that can be chosen or typed.
locale stringBCP 47 locale of the texts: it and en are built in, other languages fall back to English. Defaults to the one set with setDefaultLocale, else en.
label stringVisible label above the field, linked to the input. In the mobile drawer it is the title.
hint stringText 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 year.
invalidText stringMessage shown below the field, and given to the form, when the typed year is not valid. Defaults to "Enter a valid year", in the language of locale.
placeholder stringNative placeholder.
disabled booleanfalseNative disabled.
readonly booleanfalseNative readonly: the year is shown but cannot be changed.
required booleanfalseNative required.
id stringid of the input. Generated when missing, so the label always points to it.
name stringName of a hidden input that submits the year (empty when there is none).
clearable booleanfalseShows a button that empties the field.
clearLabel stringAccessible name of the clear button. Defaults to "Clear", in the language of locale.
closeOnSelect booleantrueCloses the menu when a year is chosen.
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 menu opens. It flips when there is not enough space.
mobileDrawer booleantrueOn screens up to 1024px wide, opens the years in a bottom drawer from the calendar button instead of a menu, and focusing the field does not open it.
drawerTitle stringTitle of the mobile drawer. Defaults to label.
closeLabel stringAccessible name of the close button of the mobile drawer. Defaults to "Close", in the language of locale.
openLabel stringAccessible name of the calendar button. Defaults to "Choose year", in the language of locale.
input bindable HTMLInputElementThe 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 YearSelector.

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

Snippets

NameParametersDescription
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.
iconSnippetReplaces the calendar icon. The button around it keeps opening the years.
chevronSnippetReplaces the chevron at the end of the field.
prependSnippetContent outside the field, on the left.
appendInnerSnippetContent inside the field, before the clear button and the chevron.
appendSnippetContent outside the field, on the right.
clearSnippetReplaces the icon of the clear button.

Events

NameTypeDescription
onchange(event: { year: number | undefined }) => voidCalled when the user changes the year: in the grid, by typing a whole valid year (or making it invalid again) and with the clear button.

CSS variables

For a single instance, pass them as style props: <YearPickerTextField --year-picker-text-field-background="tomato">. The --year-picker-text-field-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--year-picker-text-field-border-color
--year-picker-text-field-default-border-color var(--global-input-border-color)
--year-picker-text-field-hover-border-color
--year-picker-text-field-default-hover-border-color var(--global-input-hover-border-color)
--year-picker-text-field-focus-border-color
--year-picker-text-field-default-focus-border-color var(--global-input-focus-border-color)
--year-picker-text-field-focus-ring-color
--year-picker-text-field-default-focus-ring-color var(--global-input-focus-ring-color)
--year-picker-text-field-hint-color
--year-picker-text-field-default-hint-color var(--global-input-hint-color)
--year-picker-text-field-icon-size
--year-picker-text-field-default-icon-size 18px
--year-picker-text-field-gap
--year-picker-text-field-default-gap 6px
--year-picker-text-field-width
--year-picker-text-field-default-width 100%
--year-picker-text-field-max-width
--year-picker-text-field-default-max-width none
--year-picker-text-field-error-color
--year-picker-text-field-default-error-color var(--global-input-error-color)
--year-picker-text-field-success-color
--year-picker-text-field-default-success-color var(--global-input-success-color)
--year-picker-text-field-label-color
--year-picker-text-field-default-label-color var(--global-input-label-color)
--year-picker-text-field-label-font-size
--year-picker-text-field-default-label-font-size var(--global-input-label-font-size)
--year-picker-text-field-label-font-weight
--year-picker-text-field-default-label-font-weight var(--global-input-label-font-weight)
--year-picker-text-field-outer-gap
--year-picker-text-field-default-outer-gap 8px
--year-picker-text-field-icon-color
--year-picker-text-field-default-icon-color var(--global-input-icon-color)
--year-picker-text-field-inner-gap
--year-picker-text-field-default-inner-gap var(--global-input-gap)
--year-picker-text-field-height
--year-picker-text-field-default-height var(--global-input-height)
--year-picker-text-field-padding
--year-picker-text-field-default-padding 0 var(--global-input-padding-x)
--year-picker-text-field-background
--year-picker-text-field-default-background var(--global-input-background)
--year-picker-text-field-border-width
--year-picker-text-field-default-border-width var(--global-input-border-width)
--year-picker-text-field-border-radius
--year-picker-text-field-default-border-radius var(--global-input-radius)
--year-picker-text-field-box-shadow
--year-picker-text-field-default-box-shadow var(--global-input-shadow)
--year-picker-text-field-focus-ring-width
--year-picker-text-field-default-focus-ring-width var(--global-focus-ring-width)
--year-picker-text-field-color
--year-picker-text-field-default-color var(--global-input-color)
--year-picker-text-field-font-family
--year-picker-text-field-default-font-family var(--global-font-family-mono)
--year-picker-text-field-font-size
--year-picker-text-field-default-font-size var(--global-input-font-size)
--year-picker-text-field-font-weight
--year-picker-text-field-default-font-weight 400
--year-picker-text-field-placeholder-color
--year-picker-text-field-default-placeholder-color var(--global-input-placeholder-color)
--year-picker-text-field-hint-font-size
--year-picker-text-field-default-hint-font-size var(--global-input-hint-font-size)
--year-picker-text-field-disabled-background
--year-picker-text-field-default-disabled-background var(--global-input-disabled-background)
--year-picker-text-field-disabled-opacity
--year-picker-text-field-default-disabled-opacity var(--global-input-disabled-opacity)
--year-picker-text-field-menu-width
--year-picker-text-field-default-menu-width 300px
--year-picker-text-field-menu-padding
--year-picker-text-field-default-menu-padding 8px