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

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

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

## Examples

### Choose a year

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

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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `selectedYear` (bindable) | `number` |  | Chosen year. |
| `min` | `number` | `1900` | First year of the grid. |
| `max` | `number` | `2100` | Last year of the grid. |
| `disabled` | `boolean` | `false` | Shows the years without letting anyone choose them. |
| `locale` | `string` |  | BCP 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) | `HTMLDivElement` |  | The 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

| Name | Parameters | Description |
| --- | --- | --- |
| `itemSnippet` | `{ year: number; selected: boolean; current: boolean }` | Replaces the number of a year. The cell around it keeps click, keyboard and state. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { year: number }) => void` | Called when the user chooses a year. |

## Methods

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

| Signature | Description |
| --- | --- |
| `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 variable | Default |
| --- | --- |
| `--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` |
