@klinking/colander 3.0.1-alpha.1 → 3.1.0-alpha.10
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 +2 -3
- package/dist/formats/date.d.cts +10 -14
- package/dist/formats/date.d.mts +10 -14
- package/dist/formats/date.mjs +2 -3
- package/dist/formats/object.cjs +2 -3
- package/dist/formats/object.d.cts +10 -14
- package/dist/formats/object.d.mts +10 -14
- package/dist/formats/object.mjs +2 -3
- package/dist/formats/plain-date-time.cjs +2 -3
- package/dist/formats/plain-date-time.d.cts +10 -14
- package/dist/formats/plain-date-time.d.mts +10 -14
- package/dist/formats/plain-date-time.mjs +2 -3
- package/dist/formats/plain-date.cjs +2 -3
- package/dist/formats/plain-date.d.cts +10 -14
- package/dist/formats/plain-date.d.mts +10 -14
- package/dist/formats/plain-date.mjs +2 -3
- package/dist/formats/plain-month-day.cjs +2 -3
- package/dist/formats/plain-month-day.d.cts +10 -14
- package/dist/formats/plain-month-day.d.mts +10 -14
- package/dist/formats/plain-month-day.mjs +2 -3
- package/dist/formats/plain-year-month.cjs +2 -3
- package/dist/formats/plain-year-month.d.cts +10 -14
- package/dist/formats/plain-year-month.d.mts +10 -14
- package/dist/formats/plain-year-month.mjs +2 -3
- package/dist/formats/zoned-date-time.cjs +2 -3
- package/dist/formats/zoned-date-time.d.cts +10 -14
- package/dist/formats/zoned-date-time.d.mts +10 -14
- package/dist/formats/zoned-date-time.mjs +2 -3
- package/dist/{weeks-view-context-CNAq2Hiu.d.cts → index-K1_zOIpa.d.mts} +198 -114
- package/dist/{weeks-view-context-CsP3PBiu.d.mts → index-bRoYX03d.d.cts} +198 -114
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -11
- package/dist/index.d.mts +2 -11
- package/dist/index.mjs +1 -2
- package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-3UTRiTUP.cjs} +1102 -952
- package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-BoHqK5fq.mjs} +1102 -951
- package/package.json +21 -18
- package/src/calendar-provider.test.tsx +88 -1
- package/src/calendar-provider.tsx +89 -73
- package/src/calendar-types.ts +2 -0
- package/src/compute-weeks-in-window.test.ts +1 -1
- package/src/day-cell-state.test.ts +214 -0
- package/src/day-cell-state.ts +244 -0
- package/src/day-cell-store.test.tsx +273 -0
- package/src/day-cell-store.tsx +204 -0
- package/src/day-cell.test.tsx +73 -90
- package/src/day-cell.tsx +106 -444
- package/src/drag-handle.tsx +2 -2
- package/src/formats/date.ts +2 -0
- package/src/formats/formats.test.ts +1 -1
- package/src/formats/object.ts +2 -0
- package/src/formats/plain-date-time.ts +2 -0
- package/src/formats/plain-date.ts +2 -0
- package/src/formats/plain-month-day.ts +2 -0
- package/src/formats/plain-year-month.ts +2 -0
- package/src/formats/zoned-date-time.ts +2 -0
- package/src/grid-header.tsx +3 -3
- package/src/grid.event-perf.test.tsx +7 -1
- package/src/grid.perf.test.tsx +7 -1
- package/src/grid.tsx +38 -77
- package/src/index.ts +2 -0
- package/src/keyboard.test.ts +1 -1
- package/src/month-separator.test.tsx +1 -1
- package/src/month-view-navigation.ts +180 -0
- package/src/month-view-types.ts +3 -0
- package/src/month-view.bounds.test.tsx +136 -0
- package/src/month-view.test.tsx +89 -1
- package/src/month-view.tsx +74 -217
- package/src/month-window.test.ts +207 -0
- package/src/month-window.ts +93 -0
- package/src/navigation.tsx +18 -76
- package/src/out-of-range.test.ts +34 -54
- package/src/out-of-range.ts +47 -123
- package/src/range-preview.test.ts +120 -0
- package/src/range-preview.tsx +3 -5
- package/src/resolve-first-week.test.ts +1 -1
- package/src/root-selection.ts +99 -97
- package/src/selected-range.tsx +3 -5
- package/src/store.ts +72 -0
- package/src/today.test.tsx +105 -0
- package/src/types.ts +24 -1
- package/src/utils.bench.ts +158 -106
- package/src/utils.test.ts +26 -1
- package/src/utils.ts +40 -9
- package/src/view-context.test.tsx +126 -0
- package/src/view-context.ts +45 -0
- package/src/week-number.test.tsx +76 -0
- package/src/week-number.tsx +48 -19
- package/src/weeks-grid.tsx +20 -10
- package/src/weeks-keyboard.test.ts +1 -1
- package/src/weeks-navigation.tsx +15 -31
- package/src/weeks-view-state.ts +67 -101
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-view.bounds.test.tsx +246 -0
- package/src/weeks-view.test.tsx +116 -14
- package/src/weeks-view.tsx +24 -52
- package/src/weeks-window.test.ts +131 -0
- package/src/weeks-window.ts +102 -0
package/src/drag-handle.tsx
CHANGED
|
@@ -3,7 +3,6 @@ import { useRender } from "@base-ui/react/use-render";
|
|
|
3
3
|
import { forwardRef, useContext, useMemo, useRef } from "react";
|
|
4
4
|
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
5
5
|
import { DayCellDataContext, GridContext } from "./context";
|
|
6
|
-
import { useMonthViewState } from "./month-view-context";
|
|
7
6
|
import type { StateAttributesMapping } from "./types";
|
|
8
7
|
import type {
|
|
9
8
|
DragHandleState,
|
|
@@ -11,6 +10,7 @@ import type {
|
|
|
11
10
|
RangeEndDragHandleProps,
|
|
12
11
|
RangeDragHandleProps,
|
|
13
12
|
} from "./types";
|
|
13
|
+
import { useViewState } from "./view-context";
|
|
14
14
|
|
|
15
15
|
const dragHandleStateAttributesMapping = {
|
|
16
16
|
root: () => null,
|
|
@@ -31,7 +31,7 @@ function useDragHandle(
|
|
|
31
31
|
disabled,
|
|
32
32
|
} = useCalendarStable();
|
|
33
33
|
const { rangeStart, rangeEnd } = useCalendarState();
|
|
34
|
-
const { rootState } =
|
|
34
|
+
const { rootState } = useViewState();
|
|
35
35
|
const cellData = useContext(DayCellDataContext);
|
|
36
36
|
const { orientation } = useContext(GridContext);
|
|
37
37
|
const date = cellData?.date;
|
package/src/formats/date.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="vite/client" />
|
|
2
2
|
import * as root from "@klinking/colander";
|
|
3
|
-
import { describe, it, expect } from "
|
|
3
|
+
import { describe, it, expect } from "vite-plus/test";
|
|
4
4
|
|
|
5
5
|
const formatModules = import.meta.glob<Record<string, unknown>>(
|
|
6
6
|
["./*.ts", "!./*.test.ts"],
|
package/src/formats/object.ts
CHANGED
package/src/grid-header.tsx
CHANGED
|
@@ -2,7 +2,6 @@ import { mergeProps } from "@base-ui/react/merge-props";
|
|
|
2
2
|
import { useRender } from "@base-ui/react/use-render";
|
|
3
3
|
import { forwardRef, useMemo } from "react";
|
|
4
4
|
import { useCalendarStable } from "./calendar-context";
|
|
5
|
-
import { useMonthViewState } from "./month-view-context";
|
|
6
5
|
import type { StateAttributesMapping } from "./types";
|
|
7
6
|
import type {
|
|
8
7
|
GridHeaderCellProps,
|
|
@@ -11,10 +10,11 @@ import type {
|
|
|
11
10
|
GridHeaderCellState,
|
|
12
11
|
} from "./types";
|
|
13
12
|
import { getWeekdayNames, getReferenceWeekStart } from "./utils";
|
|
13
|
+
import { useViewState } from "./view-context";
|
|
14
14
|
|
|
15
15
|
function useGridHeaderCellState(index: number) {
|
|
16
16
|
const { locale, temporal: T, weekStartDay } = useCalendarStable();
|
|
17
|
-
const { rootState } =
|
|
17
|
+
const { rootState } = useViewState();
|
|
18
18
|
|
|
19
19
|
const weekdayNames = useMemo(
|
|
20
20
|
() => getWeekdayNames(locale, T, weekStartDay),
|
|
@@ -120,7 +120,7 @@ function GridHeaderFn(
|
|
|
120
120
|
ref: React.ForwardedRef<HTMLTableSectionElement>,
|
|
121
121
|
) {
|
|
122
122
|
const { render, children, ...otherProps } = props;
|
|
123
|
-
const { rootState } =
|
|
123
|
+
const { rootState } = useViewState();
|
|
124
124
|
|
|
125
125
|
const state = useMemo<GridHeaderState>(
|
|
126
126
|
() => ({ root: rootState as unknown as GridHeaderState["root"] }),
|
|
@@ -2,7 +2,13 @@ import { Temporal } from "@js-temporal/polyfill";
|
|
|
2
2
|
import { render, act, fireEvent } from "@testing-library/react";
|
|
3
3
|
import { Profiler, type ProfilerOnRenderCallback } from "react";
|
|
4
4
|
// @vitest-environment jsdom
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
describe,
|
|
7
|
+
it,
|
|
8
|
+
expect,
|
|
9
|
+
beforeAll,
|
|
10
|
+
type TestOptions,
|
|
11
|
+
} from "vite-plus/test";
|
|
6
12
|
|
|
7
13
|
const perfOptions: TestOptions = { retry: 3 };
|
|
8
14
|
import { Grid } from "./grid";
|
package/src/grid.perf.test.tsx
CHANGED
|
@@ -2,7 +2,13 @@ import { Temporal } from "@js-temporal/polyfill";
|
|
|
2
2
|
import { render } from "@testing-library/react";
|
|
3
3
|
import { Profiler, type ProfilerOnRenderCallback } from "react";
|
|
4
4
|
// @vitest-environment jsdom
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
describe,
|
|
7
|
+
it,
|
|
8
|
+
expect,
|
|
9
|
+
beforeAll,
|
|
10
|
+
type TestOptions,
|
|
11
|
+
} from "vite-plus/test";
|
|
6
12
|
|
|
7
13
|
const perfOptions: TestOptions = { retry: 3 };
|
|
8
14
|
import { WeekDataContext } from "./context";
|
package/src/grid.tsx
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
2
|
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
-
import type { Temporal } from "@js-temporal/polyfill";
|
|
4
3
|
import {
|
|
5
4
|
useContext,
|
|
6
5
|
useMemo,
|
|
@@ -15,7 +14,6 @@ import { WeekDataContext, GridContext, GridMonthContext } from "./context";
|
|
|
15
14
|
import { GridHeader, GridHeaderCell } from "./grid-header";
|
|
16
15
|
import { computeNextFocusDate } from "./keyboard";
|
|
17
16
|
import {
|
|
18
|
-
MonthViewStableContext,
|
|
19
17
|
MonthViewStateContext,
|
|
20
18
|
useMonthViewStable,
|
|
21
19
|
useMonthViewState,
|
|
@@ -29,14 +27,10 @@ import type {
|
|
|
29
27
|
WeekTemplateState,
|
|
30
28
|
WeekTemplateProps,
|
|
31
29
|
} from "./types";
|
|
32
|
-
import { useViewContext } from "./view-context";
|
|
30
|
+
import { ViewContext, useViewContext, useViewState } from "./view-context";
|
|
33
31
|
import { WeeksGrid } from "./weeks-grid";
|
|
34
32
|
import { computeWeeksKeyNav } from "./weeks-keyboard";
|
|
35
|
-
import {
|
|
36
|
-
WeeksViewStateContext,
|
|
37
|
-
useWeeksViewState,
|
|
38
|
-
useWeeksViewStable,
|
|
39
|
-
} from "./weeks-view-context";
|
|
33
|
+
import { useWeeksViewState, useWeeksViewStable } from "./weeks-view-context";
|
|
40
34
|
export { GridHeader, GridHeaderCell } from "./grid-header";
|
|
41
35
|
export { DayCellTemplate, DayButton } from "./day-cell";
|
|
42
36
|
|
|
@@ -115,7 +109,7 @@ function useWeeksGridKeyboard() {
|
|
|
115
109
|
shiftKey: e.shiftKey,
|
|
116
110
|
focusedDate,
|
|
117
111
|
windowStart: weeksState.windowInfo.windowStart,
|
|
118
|
-
weekCount:
|
|
112
|
+
weekCount: weeksState.windowInfo.weekCount,
|
|
119
113
|
minValue,
|
|
120
114
|
maxValue,
|
|
121
115
|
disabled,
|
|
@@ -177,17 +171,19 @@ const gridStateAttributesMapping = {
|
|
|
177
171
|
* delegates to the appropriate internal renderer.
|
|
178
172
|
*/
|
|
179
173
|
function GridFn(props: GridProps, ref: React.ForwardedRef<HTMLTableElement>) {
|
|
180
|
-
const
|
|
181
|
-
const weeksState = useContext(WeeksViewStateContext);
|
|
174
|
+
const view = useContext(ViewContext);
|
|
182
175
|
|
|
183
|
-
if (
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return <WeeksViewGrid ref={ref} {...props} />;
|
|
176
|
+
if (!view) {
|
|
177
|
+
throw new Error(
|
|
178
|
+
"Grid must be used inside MonthView.Root or WeeksView.Root.",
|
|
179
|
+
);
|
|
188
180
|
}
|
|
189
181
|
|
|
190
|
-
|
|
182
|
+
return view.viewType === "weeks" ? (
|
|
183
|
+
<WeeksViewGrid ref={ref} {...props} />
|
|
184
|
+
) : (
|
|
185
|
+
<MonthGrid ref={ref} {...props} />
|
|
186
|
+
);
|
|
191
187
|
}
|
|
192
188
|
|
|
193
189
|
export const Grid = forwardRef(GridFn) as (
|
|
@@ -361,7 +357,7 @@ function WeeksViewGridFn(
|
|
|
361
357
|
const weeksStable = useWeeksViewStable();
|
|
362
358
|
const weeksState = useWeeksViewState();
|
|
363
359
|
const { gridFocusedRef } = weeksStable;
|
|
364
|
-
const { gridLabelIds, weeks
|
|
360
|
+
const { gridLabelIds, weeks } = weeksState;
|
|
365
361
|
|
|
366
362
|
const handleKeyDown = useWeeksGridKeyboard();
|
|
367
363
|
const gridRef = useRef<HTMLTableElement>(null);
|
|
@@ -453,45 +449,8 @@ function WeeksViewGridFn(
|
|
|
453
449
|
[resolvedOrientation],
|
|
454
450
|
);
|
|
455
451
|
|
|
456
|
-
// Provide a MonthViewStableContext shim so shared components
|
|
457
|
-
// (DayCellTemplate, DayButton, etc.) that read it can function in WeeksView.
|
|
458
|
-
const monthViewStableShim = useMemo(
|
|
459
|
-
() => ({
|
|
460
|
-
numberOfMonths: 1,
|
|
461
|
-
fixedWeeks: false,
|
|
462
|
-
outsideDays: "enabled" as const,
|
|
463
|
-
outOfRangeBehavior: "unbounded" as const,
|
|
464
|
-
goNextMonth: () => {},
|
|
465
|
-
goPrevMonth: () => {},
|
|
466
|
-
setGridLabelId: () => {},
|
|
467
|
-
gridFocusedRef,
|
|
468
|
-
}),
|
|
469
|
-
[gridFocusedRef],
|
|
470
|
-
);
|
|
471
|
-
|
|
472
|
-
// Provide a MonthViewStateContext shim with reasonable defaults derived
|
|
473
|
-
// from the WeeksView state so WeekTemplate/DayCellTemplate/DayButton work.
|
|
474
|
-
const monthViewStateShim = useMemo(
|
|
475
|
-
() => ({
|
|
476
|
-
currentMonth: {
|
|
477
|
-
year: currentDateTime.year,
|
|
478
|
-
month: currentDateTime.month,
|
|
479
|
-
},
|
|
480
|
-
weeks: [] as Temporal.PlainDate[][],
|
|
481
|
-
allMonths: [],
|
|
482
|
-
currentDateTime,
|
|
483
|
-
gridLabelIds,
|
|
484
|
-
rootState: weeksState.rootState,
|
|
485
|
-
}),
|
|
486
|
-
[currentDateTime, gridLabelIds, weeksState.rootState],
|
|
487
|
-
);
|
|
488
|
-
|
|
489
452
|
return (
|
|
490
|
-
<
|
|
491
|
-
<MonthViewStateContext.Provider value={monthViewStateShim}>
|
|
492
|
-
<GridContext.Provider value={orientationCtx}>{el}</GridContext.Provider>
|
|
493
|
-
</MonthViewStateContext.Provider>
|
|
494
|
-
</MonthViewStableContext.Provider>
|
|
453
|
+
<GridContext.Provider value={orientationCtx}>{el}</GridContext.Provider>
|
|
495
454
|
);
|
|
496
455
|
}
|
|
497
456
|
|
|
@@ -508,11 +467,11 @@ function GridBodyFn(
|
|
|
508
467
|
props: GridBodyProps,
|
|
509
468
|
ref: React.ForwardedRef<HTMLTableSectionElement>,
|
|
510
469
|
) {
|
|
511
|
-
const
|
|
470
|
+
const view = useContext(ViewContext);
|
|
512
471
|
|
|
513
472
|
// In WeeksView, delegate to WeeksGrid which handles week iteration
|
|
514
473
|
// and MonthSeparator context provision.
|
|
515
|
-
if (
|
|
474
|
+
if (view?.viewType === "weeks") {
|
|
516
475
|
const { children, className, style } = props as any;
|
|
517
476
|
return (
|
|
518
477
|
<WeeksGrid className={className} style={style}>
|
|
@@ -533,7 +492,7 @@ function MonthGridBodyFn(
|
|
|
533
492
|
ref: React.ForwardedRef<HTMLTableSectionElement>,
|
|
534
493
|
) {
|
|
535
494
|
const { render, ...otherProps } = props;
|
|
536
|
-
const { rootState } =
|
|
495
|
+
const { rootState } = useViewState();
|
|
537
496
|
|
|
538
497
|
const state = useMemo<GridBodyState>(
|
|
539
498
|
() => ({ root: rootState as unknown as GridBodyState["root"] }),
|
|
@@ -575,7 +534,7 @@ const weekInstanceStateAttributesMapping = {
|
|
|
575
534
|
function WeekInstance(props: WeekTemplateProps) {
|
|
576
535
|
const { render, ...otherProps } = props;
|
|
577
536
|
const weekData = useContext(WeekDataContext)!;
|
|
578
|
-
const { rootState } =
|
|
537
|
+
const { rootState } = useViewState();
|
|
579
538
|
|
|
580
539
|
const state = useMemo<WeekTemplateState>(
|
|
581
540
|
() => ({
|
|
@@ -603,8 +562,8 @@ function WeekInstance(props: WeekTemplateProps) {
|
|
|
603
562
|
export function WeekTemplate(props: WeekTemplateProps) {
|
|
604
563
|
const outerWeekData = useContext(WeekDataContext);
|
|
605
564
|
const gridMonthCtx = useContext(GridMonthContext);
|
|
606
|
-
const
|
|
607
|
-
const weeks = gridMonthCtx?.weeks ??
|
|
565
|
+
const monthState = useContext(MonthViewStateContext);
|
|
566
|
+
const weeks = gridMonthCtx?.weeks ?? monthState?.weeks ?? [];
|
|
608
567
|
const gridMonth = useMemo(
|
|
609
568
|
() =>
|
|
610
569
|
gridMonthCtx
|
|
@@ -612,26 +571,28 @@ export function WeekTemplate(props: WeekTemplateProps) {
|
|
|
612
571
|
: undefined,
|
|
613
572
|
[gridMonthCtx],
|
|
614
573
|
);
|
|
615
|
-
const
|
|
574
|
+
const gridRowIndex = outerWeekData?.gridRowIndex;
|
|
575
|
+
// Built once per set of weeks, so a focus or selection change that
|
|
576
|
+
// re-renders this template leaves the day cells' context untouched.
|
|
577
|
+
const weekValues = useMemo(
|
|
578
|
+
() =>
|
|
579
|
+
weeks.map((weekDays, i) => ({
|
|
580
|
+
days: weekDays,
|
|
581
|
+
weekIndex: i,
|
|
582
|
+
...(gridMonth !== undefined && { gridMonth }),
|
|
583
|
+
...(gridRowIndex !== undefined && { gridRowIndex }),
|
|
584
|
+
})),
|
|
585
|
+
[weeks, gridMonth, gridRowIndex],
|
|
586
|
+
);
|
|
616
587
|
|
|
617
588
|
return (
|
|
618
589
|
<>
|
|
619
|
-
{
|
|
620
|
-
const firstDay =
|
|
590
|
+
{weekValues.map((value) => {
|
|
591
|
+
const firstDay = value.days[0];
|
|
621
592
|
if (firstDay === undefined) return null;
|
|
622
593
|
return (
|
|
623
|
-
<WeekDataContext.Provider
|
|
624
|
-
|
|
625
|
-
value={{
|
|
626
|
-
days: weekDays,
|
|
627
|
-
weekIndex: i,
|
|
628
|
-
...(gridMonth !== undefined && { gridMonth }),
|
|
629
|
-
...(outerWeekData?.gridRowIndex !== undefined && {
|
|
630
|
-
gridRowIndex: outerWeekData.gridRowIndex,
|
|
631
|
-
}),
|
|
632
|
-
}}
|
|
633
|
-
>
|
|
634
|
-
<Instance {...props} />
|
|
594
|
+
<WeekDataContext.Provider key={firstDay.toString()} value={value}>
|
|
595
|
+
<WeekInstance {...props} />
|
|
635
596
|
</WeekDataContext.Provider>
|
|
636
597
|
);
|
|
637
598
|
})}
|
package/src/index.ts
CHANGED
package/src/keyboard.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, it, expect } from "
|
|
1
|
+
import { describe, it, expect } from "vite-plus/test";
|
|
2
2
|
import { computeNextFocusDate, computeMonthJumpTarget } from "./keyboard";
|
|
3
3
|
import type { KeyboardNavInput } from "./keyboard";
|
|
4
4
|
import { temporalVariants } from "./test-temporal";
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { Dispatch, SetStateAction } from "react";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import type { MonthRef, MonthWindowPolicy } from "./month-window";
|
|
5
|
+
import type { TemporalNamespace } from "./types";
|
|
6
|
+
import { focusedDateForMonth } from "./utils";
|
|
7
|
+
|
|
8
|
+
export interface UseMonthNavigationInput {
|
|
9
|
+
/** Controlled first visible month. */
|
|
10
|
+
month: MonthRef | undefined;
|
|
11
|
+
/** Initial first visible month when uncontrolled. */
|
|
12
|
+
defaultMonth: MonthRef | undefined;
|
|
13
|
+
/** Date the view opens on when neither `month` nor `defaultMonth` is given. */
|
|
14
|
+
initialAnchor: () => Temporal.PlainDate;
|
|
15
|
+
onMonthChange: ((month: Temporal.PlainYearMonth) => void) | undefined;
|
|
16
|
+
policy: MonthWindowPolicy;
|
|
17
|
+
focusedDate: Temporal.PlainDate;
|
|
18
|
+
setFocusedDate: Dispatch<SetStateAction<Temporal.PlainDate>>;
|
|
19
|
+
T: TemporalNamespace;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MonthNavigation {
|
|
23
|
+
/** The first visible month. */
|
|
24
|
+
currentMonth: MonthRef;
|
|
25
|
+
/** The first visible month as an ISO `PlainYearMonth`. */
|
|
26
|
+
viewingYearMonth: Temporal.PlainYearMonth;
|
|
27
|
+
goNextMonth: () => void;
|
|
28
|
+
goPrevMonth: () => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Owns which month the view shows, in both controlled and uncontrolled mode:
|
|
33
|
+
* the month state, the Prev/Next steps, and syncing the window with focus.
|
|
34
|
+
*
|
|
35
|
+
* `onMonthChange` fires exactly once per move: from the effect below when
|
|
36
|
+
* uncontrolled, and from the step or the focus-sync effect when controlled.
|
|
37
|
+
*/
|
|
38
|
+
export function useMonthNavigation(
|
|
39
|
+
input: UseMonthNavigationInput,
|
|
40
|
+
): MonthNavigation {
|
|
41
|
+
const {
|
|
42
|
+
month,
|
|
43
|
+
defaultMonth,
|
|
44
|
+
initialAnchor,
|
|
45
|
+
onMonthChange,
|
|
46
|
+
policy,
|
|
47
|
+
focusedDate,
|
|
48
|
+
setFocusedDate,
|
|
49
|
+
T,
|
|
50
|
+
} = input;
|
|
51
|
+
|
|
52
|
+
const isControlled = month !== undefined;
|
|
53
|
+
|
|
54
|
+
const [internalMonth, setInternalMonth] = useState<MonthRef>(() => {
|
|
55
|
+
if (month) return { year: month.year, month: month.month };
|
|
56
|
+
if (defaultMonth)
|
|
57
|
+
return { year: defaultMonth.year, month: defaultMonth.month };
|
|
58
|
+
const anchor = initialAnchor();
|
|
59
|
+
return { year: anchor.year, month: anchor.month };
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const year = isControlled ? month.year : internalMonth.year;
|
|
63
|
+
const monthNumber = isControlled ? month.month : internalMonth.month;
|
|
64
|
+
const currentMonth = useMemo<MonthRef>(
|
|
65
|
+
() => ({ year, month: monthNumber }),
|
|
66
|
+
[year, monthNumber],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// ISO — the locale calendar only affects display, not this value.
|
|
70
|
+
const viewingYearMonth = useMemo(
|
|
71
|
+
() => T.PlainYearMonth.from({ year, month: monthNumber }),
|
|
72
|
+
[year, monthNumber, T],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Always-current ref so callbacks/effects don't re-subscribe on every
|
|
76
|
+
// onMonthChange identity change.
|
|
77
|
+
const onMonthChangeRef = useRef(onMonthChange);
|
|
78
|
+
onMonthChangeRef.current = onMonthChange;
|
|
79
|
+
|
|
80
|
+
// Request the parent move the view to `{year, month}` (controlled mode).
|
|
81
|
+
// Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
|
|
82
|
+
// only affects display (see MonthYearString). Passing `calendar` here would
|
|
83
|
+
// reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
|
|
84
|
+
// shifting the value by centuries.
|
|
85
|
+
const notifyMonth = useCallback(
|
|
86
|
+
(target: MonthRef) => {
|
|
87
|
+
onMonthChangeRef.current?.(T.PlainYearMonth.from(target));
|
|
88
|
+
},
|
|
89
|
+
[T],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Button navigation in controlled mode notifies the parent directly, then
|
|
93
|
+
// moves focus into the target month. This ref holds that focus value so the
|
|
94
|
+
// focus-sync effect skips re-notifying for it. Keyed by the value (not a
|
|
95
|
+
// one-shot flag) so it can't strand and suppress a later keyboard crossing
|
|
96
|
+
// if the button's focus move is a no-op or the parent ignores the change.
|
|
97
|
+
const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
|
|
98
|
+
const prevFocusedRef = useRef(focusedDate);
|
|
99
|
+
|
|
100
|
+
// Clear a stale skip target whenever the parent actually moves the view.
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
skipFocusSyncForRef.current = null;
|
|
103
|
+
}, [month?.year, month?.month]);
|
|
104
|
+
|
|
105
|
+
// Sync month when the focused date crosses a month boundary. Uncontrolled:
|
|
106
|
+
// shift internal state. Controlled: ask the parent to move the view, but
|
|
107
|
+
// only when focus actually moved out of the visible window (keyboard nav) —
|
|
108
|
+
// never as an echo of an external `month` update or a button click.
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const prevFocused = prevFocusedRef.current;
|
|
111
|
+
prevFocusedRef.current = focusedDate;
|
|
112
|
+
const target = { year: focusedDate.year, month: focusedDate.month };
|
|
113
|
+
if (isControlled) {
|
|
114
|
+
if (skipFocusSyncForRef.current === focusedDate) {
|
|
115
|
+
skipFocusSyncForRef.current = null;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (prevFocused === focusedDate) return;
|
|
119
|
+
if (policy.isVisible(currentMonth, target)) return;
|
|
120
|
+
// Emit the minimally-shifted first-visible month (what the parent binds
|
|
121
|
+
// to `month`), not the focused month — so a controlled window scrolls by
|
|
122
|
+
// the minimum, matching uncontrolled mode and the nav buttons.
|
|
123
|
+
notifyMonth(policy.reveal(currentMonth, target));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
setInternalMonth((prev) => {
|
|
127
|
+
const next = policy.reveal(prev, target);
|
|
128
|
+
if (next.year === prev.year && next.month === prev.month) return prev;
|
|
129
|
+
return next;
|
|
130
|
+
});
|
|
131
|
+
}, [focusedDate, isControlled, policy, currentMonth, notifyMonth]);
|
|
132
|
+
|
|
133
|
+
// Fire onMonthChange when uncontrolled navigation moves the view (not on
|
|
134
|
+
// mount). In controlled mode the parent owns `month`, so notification flows
|
|
135
|
+
// from the steps and the focus-sync effect instead; firing here would just
|
|
136
|
+
// echo the parent's own update back to it.
|
|
137
|
+
const mountedRef = useRef(false);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!mountedRef.current) {
|
|
140
|
+
mountedRef.current = true;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (isControlled) return;
|
|
144
|
+
onMonthChangeRef.current?.(viewingYearMonth);
|
|
145
|
+
}, [viewingYearMonth, isControlled]);
|
|
146
|
+
|
|
147
|
+
const go = useCallback(
|
|
148
|
+
(direction: "prev" | "next") => {
|
|
149
|
+
if (isControlled) {
|
|
150
|
+
const step = policy.step(month, direction);
|
|
151
|
+
if (step.blocked) return;
|
|
152
|
+
// Notify the parent directly; the follow-on focus move is suppressed in
|
|
153
|
+
// the focus-sync effect (keyed on the value) so onMonthChange fires once.
|
|
154
|
+
const nextFocus = focusedDateForMonth(
|
|
155
|
+
focusedDate,
|
|
156
|
+
step.start,
|
|
157
|
+
step.firstDay,
|
|
158
|
+
);
|
|
159
|
+
skipFocusSyncForRef.current = nextFocus;
|
|
160
|
+
notifyMonth(step.start);
|
|
161
|
+
setFocusedDate(nextFocus);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
setInternalMonth((m) => {
|
|
165
|
+
const step = policy.step(m, direction);
|
|
166
|
+
if (step.blocked) return m;
|
|
167
|
+
setFocusedDate((prev) =>
|
|
168
|
+
focusedDateForMonth(prev, step.start, step.firstDay),
|
|
169
|
+
);
|
|
170
|
+
return step.start;
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
[isControlled, month, policy, notifyMonth, focusedDate, setFocusedDate],
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const goNextMonth = useCallback(() => go("next"), [go]);
|
|
177
|
+
const goPrevMonth = useCallback(() => go("prev"), [go]);
|
|
178
|
+
|
|
179
|
+
return { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth };
|
|
180
|
+
}
|
package/src/month-view-types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { MonthWindowPolicy } from "./month-window";
|
|
2
3
|
import type { MonthOutOfRangeBehavior } from "./out-of-range";
|
|
3
4
|
import type { MonthData, OutsideDays, RootState } from "./types";
|
|
4
5
|
|
|
@@ -71,6 +72,8 @@ export interface MonthViewStableContextValue {
|
|
|
71
72
|
outsideDays: OutsideDays;
|
|
72
73
|
/** How month navigation behaves at bounds. */
|
|
73
74
|
outOfRangeBehavior: MonthOutOfRangeBehavior;
|
|
75
|
+
/** Steps and reveals the visible month window. */
|
|
76
|
+
windowPolicy: MonthWindowPolicy;
|
|
74
77
|
/** Navigate to the next month(s). */
|
|
75
78
|
goNextMonth: () => void;
|
|
76
79
|
/** Navigate to the previous month(s). */
|