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

YearSelector

Scrolling grid of years, from min to max, to choose one. Without aria-label or aria-labelledby it is named "Year", in the language of locale. When it appears it scrolls to the chosen year (or the current one, which has a border). Choosing the chosen year again keeps it. It is an ARIA listbox: one Tab stop, the arrows move in the grid, Page Up and Page Down jump three rows, Home and End go to the first and last year, Enter or Space choose. Its state is exposed as data-disabled on the grid and as data-selected and data-current on the years, for app CSS.

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

src/lib/components/simple/dates/YearSelector.svelte · Markdown for LLMs

Choose a year

Years from min to max in a grid that scrolls to the chosen year when it appears.

1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035

2026

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

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

<div class="example">
	<YearSelector bind:selectedYear min={1990} max={2035} aria-label="Year" />
	<p>{selectedYear ?? 'No year chosen'}</p>
</div>

<style>
	.example {
		display: grid;
		gap: 12px;
		width: 300px;
		--year-selector-max-height: 170px;
	}

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

Props

NameTypeDefaultDescription
selectedYear bindable numberChosen year.
min number1900First year of the grid.
max number2100Last year of the grid.
disabled booleanfalseShows the years without letting anyone choose them.
locale stringBCP 47 locale of the default accessible name: it and en are built in, other languages fall back to English. Defaults to the one set with setDefaultLocale, else en.
class { container?: string; year?: string }{}Extra classes for each part.
yearSelectorElement bindable HTMLDivElementThe grid element, which scrolls.

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

Snippets

NameParametersDescription
itemSnippet{ year: number; selected: boolean; current: boolean }Replaces the number of a year. The cell around it keeps click, keyboard and state.

Events

NameTypeDescription
onchange(event: { year: number }) => voidCalled when the user chooses a year.

Methods

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

SignatureDescription
focus()Moves the focus into the grid: to the last focused year, else the chosen one or the current one.

CSS variables

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

Per-instance variableDefault
--year-selector-columns
--year-selector-default-columns 4
--year-selector-gap
--year-selector-default-gap 6px
--year-selector-width
--year-selector-default-width 100%
--year-selector-height
--year-selector-default-height auto
--year-selector-max-height
--year-selector-default-max-height 240px
--year-selector-padding
--year-selector-default-padding 8px 0
--year-selector-border-radius
--year-selector-default-border-radius var(--global-radius-sm)
--year-selector-background
--year-selector-default-background transparent
--year-selector-color
--year-selector-default-color var(--global-color-text-2)
--year-selector-font-family
--year-selector-default-font-family var(--global-font-family-mono)
--year-selector-font-size
--year-selector-default-font-size 14px
--year-selector-font-weight
--year-selector-default-font-weight 500
--year-selector-current-border-color
--year-selector-default-current-border-color var(--global-color-border-strong)
--year-selector-hover-background
--year-selector-default-hover-background var(--global-color-surface-2)
--year-selector-hover-color
--year-selector-default-hover-color var(--global-color-text)
--year-selector-focus-ring-color
--year-selector-default-focus-ring-color var(--global-focus-ring-color)
--year-selector-selected-background
--year-selector-default-selected-background var(--global-fill-primary)
--year-selector-selected-color
--year-selector-default-selected-color var(--global-color-on-primary)
--year-selector-selected-box-shadow
--year-selector-default-selected-box-shadow var(--global-shadow-fill)
--year-selector-disabled-opacity
--year-selector-default-disabled-opacity 0.35