@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
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
+
import { forwardRef, useContext, useMemo } from "react";
|
|
4
|
+
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
5
|
+
import { WeekDataContext, GridContext } from "./context";
|
|
6
|
+
import { useMonthViewState } from "./month-view-context";
|
|
7
|
+
import { MonthViewStableContext } from "./month-view-context";
|
|
8
|
+
import { computeClippedRangeInfo, rangeOverlayStateAttributesMapping } from "./selected-range";
|
|
9
|
+
import type { ValueFormat, RangePreviewState, RangePreviewProps } from "./types";
|
|
10
|
+
|
|
11
|
+
function RangePreviewFn(props: RangePreviewProps, ref: React.ForwardedRef<HTMLTableCellElement>) {
|
|
12
|
+
const { selectionMode } = useCalendarStable();
|
|
13
|
+
if (selectionMode !== "range") {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return <RangePreviewInner ref={ref} {...props} />;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Visual overlay (`<td role="presentation">`) that highlights the hover
|
|
21
|
+
* preview range within a single week row. Structurally identical to
|
|
22
|
+
* {@link RangeSelected} but reads `previewStart`/`previewEnd` instead of
|
|
23
|
+
* the committed range boundaries.
|
|
24
|
+
*/
|
|
25
|
+
export const RangePreview = forwardRef(RangePreviewFn) as <F extends ValueFormat = ValueFormat>(
|
|
26
|
+
props: RangePreviewProps<F> & React.RefAttributes<HTMLTableCellElement>,
|
|
27
|
+
) => React.ReactElement | null;
|
|
28
|
+
|
|
29
|
+
function RangePreviewInnerFn(
|
|
30
|
+
props: RangePreviewProps,
|
|
31
|
+
ref: React.ForwardedRef<HTMLTableCellElement>,
|
|
32
|
+
) {
|
|
33
|
+
const { render, ...otherProps } = props;
|
|
34
|
+
const weekData = useContext(WeekDataContext);
|
|
35
|
+
const { orientation } = useContext(GridContext);
|
|
36
|
+
const { temporal: T } = useCalendarStable();
|
|
37
|
+
const { previewStart, previewEnd } = useCalendarState();
|
|
38
|
+
const monthStable = useContext(MonthViewStableContext);
|
|
39
|
+
const { rootState } = useMonthViewState();
|
|
40
|
+
const outsideDays = monthStable?.outsideDays ?? "enabled";
|
|
41
|
+
|
|
42
|
+
const days = weekData?.days ?? [];
|
|
43
|
+
const gridMonth = weekData?.gridMonth;
|
|
44
|
+
|
|
45
|
+
const info = useMemo(
|
|
46
|
+
() => computeClippedRangeInfo(days, previewStart, previewEnd, T, outsideDays, gridMonth),
|
|
47
|
+
[days, previewStart, previewEnd, T, outsideDays, gridMonth],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const startDate = info.active ? days[info.startIndex].toString() : "";
|
|
51
|
+
const endDate = info.active ? days[info.endIndex].toString() : "";
|
|
52
|
+
|
|
53
|
+
const weekIndex = weekData?.weekIndex ?? 0;
|
|
54
|
+
|
|
55
|
+
const state = useMemo<RangePreviewState>(
|
|
56
|
+
() => ({
|
|
57
|
+
root: rootState as any,
|
|
58
|
+
active: info.active,
|
|
59
|
+
weekIndex,
|
|
60
|
+
startIndex: info.startIndex,
|
|
61
|
+
endIndex: info.endIndex,
|
|
62
|
+
startDate,
|
|
63
|
+
endDate,
|
|
64
|
+
extendsBefore: info.extendsBefore,
|
|
65
|
+
extendsAfter: info.extendsAfter,
|
|
66
|
+
hasStart: previewStart !== undefined,
|
|
67
|
+
hasEnd: previewEnd !== undefined,
|
|
68
|
+
orientation,
|
|
69
|
+
}),
|
|
70
|
+
[rootState, info, weekIndex, startDate, endDate, previewStart, previewEnd, orientation],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const defaultProps: Record<string, unknown> = {
|
|
74
|
+
role: "presentation",
|
|
75
|
+
"aria-hidden": true,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return useRender({
|
|
79
|
+
defaultTagName: "td",
|
|
80
|
+
render,
|
|
81
|
+
ref: ref ? [ref] : [],
|
|
82
|
+
state,
|
|
83
|
+
stateAttributesMapping: rangeOverlayStateAttributesMapping,
|
|
84
|
+
props: mergeProps<"td">(defaultProps, otherProps),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const RangePreviewInner = forwardRef(RangePreviewInnerFn) as <F extends ValueFormat = ValueFormat>(
|
|
89
|
+
props: RangePreviewProps<F> & React.RefAttributes<HTMLTableCellElement>,
|
|
90
|
+
) => React.ReactElement | null;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import { resolveFirstWeekSpec, resolveFirstWeek } from "./resolve-first-week";
|
|
4
|
+
|
|
5
|
+
const T = Temporal;
|
|
6
|
+
|
|
7
|
+
describe("resolveFirstWeekSpec", () => {
|
|
8
|
+
it.each([
|
|
9
|
+
{
|
|
10
|
+
description: "PlainDate — returns containing week start (Sunday)",
|
|
11
|
+
spec: T.PlainDate.from("2026-03-04"), // Wednesday
|
|
12
|
+
weekStartDay: 0 as const,
|
|
13
|
+
expected: "2026-03-01", // Sunday
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
description: "PlainDate — returns containing week start (Monday)",
|
|
17
|
+
spec: T.PlainDate.from("2026-03-04"), // Wednesday
|
|
18
|
+
weekStartDay: 1 as const,
|
|
19
|
+
expected: "2026-03-02", // Monday
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
description: "PlainDate on week start day — no snap",
|
|
23
|
+
spec: T.PlainDate.from("2026-03-01"), // Sunday
|
|
24
|
+
weekStartDay: 0 as const,
|
|
25
|
+
expected: "2026-03-01",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
description: "ISO week spec",
|
|
29
|
+
spec: { isoWeek: 10, isoYear: 2026 },
|
|
30
|
+
weekStartDay: 0 as const,
|
|
31
|
+
// ISO week 10 of 2026 starts Monday March 2. Snap to Sunday March 1.
|
|
32
|
+
expected: "2026-03-01",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
description: "week-of-year spec (Sunday start)",
|
|
36
|
+
spec: { week: 1, year: 2026 },
|
|
37
|
+
weekStartDay: 0 as const,
|
|
38
|
+
// First Sunday of 2026 is Jan 4
|
|
39
|
+
expected: "2026-01-04",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
description: "month+year spec — first week containing a day in that month",
|
|
43
|
+
spec: { month: 3, year: 2026 },
|
|
44
|
+
weekStartDay: 0 as const,
|
|
45
|
+
expected: "2026-03-01",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
description: "month+year+day spec — week containing that day",
|
|
49
|
+
spec: { month: 3, year: 2026, day: 15 },
|
|
50
|
+
weekStartDay: 0 as const,
|
|
51
|
+
expected: "2026-03-15",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
description: "native Date object",
|
|
55
|
+
spec: new Date(2026, 2, 4), // March 4, 2026 in local time
|
|
56
|
+
weekStartDay: 0 as const,
|
|
57
|
+
expected: "2026-03-01",
|
|
58
|
+
},
|
|
59
|
+
])("$description", ({ spec, weekStartDay, expected }) => {
|
|
60
|
+
const result = resolveFirstWeekSpec(spec, weekStartDay, T);
|
|
61
|
+
expect(result.toString()).toBe(expected);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("resolveFirstWeek", () => {
|
|
66
|
+
it.each([
|
|
67
|
+
{
|
|
68
|
+
description: "snap start — target week becomes first row",
|
|
69
|
+
currentFirstWeek: T.PlainDate.from("2026-01-04"),
|
|
70
|
+
weekCount: 8,
|
|
71
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
72
|
+
snap: "start" as const,
|
|
73
|
+
expected: "2026-03-15",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
description: "snap center — target week centered in window",
|
|
77
|
+
currentFirstWeek: T.PlainDate.from("2026-01-04"),
|
|
78
|
+
weekCount: 8,
|
|
79
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
80
|
+
snap: "center" as const,
|
|
81
|
+
expected: "2026-02-15",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
description: "snap end — target week becomes last row",
|
|
85
|
+
currentFirstWeek: T.PlainDate.from("2026-01-04"),
|
|
86
|
+
weekCount: 8,
|
|
87
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
88
|
+
snap: "end" as const,
|
|
89
|
+
expected: "2026-01-25",
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
description: "snap nearest — already visible, no change",
|
|
93
|
+
currentFirstWeek: T.PlainDate.from("2026-03-01"),
|
|
94
|
+
weekCount: 8,
|
|
95
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
96
|
+
snap: "nearest" as const,
|
|
97
|
+
expected: "2026-03-01",
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
description: "snap nearest — target above window, becomes first row",
|
|
101
|
+
currentFirstWeek: T.PlainDate.from("2026-04-01"),
|
|
102
|
+
weekCount: 8,
|
|
103
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
104
|
+
snap: "nearest" as const,
|
|
105
|
+
expected: "2026-03-15",
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
description: "snap nearest — target below window, becomes last row",
|
|
109
|
+
currentFirstWeek: T.PlainDate.from("2026-01-04"),
|
|
110
|
+
weekCount: 4,
|
|
111
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
112
|
+
snap: "nearest" as const,
|
|
113
|
+
expected: "2026-02-22",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
description: "default snap is start",
|
|
117
|
+
currentFirstWeek: T.PlainDate.from("2026-01-04"),
|
|
118
|
+
weekCount: 8,
|
|
119
|
+
target: T.PlainDate.from("2026-03-15"),
|
|
120
|
+
snap: undefined,
|
|
121
|
+
expected: "2026-03-15",
|
|
122
|
+
},
|
|
123
|
+
])("$description", ({ currentFirstWeek, weekCount, target, snap, expected }) => {
|
|
124
|
+
const result = resolveFirstWeek(
|
|
125
|
+
currentFirstWeek,
|
|
126
|
+
weekCount,
|
|
127
|
+
target,
|
|
128
|
+
snap ? { snap } : undefined,
|
|
129
|
+
);
|
|
130
|
+
expect(result.toString()).toBe(expected);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A flexible specifier for a calendar week. Accepts a `Temporal.PlainDate`,
|
|
6
|
+
* a native `Date`, an ISO week number, a week-of-year, or a month/day pair.
|
|
7
|
+
*/
|
|
8
|
+
export type FirstWeekSpec =
|
|
9
|
+
| Temporal.PlainDate
|
|
10
|
+
| Date
|
|
11
|
+
| { isoWeek: number; isoYear: number }
|
|
12
|
+
| { week: number; year: number }
|
|
13
|
+
| { month: number; year: number; day?: number };
|
|
14
|
+
|
|
15
|
+
/** Controls how a scroll target week is positioned within the visible window. */
|
|
16
|
+
export type ScrollToWeekSnap = "start" | "center" | "end" | "nearest";
|
|
17
|
+
|
|
18
|
+
/** Snaps `date` back to the start of its containing week (determined by `weekStartDay`). */
|
|
19
|
+
function snapToWeekStart(date: Temporal.PlainDate, weekStartDay: WeekStartDay): Temporal.PlainDate {
|
|
20
|
+
const dow = date.dayOfWeek % 7; // 0=Sun..6=Sat
|
|
21
|
+
const offset = (dow - weekStartDay + 7) % 7;
|
|
22
|
+
return date.subtract({ days: offset });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Resolves any {@link FirstWeekSpec} variant to the `PlainDate` of the
|
|
27
|
+
* containing week's start (snapped to `weekStartDay`).
|
|
28
|
+
*
|
|
29
|
+
* @param spec - The week specifier to resolve.
|
|
30
|
+
* @param weekStartDay - Day the calendar week starts on (`0` = Sunday).
|
|
31
|
+
* @param T - Temporal namespace.
|
|
32
|
+
* @param timeZone - IANA time zone for native `Date` conversion. Defaults to system time zone.
|
|
33
|
+
*/
|
|
34
|
+
export function resolveFirstWeekSpec(
|
|
35
|
+
spec: FirstWeekSpec,
|
|
36
|
+
weekStartDay: WeekStartDay,
|
|
37
|
+
T: TemporalNamespace,
|
|
38
|
+
timeZone?: string,
|
|
39
|
+
): Temporal.PlainDate {
|
|
40
|
+
// 1. PlainDate — Temporal objects have a "calendar" property
|
|
41
|
+
if ("calendar" in spec) {
|
|
42
|
+
return snapToWeekStart(spec as Temporal.PlainDate, weekStartDay);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 2. Native Date
|
|
46
|
+
if (spec instanceof Date) {
|
|
47
|
+
const tz = timeZone ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
48
|
+
const fmt = new Intl.DateTimeFormat("en-US", {
|
|
49
|
+
timeZone: tz,
|
|
50
|
+
year: "numeric",
|
|
51
|
+
month: "numeric",
|
|
52
|
+
day: "numeric",
|
|
53
|
+
});
|
|
54
|
+
const parts = fmt.formatToParts(spec);
|
|
55
|
+
const get = (type: string) => Number(parts.find((p) => p.type === type)?.value ?? 0);
|
|
56
|
+
const plain = T.PlainDate.from({
|
|
57
|
+
year: get("year"),
|
|
58
|
+
month: get("month"),
|
|
59
|
+
day: get("day"),
|
|
60
|
+
});
|
|
61
|
+
return snapToWeekStart(plain, weekStartDay);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 3. ISO week spec
|
|
65
|
+
if ("isoWeek" in spec) {
|
|
66
|
+
const { isoWeek, isoYear } = spec;
|
|
67
|
+
// Jan 4 is always in ISO week 1. Find the Monday of that week.
|
|
68
|
+
const jan4 = T.PlainDate.from({ year: isoYear, month: 1, day: 4 });
|
|
69
|
+
const jan4dow = jan4.dayOfWeek; // ISO: 1=Mon..7=Sun
|
|
70
|
+
const isoWeek1Monday = jan4.subtract({ days: jan4dow - 1 });
|
|
71
|
+
const targetMonday = isoWeek1Monday.add({ weeks: isoWeek - 1 });
|
|
72
|
+
return snapToWeekStart(targetMonday, weekStartDay);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 4. Week-of-year spec (relative to weekStartDay)
|
|
76
|
+
if ("week" in spec && !("month" in spec)) {
|
|
77
|
+
const { week, year } = spec as { week: number; year: number };
|
|
78
|
+
// Find the first occurrence of weekStartDay in the year
|
|
79
|
+
const jan1 = T.PlainDate.from({ year, month: 1, day: 1 });
|
|
80
|
+
const jan1dow = jan1.dayOfWeek % 7; // 0=Sun..6=Sat
|
|
81
|
+
const daysUntilStart = (weekStartDay - jan1dow + 7) % 7;
|
|
82
|
+
const firstWeekStart = jan1.add({ days: daysUntilStart });
|
|
83
|
+
return firstWeekStart.add({ weeks: week - 1 });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 5. Month+year (with optional day)
|
|
87
|
+
const { month, year, day } = spec as {
|
|
88
|
+
month: number;
|
|
89
|
+
year: number;
|
|
90
|
+
day?: number;
|
|
91
|
+
};
|
|
92
|
+
const targetDay = day ?? 1;
|
|
93
|
+
const plain = T.PlainDate.from({ year, month, day: targetDay });
|
|
94
|
+
return snapToWeekStart(plain, weekStartDay);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Computes a new `firstWeek` date given a scroll target and snap mode.
|
|
99
|
+
*
|
|
100
|
+
* The `weekStartDay` is inferred from `currentFirstWeek` (its day of week).
|
|
101
|
+
*
|
|
102
|
+
* @param currentFirstWeek - The current first visible week (a week-start date).
|
|
103
|
+
* @param weekCount - Number of visible week rows.
|
|
104
|
+
* @param target - The week-start date to scroll to.
|
|
105
|
+
* @param options - Optional snap mode (default `"start"`).
|
|
106
|
+
*/
|
|
107
|
+
export function resolveFirstWeek(
|
|
108
|
+
currentFirstWeek: Temporal.PlainDate,
|
|
109
|
+
weekCount: number,
|
|
110
|
+
target: Temporal.PlainDate,
|
|
111
|
+
options?: { snap?: ScrollToWeekSnap },
|
|
112
|
+
): Temporal.PlainDate {
|
|
113
|
+
const snap = options?.snap ?? "start";
|
|
114
|
+
|
|
115
|
+
switch (snap) {
|
|
116
|
+
case "start":
|
|
117
|
+
return target;
|
|
118
|
+
|
|
119
|
+
case "center":
|
|
120
|
+
return target.subtract({ weeks: Math.floor(weekCount / 2) });
|
|
121
|
+
|
|
122
|
+
case "end":
|
|
123
|
+
return target.subtract({ weeks: weekCount - 1 });
|
|
124
|
+
|
|
125
|
+
case "nearest": {
|
|
126
|
+
const lastWeek = currentFirstWeek.add({ weeks: weekCount - 1 });
|
|
127
|
+
const cmp = (a: Temporal.PlainDate, b: Temporal.PlainDate): number => {
|
|
128
|
+
if (a.year !== b.year) return a.year - b.year;
|
|
129
|
+
if (a.month !== b.month) return a.month - b.month;
|
|
130
|
+
return a.day - b.day;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (cmp(target, currentFirstWeek) < 0) {
|
|
134
|
+
// Target is above (before) the window — snap to start
|
|
135
|
+
return target;
|
|
136
|
+
}
|
|
137
|
+
if (cmp(target, lastWeek) > 0) {
|
|
138
|
+
// Target is below (after) the window — snap to end
|
|
139
|
+
return target.subtract({ weeks: weekCount - 1 });
|
|
140
|
+
}
|
|
141
|
+
// Already visible — no change
|
|
142
|
+
return currentFirstWeek;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|