@super-calendar/dom 2.4.0 → 2.6.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/index.d.mts +62 -6
- package/dist/index.d.ts +62 -6
- package/dist/index.js +481 -144
- package/dist/index.mjs +483 -147
- package/package.json +2 -2
- package/src/Calendar.tsx +38 -8
- package/src/ResourceTimeline.tsx +192 -3
- package/src/YearView.tsx +283 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@super-calendar/dom",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Virtualized month / week / day calendar and date picker for react-dom. No React Native, no react-native-web.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"calendar",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@super-calendar/core": "2.
|
|
51
|
+
"@super-calendar/core": "2.6.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@legendapp/list": ">=3",
|
package/src/Calendar.tsx
CHANGED
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
startOfDay,
|
|
9
9
|
startOfMonth,
|
|
10
10
|
startOfWeek,
|
|
11
|
+
addYears,
|
|
12
|
+
endOfYear,
|
|
13
|
+
startOfYear,
|
|
11
14
|
} from "date-fns";
|
|
12
15
|
import {
|
|
13
16
|
type CSSProperties,
|
|
@@ -17,6 +20,7 @@ import {
|
|
|
17
20
|
useMemo,
|
|
18
21
|
} from "react";
|
|
19
22
|
import {
|
|
23
|
+
type CalendarMode,
|
|
20
24
|
type BusinessHours,
|
|
21
25
|
type CalendarEvent,
|
|
22
26
|
type DateRange,
|
|
@@ -25,11 +29,11 @@ import {
|
|
|
25
29
|
eventsInTimeZone,
|
|
26
30
|
expandRecurringEvents,
|
|
27
31
|
getViewDays,
|
|
28
|
-
type TimeGridMode,
|
|
29
32
|
type WeekdayFormat,
|
|
30
33
|
type WeekStartsOn,
|
|
31
34
|
} from "@super-calendar/core";
|
|
32
35
|
import { Agenda, type AgendaSlot, type DomAgendaEvent } from "./Agenda";
|
|
36
|
+
import { YearView, type YearViewSlot } from "./YearView";
|
|
33
37
|
import { type DomMonthEvent, MonthView, type MonthViewSlot } from "./MonthView";
|
|
34
38
|
import type { SlotStyleProps } from "./slots";
|
|
35
39
|
import { type DomRenderEvent, TimeGrid, type TimeGridSlot } from "./TimeGrid";
|
|
@@ -40,7 +44,7 @@ import type { DomCalendarTheme } from "./theme";
|
|
|
40
44
|
* for the active `mode` apply; the rest are ignored. See {@link MonthViewSlot},
|
|
41
45
|
* {@link TimeGridSlot}, and {@link AgendaSlot}.
|
|
42
46
|
*/
|
|
43
|
-
export type CalendarSlot = MonthViewSlot | TimeGridSlot | AgendaSlot;
|
|
47
|
+
export type CalendarSlot = MonthViewSlot | TimeGridSlot | AgendaSlot | YearViewSlot;
|
|
44
48
|
|
|
45
49
|
/** Props for {@link Calendar}. */
|
|
46
50
|
export interface CalendarProps<T = unknown>
|
|
@@ -49,7 +53,7 @@ export interface CalendarProps<T = unknown>
|
|
|
49
53
|
* The view to render (default "week"). `month` renders a month grid, `schedule`
|
|
50
54
|
* a day-grouped agenda list, and the others a time grid.
|
|
51
55
|
*/
|
|
52
|
-
mode?:
|
|
56
|
+
mode?: CalendarMode;
|
|
53
57
|
/** Controlled anchor date. Change it (e.g. from your own header) to navigate. */
|
|
54
58
|
date: Date;
|
|
55
59
|
/**
|
|
@@ -161,6 +165,8 @@ export interface CalendarProps<T = unknown>
|
|
|
161
165
|
keyboardDayNavigation?: boolean;
|
|
162
166
|
/** Tap a day cell. */
|
|
163
167
|
onPressDay?: (date: Date) => void;
|
|
168
|
+
/** Tap a month's title in the year view — e.g. jump to that month. */
|
|
169
|
+
onPressMonth?: (month: Date) => void;
|
|
164
170
|
/** Tap the "+N more" overflow row. */
|
|
165
171
|
onPressMore?: (events: CalendarEvent<T>[], date: Date) => void;
|
|
166
172
|
/** Custom month chip renderer. */
|
|
@@ -179,7 +185,7 @@ const EMPTY_EVENTS: CalendarEvent<never>[] = [];
|
|
|
179
185
|
// `month`, the day columns for the time-grid modes, and a forward window for the
|
|
180
186
|
// `schedule` agenda (which has no bounded viewport of its own).
|
|
181
187
|
function expansionRange(
|
|
182
|
-
mode:
|
|
188
|
+
mode: CalendarMode,
|
|
183
189
|
date: Date,
|
|
184
190
|
weekStartsOn: WeekStartsOn,
|
|
185
191
|
numberOfDays: number | undefined,
|
|
@@ -190,6 +196,12 @@ function expansionRange(
|
|
|
190
196
|
endOfWeek(endOfMonth(date), { weekStartsOn }),
|
|
191
197
|
];
|
|
192
198
|
}
|
|
199
|
+
if (mode === "year") {
|
|
200
|
+
return [
|
|
201
|
+
startOfWeek(startOfYear(date), { weekStartsOn }),
|
|
202
|
+
endOfWeek(endOfYear(date), { weekStartsOn }),
|
|
203
|
+
];
|
|
204
|
+
}
|
|
193
205
|
if (mode === "schedule") {
|
|
194
206
|
// The agenda lists forward from the anchor date; three months is a sensible
|
|
195
207
|
// default look-ahead. Pre-expand for a different window.
|
|
@@ -199,15 +211,16 @@ function expansionRange(
|
|
|
199
211
|
return [startOfDay(days[0]), endOfDay(days[days.length - 1])];
|
|
200
212
|
}
|
|
201
213
|
|
|
202
|
-
// The date one page away in `direction` (+1 next, −1 previous): a
|
|
203
|
-
// `month`, a week for `schedule`, else the view's day span.
|
|
214
|
+
// The date one page away in `direction` (+1 next, −1 previous): a year for
|
|
215
|
+
// `year`, a month for `month`, a week for `schedule`, else the view's day span.
|
|
204
216
|
function pageStep(
|
|
205
|
-
mode:
|
|
217
|
+
mode: CalendarMode,
|
|
206
218
|
date: Date,
|
|
207
219
|
direction: number,
|
|
208
220
|
weekStartsOn: WeekStartsOn,
|
|
209
221
|
numberOfDays: number | undefined,
|
|
210
222
|
): Date {
|
|
223
|
+
if (mode === "year") return addYears(date, direction);
|
|
211
224
|
if (mode === "month") return addMonths(date, direction);
|
|
212
225
|
if (mode === "schedule") return addDays(date, direction * 7);
|
|
213
226
|
return addDays(date, direction * getViewDays(mode, date, weekStartsOn, numberOfDays ?? 1).length);
|
|
@@ -274,6 +287,7 @@ export function Calendar<T = unknown>({
|
|
|
274
287
|
isDateDisabled,
|
|
275
288
|
keyboardDayNavigation,
|
|
276
289
|
onPressDay,
|
|
290
|
+
onPressMonth,
|
|
277
291
|
onPressMore,
|
|
278
292
|
renderMonthEvent,
|
|
279
293
|
// schedule
|
|
@@ -313,7 +327,23 @@ export function Calendar<T = unknown>({
|
|
|
313
327
|
};
|
|
314
328
|
|
|
315
329
|
let view: ReactElement;
|
|
316
|
-
if (mode === "
|
|
330
|
+
if (mode === "year") {
|
|
331
|
+
view = (
|
|
332
|
+
<YearView<T>
|
|
333
|
+
date={date}
|
|
334
|
+
events={displayEvents}
|
|
335
|
+
weekStartsOn={weekStartsOn}
|
|
336
|
+
locale={locale}
|
|
337
|
+
theme={theme}
|
|
338
|
+
className={className}
|
|
339
|
+
style={height != null ? { height, ...style } : style}
|
|
340
|
+
classNames={classNames}
|
|
341
|
+
styles={styles}
|
|
342
|
+
onPressDay={onPressDay}
|
|
343
|
+
onPressMonth={onPressMonth}
|
|
344
|
+
/>
|
|
345
|
+
);
|
|
346
|
+
} else if (mode === "schedule") {
|
|
317
347
|
view = (
|
|
318
348
|
<Agenda<T>
|
|
319
349
|
events={displayEvents}
|
package/src/ResourceTimeline.tsx
CHANGED
|
@@ -8,6 +8,8 @@ import type {
|
|
|
8
8
|
import { useMemo, useRef, useState } from "react";
|
|
9
9
|
import {
|
|
10
10
|
type CalendarEvent,
|
|
11
|
+
cellRangeFromDrag,
|
|
12
|
+
closedHourBands,
|
|
11
13
|
eventTimeLabel,
|
|
12
14
|
formatHour,
|
|
13
15
|
layoutDayEvents,
|
|
@@ -28,8 +30,10 @@ export type ResourceTimelineSlot =
|
|
|
28
30
|
| "row"
|
|
29
31
|
| "track"
|
|
30
32
|
| "gridLines"
|
|
33
|
+
| "businessHours"
|
|
31
34
|
| "event"
|
|
32
|
-
| "eventBox"
|
|
35
|
+
| "eventBox"
|
|
36
|
+
| "createGhost";
|
|
33
37
|
|
|
34
38
|
/** A schedulable lane (room, person, machine) that events are grouped under. */
|
|
35
39
|
export interface Resource {
|
|
@@ -94,6 +98,16 @@ export interface ResourceTimelineProps<T = unknown> extends SlotStyleProps<Resou
|
|
|
94
98
|
* proposed new start/end. Return `false` to reject the drop (it snaps back).
|
|
95
99
|
*/
|
|
96
100
|
onDragEvent?: (event: CalendarEvent<T>, start: Date, end: Date) => void | boolean;
|
|
101
|
+
/** Tap empty lane space; called with the snapped time and the lane's resource. */
|
|
102
|
+
onPressCell?: (at: Date, resource: Resource) => void;
|
|
103
|
+
/** Drag empty lane space to create; called with the swept start/end and the lane's resource. */
|
|
104
|
+
onCreateEvent?: (start: Date, end: Date, resource: Resource) => void;
|
|
105
|
+
/**
|
|
106
|
+
* Shade the hours outside this window, per lane. Same shape as the Calendar's
|
|
107
|
+
* `businessHours` plus the lane's resource, so per-resource opening hours work
|
|
108
|
+
* (return `null` for a fully closed lane). A date-only function is accepted.
|
|
109
|
+
*/
|
|
110
|
+
businessHours?: (date: Date, resource: Resource) => { start: number; end: number } | null;
|
|
97
111
|
/** Snap dragged events to this many minutes (default 15). */
|
|
98
112
|
dragStepMinutes?: number;
|
|
99
113
|
/** Class applied to the root element. */
|
|
@@ -198,6 +212,9 @@ export function ResourceTimeline<T = unknown>({
|
|
|
198
212
|
renderEvent,
|
|
199
213
|
onPressEvent,
|
|
200
214
|
onDragEvent,
|
|
215
|
+
onPressCell,
|
|
216
|
+
onCreateEvent,
|
|
217
|
+
businessHours,
|
|
201
218
|
dragStepMinutes = 15,
|
|
202
219
|
className,
|
|
203
220
|
style,
|
|
@@ -285,6 +302,90 @@ export function ResourceTimeline<T = unknown>({
|
|
|
285
302
|
origin.current = null;
|
|
286
303
|
};
|
|
287
304
|
|
|
305
|
+
// Create-by-drag / tap on empty lane space, mirroring the time grid: mouse and
|
|
306
|
+
// pen sweep out a new event, and a tap without movement fires onPressCell. Touch
|
|
307
|
+
// is left free to scroll the board, so cell taps are pointer (mouse/pen) only.
|
|
308
|
+
const [createBox, setCreateBox] = useState<{
|
|
309
|
+
resourceKey: string;
|
|
310
|
+
startPx: number;
|
|
311
|
+
curPx: number;
|
|
312
|
+
} | null>(null);
|
|
313
|
+
const createOrigin = useRef<{ el: HTMLElement; resource: Resource; startPx: number } | null>(
|
|
314
|
+
null,
|
|
315
|
+
);
|
|
316
|
+
const cellEnabled = !!onPressCell || !!onCreateEvent;
|
|
317
|
+
const pxAlongAxis = (el: HTMLElement, e: ReactPointerEvent) => {
|
|
318
|
+
const rect = el.getBoundingClientRect();
|
|
319
|
+
return vertical ? e.clientY - rect.top : e.clientX - rect.left;
|
|
320
|
+
};
|
|
321
|
+
const beginCreate = (e: ReactPointerEvent, resource: Resource) => {
|
|
322
|
+
// Only from the lane background, primary button, never on an event bar.
|
|
323
|
+
if (!cellEnabled || e.pointerType === "touch") return;
|
|
324
|
+
if (e.target !== e.currentTarget || e.button > 0) return;
|
|
325
|
+
const el = e.currentTarget as HTMLElement;
|
|
326
|
+
const startPx = pxAlongAxis(el, e);
|
|
327
|
+
try {
|
|
328
|
+
el.setPointerCapture?.(e.pointerId);
|
|
329
|
+
} catch {
|
|
330
|
+
// Best-effort capture.
|
|
331
|
+
}
|
|
332
|
+
createOrigin.current = { el, resource, startPx };
|
|
333
|
+
// Only preview a ghost when a create can happen; in press-only mode
|
|
334
|
+
// (onPressCell without onCreateEvent) the tap still commits via endCreate.
|
|
335
|
+
if (onCreateEvent) setCreateBox({ resourceKey: resource.id, startPx, curPx: startPx });
|
|
336
|
+
};
|
|
337
|
+
const moveCreate = (e: ReactPointerEvent) => {
|
|
338
|
+
const o = createOrigin.current;
|
|
339
|
+
if (!o || !onCreateEvent) return;
|
|
340
|
+
setCreateBox({ resourceKey: o.resource.id, startPx: o.startPx, curPx: pxAlongAxis(o.el, e) });
|
|
341
|
+
};
|
|
342
|
+
const endCreate = (e: ReactPointerEvent) => {
|
|
343
|
+
const o = createOrigin.current;
|
|
344
|
+
if (!o) return;
|
|
345
|
+
try {
|
|
346
|
+
o.el.releasePointerCapture?.(e.pointerId);
|
|
347
|
+
} catch {
|
|
348
|
+
// Best-effort release.
|
|
349
|
+
}
|
|
350
|
+
const endPx = pxAlongAxis(o.el, e);
|
|
351
|
+
const moved = Math.abs(endPx - o.startPx) > 4;
|
|
352
|
+
if (moved && onCreateEvent) {
|
|
353
|
+
const range = cellRangeFromDrag(date, o.startPx, endPx, hourSize, startHour, dragStepMinutes);
|
|
354
|
+
if (range) onCreateEvent(range.start, range.end, o.resource);
|
|
355
|
+
} else if (onPressCell) {
|
|
356
|
+
const at = cellRangeFromDrag(
|
|
357
|
+
date,
|
|
358
|
+
o.startPx,
|
|
359
|
+
o.startPx,
|
|
360
|
+
hourSize,
|
|
361
|
+
startHour,
|
|
362
|
+
dragStepMinutes,
|
|
363
|
+
);
|
|
364
|
+
if (at) onPressCell(at.start, o.resource);
|
|
365
|
+
}
|
|
366
|
+
createOrigin.current = null;
|
|
367
|
+
setCreateBox(null);
|
|
368
|
+
};
|
|
369
|
+
// A gesture the browser cancels (scroll takeover, etc.) must not commit.
|
|
370
|
+
const cancelCreate = () => {
|
|
371
|
+
createOrigin.current = null;
|
|
372
|
+
setCreateBox(null);
|
|
373
|
+
};
|
|
374
|
+
const trackInteractionProps = (resource: Resource) =>
|
|
375
|
+
cellEnabled
|
|
376
|
+
? {
|
|
377
|
+
onPointerDown: (e: ReactPointerEvent) => beginCreate(e, resource),
|
|
378
|
+
onPointerMove: moveCreate,
|
|
379
|
+
onPointerUp: endCreate,
|
|
380
|
+
onPointerCancel: cancelCreate,
|
|
381
|
+
}
|
|
382
|
+
: null;
|
|
383
|
+
// The closed-hours bands to shade in a lane, resolved per resource.
|
|
384
|
+
const bandsFor = (resource: Resource) =>
|
|
385
|
+
businessHours
|
|
386
|
+
? closedHourBands(date, (d) => businessHours(d, resource), startHour, endHour)
|
|
387
|
+
: [];
|
|
388
|
+
|
|
288
389
|
const hours = useMemo(
|
|
289
390
|
() => Array.from({ length: Math.max(0, endHour - startHour) }, (_, i) => startHour + i),
|
|
290
391
|
[startHour, endHour],
|
|
@@ -384,7 +485,29 @@ export function ResourceTimeline<T = unknown>({
|
|
|
384
485
|
themed: { borderLeft: `1px solid ${theme.gridLine}` },
|
|
385
486
|
})}
|
|
386
487
|
>
|
|
387
|
-
<div
|
|
488
|
+
<div
|
|
489
|
+
{...trackInteractionProps(resource)}
|
|
490
|
+
{...slot("track", { base: { position: "relative", height: trackHeight } })}
|
|
491
|
+
>
|
|
492
|
+
{/* Closed-hours shade, behind the grid lines and events. */}
|
|
493
|
+
{bandsFor(resource).map((b) => (
|
|
494
|
+
<div
|
|
495
|
+
key={b.start}
|
|
496
|
+
aria-hidden
|
|
497
|
+
{...slot("businessHours", {
|
|
498
|
+
base: {
|
|
499
|
+
position: "absolute",
|
|
500
|
+
left: 0,
|
|
501
|
+
right: 0,
|
|
502
|
+
top: (b.start - startHour) * hourHeight,
|
|
503
|
+
height: (b.end - b.start) * hourHeight,
|
|
504
|
+
pointerEvents: "none",
|
|
505
|
+
zIndex: 0,
|
|
506
|
+
},
|
|
507
|
+
themed: { background: theme.outsideHoursBackground },
|
|
508
|
+
})}
|
|
509
|
+
/>
|
|
510
|
+
))}
|
|
388
511
|
<div
|
|
389
512
|
aria-hidden
|
|
390
513
|
{...slot("gridLines", {
|
|
@@ -472,6 +595,28 @@ export function ResourceTimeline<T = unknown>({
|
|
|
472
595
|
</button>
|
|
473
596
|
);
|
|
474
597
|
})}
|
|
598
|
+
{createBox?.resourceKey === resource.id ? (
|
|
599
|
+
<div
|
|
600
|
+
aria-hidden
|
|
601
|
+
{...slot("createGhost", {
|
|
602
|
+
base: {
|
|
603
|
+
position: "absolute",
|
|
604
|
+
left: 1,
|
|
605
|
+
right: 1,
|
|
606
|
+
top: Math.min(createBox.startPx, createBox.curPx),
|
|
607
|
+
height: Math.max(Math.abs(createBox.curPx - createBox.startPx), 2),
|
|
608
|
+
opacity: 0.7,
|
|
609
|
+
pointerEvents: "none",
|
|
610
|
+
zIndex: 2,
|
|
611
|
+
},
|
|
612
|
+
themed: {
|
|
613
|
+
background: theme.rangeBackground,
|
|
614
|
+
border: `1px solid ${theme.selectedBackground}`,
|
|
615
|
+
borderRadius: 6,
|
|
616
|
+
},
|
|
617
|
+
})}
|
|
618
|
+
/>
|
|
619
|
+
) : null}
|
|
475
620
|
</div>
|
|
476
621
|
</div>
|
|
477
622
|
);
|
|
@@ -544,7 +689,29 @@ export function ResourceTimeline<T = unknown>({
|
|
|
544
689
|
>
|
|
545
690
|
{resource.title ?? resource.id}
|
|
546
691
|
</div>
|
|
547
|
-
<div
|
|
692
|
+
<div
|
|
693
|
+
{...trackInteractionProps(resource)}
|
|
694
|
+
{...slot("track", { base: { position: "relative", width: trackWidth } })}
|
|
695
|
+
>
|
|
696
|
+
{/* Closed-hours shade, behind the grid lines and events. */}
|
|
697
|
+
{bandsFor(resource).map((b) => (
|
|
698
|
+
<div
|
|
699
|
+
key={b.start}
|
|
700
|
+
aria-hidden
|
|
701
|
+
{...slot("businessHours", {
|
|
702
|
+
base: {
|
|
703
|
+
position: "absolute",
|
|
704
|
+
top: 0,
|
|
705
|
+
bottom: 0,
|
|
706
|
+
left: (b.start - startHour) * hourWidth,
|
|
707
|
+
width: (b.end - b.start) * hourWidth,
|
|
708
|
+
pointerEvents: "none",
|
|
709
|
+
zIndex: 0,
|
|
710
|
+
},
|
|
711
|
+
themed: { background: theme.outsideHoursBackground },
|
|
712
|
+
})}
|
|
713
|
+
/>
|
|
714
|
+
))}
|
|
548
715
|
<div
|
|
549
716
|
aria-hidden
|
|
550
717
|
{...slot("gridLines", {
|
|
@@ -627,6 +794,28 @@ export function ResourceTimeline<T = unknown>({
|
|
|
627
794
|
</button>
|
|
628
795
|
);
|
|
629
796
|
})}
|
|
797
|
+
{createBox?.resourceKey === resource.id ? (
|
|
798
|
+
<div
|
|
799
|
+
aria-hidden
|
|
800
|
+
{...slot("createGhost", {
|
|
801
|
+
base: {
|
|
802
|
+
position: "absolute",
|
|
803
|
+
top: 1,
|
|
804
|
+
bottom: 1,
|
|
805
|
+
left: Math.min(createBox.startPx, createBox.curPx),
|
|
806
|
+
width: Math.max(Math.abs(createBox.curPx - createBox.startPx), 2),
|
|
807
|
+
opacity: 0.7,
|
|
808
|
+
pointerEvents: "none",
|
|
809
|
+
zIndex: 2,
|
|
810
|
+
},
|
|
811
|
+
themed: {
|
|
812
|
+
background: theme.rangeBackground,
|
|
813
|
+
border: `1px solid ${theme.selectedBackground}`,
|
|
814
|
+
borderRadius: 6,
|
|
815
|
+
},
|
|
816
|
+
})}
|
|
817
|
+
/>
|
|
818
|
+
) : null}
|
|
630
819
|
</div>
|
|
631
820
|
</div>
|
|
632
821
|
);
|
package/src/YearView.tsx
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { format, startOfDay, type Locale } from "date-fns";
|
|
2
|
+
import type { CSSProperties, ReactElement } from "react";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import {
|
|
5
|
+
buildMonthWeeks,
|
|
6
|
+
type CalendarEvent,
|
|
7
|
+
getIsToday,
|
|
8
|
+
getWeekDays,
|
|
9
|
+
getYearMonths,
|
|
10
|
+
groupEventsByDay,
|
|
11
|
+
isSameCalendarDay,
|
|
12
|
+
type WeekStartsOn,
|
|
13
|
+
weekdayFormatToken,
|
|
14
|
+
} from "@super-calendar/core";
|
|
15
|
+
import { createSlots, dataState, type SlotStyleProps } from "./slots";
|
|
16
|
+
import { type DomCalendarTheme, mergeDomTheme } from "./theme";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Styleable parts of {@link YearView}. Pass a class or inline style per slot
|
|
20
|
+
* via the `classNames` / `styles` props; day cells carry `data-today` and
|
|
21
|
+
* `data-events` state attributes for variants.
|
|
22
|
+
*/
|
|
23
|
+
export type YearViewSlot =
|
|
24
|
+
| "grid"
|
|
25
|
+
| "month"
|
|
26
|
+
| "monthTitle"
|
|
27
|
+
| "weekdays"
|
|
28
|
+
| "weekday"
|
|
29
|
+
| "week"
|
|
30
|
+
| "day"
|
|
31
|
+
| "eventDot";
|
|
32
|
+
|
|
33
|
+
/** Props for {@link YearView}, the twelve mini-month year grid. */
|
|
34
|
+
export interface YearViewProps<T = unknown> extends SlotStyleProps<YearViewSlot> {
|
|
35
|
+
/** Any date inside the year to render. */
|
|
36
|
+
date: Date;
|
|
37
|
+
/** Days holding at least one event get a dot. Omit for a plain year. */
|
|
38
|
+
events?: CalendarEvent<T>[];
|
|
39
|
+
/** First day of the week. Sunday = 0 (default) … Saturday = 6. */
|
|
40
|
+
weekStartsOn?: WeekStartsOn;
|
|
41
|
+
locale?: Locale;
|
|
42
|
+
/** Highlight this date instead of the real "today". */
|
|
43
|
+
activeDate?: Date;
|
|
44
|
+
/**
|
|
45
|
+
* Smallest width a mini month may take before the grid drops a column
|
|
46
|
+
* (default 150). The grid fits as many columns as the container allows.
|
|
47
|
+
*/
|
|
48
|
+
minMonthWidth?: number;
|
|
49
|
+
/** Tap a day cell (e.g. to drill into the day or month view). */
|
|
50
|
+
onPressDay?: (date: Date) => void;
|
|
51
|
+
/** Tap a month's title (e.g. to jump to that month's view). */
|
|
52
|
+
onPressMonth?: (month: Date) => void;
|
|
53
|
+
/** Theme overrides; falls back to the default light theme. */
|
|
54
|
+
theme?: Partial<DomCalendarTheme>;
|
|
55
|
+
/** Class applied to the root element. */
|
|
56
|
+
className?: string;
|
|
57
|
+
/** Inline styles applied to the root element. */
|
|
58
|
+
style?: CSSProperties;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A year at a glance: the twelve months as compact mini grids, with today
|
|
63
|
+
* highlighted and a dot under days that hold events. It's the view `Calendar`
|
|
64
|
+
* renders for `mode="year"`.
|
|
65
|
+
*/
|
|
66
|
+
export function YearView<T = unknown>({
|
|
67
|
+
date,
|
|
68
|
+
events,
|
|
69
|
+
weekStartsOn = 0,
|
|
70
|
+
locale,
|
|
71
|
+
activeDate,
|
|
72
|
+
minMonthWidth = 150,
|
|
73
|
+
onPressDay,
|
|
74
|
+
onPressMonth,
|
|
75
|
+
theme: themeOverrides,
|
|
76
|
+
className,
|
|
77
|
+
style,
|
|
78
|
+
classNames,
|
|
79
|
+
styles,
|
|
80
|
+
}: YearViewProps<T>): ReactElement {
|
|
81
|
+
const theme = useMemo(() => mergeDomTheme(themeOverrides), [themeOverrides]);
|
|
82
|
+
const slot = createSlots<YearViewSlot>({ classNames, styles });
|
|
83
|
+
|
|
84
|
+
const months = useMemo(() => getYearMonths(date), [date]);
|
|
85
|
+
const weekdayLabels = useMemo(
|
|
86
|
+
() =>
|
|
87
|
+
getWeekDays(date, weekStartsOn).map((d) =>
|
|
88
|
+
format(d, weekdayFormatToken("narrow"), { locale }),
|
|
89
|
+
),
|
|
90
|
+
[date, weekStartsOn, locale],
|
|
91
|
+
);
|
|
92
|
+
// Days that hold at least one event, keyed like `groupEventsByDay`.
|
|
93
|
+
const eventDays = useMemo(
|
|
94
|
+
() => new Set(events && events.length > 0 ? groupEventsByDay(events).keys() : []),
|
|
95
|
+
[events],
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<div
|
|
100
|
+
className={className}
|
|
101
|
+
style={{ fontFamily: theme.fontFamily, color: theme.text, overflowY: "auto", ...style }}
|
|
102
|
+
>
|
|
103
|
+
<div
|
|
104
|
+
{...slot("grid", {
|
|
105
|
+
base: {
|
|
106
|
+
display: "grid",
|
|
107
|
+
gridTemplateColumns: `repeat(auto-fill, minmax(${minMonthWidth}px, 1fr))`,
|
|
108
|
+
},
|
|
109
|
+
})}
|
|
110
|
+
>
|
|
111
|
+
{months.map((month) => {
|
|
112
|
+
const weeks = buildMonthWeeks(month, weekStartsOn);
|
|
113
|
+
const monthLabel = format(month, "MMMM yyyy", { locale });
|
|
114
|
+
const titleContent = format(month, "MMMM", { locale });
|
|
115
|
+
return (
|
|
116
|
+
<div key={month.toISOString()} {...slot("month", { base: { padding: 8 } })}>
|
|
117
|
+
{onPressMonth ? (
|
|
118
|
+
<button
|
|
119
|
+
type="button"
|
|
120
|
+
onClick={() => onPressMonth(month)}
|
|
121
|
+
aria-label={monthLabel}
|
|
122
|
+
{...slot("monthTitle", {
|
|
123
|
+
base: {
|
|
124
|
+
display: "block",
|
|
125
|
+
border: "none",
|
|
126
|
+
background: "transparent",
|
|
127
|
+
padding: "0 0 4px",
|
|
128
|
+
cursor: "pointer",
|
|
129
|
+
font: "inherit",
|
|
130
|
+
textAlign: "left",
|
|
131
|
+
},
|
|
132
|
+
themed: {
|
|
133
|
+
fontSize: 13,
|
|
134
|
+
fontWeight: 700,
|
|
135
|
+
color: theme.todayBackground,
|
|
136
|
+
},
|
|
137
|
+
})}
|
|
138
|
+
>
|
|
139
|
+
{titleContent}
|
|
140
|
+
</button>
|
|
141
|
+
) : (
|
|
142
|
+
<div
|
|
143
|
+
role="heading"
|
|
144
|
+
aria-level={3}
|
|
145
|
+
aria-label={monthLabel}
|
|
146
|
+
{...slot("monthTitle", {
|
|
147
|
+
base: { paddingBottom: 4 },
|
|
148
|
+
themed: { fontSize: 13, fontWeight: 700, color: theme.todayBackground },
|
|
149
|
+
})}
|
|
150
|
+
>
|
|
151
|
+
{titleContent}
|
|
152
|
+
</div>
|
|
153
|
+
)}
|
|
154
|
+
<div
|
|
155
|
+
{...slot("weekdays", {
|
|
156
|
+
base: { display: "grid", gridTemplateColumns: "repeat(7, 1fr)" },
|
|
157
|
+
})}
|
|
158
|
+
>
|
|
159
|
+
{weekdayLabels.map((label, i) => (
|
|
160
|
+
<div
|
|
161
|
+
key={i}
|
|
162
|
+
aria-hidden
|
|
163
|
+
{...slot("weekday", {
|
|
164
|
+
base: { textAlign: "center" },
|
|
165
|
+
themed: { fontSize: 9, fontWeight: 600, color: theme.textMuted },
|
|
166
|
+
})}
|
|
167
|
+
>
|
|
168
|
+
{label}
|
|
169
|
+
</div>
|
|
170
|
+
))}
|
|
171
|
+
</div>
|
|
172
|
+
<div role="grid" aria-label={monthLabel}>
|
|
173
|
+
{weeks.map((week) => (
|
|
174
|
+
<div
|
|
175
|
+
key={week[0].toISOString()}
|
|
176
|
+
role="row"
|
|
177
|
+
{...slot("week", {
|
|
178
|
+
base: { display: "grid", gridTemplateColumns: "repeat(7, 1fr)" },
|
|
179
|
+
})}
|
|
180
|
+
>
|
|
181
|
+
{week.map((day) => {
|
|
182
|
+
// Adjacent-month days keep the grid shape but stay blank,
|
|
183
|
+
// like the mini months of other year views.
|
|
184
|
+
if (day.getMonth() !== month.getMonth()) {
|
|
185
|
+
return (
|
|
186
|
+
<div
|
|
187
|
+
key={day.toISOString()}
|
|
188
|
+
role="gridcell"
|
|
189
|
+
aria-hidden
|
|
190
|
+
style={{ aspectRatio: "1" }}
|
|
191
|
+
/>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const isHighlighted = activeDate
|
|
195
|
+
? isSameCalendarDay(day, activeDate)
|
|
196
|
+
: getIsToday(day);
|
|
197
|
+
const hasEvents = eventDays.has(startOfDay(day).toISOString());
|
|
198
|
+
const label = `${format(day, "EEEE, d LLLL yyyy", { locale })}${
|
|
199
|
+
getIsToday(day) ? ", today" : ""
|
|
200
|
+
}${hasEvents ? ", has events" : ""}`;
|
|
201
|
+
const daySlot = slot("day", {
|
|
202
|
+
base: {
|
|
203
|
+
position: "relative",
|
|
204
|
+
aspectRatio: "1",
|
|
205
|
+
display: "flex",
|
|
206
|
+
alignItems: "center",
|
|
207
|
+
justifyContent: "center",
|
|
208
|
+
padding: 0,
|
|
209
|
+
border: "none",
|
|
210
|
+
background: "transparent",
|
|
211
|
+
font: "inherit",
|
|
212
|
+
cursor: onPressDay ? "pointer" : "default",
|
|
213
|
+
boxSizing: "border-box",
|
|
214
|
+
},
|
|
215
|
+
themed: {
|
|
216
|
+
fontSize: 11,
|
|
217
|
+
borderRadius: "50%",
|
|
218
|
+
...(isHighlighted
|
|
219
|
+
? { background: theme.todayBackground, color: theme.todayText }
|
|
220
|
+
: { color: theme.text }),
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
const states = dataState({
|
|
224
|
+
// `data-today` states the fact; the activeDate highlight is
|
|
225
|
+
// visual only (themed), so the two can differ.
|
|
226
|
+
"data-today": getIsToday(day),
|
|
227
|
+
"data-events": hasEvents,
|
|
228
|
+
});
|
|
229
|
+
const dot = hasEvents ? (
|
|
230
|
+
<span
|
|
231
|
+
aria-hidden
|
|
232
|
+
{...slot("eventDot", {
|
|
233
|
+
base: {
|
|
234
|
+
position: "absolute",
|
|
235
|
+
bottom: 1,
|
|
236
|
+
left: "50%",
|
|
237
|
+
marginLeft: -1.5,
|
|
238
|
+
width: 3,
|
|
239
|
+
height: 3,
|
|
240
|
+
borderRadius: 2,
|
|
241
|
+
},
|
|
242
|
+
themed: {
|
|
243
|
+
background: isHighlighted ? theme.todayText : theme.todayBackground,
|
|
244
|
+
},
|
|
245
|
+
})}
|
|
246
|
+
/>
|
|
247
|
+
) : null;
|
|
248
|
+
return onPressDay ? (
|
|
249
|
+
<button
|
|
250
|
+
key={day.toISOString()}
|
|
251
|
+
type="button"
|
|
252
|
+
role="gridcell"
|
|
253
|
+
onClick={() => onPressDay(day)}
|
|
254
|
+
aria-label={label}
|
|
255
|
+
{...states}
|
|
256
|
+
{...daySlot}
|
|
257
|
+
>
|
|
258
|
+
{day.getDate()}
|
|
259
|
+
{dot}
|
|
260
|
+
</button>
|
|
261
|
+
) : (
|
|
262
|
+
<div
|
|
263
|
+
key={day.toISOString()}
|
|
264
|
+
role="gridcell"
|
|
265
|
+
aria-label={label}
|
|
266
|
+
{...states}
|
|
267
|
+
{...daySlot}
|
|
268
|
+
>
|
|
269
|
+
{day.getDate()}
|
|
270
|
+
{dot}
|
|
271
|
+
</div>
|
|
272
|
+
);
|
|
273
|
+
})}
|
|
274
|
+
</div>
|
|
275
|
+
))}
|
|
276
|
+
</div>
|
|
277
|
+
</div>
|
|
278
|
+
);
|
|
279
|
+
})}
|
|
280
|
+
</div>
|
|
281
|
+
</div>
|
|
282
|
+
);
|
|
283
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -42,6 +42,7 @@ export {
|
|
|
42
42
|
type MonthViewSlot,
|
|
43
43
|
} from "./MonthView";
|
|
44
44
|
export { MonthList, type MonthListProps, type MonthListSlot } from "./MonthList";
|
|
45
|
+
export { YearView, type YearViewProps, type YearViewSlot } from "./YearView";
|
|
45
46
|
export {
|
|
46
47
|
type Resource,
|
|
47
48
|
type ResourceEventArgs,
|