# PeriodSelector

Panel to choose a period: a column of presets (today, this month, ...), a month for a custom range (two side by side with `months={2}`), a relative period ("last 3 months", counting today) and, at the bottom, the start and end dates, which can also be typed. Its size does not change between the views. Dates that are not a preset highlight no entry of the list; the relative period goes back to the calendar with a preset or with "Choose dates on the calendar". Every change updates `timespanSettings` at once and calls `onchange` with the days it covers; `PeriodPicker` shows it in a menu with Cancel and Apply. The value has the JSON shape the app backends save (`quick`, `rolling` or `picker`) and the days follow their meaning (see `resolvePeriod`). A range can stay open: with one day chosen it runs from that day on. On screens up to 768px wide, or with `compact`, the presets wrap as chips and one month is shown. Its state is exposed as `data-compact` and `data-view` (`calendar` or `rolling`) for app CSS.

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

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

## Examples

### Panel

`PeriodSelector` is the panel of `PeriodPicker` on its own, for a page or a side panel: every click changes the value at once and calls `onchange` with the days it covers.

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

	let timespanSettings = $state<TimespanSettings>({
		method: 'picker',
		values: { from: new Date(2026, 8, 10), to: new Date(2026, 8, 23) }
	});
	let days = $state('');
</script>

<div class="demo">
	<div class="panel">
		<PeriodSelector
			bind:timespanSettings
			onchange={({ from, to }) => (days = [from?.toDateString(), to?.toDateString()].join(' → '))}
		/>
	</div>
	<p>{days || 'Pick a period'}</p>
</div>

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

	.panel {
		max-width: 100%;
		overflow: hidden;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface-pop);
	}

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

### Two months

`months={2}` shows two months side by side, handy for ranges across the end of a month. On screens up to 768px wide the panel shows one month anyway.

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

	let timespanSettings = $state<TimespanSettings>({
		method: 'picker',
		values: { from: new Date(2026, 7, 24), to: new Date(2026, 8, 6) }
	});
</script>

<div class="panel">
	<PeriodSelector bind:timespanSettings months={2} />
</div>

<style>
	.panel {
		width: fit-content;
		max-width: 100%;
		overflow: hidden;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface-pop);
	}
</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. |
| `months` | `1 \| 2` | `1` | Months shown for the custom range: one, or two side by side. A compact panel always shows one. |
| `compact` | `boolean` |  | Shows the presets as chips and one month, as on a phone. Defaults to `true` on screens up to 768px wide. |
| `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. |
| `periodSelectorElement` (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 |
| --- | --- | --- |
| `presetSnippet` | `{ preset: PeriodPreset<T>; selected: boolean }` | Replaces the content of a preset in the list. |
| `actionsSnippet` |  | Content at the end of the bottom bar, such as the buttons of `PeriodPicker`. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(change: { timespanSettings: TimespanSettings<T>; from?: Date; to?: Date }) => void` | Called after every change, 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`). |

## 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: `<PeriodSelector --period-selector-background="tomato">`. The `--period-selector-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--period-selector-width` | `--period-selector-default-width`: `auto` |
| `--period-selector-max-width` | `--period-selector-default-max-width`: `100%` |
| `--period-selector-min-height` | `--period-selector-default-min-height`: `0` |
| `--period-selector-background` | `--period-selector-default-background`: `transparent` |
| `--period-selector-color` | `--period-selector-default-color`: `var(--global-color-text)` |
| `--period-selector-font-size` | `--period-selector-default-font-size`: `13px` |
| `--period-selector-presets-width` | `--period-selector-default-presets-width`: `176px` |
| `--period-selector-presets-padding` | `--period-selector-default-presets-padding`: `8px` |
| `--period-selector-divider-color` | `--period-selector-default-divider-color`: `var(--global-color-border)` |
| `--period-selector-presets-background` | `--period-selector-default-presets-background`: `var(--global-color-surface)` |
| `--period-selector-preset-padding` | `--period-selector-default-preset-padding`: `7px 10px` |
| `--period-selector-preset-border-radius` | `--period-selector-default-preset-border-radius`: `var(--global-radius-sm)` |
| `--period-selector-preset-color` | `--period-selector-default-preset-color`: `var(--global-color-text-2)` |
| `--period-selector-preset-font-size` | `--period-selector-default-preset-font-size`: `13px` |
| `--period-selector-preset-days-color` | `--period-selector-default-preset-days-color`: `var(--global-color-text-3)` |
| `--period-selector-preset-days-font-size` | `--period-selector-default-preset-days-font-size`: `10.5px` |
| `--period-selector-preset-hover-background` | `--period-selector-default-preset-hover-background`: `var(--global-color-surface-2)` |
| `--period-selector-preset-hover-color` | `--period-selector-default-preset-hover-color`: `var(--global-color-text)` |
| `--period-selector-preset-selected-background` | `--period-selector-default-preset-selected-background`: `var(--global-color-primary-soft)` |
| `--period-selector-preset-selected-color` | `--period-selector-default-preset-selected-color`: `var(--global-color-text)` |
| `--period-selector-preset-selected-days-color` | `--period-selector-default-preset-selected-days-color`: `var(--global-color-primary)` |
| `--period-selector-months-padding` | `--period-selector-default-months-padding`: `12px 16px 8px` |
| `--period-selector-months-gap` | `--period-selector-default-months-gap`: `24px` |
| `--period-selector-month-width` | `--period-selector-default-month-width`: `252px` |
| `--period-selector-month-title-color` | `--period-selector-default-month-title-color`: `var(--global-color-text)` |
| `--period-selector-month-title-font-size` | `--period-selector-default-month-title-font-size`: `14px` |
| `--period-selector-rolling-summary-color` | `--period-selector-default-rolling-summary-color`: `var(--global-color-text)` |
| `--period-selector-footer-padding` | `--period-selector-default-footer-padding`: `10px 16px 12px` |
| `--period-selector-field-width` | `--period-selector-default-field-width`: `100%` |
| `--period-selector-field-height` | `--period-selector-default-field-height`: `32px` |
| `--period-selector-field-font-size` | `--period-selector-default-field-font-size`: `12.5px` |
| `--period-selector-field-padding-x` | `--period-selector-default-field-padding-x`: `10px` |
| `--period-selector-field-label-color` | `--period-selector-default-field-label-color`: `var(--global-color-text-3)` |
| `--period-selector-duration-background` | `--period-selector-default-duration-background`: `var(--global-color-surface-2)` |
| `--period-selector-duration-color` | `--period-selector-default-duration-color`: `var(--global-color-text-2)` |
| `--period-selector-duration-font-size` | `--period-selector-default-duration-font-size`: `12px` |

### Other defaults

| Variable | Value |
| --- | --- |
| `--period-selector-default-group-gap` | `6px` |
| `--period-selector-default-arrow-size` | `28px` |
| `--period-selector-default-footer-row-height` | `28px` |
