UNPKG

12.9 kBTypeScriptView Raw
1import type { CSSProperties } from "react";
2import { UI, DayFlag, SelectionState } from "../UI.js";
3import * as components from "../components/custom-components.js";
4import { formatCaption, formatDay, formatMonthCaption, formatMonthDropdown, formatWeekdayName, formatWeekNumber, formatWeekNumberHeader, formatYearCaption, formatYearDropdown } from "../formatters/index.js";
5import { labelDayButton, labelNav, labelGrid, labelGridcell, labelMonthDropdown, labelNext, labelPrevious, labelWeekday, labelWeekNumber, labelWeekNumberHeader, labelYearDropdown } from "../labels/index.js";
6/**
7 * Selection modes supported by DayPicker.
8 *
9 * - `single`: use DayPicker to select single days.
10 * - `multiple`: allow selecting multiple days.
11 * - `range`: use DayPicker to select a range of days.
12 *
13 * @see https://daypicker.dev/docs/selection-modes
14 */
15export type Mode = "single" | "multiple" | "range";
16/**
17 * The components that can be changed using the `components` prop.
18 *
19 * @see https://daypicker.dev/guides/custom-components
20 */
21export type CustomComponents = {
22 /**
23 * Render any button element in DayPicker.
24 *
25 * @deprecated Use {@link CustomComponents.NextMonthButton} or
26 * {@link CustomComponents.PreviousMonthButton} instead.
27 */
28 Button: typeof components.Button;
29 /** Render the chevron icon used in the navigation buttons and dropdowns. */
30 Chevron: typeof components.Chevron;
31 /** Render the caption label of the month grid. */
32 CaptionLabel: typeof components.CaptionLabel;
33 /** Render the day cell in the month grid. */
34 Day: typeof components.Day;
35 /** Render the button containing the day in the day cell. */
36 DayButton: typeof components.DayButton;
37 /** Render the dropdown element to select years and months. */
38 Dropdown: typeof components.Dropdown;
39 /** Render the container of the dropdowns. */
40 DropdownNav: typeof components.DropdownNav;
41 /** Render the footer element announced by screen readers. */
42 Footer: typeof components.Footer;
43 /** Render the container of the MonthGrid. */
44 Month: typeof components.Month;
45 /** Render the caption of the month grid. */
46 MonthCaption: typeof components.MonthCaption;
47 /** Render the grid of days in a month. */
48 MonthGrid: typeof components.MonthGrid;
49 /** Wrapper of the month grids. */
50 Months: typeof components.Months;
51 /** Render the navigation element with the next and previous buttons. */
52 Nav: typeof components.Nav;
53 /** Render the `<option>` HTML element in the dropdown. */
54 Option: typeof components.Option;
55 /** Render the previous month button element in the navigation. */
56 PreviousMonthButton: typeof components.PreviousMonthButton;
57 /** Render the next month button element in the navigation. */
58 NextMonthButton: typeof components.NextMonthButton;
59 /** Render the root element of the calendar. */
60 Root: typeof components.Root;
61 /** Render the select element in the dropdowns. */
62 Select: typeof components.Select;
63 /** Render the weeks section in the month grid. */
64 Weeks: typeof components.Weeks;
65 /** Render the week rows. */
66 Week: typeof components.Week;
67 /** Render the weekday name in the header. */
68 Weekday: typeof components.Weekday;
69 /** Render the row containing the week days. */
70 Weekdays: typeof components.Weekdays;
71 /** Render the cell with the number of the week. */
72 WeekNumber: typeof components.WeekNumber;
73 /** Render the header of the week number column. */
74 WeekNumberHeader: typeof components.WeekNumberHeader;
75 /** Render the dropdown with the months. */
76 MonthsDropdown: typeof components.MonthsDropdown;
77 /** Render the dropdown with the years. */
78 YearsDropdown: typeof components.YearsDropdown;
79};
80/** Represent a map of formatters used to render localized content. */
81export type Formatters = {
82 /** Format the caption of a month grid. */
83 formatCaption: typeof formatCaption;
84 /** Format the day in the day cell. */
85 formatDay: typeof formatDay;
86 /** Format the label in the month dropdown. */
87 formatMonthDropdown: typeof formatMonthDropdown;
88 /** @deprecated Use {@link Formatters.formatCaption} instead. */
89 formatMonthCaption: typeof formatMonthCaption;
90 /** Format the week number. */
91 formatWeekNumber: typeof formatWeekNumber;
92 /** Format the header of the week number column. */
93 formatWeekNumberHeader: typeof formatWeekNumberHeader;
94 /** Format the week day name in the header. */
95 formatWeekdayName: typeof formatWeekdayName;
96 /** Format the label in the year dropdown. */
97 formatYearDropdown: typeof formatYearDropdown;
98 /** @deprecated Use {@link Formatters.formatYearDropdown} instead. */
99 formatYearCaption: typeof formatYearCaption;
100};
101/** Map of functions to translate ARIA labels for the relative elements. */
102export type Labels = {
103 /** The label for the navigation toolbar. */
104 labelNav: typeof labelNav;
105 /** The label for the month grid. */
106 labelGrid: typeof labelGrid;
107 /** The label for the gridcell, when the calendar is not interactive. */
108 labelGridcell: typeof labelGridcell;
109 /** The label for the month dropdown. */
110 labelMonthDropdown: typeof labelMonthDropdown;
111 /** The label for the year dropdown. */
112 labelYearDropdown: typeof labelYearDropdown;
113 /** The label for the "next month" button. */
114 labelNext: typeof labelNext;
115 /** The label for the "previous month" button. */
116 labelPrevious: typeof labelPrevious;
117 /** The label for the day button. */
118 labelDayButton: typeof labelDayButton;
119 /** @deprecated Use {@link labelDayButton} instead. */
120 labelDay: typeof labelDayButton;
121 /** The label for the weekday. */
122 labelWeekday: typeof labelWeekday;
123 /** The label for the week number. */
124 labelWeekNumber: typeof labelWeekNumber;
125 /**
126 * Return the label for the column of the week number.
127 *
128 * @since 9.0.0
129 */
130 labelWeekNumberHeader: typeof labelWeekNumberHeader;
131};
132/**
133 * A value or a function that matches a specific day.
134 *
135 * @example
136 * // will always match the day
137 * const booleanMatcher: Matcher = true;
138 *
139 * // will match the today's date
140 * const dateMatcher: Matcher = new Date();
141 *
142 * // will match the days in the array
143 * const arrayMatcher: Matcher = [
144 * new Date(2019, 1, 2),
145 * new Date(2019, 1, 4)
146 * ];
147 *
148 * // will match days after the 2nd of February 2019
149 * const afterMatcher: DateAfter = { after: new Date(2019, 1, 2) };
150 *
151 * // will match days before the 2nd of February 2019 }
152 * const beforeMatcher: DateBefore = { before: new Date(2019, 1, 2) };
153 *
154 * // will match Sundays
155 * const dayOfWeekMatcher: DayOfWeek = {
156 * dayOfWeek: 0
157 * };
158 *
159 * // will match the included days, except the two dates
160 * const intervalMatcher: DateInterval = {
161 * after: new Date(2019, 1, 2),
162 * before: new Date(2019, 1, 5)
163 * };
164 *
165 * // will match the included days, including the two dates
166 * const rangeMatcher: DateRange = {
167 * from: new Date(2019, 1, 2),
168 * to: new Date(2019, 1, 5)
169 * };
170 *
171 * // will match when the function return true
172 * const functionMatcher: Matcher = (day: Date) => {
173 * return day.getMonth() === 2; // match when month is March
174 * };
175 */
176export type Matcher = boolean | ((date: Date) => boolean) | Date | Date[] | DateRange | DateBefore | DateAfter | DateInterval | DayOfWeek;
177/**
178 * Match a day falling after the specified date, with the date not included.
179 *
180 * @example
181 * // Match days after the 2nd of February 2019
182 * const matcher: DateAfter = { after: new Date(2019, 1, 2) };
183 */
184export type DateAfter = {
185 after: Date;
186};
187/**
188 * Match a day falling before the specified date, with the date not included.
189 *
190 * @example
191 * // Match days before the 2nd of February 2019
192 * const matcher: DateBefore = { before: new Date(2019, 1, 2) };
193 */
194export type DateBefore = {
195 before: Date;
196};
197/**
198 * An interval of dates. Differently from {@link DateRange}, the range ends here
199 * are not included.
200 *
201 * @example
202 * // Match the days between the 2nd and the 5th of February 2019
203 * const matcher: DateInterval = {
204 * after: new Date(2019, 1, 2),
205 * before: new Date(2019, 1, 5)
206 * };
207 */
208export type DateInterval = {
209 before: Date;
210 after: Date;
211};
212/**
213 * A range of dates. The range can be open. Differently from
214 * {@link DateInterval}, the range ends here are included.
215 *
216 * @example
217 * // Match the days between the 2nd and the 5th of February 2019
218 * const matcher: DateRange = {
219 * from: new Date(2019, 1, 2),
220 * to: new Date(2019, 1, 5)
221 * };
222 */
223export type DateRange = {
224 from: Date | undefined;
225 to?: Date | undefined;
226};
227/**
228 * Match dates being one of the specified days of the week (`0-6`, where `0` is
229 * Sunday).
230 *
231 * @example
232 * // Match Sundays
233 * const matcher: DayOfWeek = { dayOfWeek: 0 };
234 * // Match weekends
235 * const matcher: DayOfWeek = { dayOfWeek: [0, 6] };
236 */
237export type DayOfWeek = {
238 dayOfWeek: number | number[];
239};
240/**
241 * The event handler triggered when clicking or interacting with a day.
242 *
243 * @template EventType - The event type that triggered the event (e.g.
244 * `React.MouseEvent`, `React.KeyboardEvent`, etc.).
245 * @param date - The date that has triggered the event.
246 * @param modifiers - The modifiers belonging to the date.
247 * @param e - The DOM event that triggered the event.
248 */
249export type DayEventHandler<EventType> = (date: Date, modifiers: Modifiers, e: EventType) => void;
250/**
251 * The event handler when a month is changed in the calendar.
252 *
253 * ```tsx
254 * <DayPicker onMonthChange={(month) => console.log(month)} />
255 * ```
256 *
257 * @see https://daypicker.dev/docs/navigation
258 */
259export type MonthChangeEventHandler = (month: Date) => void;
260/**
261 * The CSS classnames to use for the {@link UI} elements, the
262 * {@link SelectionState} and the {@link DayFlag}.
263 *
264 * @example
265 * const classNames: ClassNames = {
266 * [UI.Root]: "root",
267 * [UI.Outside]: "outside",
268 * [UI.Nav]: "nav"
269 * // etc.
270 * };
271 */
272export type ClassNames = {
273 [key in UI | SelectionState | DayFlag]: string;
274};
275/**
276 * The CSS styles to use for the {@link UI} elements, the {@link SelectionState}
277 * and the {@link DayFlag}.
278 */
279export type Styles = {
280 [key in UI | SelectionState | DayFlag]: CSSProperties | undefined;
281};
282/**
283 * Represents the modifiers that match a specific day in the calendar.
284 *
285 * - Retrieve modifiers using the {@link OnSelectHandler} via the `onSelect` prop,
286 * or within custom components using the {@link useDayPicker} hook.
287 * - Includes built-in modifiers from {@link DayFlag} and {@link SelectionState}.
288 * - Add custom modifiers using the `modifiers` prop.
289 *
290 * @example
291 * const modifiers: Modifiers = {
292 * today: false, // the day is not today
293 * selected: true, // the day is selected
294 * disabled: false, // the day is not disabled
295 * outside: false, // the day is not outside the month
296 * focused: false, // the day is not focused
297 *
298 * weekend: false // custom modifier example for matching a weekend
299 * booked: true // custom modifier example for matching a booked day
300 * available: false // custom modifier example for matching an available day
301 * };
302 *
303 * @see https://daypicker.dev/guides/custom-modifiers
304 */
305export type Modifiers = Record<string, boolean>;
306/**
307 * The style to apply to each day element matching a modifier.
308 *
309 * @example
310 * const modifiersStyles: ModifiersStyles = {
311 * today: { color: "red" },
312 * selected: { backgroundColor: "blue" },
313 * weekend: { color: "green" }
314 * };
315 */
316export type ModifiersStyles = Record<string, CSSProperties>;
317/**
318 * The classnames to assign to each day element matching a modifier.
319 *
320 * @example
321 * const modifiersClassNames: ModifiersClassNames = {
322 * today: "today", // Use the "today" class for the today's day
323 * selected: "highlight", // Use the "highlight" class for the selected day
324 * weekend: "weekend" // Use the "weekend" class for the weekend days
325 * };
326 */
327export type ModifiersClassNames = Record<string, string>;
328/**
329 * The props that have been deprecated since version 9.0.0.
330 *
331 * @private
332 * @since 9.0.0
333 * @see https://daypicker.dev/upgrading
334 */
335export type V9DeprecatedProps =
336/** Use `hidden` prop instead. */
337"fromDate"
338/** Use `hidden` prop instead. */
339 | "toDate"
340/** Use `startMonth` instead. */
341 | "fromMonth"
342/** Use `endMonth` instead. */
343 | "toMonth"
344/** Use `startMonth` instead. */
345 | "fromYear"
346/** Use `endMonth` instead. */
347 | "toYear";
348/** The direction to move the focus relative to the current focused date. */
349export type MoveFocusDir = "after" | "before";
350/** The temporal unit to move the focus by. */
351export type MoveFocusBy = "day" | "week" | "startOfWeek" | "endOfWeek" | "month" | "year";