Navigation
Pure functions for navigating between months, years, and decades.
import { navigate, canNavigate, goToMonth, goToYear, goToNextMonth, goToPrevMonth } from '@gentleduck/calendar'
import type { Navigation, Adapter } from '@gentleduck/calendar'import { navigate, canNavigate, goToMonth, goToYear, goToNextMonth, goToPrevMonth } from '@gentleduck/calendar'
import type { Navigation, Adapter } from '@gentleduck/calendar'Functions
navigate
Navigate to the next or previous month, year, or decade. Pure function.
navigate(adapter: Adapter.IDateAdapter<TDate>, date: TDate, direction: Navigation.Direction, unit: Navigation.Unit) => TDatenavigate(adapter: Adapter.IDateAdapter<TDate>, date: TDate, direction: Navigation.Direction, unit: Navigation.Unit) => TDate| Param | Type | Description |
|---|---|---|
adapter | Adapter.IDateAdapter<TDate> | Date adapter |
date | TDate | Current date |
direction | Navigation.Direction | Navigation direction |
unit | Navigation.Unit | Navigation unit |
Returns the new date after navigation.
canNavigate
Check whether navigation in a given direction is allowed, respecting fromDate and toDate constraints.
canNavigate(
adapter: Adapter.IDateAdapter<TDate>,
date: TDate,
direction: Navigation.Direction,
unit: Navigation.Unit,
constraints?: { fromDate?: TDate; toDate?: TDate }
) => booleancanNavigate(
adapter: Adapter.IDateAdapter<TDate>,
date: TDate,
direction: Navigation.Direction,
unit: Navigation.Unit,
constraints?: { fromDate?: TDate; toDate?: TDate }
) => boolean| Param | Type | Description |
|---|---|---|
adapter | Adapter.IDateAdapter<TDate> | Date adapter |
date | TDate | Current date |
direction | Navigation.Direction | Direction to check |
unit | Navigation.Unit | Navigation unit |
constraints | { fromDate?: TDate; toDate?: TDate } | Optional bounds. fromDate is the earliest navigable date, toDate is the latest. |
Returns true if navigation is within bounds.
Helper functions
goToMonth
Navigate to a specific month within the current year. Used by the month picker view. The month parameter is 0-indexed (0 = January, 11 = December). Returns the 1st of the target month.
import { goToMonth } from '@gentleduck/calendar'
goToMonth(adapter: Adapter.IDateAdapter<TDate>, current: TDate, month: number) => TDateimport { goToMonth } from '@gentleduck/calendar'
goToMonth(adapter: Adapter.IDateAdapter<TDate>, current: TDate, month: number) => TDategoToYear
Navigate to a specific year. Used by the year picker view.
goToYear(adapter: Adapter.IDateAdapter<TDate>, current: TDate, year: number) => TDategoToYear(adapter: Adapter.IDateAdapter<TDate>, current: TDate, year: number) => TDategoToNextMonth
Shorthand for navigate(adapter, date, 'next', 'month').
goToNextMonth(adapter: Adapter.IDateAdapter<TDate>, current: TDate) => TDategoToNextMonth(adapter: Adapter.IDateAdapter<TDate>, current: TDate) => TDategoToPrevMonth
Shorthand for navigate(adapter, date, 'prev', 'month').
goToPrevMonth(adapter: Adapter.IDateAdapter<TDate>, current: TDate) => TDategoToPrevMonth(adapter: Adapter.IDateAdapter<TDate>, current: TDate) => TDate