UNPKG

1.64 kBPlain TextView Raw
1import { createContext, useContext } from "react";
2
3import { CalendarDay } from "./classes/CalendarDay.js";
4import { CalendarMonth } from "./classes/CalendarMonth.js";
5import type { DayPickerProps } from "./types/props.js";
6import type { SelectedValue, SelectHandler } from "./types/selection.js";
7import { Modifiers } from "./types/shared.js";
8
9/** @private */
10export const dayPickerContext = createContext<
11 DayPickerContext<DayPickerProps> | undefined
12>(undefined);
13
14export type DayPickerContext<T extends DayPickerProps> = {
15 /** The months displayed in the calendar. */
16 months: CalendarMonth[];
17 /** The next month to display. */
18 nextMonth: Date | undefined;
19 /** The previous month to display. */
20 previousMonth: Date | undefined;
21 /** Navigate to the specified month. Will fire the `onMonthChange` callback. */
22 goToMonth: (month: Date) => void;
23 /** Returns the modifiers for the given day. */
24 getModifiers: (day: CalendarDay) => Modifiers;
25 /** The selected date(s). */
26 selected: SelectedValue<T> | undefined;
27 /** Set a selection. */
28 select: SelectHandler<T> | undefined;
29 /** Whether the given date is selected. */
30 isSelected: ((date: Date) => boolean) | undefined;
31};
32
33/**
34 * Return the context to work with `<DayPicker />` inside custom components.
35 *
36 * @group Hooks
37 * @see https://daypicker.dev/guides/custom-components
38 */
39export function useDayPicker<T extends DayPickerProps>(props?: T) {
40 const context = useContext(dayPickerContext);
41 if (context === undefined) {
42 throw new Error("useDayPicker() must be used within a custom component.");
43 }
44 return context as DayPickerContext<T>;
45}
46
\No newline at end of file