UNPKG

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