@octanejs/day-picker 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/UPSTREAM.md +24 -0
- package/package.json +77 -0
- package/src/DayPicker.tsrx +717 -0
- package/src/UI.ts +121 -0
- package/src/classes/CalendarDay.ts +82 -0
- package/src/classes/CalendarMonth.ts +20 -0
- package/src/classes/CalendarWeek.ts +19 -0
- package/src/classes/DateLib.ts +695 -0
- package/src/classes/index.ts +4 -0
- package/src/components/custom-components.tsrx +237 -0
- package/src/formatters/formatCaption.ts +18 -0
- package/src/formatters/formatDay.ts +17 -0
- package/src/formatters/formatMonthDropdown.ts +16 -0
- package/src/formatters/formatWeekNumber.ts +19 -0
- package/src/formatters/formatWeekNumberHeader.ts +11 -0
- package/src/formatters/formatWeekdayName.ts +17 -0
- package/src/formatters/formatYearDropdown.ts +15 -0
- package/src/formatters/index.ts +7 -0
- package/src/helpers/calculateFocusTarget.ts +79 -0
- package/src/helpers/createGetModifiers.ts +117 -0
- package/src/helpers/endOfBroadcastWeek.ts +22 -0
- package/src/helpers/getBroadcastWeeksInMonth.ts +32 -0
- package/src/helpers/getClassNamesForModifiers.ts +38 -0
- package/src/helpers/getComponents.ts +19 -0
- package/src/helpers/getDataAttributes.ts +17 -0
- package/src/helpers/getDates.ts +87 -0
- package/src/helpers/getDays.ts +19 -0
- package/src/helpers/getDefaultClassNames.ts +36 -0
- package/src/helpers/getDisplayMonths.ts +30 -0
- package/src/helpers/getFocusableDate.ts +66 -0
- package/src/helpers/getFormatters.ts +16 -0
- package/src/helpers/getInitialMonth.ts +35 -0
- package/src/helpers/getLabels.ts +93 -0
- package/src/helpers/getMonthOptions.ts +45 -0
- package/src/helpers/getMonths.ts +89 -0
- package/src/helpers/getNavMonth.ts +35 -0
- package/src/helpers/getNextFocus.ts +45 -0
- package/src/helpers/getNextMonth.ts +46 -0
- package/src/helpers/getPreviousMonth.ts +45 -0
- package/src/helpers/getStyleForModifiers.ts +31 -0
- package/src/helpers/getWeekdays.ts +33 -0
- package/src/helpers/getWeeks.ts +14 -0
- package/src/helpers/getYearOptions.ts +43 -0
- package/src/helpers/index.ts +2 -0
- package/src/helpers/startOfBroadcastWeek.ts +26 -0
- package/src/helpers/useControlledValue.ts +38 -0
- package/src/index.ts +13 -0
- package/src/labels/index.ts +12 -0
- package/src/labels/labelDayButton.ts +29 -0
- package/src/labels/labelGrid.ts +18 -0
- package/src/labels/labelGridcell.ts +26 -0
- package/src/labels/labelMonthDropdown.ts +14 -0
- package/src/labels/labelNav.ts +11 -0
- package/src/labels/labelNext.ts +17 -0
- package/src/labels/labelPrevious.ts +13 -0
- package/src/labels/labelWeekNumber.ts +15 -0
- package/src/labels/labelWeekNumberHeader.ts +14 -0
- package/src/labels/labelWeekday.ts +16 -0
- package/src/labels/labelYearDropdown.ts +14 -0
- package/src/locale/af.ts +48 -0
- package/src/locale/ar-DZ.ts +48 -0
- package/src/locale/ar-EG.ts +48 -0
- package/src/locale/ar-MA.ts +48 -0
- package/src/locale/ar-SA.ts +48 -0
- package/src/locale/ar-TN.ts +48 -0
- package/src/locale/ar.ts +48 -0
- package/src/locale/az.ts +48 -0
- package/src/locale/be-tarask.ts +51 -0
- package/src/locale/be.ts +48 -0
- package/src/locale/bg.ts +48 -0
- package/src/locale/bn.ts +48 -0
- package/src/locale/bs.ts +48 -0
- package/src/locale/ca.ts +48 -0
- package/src/locale/ckb.ts +51 -0
- package/src/locale/cs.ts +48 -0
- package/src/locale/cy.ts +48 -0
- package/src/locale/da.ts +48 -0
- package/src/locale/de-AT.ts +48 -0
- package/src/locale/de.ts +48 -0
- package/src/locale/el.ts +48 -0
- package/src/locale/en-AU.ts +48 -0
- package/src/locale/en-CA.ts +48 -0
- package/src/locale/en-GB.ts +51 -0
- package/src/locale/en-IE.ts +48 -0
- package/src/locale/en-IN.ts +48 -0
- package/src/locale/en-NZ.ts +48 -0
- package/src/locale/en-US.ts +72 -0
- package/src/locale/en-ZA.ts +48 -0
- package/src/locale/eo.ts +48 -0
- package/src/locale/es.ts +48 -0
- package/src/locale/et.ts +48 -0
- package/src/locale/eu.ts +48 -0
- package/src/locale/fa-IR.ts +48 -0
- package/src/locale/fi.ts +48 -0
- package/src/locale/fr-CA.ts +48 -0
- package/src/locale/fr-CH.ts +48 -0
- package/src/locale/fr.ts +48 -0
- package/src/locale/fy.ts +48 -0
- package/src/locale/gd.ts +48 -0
- package/src/locale/gl.ts +48 -0
- package/src/locale/gu.ts +48 -0
- package/src/locale/he.ts +48 -0
- package/src/locale/hi.ts +48 -0
- package/src/locale/hr.ts +48 -0
- package/src/locale/ht.ts +48 -0
- package/src/locale/hu.ts +48 -0
- package/src/locale/hy.ts +48 -0
- package/src/locale/id.ts +48 -0
- package/src/locale/is.ts +48 -0
- package/src/locale/it-CH.ts +48 -0
- package/src/locale/it.ts +48 -0
- package/src/locale/ja-Hira.ts +48 -0
- package/src/locale/ja.ts +48 -0
- package/src/locale/ka.ts +48 -0
- package/src/locale/kk.ts +48 -0
- package/src/locale/km.ts +48 -0
- package/src/locale/kn.ts +48 -0
- package/src/locale/ko.ts +48 -0
- package/src/locale/lb.ts +48 -0
- package/src/locale/lt.ts +48 -0
- package/src/locale/lv.ts +48 -0
- package/src/locale/mk.ts +48 -0
- package/src/locale/mn.ts +48 -0
- package/src/locale/ms.ts +48 -0
- package/src/locale/mt.ts +48 -0
- package/src/locale/nb.ts +48 -0
- package/src/locale/nl-BE.ts +48 -0
- package/src/locale/nl.ts +48 -0
- package/src/locale/nn.ts +48 -0
- package/src/locale/oc.ts +48 -0
- package/src/locale/pl.ts +48 -0
- package/src/locale/pt-BR.ts +48 -0
- package/src/locale/pt.ts +48 -0
- package/src/locale/ro.ts +48 -0
- package/src/locale/ru.ts +48 -0
- package/src/locale/se.ts +48 -0
- package/src/locale/sk.ts +48 -0
- package/src/locale/sl.ts +48 -0
- package/src/locale/sq.ts +48 -0
- package/src/locale/sr-Latn.ts +48 -0
- package/src/locale/sr.ts +48 -0
- package/src/locale/sv.ts +48 -0
- package/src/locale/ta.ts +48 -0
- package/src/locale/te.ts +48 -0
- package/src/locale/th.ts +48 -0
- package/src/locale/tr.ts +48 -0
- package/src/locale/ug.ts +48 -0
- package/src/locale/uk.ts +48 -0
- package/src/locale/uz-Cyrl.ts +48 -0
- package/src/locale/uz.ts +48 -0
- package/src/locale/vi.ts +48 -0
- package/src/locale/zh-CN.ts +48 -0
- package/src/locale/zh-HK.ts +51 -0
- package/src/locale/zh-TW.ts +51 -0
- package/src/locale.ts +108 -0
- package/src/noonDateLib.ts +202 -0
- package/src/selection/useMulti.ts +74 -0
- package/src/selection/useRange.ts +89 -0
- package/src/selection/useSingle.ts +77 -0
- package/src/style.css +451 -0
- package/src/style.css.d.ts +3 -0
- package/src/style.module.css +451 -0
- package/src/style.module.css.d.ts +29 -0
- package/src/types/index.ts +3 -0
- package/src/types/props.ts +671 -0
- package/src/types/selection.ts +97 -0
- package/src/types/shared.ts +346 -0
- package/src/useAnimation.ts +242 -0
- package/src/useCalendar.ts +233 -0
- package/src/useDayPicker.ts +91 -0
- package/src/useFocus.ts +109 -0
- package/src/useSelection.ts +36 -0
- package/src/utils/addToRange.ts +91 -0
- package/src/utils/convertMatchersToTimeZone.ts +88 -0
- package/src/utils/dateMatchModifiers.ts +72 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/rangeContainsDayOfWeek.ts +33 -0
- package/src/utils/rangeContainsModifiers.ts +106 -0
- package/src/utils/rangeIncludesDate.ts +40 -0
- package/src/utils/rangeOverlaps.ts +26 -0
- package/src/utils/toTimeZone.ts +13 -0
- package/src/utils/typeguards.ts +71 -0
- package/status.json +13 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { DayPickerProps } from './props.js';
|
|
3
|
+
import type { DateRange, Mode, Modifiers } from './shared.js';
|
|
4
|
+
|
|
5
|
+
/** Selection state and helpers for the active selection mode. */
|
|
6
|
+
export type Selection<T extends DayPickerProps> = {
|
|
7
|
+
/** The selected date(s). */
|
|
8
|
+
selected: SelectedValue<T> | undefined;
|
|
9
|
+
/** Set a selection. */
|
|
10
|
+
select: SelectHandler<T> | undefined;
|
|
11
|
+
/** Whether the given date is selected. */
|
|
12
|
+
isSelected: (date: Date) => boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Selected value for single selection mode, respecting required selections. */
|
|
16
|
+
export type SelectedSingle<T extends { required?: boolean }> = T['required'] extends true
|
|
17
|
+
? Date
|
|
18
|
+
: Date | undefined;
|
|
19
|
+
/** Selected value for multiple selection mode, respecting required selections. */
|
|
20
|
+
export type SelectedMulti<T extends { required?: boolean }> = T['required'] extends true
|
|
21
|
+
? Date[]
|
|
22
|
+
: Date[] | undefined;
|
|
23
|
+
/** Selected value for range selection mode, respecting required selections. */
|
|
24
|
+
export type SelectedRange<T extends { required?: boolean }> = T['required'] extends true
|
|
25
|
+
? DateRange
|
|
26
|
+
: DateRange | undefined;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Represents the selected value based on the selection mode.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Single selection mode
|
|
33
|
+
* const selected: SelectedValue<{ mode: "single" }> = new Date();
|
|
34
|
+
*
|
|
35
|
+
* // Multiple selection mode
|
|
36
|
+
* const selected: SelectedValue<{ mode: "multiple" }> = [
|
|
37
|
+
* new Date(),
|
|
38
|
+
* new Date(),
|
|
39
|
+
* ];
|
|
40
|
+
*
|
|
41
|
+
* // Range selection mode
|
|
42
|
+
* const selected: SelectedValue<{ mode: "range" }> = {
|
|
43
|
+
* from: new Date(),
|
|
44
|
+
* to: new Date(),
|
|
45
|
+
* };
|
|
46
|
+
*/
|
|
47
|
+
export type SelectedValue<T> = T extends { mode: 'single'; required?: boolean }
|
|
48
|
+
? SelectedSingle<T>
|
|
49
|
+
: T extends { mode: 'multiple'; required?: boolean }
|
|
50
|
+
? SelectedMulti<T>
|
|
51
|
+
: T extends { mode: 'range'; required?: boolean }
|
|
52
|
+
? SelectedRange<T>
|
|
53
|
+
: undefined;
|
|
54
|
+
|
|
55
|
+
/** Selection handler for single selection mode. */
|
|
56
|
+
export type SelectHandlerSingle<T extends { required?: boolean | undefined }> = (
|
|
57
|
+
triggerDate: Date,
|
|
58
|
+
modifiers: Modifiers,
|
|
59
|
+
e: React.MouseEvent | React.KeyboardEvent,
|
|
60
|
+
) => T['required'] extends true ? Date : Date | undefined;
|
|
61
|
+
|
|
62
|
+
/** Selection handler for multiple selection mode. */
|
|
63
|
+
export type SelectHandlerMulti<T extends { required?: boolean | undefined }> = (
|
|
64
|
+
triggerDate: Date,
|
|
65
|
+
modifiers: Modifiers,
|
|
66
|
+
e: React.MouseEvent | React.KeyboardEvent,
|
|
67
|
+
) => T['required'] extends true ? Date[] : Date[] | undefined;
|
|
68
|
+
|
|
69
|
+
/** Selection handler for range selection mode. */
|
|
70
|
+
export type SelectHandlerRange<T extends { required?: boolean | undefined }> = (
|
|
71
|
+
triggerDate: Date,
|
|
72
|
+
modifiers: Modifiers,
|
|
73
|
+
e: React.MouseEvent | React.KeyboardEvent,
|
|
74
|
+
) => T['required'] extends true ? DateRange : DateRange | undefined;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The handler to set a selection based on the mode.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* const handleSelect: SelectHandler<{ mode: "single" }> = (
|
|
81
|
+
* triggerDate,
|
|
82
|
+
* modifiers,
|
|
83
|
+
* e,
|
|
84
|
+
* ) => {
|
|
85
|
+
* console.log("Selected date:", triggerDate);
|
|
86
|
+
* };
|
|
87
|
+
*/
|
|
88
|
+
export type SelectHandler<T extends { mode?: Mode | undefined; required?: boolean | undefined }> =
|
|
89
|
+
T extends {
|
|
90
|
+
mode: 'single';
|
|
91
|
+
}
|
|
92
|
+
? SelectHandlerSingle<T>
|
|
93
|
+
: T extends { mode: 'multiple' }
|
|
94
|
+
? SelectHandlerMulti<T>
|
|
95
|
+
: T extends { mode: 'range' }
|
|
96
|
+
? SelectHandlerRange<T>
|
|
97
|
+
: undefined;
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
import type * as components from '../components/custom-components.tsrx';
|
|
3
|
+
import type {
|
|
4
|
+
formatCaption,
|
|
5
|
+
formatDay,
|
|
6
|
+
formatMonthDropdown,
|
|
7
|
+
formatWeekdayName,
|
|
8
|
+
formatWeekNumber,
|
|
9
|
+
formatWeekNumberHeader,
|
|
10
|
+
formatYearDropdown,
|
|
11
|
+
} from '../formatters/index.js';
|
|
12
|
+
import type {
|
|
13
|
+
labelDayButton,
|
|
14
|
+
labelGrid,
|
|
15
|
+
labelGridcell,
|
|
16
|
+
labelMonthDropdown,
|
|
17
|
+
labelNav,
|
|
18
|
+
labelNext,
|
|
19
|
+
labelPrevious,
|
|
20
|
+
labelWeekday,
|
|
21
|
+
labelWeekNumber,
|
|
22
|
+
labelWeekNumberHeader,
|
|
23
|
+
labelYearDropdown,
|
|
24
|
+
} from '../labels/index.js';
|
|
25
|
+
import type { Animation, DayFlag, SelectionState, UI } from '../UI.js';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Selection modes supported by DayPicker.
|
|
29
|
+
*
|
|
30
|
+
* - `single`: Select a single day.
|
|
31
|
+
* - `multiple`: Select multiple days.
|
|
32
|
+
* - `range`: Select a range of days.
|
|
33
|
+
*
|
|
34
|
+
* @see https://daypicker.dev/docs/selection-modes
|
|
35
|
+
*/
|
|
36
|
+
export type Mode = 'single' | 'multiple' | 'range';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The components that can be customized using the `components` prop.
|
|
40
|
+
*
|
|
41
|
+
* @see https://daypicker.dev/guides/custom-components
|
|
42
|
+
*/
|
|
43
|
+
export type CustomComponents = {
|
|
44
|
+
/** Render the chevron icon used in the navigation buttons and dropdowns. */
|
|
45
|
+
Chevron: typeof components.Chevron;
|
|
46
|
+
/** Render the caption label of the month grid. */
|
|
47
|
+
CaptionLabel: typeof components.CaptionLabel;
|
|
48
|
+
/** Render the day cell in the month grid. */
|
|
49
|
+
Day: typeof components.Day;
|
|
50
|
+
/** Render the button containing the day in the day cell. */
|
|
51
|
+
DayButton: typeof components.DayButton;
|
|
52
|
+
/** Render the dropdown element to select years and months. */
|
|
53
|
+
Dropdown: typeof components.Dropdown;
|
|
54
|
+
/** Render the container of the dropdowns. */
|
|
55
|
+
DropdownNav: typeof components.DropdownNav;
|
|
56
|
+
/** Render the footer element announced by screen readers. */
|
|
57
|
+
Footer: typeof components.Footer;
|
|
58
|
+
/** Render the container of the MonthGrid. */
|
|
59
|
+
Month: typeof components.Month;
|
|
60
|
+
/** Render the caption of the month grid. */
|
|
61
|
+
MonthCaption: typeof components.MonthCaption;
|
|
62
|
+
/** Render the grid of days in a month. */
|
|
63
|
+
MonthGrid: typeof components.MonthGrid;
|
|
64
|
+
/** Wrapper of the month grids. */
|
|
65
|
+
Months: typeof components.Months;
|
|
66
|
+
/** Render the navigation element with the next and previous buttons. */
|
|
67
|
+
Nav: typeof components.Nav;
|
|
68
|
+
/** Render the `<option>` HTML element in the dropdown. */
|
|
69
|
+
Option: typeof components.Option;
|
|
70
|
+
/** Render the previous month button element in the navigation. */
|
|
71
|
+
PreviousMonthButton: typeof components.PreviousMonthButton;
|
|
72
|
+
/** Render the next month button element in the navigation. */
|
|
73
|
+
NextMonthButton: typeof components.NextMonthButton;
|
|
74
|
+
/** Render the root element of the calendar. */
|
|
75
|
+
Root: typeof components.Root;
|
|
76
|
+
/** Render the select element in the dropdowns. */
|
|
77
|
+
Select: typeof components.Select;
|
|
78
|
+
/** Render the weeks section in the month grid. */
|
|
79
|
+
Weeks: typeof components.Weeks;
|
|
80
|
+
/** Render the week rows. */
|
|
81
|
+
Week: typeof components.Week;
|
|
82
|
+
/** Render the weekday name in the header. */
|
|
83
|
+
Weekday: typeof components.Weekday;
|
|
84
|
+
/** Render the row containing the week days. */
|
|
85
|
+
Weekdays: typeof components.Weekdays;
|
|
86
|
+
/** Render the cell with the number of the week. */
|
|
87
|
+
WeekNumber: typeof components.WeekNumber;
|
|
88
|
+
/** Render the header of the week number column. */
|
|
89
|
+
WeekNumberHeader: typeof components.WeekNumberHeader;
|
|
90
|
+
/** Render the dropdown for selecting months. */
|
|
91
|
+
MonthsDropdown: typeof components.MonthsDropdown;
|
|
92
|
+
/** Render the dropdown for selecting years. */
|
|
93
|
+
YearsDropdown: typeof components.YearsDropdown;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Represents a map of formatters used to render localized content. */
|
|
97
|
+
export type Formatters = {
|
|
98
|
+
/** Format the caption of a month grid. */
|
|
99
|
+
formatCaption: typeof formatCaption;
|
|
100
|
+
/** Format the day in the day cell. */
|
|
101
|
+
formatDay: typeof formatDay;
|
|
102
|
+
/** Format the label in the month dropdown. */
|
|
103
|
+
formatMonthDropdown: typeof formatMonthDropdown;
|
|
104
|
+
/** Format the week number. */
|
|
105
|
+
formatWeekNumber: typeof formatWeekNumber;
|
|
106
|
+
/** Format the header of the week number column. */
|
|
107
|
+
formatWeekNumberHeader: typeof formatWeekNumberHeader;
|
|
108
|
+
/** Format the week day name in the header. */
|
|
109
|
+
formatWeekdayName: typeof formatWeekdayName;
|
|
110
|
+
/** Format the label in the year dropdown. */
|
|
111
|
+
formatYearDropdown: typeof formatYearDropdown;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** A map of functions to translate ARIA labels for various elements. */
|
|
115
|
+
export type Labels = {
|
|
116
|
+
/** The label for the navigation toolbar. */
|
|
117
|
+
labelNav: typeof labelNav;
|
|
118
|
+
/** The label for the month grid. */
|
|
119
|
+
labelGrid: typeof labelGrid;
|
|
120
|
+
/** The label for the gridcell, when the calendar is not interactive. */
|
|
121
|
+
labelGridcell: typeof labelGridcell;
|
|
122
|
+
/** The label for the month dropdown. */
|
|
123
|
+
labelMonthDropdown: typeof labelMonthDropdown;
|
|
124
|
+
/** The label for the year dropdown. */
|
|
125
|
+
labelYearDropdown: typeof labelYearDropdown;
|
|
126
|
+
/** The label for the "next month" button. */
|
|
127
|
+
labelNext: typeof labelNext;
|
|
128
|
+
/** The label for the "previous month" button. */
|
|
129
|
+
labelPrevious: typeof labelPrevious;
|
|
130
|
+
/** The label for the day button. */
|
|
131
|
+
labelDayButton: typeof labelDayButton;
|
|
132
|
+
/** The label for the weekday. */
|
|
133
|
+
labelWeekday: typeof labelWeekday;
|
|
134
|
+
/** The label for the week number. */
|
|
135
|
+
labelWeekNumber: typeof labelWeekNumber;
|
|
136
|
+
/** The label for the column of week numbers. */
|
|
137
|
+
labelWeekNumberHeader: typeof labelWeekNumberHeader;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* A value or a function that matches specific days.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // Match weekends and specific holidays
|
|
145
|
+
* const matcher: Matcher = [
|
|
146
|
+
* { dayOfWeek: [0, 6] }, // Weekends
|
|
147
|
+
* { from: new Date(2023, 11, 24), to: new Date(2023, 11, 26) }, // Christmas
|
|
148
|
+
* ];
|
|
149
|
+
*/
|
|
150
|
+
export type Matcher =
|
|
151
|
+
| boolean
|
|
152
|
+
| ((date: Date) => boolean)
|
|
153
|
+
| Date
|
|
154
|
+
| Date[]
|
|
155
|
+
| DateRange
|
|
156
|
+
| DateBefore
|
|
157
|
+
| DateAfter
|
|
158
|
+
| DateInterval
|
|
159
|
+
| DayOfWeek;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Match a day falling after the specified date (exclusive).
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* // Match days after February 2, 2019
|
|
166
|
+
* const matcher: DateAfter = { after: new Date(2019, 1, 2) };
|
|
167
|
+
*/
|
|
168
|
+
export type DateAfter = { after: Date };
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Match a day falling before the specified date (exclusive).
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* // Match days before February 2, 2019
|
|
175
|
+
* const matcher: DateBefore = { before: new Date(2019, 1, 2) };
|
|
176
|
+
*/
|
|
177
|
+
export type DateBefore = { before: Date };
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* An interval of dates. Unlike {@link DateRange}, the range ends are not
|
|
181
|
+
* included.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* // Match days between February 2 and February 5, 2019
|
|
185
|
+
* const matcher: DateInterval = {
|
|
186
|
+
* after: new Date(2019, 1, 2),
|
|
187
|
+
* before: new Date(2019, 1, 5),
|
|
188
|
+
* };
|
|
189
|
+
*/
|
|
190
|
+
export type DateInterval = { before: Date; after: Date };
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* A range of dates. Unlike {@link DateInterval}, the range ends are included.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* // Match days between February 2 and February 5, 2019
|
|
197
|
+
* const matcher: DateRange = {
|
|
198
|
+
* from: new Date(2019, 1, 2),
|
|
199
|
+
* to: new Date(2019, 1, 5),
|
|
200
|
+
* };
|
|
201
|
+
*/
|
|
202
|
+
export type DateRange = { from: Date | undefined; to?: Date | undefined };
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Match days of the week (`0-6`, where `0` is Sunday).
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* // Match Sundays
|
|
209
|
+
* const matcher: DayOfWeek = { dayOfWeek: 0 };
|
|
210
|
+
* // Match weekends
|
|
211
|
+
* const matcher: DayOfWeek = { dayOfWeek: [0, 6] };
|
|
212
|
+
*/
|
|
213
|
+
export type DayOfWeek = { dayOfWeek: number | number[] };
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The event handler triggered when clicking or interacting with a day.
|
|
217
|
+
*
|
|
218
|
+
* @template EventType - The event type that triggered the event (e.g.
|
|
219
|
+
* `React.MouseEvent`, `React.KeyboardEvent`, etc.).
|
|
220
|
+
* @param date - The date that has triggered the event.
|
|
221
|
+
* @param modifiers - The modifiers belonging to the date.
|
|
222
|
+
* @param e - The DOM event that triggered the event.
|
|
223
|
+
*/
|
|
224
|
+
export type DayEventHandler<EventType> = (date: Date, modifiers: Modifiers, e: EventType) => void;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The event handler when a month is changed in the calendar.
|
|
228
|
+
*
|
|
229
|
+
* ```tsx
|
|
230
|
+
* <DayPicker onMonthChange={(month) => console.log(month)} />
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* @see https://daypicker.dev/docs/navigation
|
|
234
|
+
*/
|
|
235
|
+
export type MonthChangeEventHandler = (month: Date) => void;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The CSS classnames to use for the {@link UI} elements, the
|
|
239
|
+
* {@link SelectionState} and the {@link DayFlag}.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* const classNames: ClassNames = {
|
|
243
|
+
* [UI.Root]: "root",
|
|
244
|
+
* [UI.Outside]: "outside",
|
|
245
|
+
* [UI.Nav]: "nav",
|
|
246
|
+
* // etc.
|
|
247
|
+
* };
|
|
248
|
+
*/
|
|
249
|
+
export type ClassNames = {
|
|
250
|
+
[key in UI | SelectionState | DayFlag | Animation]: string;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The CSS styles to use for the {@link UI} elements, the {@link SelectionState}
|
|
255
|
+
* and the {@link DayFlag}.
|
|
256
|
+
*/
|
|
257
|
+
export type Styles = {
|
|
258
|
+
[key in UI | SelectionState | DayFlag]: CSSProperties | undefined;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Represents the modifiers that match a specific day in the calendar.
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* const modifiers: Modifiers = {
|
|
266
|
+
* today: true, // The day is today
|
|
267
|
+
* selected: false, // The day is not selected
|
|
268
|
+
* weekend: true, // Custom modifier for weekends
|
|
269
|
+
* };
|
|
270
|
+
*
|
|
271
|
+
* @see https://daypicker.dev/guides/custom-modifiers
|
|
272
|
+
*/
|
|
273
|
+
export type Modifiers = Record<string, boolean>;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The style to apply to each day element matching a modifier.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* const modifiersStyles: ModifiersStyles = {
|
|
280
|
+
* today: { color: "red" },
|
|
281
|
+
* selected: { backgroundColor: "blue" },
|
|
282
|
+
* weekend: { color: "green" },
|
|
283
|
+
* };
|
|
284
|
+
*/
|
|
285
|
+
export type ModifiersStyles = Record<string, CSSProperties>;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The classnames to assign to each day element matching a modifier.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* const modifiersClassNames: ModifiersClassNames = {
|
|
292
|
+
* today: "today", // Use the "today" class for the today's day
|
|
293
|
+
* selected: "highlight", // Use the "highlight" class for the selected day
|
|
294
|
+
* weekend: "weekend", // Use the "weekend" class for the weekend days
|
|
295
|
+
* };
|
|
296
|
+
*/
|
|
297
|
+
export type ModifiersClassNames = Record<string, string>;
|
|
298
|
+
|
|
299
|
+
/** The direction to move the focus relative to the current focused date. */
|
|
300
|
+
export type MoveFocusDir = 'after' | 'before';
|
|
301
|
+
|
|
302
|
+
/** The temporal unit to move the focus by. */
|
|
303
|
+
export type MoveFocusBy = 'day' | 'week' | 'startOfWeek' | 'endOfWeek' | 'month' | 'year';
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The numbering system supported by DayPicker.
|
|
307
|
+
*
|
|
308
|
+
* - `latn`: Latin (Western Arabic)
|
|
309
|
+
* - `arab`: Arabic-Indic
|
|
310
|
+
* - `arabext`: Eastern Arabic-Indic (Persian)
|
|
311
|
+
* - `deva`: Devanagari
|
|
312
|
+
* - `beng`: Bengali
|
|
313
|
+
* - `guru`: Gurmukhi
|
|
314
|
+
* - `gujr`: Gujarati
|
|
315
|
+
* - `orya`: Oriya
|
|
316
|
+
* - `tamldec`: Tamil
|
|
317
|
+
* - `telu`: Telugu
|
|
318
|
+
* - `knda`: Kannada
|
|
319
|
+
* - `mlym`: Malayalam
|
|
320
|
+
* - `thai`: Thai
|
|
321
|
+
* - `mymr`: Myanmar
|
|
322
|
+
* - `khmr`: Khmer
|
|
323
|
+
* - `laoo`: Lao
|
|
324
|
+
* - `tibt`: Tibetan
|
|
325
|
+
*
|
|
326
|
+
* @see https://daypicker.dev/docs/translation#numeral-systems
|
|
327
|
+
*/
|
|
328
|
+
export type Numerals =
|
|
329
|
+
| 'latn'
|
|
330
|
+
| 'arab'
|
|
331
|
+
| 'arabext'
|
|
332
|
+
| 'deva'
|
|
333
|
+
| 'geez'
|
|
334
|
+
| 'beng'
|
|
335
|
+
| 'guru'
|
|
336
|
+
| 'gujr'
|
|
337
|
+
| 'orya'
|
|
338
|
+
| 'tamldec'
|
|
339
|
+
| 'telu'
|
|
340
|
+
| 'knda'
|
|
341
|
+
| 'mlym'
|
|
342
|
+
| 'thai'
|
|
343
|
+
| 'mymr'
|
|
344
|
+
| 'khmr'
|
|
345
|
+
| 'laoo'
|
|
346
|
+
| 'tibt';
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import { useLayoutEffect, useRef } from 'octane';
|
|
3
|
+
import type { CalendarDay } from './classes/CalendarDay.js';
|
|
4
|
+
import type { CalendarMonth } from './classes/CalendarMonth.js';
|
|
5
|
+
import type { DateLib } from './classes/DateLib.js';
|
|
6
|
+
import type { ClassNames } from './types/shared.js';
|
|
7
|
+
import { Animation } from './UI.js';
|
|
8
|
+
|
|
9
|
+
const previousRootSlot = Symbol.for('@octanejs/day-picker/useAnimation/root');
|
|
10
|
+
const previousMonthsSlot = Symbol.for('@octanejs/day-picker/useAnimation/months');
|
|
11
|
+
const animatingSlot = Symbol.for('@octanejs/day-picker/useAnimation/animating');
|
|
12
|
+
const animationEffectSlot = Symbol.for('@octanejs/day-picker/useAnimation/effect');
|
|
13
|
+
|
|
14
|
+
const asHtmlElement = (element: Element | null): HTMLElement | null => {
|
|
15
|
+
if (element instanceof HTMLElement) return element;
|
|
16
|
+
return null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const queryMonthEls = (element: HTMLElement) => [
|
|
20
|
+
...(element.querySelectorAll('[data-animated-month]') ?? []),
|
|
21
|
+
];
|
|
22
|
+
const queryMonthEl = (element: HTMLElement) =>
|
|
23
|
+
asHtmlElement(element.querySelector('[data-animated-month]'));
|
|
24
|
+
const queryCaptionEl = (element: HTMLElement) =>
|
|
25
|
+
asHtmlElement(element.querySelector('[data-animated-caption]'));
|
|
26
|
+
const queryWeeksEl = (element: HTMLElement) =>
|
|
27
|
+
asHtmlElement(element.querySelector('[data-animated-weeks]'));
|
|
28
|
+
const queryNavEl = (element: HTMLElement) =>
|
|
29
|
+
asHtmlElement(element.querySelector('[data-animated-nav]'));
|
|
30
|
+
const queryWeekdaysEl = (element: HTMLElement) =>
|
|
31
|
+
asHtmlElement(element.querySelector('[data-animated-weekdays]'));
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Handles animations for transitioning between months in the DayPicker
|
|
35
|
+
* component.
|
|
36
|
+
*
|
|
37
|
+
* @private
|
|
38
|
+
* @param rootElRef - A reference to the root element of the DayPicker
|
|
39
|
+
* component.
|
|
40
|
+
* @param enabled - Whether animations are enabled.
|
|
41
|
+
* @param options - Configuration options for the animation, including class
|
|
42
|
+
* names, months, focused day, and the date utility library.
|
|
43
|
+
*/
|
|
44
|
+
export function useAnimation(
|
|
45
|
+
rootElRef: React.RefObject<HTMLDivElement | null>,
|
|
46
|
+
enabled: boolean,
|
|
47
|
+
{
|
|
48
|
+
classNames,
|
|
49
|
+
months,
|
|
50
|
+
focused,
|
|
51
|
+
dateLib,
|
|
52
|
+
}: {
|
|
53
|
+
classNames: ClassNames;
|
|
54
|
+
months: CalendarMonth[];
|
|
55
|
+
focused: CalendarDay | undefined;
|
|
56
|
+
dateLib: DateLib;
|
|
57
|
+
},
|
|
58
|
+
): void {
|
|
59
|
+
const previousRootElSnapshotRef = useRef<HTMLElement | null>(null, previousRootSlot);
|
|
60
|
+
const previousMonthsRef = useRef(months, previousMonthsSlot);
|
|
61
|
+
const animatingRef = useRef(false, animatingSlot);
|
|
62
|
+
|
|
63
|
+
useLayoutEffect(
|
|
64
|
+
() => {
|
|
65
|
+
// get previous months before updating the previous months ref
|
|
66
|
+
const previousMonths = previousMonthsRef.current;
|
|
67
|
+
// update previous months ref for next effect trigger
|
|
68
|
+
previousMonthsRef.current = months;
|
|
69
|
+
|
|
70
|
+
if (
|
|
71
|
+
!enabled ||
|
|
72
|
+
!rootElRef.current ||
|
|
73
|
+
// safety check because the ref can be set to anything by consumers
|
|
74
|
+
!(rootElRef.current instanceof HTMLElement) ||
|
|
75
|
+
// validation required for the animation to work as expected
|
|
76
|
+
months.length === 0 ||
|
|
77
|
+
previousMonths.length === 0 ||
|
|
78
|
+
months.length !== previousMonths.length
|
|
79
|
+
) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const isSameMonth = dateLib.isSameMonth(months[0].date, previousMonths[0].date);
|
|
84
|
+
|
|
85
|
+
const isAfterPreviousMonth = dateLib.isAfter(months[0].date, previousMonths[0].date);
|
|
86
|
+
|
|
87
|
+
const captionAnimationClass = isAfterPreviousMonth
|
|
88
|
+
? classNames[Animation.caption_after_enter]
|
|
89
|
+
: classNames[Animation.caption_before_enter];
|
|
90
|
+
|
|
91
|
+
const weeksAnimationClass = isAfterPreviousMonth
|
|
92
|
+
? classNames[Animation.weeks_after_enter]
|
|
93
|
+
: classNames[Animation.weeks_before_enter];
|
|
94
|
+
|
|
95
|
+
// get previous root element snapshot before updating the snapshot ref
|
|
96
|
+
const previousRootElSnapshot = previousRootElSnapshotRef.current;
|
|
97
|
+
|
|
98
|
+
// update snapshot for next effect trigger
|
|
99
|
+
const rootElSnapshot = rootElRef.current.cloneNode(true);
|
|
100
|
+
if (rootElSnapshot instanceof HTMLElement) {
|
|
101
|
+
// if this effect is triggered while animating, we need to clean up the new root snapshot
|
|
102
|
+
// to put it in the same state as when not animating, to correctly animate the next month change
|
|
103
|
+
const currentMonthElsSnapshot = queryMonthEls(rootElSnapshot);
|
|
104
|
+
currentMonthElsSnapshot.forEach((currentMonthElSnapshot) => {
|
|
105
|
+
if (!(currentMonthElSnapshot instanceof HTMLElement)) return;
|
|
106
|
+
|
|
107
|
+
// remove the old month snapshots from the new root snapshot
|
|
108
|
+
const previousMonthElSnapshot = queryMonthEl(currentMonthElSnapshot);
|
|
109
|
+
if (previousMonthElSnapshot && currentMonthElSnapshot.contains(previousMonthElSnapshot)) {
|
|
110
|
+
currentMonthElSnapshot.removeChild(previousMonthElSnapshot);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// remove animation classes from the new month snapshots
|
|
114
|
+
const captionEl = queryCaptionEl(currentMonthElSnapshot);
|
|
115
|
+
if (captionEl) {
|
|
116
|
+
captionEl.classList.remove(captionAnimationClass);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const weeksEl = queryWeeksEl(currentMonthElSnapshot);
|
|
120
|
+
if (weeksEl) {
|
|
121
|
+
weeksEl.classList.remove(weeksAnimationClass);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
previousRootElSnapshotRef.current = rootElSnapshot;
|
|
126
|
+
} else {
|
|
127
|
+
previousRootElSnapshotRef.current = null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (
|
|
131
|
+
animatingRef.current ||
|
|
132
|
+
isSameMonth ||
|
|
133
|
+
// skip animation if a day is focused because it can cause issues to the animation and is better for a11y
|
|
134
|
+
focused
|
|
135
|
+
) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const previousMonthEls =
|
|
140
|
+
previousRootElSnapshot instanceof HTMLElement ? queryMonthEls(previousRootElSnapshot) : [];
|
|
141
|
+
|
|
142
|
+
const currentMonthEls = queryMonthEls(rootElRef.current);
|
|
143
|
+
|
|
144
|
+
if (
|
|
145
|
+
currentMonthEls?.every((el) => el instanceof HTMLElement) &&
|
|
146
|
+
previousMonthEls?.every((el) => el instanceof HTMLElement)
|
|
147
|
+
) {
|
|
148
|
+
animatingRef.current = true;
|
|
149
|
+
const cleanUpFunctions: (() => void)[] = [];
|
|
150
|
+
|
|
151
|
+
// set isolation to isolate to isolate the stacking context during animation
|
|
152
|
+
rootElRef.current.style.isolation = 'isolate';
|
|
153
|
+
// set z-index to 1 to ensure the nav is clickable over the other elements being animated
|
|
154
|
+
const navEl = queryNavEl(rootElRef.current);
|
|
155
|
+
if (navEl) {
|
|
156
|
+
navEl.style.zIndex = '1';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
currentMonthEls.forEach((currentMonthEl, index) => {
|
|
160
|
+
const previousMonthEl = previousMonthEls[index];
|
|
161
|
+
|
|
162
|
+
if (!previousMonthEl) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// animate new displayed month
|
|
167
|
+
currentMonthEl.style.position = 'relative';
|
|
168
|
+
currentMonthEl.style.overflow = 'hidden';
|
|
169
|
+
const captionEl = queryCaptionEl(currentMonthEl);
|
|
170
|
+
if (captionEl) {
|
|
171
|
+
captionEl.classList.add(captionAnimationClass);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const weeksEl = queryWeeksEl(currentMonthEl);
|
|
175
|
+
if (weeksEl) {
|
|
176
|
+
weeksEl.classList.add(weeksAnimationClass);
|
|
177
|
+
}
|
|
178
|
+
// animate new displayed month end
|
|
179
|
+
|
|
180
|
+
const cleanUp = () => {
|
|
181
|
+
animatingRef.current = false;
|
|
182
|
+
|
|
183
|
+
if (rootElRef.current) {
|
|
184
|
+
rootElRef.current.style.isolation = '';
|
|
185
|
+
}
|
|
186
|
+
if (navEl) {
|
|
187
|
+
navEl.style.zIndex = '';
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (captionEl) {
|
|
191
|
+
captionEl.classList.remove(captionAnimationClass);
|
|
192
|
+
}
|
|
193
|
+
if (weeksEl) {
|
|
194
|
+
weeksEl.classList.remove(weeksAnimationClass);
|
|
195
|
+
}
|
|
196
|
+
currentMonthEl.style.position = '';
|
|
197
|
+
currentMonthEl.style.overflow = '';
|
|
198
|
+
if (currentMonthEl.contains(previousMonthEl)) {
|
|
199
|
+
currentMonthEl.removeChild(previousMonthEl);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
cleanUpFunctions.push(cleanUp);
|
|
203
|
+
|
|
204
|
+
// animate old displayed month
|
|
205
|
+
previousMonthEl.style.pointerEvents = 'none';
|
|
206
|
+
previousMonthEl.style.position = 'absolute';
|
|
207
|
+
previousMonthEl.style.overflow = 'hidden';
|
|
208
|
+
previousMonthEl.setAttribute('aria-hidden', 'true');
|
|
209
|
+
|
|
210
|
+
// hide the weekdays container of the old month and only the new one
|
|
211
|
+
const previousWeekdaysEl = queryWeekdaysEl(previousMonthEl);
|
|
212
|
+
if (previousWeekdaysEl) {
|
|
213
|
+
previousWeekdaysEl.style.opacity = '0';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const previousCaptionEl = queryCaptionEl(previousMonthEl);
|
|
217
|
+
if (previousCaptionEl) {
|
|
218
|
+
previousCaptionEl.classList.add(
|
|
219
|
+
isAfterPreviousMonth
|
|
220
|
+
? classNames[Animation.caption_before_exit]
|
|
221
|
+
: classNames[Animation.caption_after_exit],
|
|
222
|
+
);
|
|
223
|
+
previousCaptionEl.addEventListener('animationend', cleanUp);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const previousWeeksEl = queryWeeksEl(previousMonthEl);
|
|
227
|
+
if (previousWeeksEl) {
|
|
228
|
+
previousWeeksEl.classList.add(
|
|
229
|
+
isAfterPreviousMonth
|
|
230
|
+
? classNames[Animation.weeks_before_exit]
|
|
231
|
+
: classNames[Animation.weeks_after_exit],
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
currentMonthEl.insertBefore(previousMonthEl, currentMonthEl.firstChild);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
undefined,
|
|
240
|
+
animationEffectSlot,
|
|
241
|
+
);
|
|
242
|
+
}
|