# PeriodPicker

Field to choose a period, such as the range of a report or of a dashboard widget: a bar with the name of the period and its dates that opens a `PeriodSelector` in a menu, or in a bottom drawer on screens up to 1024px wide. A preset applies at once and closes the menu; a custom range or a relative period stays a draft until Apply, and Cancel, Escape or a click outside discard it. The two arrows at the start move the period back or forth by its length (by one month, or one year, when it covers whole months): the result is a range of dates, named "August 2026", "2025" or after its day when it is a whole month, year or day, else shown as its dates, and it goes back to a preset when it lands exactly on its days ("This year" ‹ › is "This year" again); `shiftable={false}` hides them. The field is at least 300px wide (`--period-picker-min-width`), so it does not jump while the period changes. The value has the JSON shape the backends save, and `onchange` also gets the days it covers. Its state is exposed as `data-open`, `data-disabled` and `data-empty` for app CSS.

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

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

## Examples

### Basic

The field shows the name of the period and its dates. A preset applies at once; a custom range or a relative period is applied with Apply. The arrows move the period back and forth by its length.

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

	let timespanSettings = $state<TimespanSettings>({ method: 'quick', values: 'this-month' });
	let days = $state('');
</script>

<div class="demo">
	<PeriodPicker
		bind:timespanSettings
		onchange={({ from, to }) => (days = [from?.toDateString(), to?.toDateString()].join(' → '))}
	/>
	<pre>{JSON.stringify(timespanSettings)}</pre>
	{#if days}<p>{days}</p>{/if}
</div>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

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

### Presets of the app

`quickSelectOptions` replaces the presets; those the library does not know need a `range`, which returns their first and last day. Remember that the backend must understand the saved values too. Texts and dates follow `locale`.

```svelte
<script lang="ts">
	import { PeriodPicker, type PeriodPreset, type TimespanSettings } from '@likable-hair/svelte';

	type Preset = 'yesterday' | 'last-7-days' | 'last-30-days' | 'this-month';

	const day = (now: Date, offset: number) => new Date(now.getFullYear(), now.getMonth(), now.getDate() + offset);

	const presets: PeriodPreset<Preset>[] = [
		{ value: 'yesterday', label: 'Ieri', range: (now) => [day(now, -1), day(now, -1)] },
		{ value: 'last-7-days', label: 'Ultimi 7 giorni', range: (now) => [day(now, -6), day(now, 0)] },
		{ value: 'last-30-days', label: 'Ultimi 30 giorni', range: (now) => [day(now, -29), day(now, 0)] },
		{ value: 'this-month', label: 'Questo mese' }
	];

	let timespanSettings = $state<TimespanSettings<Preset>>({ method: 'quick', values: 'last-7-days' });
</script>

<PeriodPicker bind:timespanSettings quickSelectOptions={presets} locale="it" />
```

### Values and states

A custom range, a relative period, an open range (from a day on), no value with its placeholder, `shiftable={false}` without the arrows, and `disabled`.

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

	let custom = $state<TimespanSettings>({
		method: 'picker',
		values: { from: new Date(2026, 8, 10), to: new Date(2026, 8, 23) }
	});
	let rolling = $state<TimespanSettings>({
		method: 'rolling',
		values: { numberOfUnits: 3, measurementUnit: 'months', direction: 'last' }
	});
	let open = $state<TimespanSettings>({ method: 'picker', values: { from: new Date(2026, 8, 1) } });
	let empty = $state<TimespanSettings>();
	let fixed = $state<TimespanSettings>({ method: 'quick', values: 'this-year' });
	let off = $state<TimespanSettings>({ method: 'quick', values: 'total' });
</script>

<div class="demo">
	<PeriodPicker bind:timespanSettings={custom} />
	<PeriodPicker bind:timespanSettings={rolling} />
	<PeriodPicker bind:timespanSettings={open} />
	<PeriodPicker bind:timespanSettings={empty} />
	<PeriodPicker bind:timespanSettings={fixed} shiftable={false} />
	<PeriodPicker bind:timespanSettings={off} disabled />
</div>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `timespanSettings` (bindable) | `TimespanSettings<T>` |  | The chosen period, in the JSON shape the backends save: `{ method: 'quick', values: 'this-month' }`, `{ method: 'rolling', values: { numberOfUnits, measurementUnit, direction } }` or `{ method: 'picker', values: { from, to } }`. |
| `quickSelectOptions` | `PeriodPreset<T>[]` |  | The presets of the list. Defaults to today, this week, this month, last 3 months, last 6 months, this year and all time, the ones the backends understand. Presets of the app need a `range`. |
| `locale` | `string` |  | BCP 47 locale of the texts, of the names of months and days and of the date format: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `weekStart` | `0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6` |  | First day of the week, from 0 (Sunday) to 6. It changes the calendars and the days of "this week". Defaults to the one of `locale` for the calendars and to Monday for "this week", as the backends. |
| `min` | `Date` |  | Earliest day that can be chosen in the calendars. |
| `max` | `Date` |  | Latest day that can be chosen in the calendars. |
| `shiftable` | `boolean` | `true` | Shows the arrows that move the period back and forth, at the start of the field. They are off for "all time" and for open ranges. |
| `months` | `1 \| 2` | `1` | Months shown in the menu for the custom range: one, or two side by side. The bottom drawer always shows one. |
| `disabled` | `boolean` | `false` | Disables the field and the arrows. |
| `open` (bindable) | `boolean` |  | Whether the menu is open. |
| `placement` | `'bottom-start' \| 'bottom-end' \| 'bottom' \| 'top-start' \| 'top-end' \| 'top'` | `'bottom-start'` | Side of the field and alignment of the menu. |
| `placeholder` | `string` |  | Text of the field while no period is chosen. Defaults to "Select a period", in the language of `locale`. |
| `applyLabel` | `string` |  | Text of the button that confirms the draft. Defaults to "Apply", in the language of `locale`. |
| `cancelLabel` | `string` |  | Text of the button that discards the draft. Defaults to "Cancel", in the language of `locale`. |
| `previousLabel` | `string` |  | Name of the arrow that moves the period back. Defaults to "Previous period", in the language of `locale`. |
| `nextLabel` | `string` |  | Name of the arrow that moves the period forth. Defaults to "Next period", in the language of `locale`. |
| `rollingLabel` | `string` |  | Text of the relative period in the list. Defaults to "Relative period", in the language of `locale`. |
| `class` | `string` | `''` | Extra classes on the root element. |
| `periodPickerElement` (bindable) | `HTMLDivElement` |  | The root element. |

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 |
| --- | --- | --- |
| `labelSnippet` | `{ label: string; dates: string; from?: Date; to?: Date; timespanSettings?: TimespanSettings<T> }` | Replaces the name and the dates in the field. `label` is the name of the period, `dates` its days as text. |
| `presetSnippet` | `{ preset: PeriodPreset<T>; selected: boolean }` | Replaces the content of a preset in the list. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(change: { timespanSettings: TimespanSettings<T>; from?: Date; to?: Date }) => void` | Called when the period changes (a preset, Apply or an arrow), with the new value and the days it covers (`from` and `to` at local midnight, both included; missing for an open end and for `total`). |
| `onclose` | `() => void` | Called whenever the menu closes. |

## Types

Exported by the package: `import type { TimespanSettings, PeriodPreset, PeriodQuickValue, PeriodRolling, PeriodDates } from '@likable-hair/svelte';`

### TimespanSettings

The value of `PeriodPicker` and `PeriodSelector`, as saved by the app backends: a preset
(`quick`), a period relative to today (`rolling`) or two days (`picker`). The same JSON shape as
v4. `T` is the type of the preset values, `PeriodQuickValue` unless the app passes its own presets.

`type TimespanSettings<T extends string = PeriodQuickValue> = { method?: 'quick'; values?: T } \| { method?: 'rolling'; values?: PeriodRolling } \| { method?: 'picker'; values?: PeriodDates }`

### PeriodPreset

A preset of `PeriodPicker` and `PeriodSelector`.

`type PeriodPreset<T extends string = PeriodQuickValue> = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `value` (required) | `T` | Value saved in `timespanSettings.values`. |
| `label` (required) | `string` | Text of the preset in the list and in the field. |
| `range` | `(now: Date) => [Date \| undefined, Date \| undefined]` | First and last day of the preset for a given `now`, both included; `undefined` for an open end. Needed for presets other than the default ones, which the library already knows. |

### PeriodQuickValue

The presets that `PeriodPicker` offers by default, and the only ones the app backends understand.

`type PeriodQuickValue = 'today' \| 'this-week' \| 'this-month' \| 'last-3month' \| 'last-6month' \| 'this-year' \| 'total'`

### PeriodRolling

A period relative to today, such as "last 3 months" or "next 2 weeks".

`type PeriodRolling = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `numberOfUnits` | `number` | How many units, a whole number from 1. |
| `measurementUnit` | `'days' \| 'weeks' \| 'months' \| 'years'` | Unit of `numberOfUnits`. |
| `direction` | `'last' \| 'next'` | `last` goes back from today, `next` forward. |

### PeriodDates

Two chosen days, both included. One of them can be missing for an open range.

`type PeriodDates = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `from` | `Date \| string` | First day. A `Date` at local midnight; a string is read as an ISO date or timestamp (what a JSON round trip gives back). |
| `to` | `Date \| string` | Last day, included. Same formats as `from`. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--period-picker-width` | `--period-picker-default-width`: `auto` |
| `--period-picker-min-width` | `--period-picker-default-min-width`: `300px` |
| `--period-picker-height` | `--period-picker-default-height`: `36px` |
| `--period-picker-padding` | `--period-picker-default-padding`: `3px` |
| `--period-picker-border-width` | `--period-picker-default-border-width`: `1px` |
| `--period-picker-border-color` | `--period-picker-default-border-color`: `var(--global-color-border-strong)` |
| `--period-picker-border-radius` | `--period-picker-default-border-radius`: `var(--global-radius-md)` |
| `--period-picker-background` | `--period-picker-default-background`: `var(--global-color-surface-solid)` |
| `--period-picker-box-shadow` | `--period-picker-default-box-shadow`: `var(--global-shadow-sm)` |
| `--period-picker-color` | `--period-picker-default-color`: `var(--global-color-text)` |
| `--period-picker-font-size` | `--period-picker-default-font-size`: `13px` |
| `--period-picker-divider-color` | `--period-picker-default-divider-color`: `var(--global-color-border)` |
| `--period-picker-hover-background` | `--period-picker-default-hover-background`: `var(--global-color-surface-2)` |
| `--period-picker-disabled-opacity` | `--period-picker-default-disabled-opacity`: `0.5` |
| `--period-picker-icon-color` | `--period-picker-default-icon-color`: `var(--global-color-primary)` |
| `--period-picker-icon-size` | `--period-picker-default-icon-size`: `16px` |
| `--period-picker-label-font-weight` | `--period-picker-default-label-font-weight`: `600` |
| `--period-picker-placeholder-color` | `--period-picker-default-placeholder-color`: `var(--global-color-text-3)` |
| `--period-picker-dates-color` | `--period-picker-default-dates-color`: `var(--global-color-text-2)` |
| `--period-picker-dates-font-size` | `--period-picker-default-dates-font-size`: `12px` |
| `--period-picker-chevron-color` | `--period-picker-default-chevron-color`: `var(--global-color-text-3)` |
