Skip to main content

useCalendar

The main calendar hook providing state, actions, and prop getters for building date pickers.

import { useCalendar } from '@gentleduck/calendar'
import { useCalendar } from '@gentleduck/calendar'

Usage

const { state, actions, getDayProps, getGridProps, getNavProps, getHeaderProps, announcer } =
  useCalendar({
    adapter,
    mode: 'single',
  })
const { state, actions, getDayProps, getGridProps, getNavProps, getHeaderProps, announcer } =
  useCalendar({
    adapter,
    mode: 'single',
  })

Config

useCalendar accepts a UseCalendar.IConfig<TDate, M> object:

PropTypeDefaultDescription
adapterAdapter.IDateAdapter<TDate>requiredDate adapter instance
modeSelection.SelectionModerequired'single', 'range', 'multi', or 'multi-range'
localeCalendar.ICalendarLocaleConfig-Locale, week start day, and direction
monthTDate-Controlled displayed month
defaultMonthTDateadapter.today()Default month (uncontrolled)
selectedSelection.CalendarValue<TDate, M>-Controlled selection value
defaultSelectedSelection.CalendarValue<TDate, M>-Default selection (uncontrolled)
onSelect(value) => void-Called when selection changes
onMonthChange(month) => void-Called when displayed month changes
onDismiss() => void-Called on Escape
numberOfMonthsnumber1Number of months to display side by side
showOutsideDaysbooleantrueShow days from adjacent months
fixedWeeksbooleanfalseAlways show 6 weeks
disabledTDate[] | (date) => boolean-Dates that cannot be selected
fromDateTDate-Earliest selectable date
toDateTDate-Latest selectable date

Calendar.ICalendarLocaleConfig

PropertyTypeDescription
localestringBCP 47 locale tag (e.g. 'en-US', 'ar-SA', 'fa-IR')
weekStartDay0 | 1 | 2 | 3 | 4 | 5 | 6Day the week starts on (0 = Sunday)
direction'ltr' | 'rtl'Text direction

Return value

state

PropertyTypeDescription
monthTDateCurrently visible month
valueSelection.CalendarValue<TDate, M>Current selection. Shape depends on mode
focusedDateTDateDate with keyboard focus
viewModeCalendar.ViewMode'days', 'months', or 'years'
weeksGrid.ICalendarWeek<TDate>[]Decorated weeks for the first month
monthsGrid.ICalendarMonth<TDate>[]All month grids (for multi-month)
weekdaysstring[]Localized weekday labels (7 items)
canGoNextbooleanForward navigation available (respects toDate)
canGoPreviousbooleanBackward navigation available (respects fromDate)

actions

MethodSignatureDescription
setMonth(month: TDate) => voidSet displayed month
setViewMode(mode: Calendar.ViewMode) => voidSwitch between day/month/year view
goToNext() => voidNavigate to next month
goToPrevious() => voidNavigate to previous month
selectDate(date: TDate) => voidSelect a date
focusDate(date: TDate) => voidMove keyboard focus to a date

Prop getters

GetterSignatureDescription
getDayProps(day: Grid.ICalendarDay<TDate>) => DayPropsSpread onto each day cell
getGridProps() => GridPropsSpread onto the grid container
getNavProps(direction: 'prev' | 'next') => NavPropsSpread onto nav buttons
getHeaderProps() => HeaderPropsSpread onto the month header

announcer

An AnnouncerReturn object with an AnnouncerPortal component. Render it inside the calendar tree for screen reader announcements.


Selection modes

Single

const { state } = useCalendar({ adapter, mode: 'single' })
// state.value: Date | null
const { state } = useCalendar({ adapter, mode: 'single' })
// state.value: Date | null

Range

const { state } = useCalendar({ adapter, mode: 'range' })
// state.value: { from: Date, to: Date | null } | null
const { state } = useCalendar({ adapter, mode: 'range' })
// state.value: { from: Date, to: Date | null } | null

Multi

const { state } = useCalendar({ adapter, mode: 'multi' })
// state.value: Date[]
const { state } = useCalendar({ adapter, mode: 'multi' })
// state.value: Date[]

Multi-month

const { state } = useCalendar({
  adapter,
  mode: 'range',
  numberOfMonths: 2,
})
 
// state.months - array of Grid.ICalendarMonth objects
// state.weeks - first month's weeks (backward compat)
const { state } = useCalendar({
  adapter,
  mode: 'range',
  numberOfMonths: 2,
})
 
// state.months - array of Grid.ICalendarMonth objects
// state.weeks - first month's weeks (backward compat)