@klinking/colander 0.0.0
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/dist/formats/date.cjs +49 -0
- package/dist/formats/date.d.cts +45 -0
- package/dist/formats/date.d.mts +45 -0
- package/dist/formats/date.mjs +2 -0
- package/dist/formats/object.cjs +49 -0
- package/dist/formats/object.d.cts +45 -0
- package/dist/formats/object.d.mts +45 -0
- package/dist/formats/object.mjs +2 -0
- package/dist/formats/plain-date-time.cjs +49 -0
- package/dist/formats/plain-date-time.d.cts +45 -0
- package/dist/formats/plain-date-time.d.mts +45 -0
- package/dist/formats/plain-date-time.mjs +2 -0
- package/dist/formats/plain-date.cjs +49 -0
- package/dist/formats/plain-date.d.cts +45 -0
- package/dist/formats/plain-date.d.mts +45 -0
- package/dist/formats/plain-date.mjs +2 -0
- package/dist/formats/plain-month-day.cjs +49 -0
- package/dist/formats/plain-month-day.d.cts +45 -0
- package/dist/formats/plain-month-day.d.mts +45 -0
- package/dist/formats/plain-month-day.mjs +2 -0
- package/dist/formats/plain-year-month.cjs +49 -0
- package/dist/formats/plain-year-month.d.cts +45 -0
- package/dist/formats/plain-year-month.d.mts +45 -0
- package/dist/formats/plain-year-month.mjs +2 -0
- package/dist/formats/zoned-date-time.cjs +49 -0
- package/dist/formats/zoned-date-time.d.cts +45 -0
- package/dist/formats/zoned-date-time.d.mts +45 -0
- package/dist/formats/zoned-date-time.mjs +2 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
- package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
- package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
- package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
- package/package.json +89 -0
- package/src/calendar-context.ts +22 -0
- package/src/calendar-provider.test.tsx +123 -0
- package/src/calendar-provider.tsx +538 -0
- package/src/calendar-types.ts +212 -0
- package/src/compute-weeks-in-window.test.ts +83 -0
- package/src/compute-weeks-in-window.ts +40 -0
- package/src/context.ts +34 -0
- package/src/day-cell.tsx +635 -0
- package/src/drag-handle.tsx +137 -0
- package/src/formats/date.ts +110 -0
- package/src/formats/formats.test.ts +18 -0
- package/src/formats/object.ts +110 -0
- package/src/formats/plain-date-time.ts +110 -0
- package/src/formats/plain-date.ts +110 -0
- package/src/formats/plain-month-day.ts +110 -0
- package/src/formats/plain-year-month.ts +110 -0
- package/src/formats/zoned-date-time.ts +110 -0
- package/src/grid-header.tsx +141 -0
- package/src/grid.event-perf.test.tsx +282 -0
- package/src/grid.perf.test.tsx +467 -0
- package/src/grid.tsx +608 -0
- package/src/index.ts +116 -0
- package/src/keyboard.test.ts +498 -0
- package/src/keyboard.ts +142 -0
- package/src/month-separator.test.tsx +183 -0
- package/src/month-separator.tsx +238 -0
- package/src/month-view-context.ts +26 -0
- package/src/month-view-types.ts +89 -0
- package/src/month-view.test.tsx +1433 -0
- package/src/month-view.tsx +373 -0
- package/src/navigation.tsx +350 -0
- package/src/overflow.test.ts +124 -0
- package/src/overflow.ts +233 -0
- package/src/range-preview.tsx +90 -0
- package/src/resolve-first-week.test.ts +132 -0
- package/src/resolve-first-week.ts +145 -0
- package/src/root-selection.ts +621 -0
- package/src/selected-range.tsx +163 -0
- package/src/temporal-polyfill.test.ts +317 -0
- package/src/temporal-polyfill.ts +566 -0
- package/src/test-temporal.ts +17 -0
- package/src/types.ts +814 -0
- package/src/utils.bench.ts +183 -0
- package/src/utils.test.ts +983 -0
- package/src/utils.ts +493 -0
- package/src/view-context.ts +28 -0
- package/src/week-number.tsx +118 -0
- package/src/weeks-grid.tsx +265 -0
- package/src/weeks-keyboard.test.ts +222 -0
- package/src/weeks-keyboard.ts +156 -0
- package/src/weeks-navigation.tsx +209 -0
- package/src/weeks-view-context.ts +26 -0
- package/src/weeks-view-state.ts +421 -0
- package/src/weeks-view-types.ts +102 -0
- package/src/weeks-view.test.tsx +479 -0
- package/src/weeks-view.tsx +127 -0
package/src/keyboard.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
3
|
+
|
|
4
|
+
/** Result of a keyboard navigation action. */
|
|
5
|
+
export type KeyboardNavResult =
|
|
6
|
+
| { action: "move"; date: Temporal.PlainDate }
|
|
7
|
+
| { action: "select" }
|
|
8
|
+
| { action: "none" };
|
|
9
|
+
|
|
10
|
+
/** Input parameters for keyboard navigation computation. */
|
|
11
|
+
export interface KeyboardNavInput {
|
|
12
|
+
/** The `KeyboardEvent.key` value. */
|
|
13
|
+
key: string;
|
|
14
|
+
/** Whether the Shift modifier was held. */
|
|
15
|
+
shiftKey: boolean;
|
|
16
|
+
/** The currently focused date in the grid. */
|
|
17
|
+
focusedDate: Temporal.PlainDate;
|
|
18
|
+
/** Minimum selectable date bound. */
|
|
19
|
+
minValue: Temporal.PlainDate | undefined;
|
|
20
|
+
/** Maximum selectable date bound. */
|
|
21
|
+
maxValue: Temporal.PlainDate | undefined;
|
|
22
|
+
/** Whether the entire calendar is disabled. */
|
|
23
|
+
disabled: boolean;
|
|
24
|
+
/** Whether the calendar is read-only (navigation allowed, selection blocked). */
|
|
25
|
+
readOnly?: boolean;
|
|
26
|
+
/** Predicate for individually disabled dates. */
|
|
27
|
+
isDateDisabled?: (date: Temporal.PlainDate) => boolean;
|
|
28
|
+
/** Temporal namespace. */
|
|
29
|
+
T: TemporalNamespace;
|
|
30
|
+
/** Day the week starts on (`0` = Sunday). */
|
|
31
|
+
weekStartDay?: WeekStartDay;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Computes the target date when jumping by `months` from `focusedDate`.
|
|
36
|
+
*
|
|
37
|
+
* Day-of-month is constrained (e.g. Jan 31 + 1 month → Feb 28/29).
|
|
38
|
+
*/
|
|
39
|
+
export function computeMonthJumpTarget(
|
|
40
|
+
focusedDate: Temporal.PlainDate,
|
|
41
|
+
months: number,
|
|
42
|
+
T: TemporalNamespace,
|
|
43
|
+
): Temporal.PlainDate {
|
|
44
|
+
const totalMonths = focusedDate.year * 12 + (focusedDate.month - 1) + months;
|
|
45
|
+
const targetYear = Math.floor(totalMonths / 12);
|
|
46
|
+
const targetMonth = (totalMonths % 12) + 1;
|
|
47
|
+
return T.PlainDate.from(
|
|
48
|
+
{ year: targetYear, month: targetMonth, day: focusedDate.day },
|
|
49
|
+
{ overflow: "constrain" },
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function clampToBounds(
|
|
54
|
+
target: Temporal.PlainDate,
|
|
55
|
+
focusedDate: Temporal.PlainDate,
|
|
56
|
+
minValue: Temporal.PlainDate | undefined,
|
|
57
|
+
maxValue: Temporal.PlainDate | undefined,
|
|
58
|
+
T: TemporalNamespace,
|
|
59
|
+
): KeyboardNavResult {
|
|
60
|
+
let clamped = target;
|
|
61
|
+
if (minValue && T.PlainDate.compare(clamped, minValue) < 0) clamped = minValue;
|
|
62
|
+
if (maxValue && T.PlainDate.compare(clamped, maxValue) > 0) clamped = maxValue;
|
|
63
|
+
if (T.PlainDate.compare(clamped, focusedDate) === 0) return { action: "none" };
|
|
64
|
+
return { action: "move", date: clamped };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Pure function that maps a keyboard event to a navigation result.
|
|
69
|
+
*
|
|
70
|
+
* Handles Arrow keys (±1 day / ±1 week), Home/End (start/end of week),
|
|
71
|
+
* PageUp/PageDown (±1 month, ±1 year with Shift), and Enter/Space (select).
|
|
72
|
+
* Results are clamped to `minValue`/`maxValue` bounds.
|
|
73
|
+
*/
|
|
74
|
+
export function computeNextFocusDate(input: KeyboardNavInput): KeyboardNavResult {
|
|
75
|
+
const {
|
|
76
|
+
key,
|
|
77
|
+
shiftKey,
|
|
78
|
+
focusedDate,
|
|
79
|
+
minValue,
|
|
80
|
+
maxValue,
|
|
81
|
+
disabled,
|
|
82
|
+
readOnly,
|
|
83
|
+
isDateDisabled,
|
|
84
|
+
T,
|
|
85
|
+
weekStartDay = 0,
|
|
86
|
+
} = input;
|
|
87
|
+
|
|
88
|
+
if (disabled) return { action: "none" };
|
|
89
|
+
|
|
90
|
+
const daysInWeek = focusedDate.daysInWeek;
|
|
91
|
+
let nextDate: Temporal.PlainDate | null = null;
|
|
92
|
+
|
|
93
|
+
switch (key) {
|
|
94
|
+
case "ArrowRight":
|
|
95
|
+
nextDate = focusedDate.add({ days: 1 });
|
|
96
|
+
break;
|
|
97
|
+
case "ArrowLeft":
|
|
98
|
+
nextDate = focusedDate.subtract({ days: 1 });
|
|
99
|
+
break;
|
|
100
|
+
case "ArrowDown":
|
|
101
|
+
nextDate = focusedDate.add({ weeks: 1 });
|
|
102
|
+
break;
|
|
103
|
+
case "ArrowUp":
|
|
104
|
+
nextDate = focusedDate.subtract({ weeks: 1 });
|
|
105
|
+
break;
|
|
106
|
+
case "Home": {
|
|
107
|
+
const adjustedDow =
|
|
108
|
+
((focusedDate.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) % daysInWeek;
|
|
109
|
+
nextDate = focusedDate.subtract({ days: adjustedDow });
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
case "End": {
|
|
113
|
+
const adjustedDow =
|
|
114
|
+
((focusedDate.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) % daysInWeek;
|
|
115
|
+
nextDate = focusedDate.add({ days: daysInWeek - 1 - adjustedDow });
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case "PageUp": {
|
|
119
|
+
const target = computeMonthJumpTarget(focusedDate, shiftKey ? -12 : -1, T);
|
|
120
|
+
return clampToBounds(target, focusedDate, minValue, maxValue, T);
|
|
121
|
+
}
|
|
122
|
+
case "PageDown": {
|
|
123
|
+
const target = computeMonthJumpTarget(focusedDate, shiftKey ? 12 : 1, T);
|
|
124
|
+
return clampToBounds(target, focusedDate, minValue, maxValue, T);
|
|
125
|
+
}
|
|
126
|
+
case "Enter":
|
|
127
|
+
case " ":
|
|
128
|
+
if (readOnly) return { action: "none" };
|
|
129
|
+
if (!isDateDisabled?.(focusedDate)) {
|
|
130
|
+
return { action: "select" };
|
|
131
|
+
}
|
|
132
|
+
return { action: "none" };
|
|
133
|
+
default:
|
|
134
|
+
return { action: "none" };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (nextDate) {
|
|
138
|
+
return clampToBounds(nextDate, focusedDate, minValue, maxValue, T);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { action: "none" };
|
|
142
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { render, cleanup, within } from "@testing-library/react";
|
|
2
|
+
import { describe, it, expect, afterEach } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
MonthSeparator,
|
|
5
|
+
MonthSeparatorMonth,
|
|
6
|
+
MonthSeparatorYear,
|
|
7
|
+
MonthSeparatorWeekCount,
|
|
8
|
+
MonthSeparatorDataContext,
|
|
9
|
+
type MonthSeparatorState,
|
|
10
|
+
} from "./month-separator";
|
|
11
|
+
|
|
12
|
+
afterEach(cleanup);
|
|
13
|
+
|
|
14
|
+
const mockData: MonthSeparatorState = {
|
|
15
|
+
month: 3,
|
|
16
|
+
year: 2026,
|
|
17
|
+
firstOfYear: false,
|
|
18
|
+
firstVisible: true,
|
|
19
|
+
weeksVisibleBefore: 2,
|
|
20
|
+
weeksVisibleAfter: 4,
|
|
21
|
+
firstDayColumn: 0,
|
|
22
|
+
totalColumns: 7,
|
|
23
|
+
firstDayVisible: true,
|
|
24
|
+
fullWeeksVisibleAfter: 4,
|
|
25
|
+
gridRowStart: 2,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function renderInTable(ui: React.ReactElement, data = mockData) {
|
|
29
|
+
return render(
|
|
30
|
+
<table>
|
|
31
|
+
<tbody>
|
|
32
|
+
<MonthSeparatorDataContext.Provider value={data}>{ui}</MonthSeparatorDataContext.Provider>
|
|
33
|
+
</tbody>
|
|
34
|
+
</table>,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("MonthSeparator", () => {
|
|
39
|
+
it("renders data attributes from state", () => {
|
|
40
|
+
const { container } = renderInTable(<MonthSeparator />);
|
|
41
|
+
const tr = container.querySelector("tr");
|
|
42
|
+
expect(tr?.getAttribute("data-month")).toBe("3");
|
|
43
|
+
expect(tr?.getAttribute("data-year")).toBe("2026");
|
|
44
|
+
expect(tr?.hasAttribute("data-first-visible")).toBe(true);
|
|
45
|
+
expect(tr?.hasAttribute("data-first-of-year")).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("renders td with colspan=7", () => {
|
|
49
|
+
const { container } = renderInTable(<MonthSeparator />);
|
|
50
|
+
const td = container.querySelector("td");
|
|
51
|
+
expect(td?.getAttribute("colspan")).toBe("7");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("renders default children (Month + Year) when no children provided", () => {
|
|
55
|
+
const { container } = renderInTable(<MonthSeparator />);
|
|
56
|
+
const view = within(container);
|
|
57
|
+
expect(view.getByText("March")).toBeTruthy();
|
|
58
|
+
expect(view.getByText("2026")).toBeTruthy();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("renders custom children instead of defaults", () => {
|
|
62
|
+
const { container } = renderInTable(
|
|
63
|
+
<MonthSeparator>
|
|
64
|
+
<MonthSeparatorWeekCount />
|
|
65
|
+
</MonthSeparator>,
|
|
66
|
+
);
|
|
67
|
+
const view = within(container);
|
|
68
|
+
expect(view.getByText("4")).toBeTruthy();
|
|
69
|
+
expect(view.queryByText("March")).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("data-first-of-year", () => {
|
|
73
|
+
it.each([
|
|
74
|
+
{
|
|
75
|
+
description: "present when firstOfYear is true",
|
|
76
|
+
data: { ...mockData, firstOfYear: true },
|
|
77
|
+
expected: { hasAttr: true },
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
description: "absent when firstOfYear is false",
|
|
81
|
+
data: { ...mockData, firstOfYear: false },
|
|
82
|
+
expected: { hasAttr: false },
|
|
83
|
+
},
|
|
84
|
+
])("$description", ({ data, expected }) => {
|
|
85
|
+
const { container } = renderInTable(<MonthSeparator />, data);
|
|
86
|
+
const tr = container.querySelector("tr");
|
|
87
|
+
expect(tr?.hasAttribute("data-first-of-year")).toBe(expected.hasAttr);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("data-first-visible", () => {
|
|
92
|
+
it.each([
|
|
93
|
+
{
|
|
94
|
+
description: "present when firstVisible is true",
|
|
95
|
+
data: { ...mockData, firstVisible: true },
|
|
96
|
+
expected: { hasAttr: true },
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
description: "absent when firstVisible is false",
|
|
100
|
+
data: { ...mockData, firstVisible: false },
|
|
101
|
+
expected: { hasAttr: false },
|
|
102
|
+
},
|
|
103
|
+
])("$description", ({ data, expected }) => {
|
|
104
|
+
const { container } = renderInTable(<MonthSeparator />, data);
|
|
105
|
+
const tr = container.querySelector("tr");
|
|
106
|
+
expect(tr?.hasAttribute("data-first-visible")).toBe(expected.hasAttr);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("MonthSeparatorMonth", () => {
|
|
112
|
+
it.each([
|
|
113
|
+
{
|
|
114
|
+
description: "January",
|
|
115
|
+
data: { ...mockData, month: 1 },
|
|
116
|
+
expected: { text: "January" },
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
description: "June",
|
|
120
|
+
data: { ...mockData, month: 6 },
|
|
121
|
+
expected: { text: "June" },
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
description: "December",
|
|
125
|
+
data: { ...mockData, month: 12 },
|
|
126
|
+
expected: { text: "December" },
|
|
127
|
+
},
|
|
128
|
+
])("renders $description", ({ data, expected }) => {
|
|
129
|
+
const { container } = renderInTable(
|
|
130
|
+
<MonthSeparator>
|
|
131
|
+
<MonthSeparatorMonth />
|
|
132
|
+
</MonthSeparator>,
|
|
133
|
+
data,
|
|
134
|
+
);
|
|
135
|
+
expect(within(container).getByText(expected.text)).toBeTruthy();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("MonthSeparatorYear", () => {
|
|
140
|
+
it("renders the year number", () => {
|
|
141
|
+
const { container } = renderInTable(
|
|
142
|
+
<MonthSeparator>
|
|
143
|
+
<MonthSeparatorYear />
|
|
144
|
+
</MonthSeparator>,
|
|
145
|
+
);
|
|
146
|
+
expect(within(container).getByText("2026")).toBeTruthy();
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe("MonthSeparatorWeekCount", () => {
|
|
151
|
+
it("renders weeksVisibleAfter", () => {
|
|
152
|
+
const { container } = renderInTable(
|
|
153
|
+
<MonthSeparator>
|
|
154
|
+
<MonthSeparatorWeekCount />
|
|
155
|
+
</MonthSeparator>,
|
|
156
|
+
);
|
|
157
|
+
expect(within(container).getByText("4")).toBeTruthy();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("renders 0 when no weeks visible after", () => {
|
|
161
|
+
const { container } = renderInTable(
|
|
162
|
+
<MonthSeparator>
|
|
163
|
+
<MonthSeparatorWeekCount />
|
|
164
|
+
</MonthSeparator>,
|
|
165
|
+
{ ...mockData, weeksVisibleAfter: 0 },
|
|
166
|
+
);
|
|
167
|
+
expect(within(container).getByText("0")).toBeTruthy();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe("MonthSeparator error handling", () => {
|
|
172
|
+
it("throws when used outside context provider", () => {
|
|
173
|
+
expect(() =>
|
|
174
|
+
render(
|
|
175
|
+
<table>
|
|
176
|
+
<tbody>
|
|
177
|
+
<MonthSeparator />
|
|
178
|
+
</tbody>
|
|
179
|
+
</table>,
|
|
180
|
+
),
|
|
181
|
+
).toThrow("MonthSeparator child components must be used within");
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
+
import React, { createContext, useContext, forwardRef, useMemo } from "react";
|
|
4
|
+
import type { StateAttributesMapping } from "./types";
|
|
5
|
+
|
|
6
|
+
/** State exposed by the MonthSeparator component and its children. */
|
|
7
|
+
export type MonthSeparatorState = {
|
|
8
|
+
/** Calendar month (1-12). */
|
|
9
|
+
month: number;
|
|
10
|
+
/** Calendar year. */
|
|
11
|
+
year: number;
|
|
12
|
+
/** True when this is the first month of a new year in the visible window. */
|
|
13
|
+
firstOfYear: boolean;
|
|
14
|
+
/** True for the separator at the top of the visible window. */
|
|
15
|
+
firstVisible: boolean;
|
|
16
|
+
/** Number of weeks of the previous month visible above this separator. */
|
|
17
|
+
weeksVisibleBefore: number;
|
|
18
|
+
/** Number of weeks of this month visible below this separator. */
|
|
19
|
+
weeksVisibleAfter: number;
|
|
20
|
+
/**
|
|
21
|
+
* 0-based column index where the 1st of the month falls within the week row.
|
|
22
|
+
* Accounts for week number column if present (column 0 = week number).
|
|
23
|
+
*/
|
|
24
|
+
firstDayColumn: number;
|
|
25
|
+
/** Total number of columns (7 days + optional week number column). */
|
|
26
|
+
totalColumns: number;
|
|
27
|
+
/** True when the 1st day of this month is visible in the weeks below. */
|
|
28
|
+
firstDayVisible: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Number of week rows between this separator and the next separator
|
|
31
|
+
* (or end of window). Use this for CSS grid `gridRow: span N` to
|
|
32
|
+
* avoid overlapping the next month's label.
|
|
33
|
+
*/
|
|
34
|
+
fullWeeksVisibleAfter: number;
|
|
35
|
+
/** 1-based grid row index where this separator's weeks start. */
|
|
36
|
+
gridRowStart: number;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Context providing MonthSeparator data to child components. */
|
|
40
|
+
export const MonthSeparatorDataContext = createContext<MonthSeparatorState | null>(null);
|
|
41
|
+
|
|
42
|
+
function useMonthSeparatorData(): MonthSeparatorState {
|
|
43
|
+
const ctx = useContext(MonthSeparatorDataContext);
|
|
44
|
+
if (!ctx) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"MonthSeparator child components must be used within a MonthSeparatorDataContext.Provider",
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return ctx;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const stateAttributesMapping = {
|
|
53
|
+
month: (v: number) => ({ "data-month": String(v) }),
|
|
54
|
+
year: (v: number) => ({ "data-year": String(v) }),
|
|
55
|
+
firstOfYear: (v: boolean) => (v ? { "data-first-of-year": "" } : null),
|
|
56
|
+
firstVisible: (v: boolean) => (v ? { "data-first-visible": "" } : null),
|
|
57
|
+
weeksVisibleBefore: () => null,
|
|
58
|
+
weeksVisibleAfter: () => null,
|
|
59
|
+
firstDayColumn: (v: number) => ({ "data-first-day-column": String(v) }),
|
|
60
|
+
totalColumns: (v: number) => ({ "data-total-columns": String(v) }),
|
|
61
|
+
firstDayVisible: (v: boolean) => (v ? { "data-first-day-visible": "" } : null),
|
|
62
|
+
fullWeeksVisibleAfter: (v: number) => ({
|
|
63
|
+
"data-full-weeks-visible-after": String(v),
|
|
64
|
+
}),
|
|
65
|
+
gridRowStart: (v: number) => ({ "data-grid-row-start": String(v) }),
|
|
66
|
+
} as const satisfies StateAttributesMapping<MonthSeparatorState>;
|
|
67
|
+
|
|
68
|
+
// ─── Child: Month ────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
type MonthChildProps = useRender.ComponentProps<"span", MonthSeparatorState> & {
|
|
71
|
+
/** BCP 47 locale(s) for month name formatting. @default "en-US" */
|
|
72
|
+
locale?: string;
|
|
73
|
+
/** Month name format. @default "long" */
|
|
74
|
+
format?: "long" | "short" | "narrow";
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
function MonthSeparatorMonthFn(props: MonthChildProps, ref: React.ForwardedRef<HTMLSpanElement>) {
|
|
78
|
+
const { render, locale = "en-US", format = "long", ...otherProps } = props;
|
|
79
|
+
const data = useMonthSeparatorData();
|
|
80
|
+
|
|
81
|
+
const monthName = useMemo(() => {
|
|
82
|
+
const date = new Date(data.year, data.month - 1, 1);
|
|
83
|
+
return new Intl.DateTimeFormat(locale, { month: format }).format(date);
|
|
84
|
+
}, [data.year, data.month, locale, format]);
|
|
85
|
+
|
|
86
|
+
return useRender({
|
|
87
|
+
defaultTagName: "span",
|
|
88
|
+
render,
|
|
89
|
+
ref: ref ? [ref] : [],
|
|
90
|
+
state: data,
|
|
91
|
+
stateAttributesMapping,
|
|
92
|
+
props: mergeProps<"span">({ children: monthName }, otherProps),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const MonthSeparatorMonth = forwardRef(MonthSeparatorMonthFn);
|
|
97
|
+
|
|
98
|
+
// ─── Child: Year ─────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
type YearChildProps = useRender.ComponentProps<"span", MonthSeparatorState>;
|
|
101
|
+
|
|
102
|
+
function MonthSeparatorYearFn(props: YearChildProps, ref: React.ForwardedRef<HTMLSpanElement>) {
|
|
103
|
+
const { render, ...otherProps } = props;
|
|
104
|
+
const data = useMonthSeparatorData();
|
|
105
|
+
|
|
106
|
+
return useRender({
|
|
107
|
+
defaultTagName: "span",
|
|
108
|
+
render,
|
|
109
|
+
ref: ref ? [ref] : [],
|
|
110
|
+
state: data,
|
|
111
|
+
stateAttributesMapping,
|
|
112
|
+
props: mergeProps<"span">({ children: data.year }, otherProps),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const MonthSeparatorYear = forwardRef(MonthSeparatorYearFn);
|
|
117
|
+
|
|
118
|
+
// ─── Child: WeekCount ────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
type WeekCountChildProps = useRender.ComponentProps<"span", MonthSeparatorState>;
|
|
121
|
+
|
|
122
|
+
function MonthSeparatorWeekCountFn(
|
|
123
|
+
props: WeekCountChildProps,
|
|
124
|
+
ref: React.ForwardedRef<HTMLSpanElement>,
|
|
125
|
+
) {
|
|
126
|
+
const { render, ...otherProps } = props;
|
|
127
|
+
const data = useMonthSeparatorData();
|
|
128
|
+
|
|
129
|
+
return useRender({
|
|
130
|
+
defaultTagName: "span",
|
|
131
|
+
render,
|
|
132
|
+
ref: ref ? [ref] : [],
|
|
133
|
+
state: data,
|
|
134
|
+
stateAttributesMapping,
|
|
135
|
+
props: mergeProps<"span">({ children: data.weeksVisibleAfter }, otherProps),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const MonthSeparatorWeekCount = forwardRef(MonthSeparatorWeekCountFn);
|
|
140
|
+
|
|
141
|
+
// ─── MonthSeparatorRow ──────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
type MonthSeparatorRowProps = useRender.ComponentProps<"tr", MonthSeparatorState>;
|
|
144
|
+
|
|
145
|
+
function MonthSeparatorRowFn(
|
|
146
|
+
props: MonthSeparatorRowProps,
|
|
147
|
+
ref: React.ForwardedRef<HTMLTableRowElement>,
|
|
148
|
+
) {
|
|
149
|
+
const { render, ...otherProps } = props;
|
|
150
|
+
const data = useMonthSeparatorData();
|
|
151
|
+
|
|
152
|
+
const rendered = useRender({
|
|
153
|
+
defaultTagName: "tr",
|
|
154
|
+
render,
|
|
155
|
+
ref: ref ? [ref] : [],
|
|
156
|
+
state: data,
|
|
157
|
+
stateAttributesMapping,
|
|
158
|
+
props: mergeProps<"tr">({}, otherProps),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<MonthSeparatorDataContext.Provider value={data}>{rendered}</MonthSeparatorDataContext.Provider>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Renders the `<tr>` for a month separator. Provides MonthSeparatorDataContext. */
|
|
167
|
+
export const MonthSeparatorRow = forwardRef(MonthSeparatorRowFn);
|
|
168
|
+
|
|
169
|
+
// ─── MonthSeparatorCell ─────────────────────────────────────────────
|
|
170
|
+
|
|
171
|
+
type MonthSeparatorCellProps = useRender.ComponentProps<"td", MonthSeparatorState>;
|
|
172
|
+
|
|
173
|
+
function MonthSeparatorCellFn(
|
|
174
|
+
props: MonthSeparatorCellProps,
|
|
175
|
+
ref: React.ForwardedRef<HTMLTableCellElement>,
|
|
176
|
+
) {
|
|
177
|
+
const { render, ...otherProps } = props;
|
|
178
|
+
const data = useMonthSeparatorData();
|
|
179
|
+
|
|
180
|
+
return useRender({
|
|
181
|
+
defaultTagName: "td",
|
|
182
|
+
render,
|
|
183
|
+
ref: ref ? [ref] : [],
|
|
184
|
+
state: data,
|
|
185
|
+
stateAttributesMapping,
|
|
186
|
+
props: mergeProps<"td">({ colSpan: 7 }, otherProps),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Renders the `<td>` inside a MonthSeparatorRow. */
|
|
191
|
+
export const MonthSeparatorCell = forwardRef(MonthSeparatorCellFn);
|
|
192
|
+
|
|
193
|
+
// ─── MonthSeparator (convenience) ───────────────────────────────────
|
|
194
|
+
|
|
195
|
+
type MonthSeparatorProps = useRender.ComponentProps<"tr", MonthSeparatorState>;
|
|
196
|
+
|
|
197
|
+
function MonthSeparatorFn(
|
|
198
|
+
props: MonthSeparatorProps,
|
|
199
|
+
ref: React.ForwardedRef<HTMLTableRowElement>,
|
|
200
|
+
) {
|
|
201
|
+
const { render, children, ...otherProps } = props;
|
|
202
|
+
const data = useMonthSeparatorData();
|
|
203
|
+
|
|
204
|
+
const rendered = useRender({
|
|
205
|
+
defaultTagName: "tr",
|
|
206
|
+
render,
|
|
207
|
+
ref: ref ? [ref] : [],
|
|
208
|
+
state: data,
|
|
209
|
+
stateAttributesMapping,
|
|
210
|
+
props: mergeProps<"tr">(
|
|
211
|
+
{
|
|
212
|
+
children: (
|
|
213
|
+
<td colSpan={7}>
|
|
214
|
+
{children ?? (
|
|
215
|
+
<>
|
|
216
|
+
<MonthSeparatorMonth /> <MonthSeparatorYear />
|
|
217
|
+
</>
|
|
218
|
+
)}
|
|
219
|
+
</td>
|
|
220
|
+
),
|
|
221
|
+
},
|
|
222
|
+
otherProps,
|
|
223
|
+
),
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<MonthSeparatorDataContext.Provider value={data}>{rendered}</MonthSeparatorDataContext.Provider>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Convenience component: `<tr><td colspan="7">children</td></tr>`.
|
|
233
|
+
* Combines MonthSeparatorRow + MonthSeparatorCell.
|
|
234
|
+
* Default children: `<Month /> <Year />`.
|
|
235
|
+
*/
|
|
236
|
+
export const MonthSeparator = forwardRef(MonthSeparatorFn);
|
|
237
|
+
|
|
238
|
+
export { MonthSeparatorMonth, MonthSeparatorYear, MonthSeparatorWeekCount };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
import type { MonthViewStableContextValue, MonthViewStateContextValue } from "./month-view-types";
|
|
3
|
+
|
|
4
|
+
/** @internal React context for MonthView stable values (config + callbacks). */
|
|
5
|
+
export const MonthViewStableContext = createContext<MonthViewStableContextValue | null>(null);
|
|
6
|
+
|
|
7
|
+
/** @internal React context for MonthView volatile state. */
|
|
8
|
+
export const MonthViewStateContext = createContext<MonthViewStateContextValue | null>(null);
|
|
9
|
+
|
|
10
|
+
/** Returns the MonthView stable context (config + callbacks). */
|
|
11
|
+
export function useMonthViewStable(): MonthViewStableContextValue {
|
|
12
|
+
const ctx = useContext(MonthViewStableContext);
|
|
13
|
+
if (!ctx) {
|
|
14
|
+
throw new Error("useMonthViewStable must be used within MonthView.Root");
|
|
15
|
+
}
|
|
16
|
+
return ctx;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Returns the MonthView state context (volatile grid data). */
|
|
20
|
+
export function useMonthViewState(): MonthViewStateContextValue {
|
|
21
|
+
const ctx = useContext(MonthViewStateContext);
|
|
22
|
+
if (!ctx) {
|
|
23
|
+
throw new Error("useMonthViewState must be used within MonthView.Root");
|
|
24
|
+
}
|
|
25
|
+
return ctx;
|
|
26
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { MonthOverflowBehavior } from "./overflow";
|
|
3
|
+
import type { MonthData, OutsideDays, RootState } from "./types";
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// MonthView.Root props
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/** Props accepted by `MonthView.Root`. */
|
|
10
|
+
export interface MonthViewRootProps {
|
|
11
|
+
/**
|
|
12
|
+
* Number of months to display simultaneously (1–12).
|
|
13
|
+
* @default 1
|
|
14
|
+
*/
|
|
15
|
+
numberOfMonths?: number;
|
|
16
|
+
/**
|
|
17
|
+
* When `true`, always render 6 week rows per month grid.
|
|
18
|
+
* Prevents layout shifts when navigating between months.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
fixedWeeks?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Controls how days from adjacent months are displayed.
|
|
24
|
+
* @default "enabled"
|
|
25
|
+
*/
|
|
26
|
+
outsideDays?: OutsideDays;
|
|
27
|
+
/**
|
|
28
|
+
* How month navigation behaves at `min`/`max` bounds.
|
|
29
|
+
* - `"unbounded"` — navigation is always allowed.
|
|
30
|
+
* - `"stop"` — navigation buttons disable at the boundary.
|
|
31
|
+
* @default "unbounded"
|
|
32
|
+
*/
|
|
33
|
+
overflowBehavior?: MonthOverflowBehavior;
|
|
34
|
+
/**
|
|
35
|
+
* The controlled visible month. When provided, the component is controlled.
|
|
36
|
+
*/
|
|
37
|
+
month?: Temporal.PlainYearMonth;
|
|
38
|
+
/**
|
|
39
|
+
* The initial visible month (uncontrolled).
|
|
40
|
+
*/
|
|
41
|
+
defaultMonth?: Temporal.PlainYearMonth;
|
|
42
|
+
/**
|
|
43
|
+
* Called when the visible month changes via navigation or focus movement.
|
|
44
|
+
* Not called on initial mount.
|
|
45
|
+
*/
|
|
46
|
+
onMonthChange?: (month: Temporal.PlainYearMonth) => void;
|
|
47
|
+
/** React children. */
|
|
48
|
+
children?: React.ReactNode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// MonthView context values
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
/** Stable values (config + callbacks) provided by MonthView.Root. */
|
|
56
|
+
export interface MonthViewStableContextValue {
|
|
57
|
+
/** Number of simultaneously visible months. */
|
|
58
|
+
numberOfMonths: number;
|
|
59
|
+
/** Whether month grids always render 6 week rows. */
|
|
60
|
+
fixedWeeks: boolean;
|
|
61
|
+
/** How outside-month days are displayed. */
|
|
62
|
+
outsideDays: OutsideDays;
|
|
63
|
+
/** How month navigation behaves at bounds. */
|
|
64
|
+
overflowBehavior: MonthOverflowBehavior;
|
|
65
|
+
/** Navigate to the next month(s). */
|
|
66
|
+
goNextMonth: () => void;
|
|
67
|
+
/** Navigate to the previous month(s). */
|
|
68
|
+
goPrevMonth: () => void;
|
|
69
|
+
/** Register (or clear) the id of a label element for `aria-labelledby`, keyed by month index. */
|
|
70
|
+
setGridLabelId: (monthIndex: number, id: string | undefined) => void;
|
|
71
|
+
/** Ref tracking whether the grid currently holds DOM focus (avoids state re-renders). */
|
|
72
|
+
gridFocusedRef: React.RefObject<boolean>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Volatile state provided by MonthView.Root. */
|
|
76
|
+
export interface MonthViewStateContextValue {
|
|
77
|
+
/** The primary displayed month (year + month). */
|
|
78
|
+
currentMonth: { year: number; month: number };
|
|
79
|
+
/** 2D array of weeks for the first visible month grid. */
|
|
80
|
+
weeks: Temporal.PlainDate[][];
|
|
81
|
+
/** Pre-computed data for all visible months (length = `numberOfMonths`). */
|
|
82
|
+
allMonths: MonthData[];
|
|
83
|
+
/** Date-time representing the viewed month with time from the selection (for "today" highlighting). */
|
|
84
|
+
currentDateTime: Temporal.PlainDateTime;
|
|
85
|
+
/** Map of month index to label element id (for per-grid `aria-labelledby`). */
|
|
86
|
+
gridLabelIds: Record<number, string>;
|
|
87
|
+
/** The root component's state object for render functions. */
|
|
88
|
+
rootState: RootState;
|
|
89
|
+
}
|