@super-calendar/core 2.14.2 → 2.16.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/README.md +45 -2
- package/dist/index.d.mts +29 -1019
- package/dist/index.d.ts +29 -1019
- package/dist/index.js +124 -1599
- package/dist/index.mjs +58 -1534
- package/dist/recurrence-BvmTFytR.js +1943 -0
- package/dist/recurrence-CvUX9ncg.mjs +1548 -0
- package/dist/timezone-d6EaWlvP.d.mts +996 -0
- package/dist/timezone-d6EaWlvP.d.ts +996 -0
- package/dist/vanilla.d.mts +2 -0
- package/dist/vanilla.d.ts +2 -0
- package/dist/vanilla.js +68 -0
- package/dist/vanilla.mjs +1 -0
- package/package.json +18 -2
- package/src/index.ts +3 -0
- package/src/types.ts +19 -2
- package/src/utils/dateRange.ts +0 -71
- package/src/utils/eventSource.ts +166 -0
- package/src/utils/labels.ts +15 -2
- package/src/utils/monthGrid.ts +0 -41
- package/src/utils/recurrence.ts +9 -10
- package/src/utils/useDateRange.ts +80 -0
- package/src/utils/useEventSource.ts +20 -41
- package/src/utils/useMonthGrid.ts +42 -0
- package/src/vanilla.ts +15 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { startOfDay } from "date-fns";
|
|
2
|
+
import {
|
|
3
|
+
type Context,
|
|
4
|
+
createContext,
|
|
5
|
+
type Dispatch,
|
|
6
|
+
type SetStateAction,
|
|
7
|
+
useCallback,
|
|
8
|
+
useContext,
|
|
9
|
+
useMemo,
|
|
10
|
+
useState,
|
|
11
|
+
} from "react";
|
|
12
|
+
import {
|
|
13
|
+
type CalendarSelection,
|
|
14
|
+
type DateRange,
|
|
15
|
+
type DateSelectionConstraints,
|
|
16
|
+
type UseDateRangeOptions,
|
|
17
|
+
isDateSelectable,
|
|
18
|
+
nextDateRange,
|
|
19
|
+
} from "./dateRange";
|
|
20
|
+
|
|
21
|
+
const CalendarSelectionContext: Context<CalendarSelection> = createContext<CalendarSelection>({});
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Provides the active selection to the month grid. Day cells read it via
|
|
25
|
+
* {@link useCalendarSelection} so they repaint on selection changes even when
|
|
26
|
+
* the virtualized list has cached (and so won't re-render) their page.
|
|
27
|
+
*/
|
|
28
|
+
export const CalendarSelectionProvider = CalendarSelectionContext.Provider;
|
|
29
|
+
|
|
30
|
+
/** Reads the active selection provided by {@link CalendarSelectionProvider}. */
|
|
31
|
+
export const useCalendarSelection = (): CalendarSelection => useContext(CalendarSelectionContext);
|
|
32
|
+
|
|
33
|
+
/** The state and handlers returned by {@link useDateRange}. */
|
|
34
|
+
export interface UseDateRangeResult {
|
|
35
|
+
/** The current selection; `null` until the first endpoint is picked. */
|
|
36
|
+
range: DateRange | null;
|
|
37
|
+
/** Wire to `onPressDay`: advances the range (start, then end, then restarts). */
|
|
38
|
+
onPressDate: (date: Date) => void;
|
|
39
|
+
/** Set both endpoints at once (ordered); ignored if either isn't selectable. */
|
|
40
|
+
selectRange: (a: Date, b: Date) => void;
|
|
41
|
+
/** Clear the selection. */
|
|
42
|
+
reset: () => void;
|
|
43
|
+
/** The raw state setter, for full control. */
|
|
44
|
+
setRange: Dispatch<SetStateAction<DateRange | null>>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Controlled-ish range selection state for the month view. Returns the current
|
|
49
|
+
* `range` plus an `onPressDate` handler to wire to `Calendar`'s `onPressDay`, a
|
|
50
|
+
* `reset`, and the raw `setRange` for full control.
|
|
51
|
+
*
|
|
52
|
+
* ```tsx
|
|
53
|
+
* const { range, onPressDate } = useDateRange({ minDate: new Date() });
|
|
54
|
+
* <Calendar mode="month" selectedRange={range ?? undefined} onPressDay={onPressDate} … />
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function useDateRange(options: UseDateRangeOptions = {}): UseDateRangeResult {
|
|
58
|
+
const { initialRange = null, minDate, maxDate, isDateDisabled } = options;
|
|
59
|
+
const [range, setRange] = useState<DateRange | null>(initialRange);
|
|
60
|
+
const constraints = useMemo<DateSelectionConstraints>(
|
|
61
|
+
() => ({ minDate, maxDate, isDateDisabled }),
|
|
62
|
+
[minDate, maxDate, isDateDisabled],
|
|
63
|
+
);
|
|
64
|
+
const onPressDate = useCallback(
|
|
65
|
+
(date: Date) => setRange((previous) => nextDateRange(previous, date, constraints)),
|
|
66
|
+
[constraints],
|
|
67
|
+
);
|
|
68
|
+
// Set both endpoints at once (ordered), for drag-to-select. Ignores the update
|
|
69
|
+
// if either endpoint isn't selectable.
|
|
70
|
+
const selectRange = useCallback(
|
|
71
|
+
(a: Date, b: Date) => {
|
|
72
|
+
if (!isDateSelectable(a, constraints) || !isDateSelectable(b, constraints)) return;
|
|
73
|
+
const [start, end] = a.getTime() <= b.getTime() ? [a, b] : [b, a];
|
|
74
|
+
setRange({ start: startOfDay(start), end: startOfDay(end) });
|
|
75
|
+
},
|
|
76
|
+
[constraints],
|
|
77
|
+
);
|
|
78
|
+
const reset = useCallback(() => setRange(null), []);
|
|
79
|
+
return { range, onPressDate, selectRange, reset, setRange };
|
|
80
|
+
}
|
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
2
|
import type { CalendarEvent } from "../types";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
/** Options for {@link useEventSource}. */
|
|
6
|
-
export interface EventSourceOptions<T> {
|
|
7
|
-
/**
|
|
8
|
-
* Feed format for URL sources: `"json"` (default) expects an array of event
|
|
9
|
-
* objects with ISO `start`/`end` strings; `"ics"` parses an iCalendar feed.
|
|
10
|
-
* URLs ending in `.ics` default to `"ics"`.
|
|
11
|
-
*/
|
|
12
|
-
format?: "json" | "ics";
|
|
13
|
-
/**
|
|
14
|
-
* Map the parsed JSON array into events, for feeds whose shape doesn't match
|
|
15
|
-
* `CalendarEvent` (rename fields, attach `resourceId`, filter). The default
|
|
16
|
-
* revives `start`/`end` ISO strings on each item.
|
|
17
|
-
*/
|
|
18
|
-
map?: (items: unknown[]) => CalendarEvent<T>[];
|
|
19
|
-
/** Re-fetch every N ms (a live feed). Omit to fetch once. */
|
|
20
|
-
refetchIntervalMs?: number;
|
|
21
|
-
}
|
|
3
|
+
import { loadEventSource, type EventSource, type EventSourceOptions } from "./eventSource";
|
|
4
|
+
export type { EventSourceOptions } from "./eventSource";
|
|
22
5
|
|
|
23
6
|
/** What {@link useEventSource} returns. */
|
|
24
7
|
export interface EventSourceState<T> {
|
|
@@ -32,12 +15,6 @@ export interface EventSourceState<T> {
|
|
|
32
15
|
refetch: () => Promise<void>;
|
|
33
16
|
}
|
|
34
17
|
|
|
35
|
-
const defaultMap = <T>(items: unknown[]): CalendarEvent<T>[] =>
|
|
36
|
-
items.map((raw) => {
|
|
37
|
-
const item = raw as Record<string, unknown> & { start: string; end: string };
|
|
38
|
-
return { ...item, start: new Date(item.start), end: new Date(item.end) } as CalendarEvent<T>;
|
|
39
|
-
});
|
|
40
|
-
|
|
41
18
|
/**
|
|
42
19
|
* Load events from a refreshable source: a JSON feed URL, an iCalendar feed
|
|
43
20
|
* URL, or your own async function. The hook owns fetching, optional interval
|
|
@@ -53,7 +30,7 @@ const defaultMap = <T>(items: unknown[]): CalendarEvent<T>[] =>
|
|
|
53
30
|
* ```
|
|
54
31
|
*/
|
|
55
32
|
export function useEventSource<T = unknown>(
|
|
56
|
-
source:
|
|
33
|
+
source: EventSource<T>,
|
|
57
34
|
{ format, map, refetchIntervalMs }: EventSourceOptions<T> = {},
|
|
58
35
|
): EventSourceState<T> {
|
|
59
36
|
const [events, setEvents] = useState<CalendarEvent<T>[]>([]);
|
|
@@ -64,6 +41,7 @@ export function useEventSource<T = unknown>(
|
|
|
64
41
|
// Abort the superseded URL request too, so it stops consuming the network
|
|
65
42
|
// instead of just being ignored when it lands.
|
|
66
43
|
const abortRef = useRef<AbortController | null>(null);
|
|
44
|
+
const active = useRef(false);
|
|
67
45
|
|
|
68
46
|
// The latest source/options without re-subscribing the interval on every
|
|
69
47
|
// render (inline functions and option literals are expected).
|
|
@@ -71,6 +49,7 @@ export function useEventSource<T = unknown>(
|
|
|
71
49
|
latest.current = { source, format, map };
|
|
72
50
|
|
|
73
51
|
const load = useCallback(async (isManual: boolean) => {
|
|
52
|
+
if (!active.current) return;
|
|
74
53
|
const gen = ++generation.current;
|
|
75
54
|
abortRef.current?.abort();
|
|
76
55
|
const controller = typeof AbortController === "undefined" ? null : new AbortController();
|
|
@@ -78,20 +57,12 @@ export function useEventSource<T = unknown>(
|
|
|
78
57
|
const { source: src, format: fmt, map: mapper } = latest.current;
|
|
79
58
|
if (isManual) setLoading(true);
|
|
80
59
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const isIcs = fmt === "ics" || (fmt == null && /\.ics(\?|$)/.test(src));
|
|
88
|
-
if (isIcs) {
|
|
89
|
-
next = parseICalendar(await response.text()) as CalendarEvent<T>[];
|
|
90
|
-
} else {
|
|
91
|
-
const body = (await response.json()) as unknown[];
|
|
92
|
-
next = (mapper ?? defaultMap<T>)(body);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
60
|
+
const next = await loadEventSource({
|
|
61
|
+
source: src,
|
|
62
|
+
format: fmt,
|
|
63
|
+
map: mapper,
|
|
64
|
+
signal: controller?.signal,
|
|
65
|
+
});
|
|
95
66
|
if (gen !== generation.current) return;
|
|
96
67
|
setEvents(next);
|
|
97
68
|
setError(null);
|
|
@@ -105,15 +76,23 @@ export function useEventSource<T = unknown>(
|
|
|
105
76
|
|
|
106
77
|
const sourceKey = typeof source === "string" ? source : "fn";
|
|
107
78
|
useEffect(() => {
|
|
79
|
+
active.current = true;
|
|
108
80
|
void load(true);
|
|
109
|
-
if (
|
|
81
|
+
if (
|
|
82
|
+
!refetchIntervalMs ||
|
|
83
|
+
!Number.isFinite(refetchIntervalMs) ||
|
|
84
|
+
refetchIntervalMs <= 0 ||
|
|
85
|
+
refetchIntervalMs > 2 ** 31 - 1
|
|
86
|
+
) {
|
|
110
87
|
return () => {
|
|
88
|
+
active.current = false;
|
|
111
89
|
generation.current++;
|
|
112
90
|
abortRef.current?.abort();
|
|
113
91
|
};
|
|
114
92
|
}
|
|
115
93
|
const id = setInterval(() => void load(false), refetchIntervalMs);
|
|
116
94
|
return () => {
|
|
95
|
+
active.current = false;
|
|
117
96
|
clearInterval(id);
|
|
118
97
|
generation.current++;
|
|
119
98
|
abortRef.current?.abort();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { buildMonthGrid, type MonthGrid, type UseMonthGridOptions } from "./monthGrid";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Headless month-grid hook. Returns the weeks and weekday headers for `month`,
|
|
6
|
+
* each day annotated with selection/disabled/today state, so you can render a
|
|
7
|
+
* fully custom calendar without reimplementing the date maths.
|
|
8
|
+
*
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const { weeks, weekdays } = useMonthGrid(month, { selectedRange: range });
|
|
11
|
+
* // map weekdays -> header cells, weeks -> rows, days -> your own <DayCell />
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function useMonthGrid(month: Date, options: UseMonthGridOptions = {}): MonthGrid {
|
|
15
|
+
const {
|
|
16
|
+
weekStartsOn,
|
|
17
|
+
showSixWeeks,
|
|
18
|
+
isRTL,
|
|
19
|
+
selectedDates,
|
|
20
|
+
selectedRange,
|
|
21
|
+
minDate,
|
|
22
|
+
maxDate,
|
|
23
|
+
isDateDisabled,
|
|
24
|
+
locale,
|
|
25
|
+
} = options;
|
|
26
|
+
return useMemo(
|
|
27
|
+
() => buildMonthGrid(month, options),
|
|
28
|
+
// Deps are the destructured option fields, not the (unstable) options object.
|
|
29
|
+
[
|
|
30
|
+
month,
|
|
31
|
+
weekStartsOn,
|
|
32
|
+
showSixWeeks,
|
|
33
|
+
isRTL,
|
|
34
|
+
selectedDates,
|
|
35
|
+
selectedRange,
|
|
36
|
+
minDate,
|
|
37
|
+
maxDate,
|
|
38
|
+
isDateDisabled,
|
|
39
|
+
locale,
|
|
40
|
+
],
|
|
41
|
+
);
|
|
42
|
+
}
|
package/src/vanilla.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Framework-independent calendar types, date calculations, layout, and theme tokens. */
|
|
2
|
+
export * from "./types";
|
|
3
|
+
export * from "./tokens";
|
|
4
|
+
export * from "./presentation";
|
|
5
|
+
export * from "./utils/dateRange";
|
|
6
|
+
export * from "./utils/dates";
|
|
7
|
+
export * from "./utils/drag";
|
|
8
|
+
export * from "./utils/eventDisplay";
|
|
9
|
+
export * from "./utils/ical";
|
|
10
|
+
export * from "./utils/labels";
|
|
11
|
+
export * from "./utils/layout";
|
|
12
|
+
export * from "./utils/monthGrid";
|
|
13
|
+
export * from "./utils/recurrence";
|
|
14
|
+
export * from "./utils/timezone";
|
|
15
|
+
export * from "./utils/eventSource";
|