@super-calendar/dom 2.5.0 → 2.7.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.
@@ -0,0 +1,293 @@
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
+ filterHiddenDays,
9
+ getWeekDays,
10
+ getYearMonths,
11
+ groupEventsByDay,
12
+ isBackgroundEvent,
13
+ isSameCalendarDay,
14
+ type WeekStartsOn,
15
+ weekdayFormatToken,
16
+ } from "@super-calendar/core";
17
+ import { createSlots, dataState, type SlotStyleProps } from "./slots";
18
+ import { type DomCalendarTheme, mergeDomTheme } from "./theme";
19
+
20
+ /**
21
+ * Styleable parts of {@link YearView}. Pass a class or inline style per slot
22
+ * via the `classNames` / `styles` props; day cells carry `data-today` and
23
+ * `data-events` state attributes for variants.
24
+ */
25
+ export type YearViewSlot =
26
+ | "grid"
27
+ | "month"
28
+ | "monthTitle"
29
+ | "weekdays"
30
+ | "weekday"
31
+ | "week"
32
+ | "day"
33
+ | "eventDot";
34
+
35
+ /** Props for {@link YearView}, the twelve mini-month year grid. */
36
+ export interface YearViewProps<T = unknown> extends SlotStyleProps<YearViewSlot> {
37
+ /** Any date inside the year to render. */
38
+ date: Date;
39
+ /** Days holding at least one event get a dot. Omit for a plain year. */
40
+ events?: CalendarEvent<T>[];
41
+ /** First day of the week. Sunday = 0 (default) … Saturday = 6. */
42
+ weekStartsOn?: WeekStartsOn;
43
+ /** Weekdays (0=Sunday…6=Saturday) hidden from the grid, e.g. `[0, 6]` for weekends off. */
44
+ hiddenDays?: number[];
45
+ locale?: Locale;
46
+ /** Highlight this date instead of the real "today". */
47
+ activeDate?: Date;
48
+ /**
49
+ * Smallest width a mini month may take before the grid drops a column
50
+ * (default 150). The grid fits as many columns as the container allows.
51
+ */
52
+ minMonthWidth?: number;
53
+ /** Tap a day cell (e.g. to drill into the day or month view). */
54
+ onPressDay?: (date: Date) => void;
55
+ /** Tap a month's title (e.g. to jump to that month's view). */
56
+ onPressMonth?: (month: Date) => void;
57
+ /** Theme overrides; falls back to the default light theme. */
58
+ theme?: Partial<DomCalendarTheme>;
59
+ /** Class applied to the root element. */
60
+ className?: string;
61
+ /** Inline styles applied to the root element. */
62
+ style?: CSSProperties;
63
+ }
64
+
65
+ /**
66
+ * A year at a glance: the twelve months as compact mini grids, with today
67
+ * highlighted and a dot under days that hold events. It's the view `Calendar`
68
+ * renders for `mode="year"`.
69
+ */
70
+ export function YearView<T = unknown>({
71
+ date,
72
+ events,
73
+ weekStartsOn = 0,
74
+ hiddenDays,
75
+ locale,
76
+ activeDate,
77
+ minMonthWidth = 150,
78
+ onPressDay,
79
+ onPressMonth,
80
+ theme: themeOverrides,
81
+ className,
82
+ style,
83
+ classNames,
84
+ styles,
85
+ }: YearViewProps<T>): ReactElement {
86
+ const theme = useMemo(() => mergeDomTheme(themeOverrides), [themeOverrides]);
87
+ const slot = createSlots<YearViewSlot>({ classNames, styles });
88
+
89
+ const months = useMemo(() => getYearMonths(date), [date]);
90
+ const weekdayLabels = useMemo(
91
+ () =>
92
+ filterHiddenDays(getWeekDays(date, weekStartsOn), hiddenDays).map((d) =>
93
+ format(d, weekdayFormatToken("narrow"), { locale }),
94
+ ),
95
+ [date, weekStartsOn, locale, hiddenDays],
96
+ );
97
+ // Days that hold at least one event, keyed like `groupEventsByDay`.
98
+ const eventDays = useMemo(
99
+ () =>
100
+ new Set(
101
+ events && events.length > 0
102
+ ? groupEventsByDay(events.filter((e) => !isBackgroundEvent(e))).keys()
103
+ : [],
104
+ ),
105
+ [events],
106
+ );
107
+
108
+ return (
109
+ <div
110
+ className={className}
111
+ style={{ fontFamily: theme.fontFamily, color: theme.text, overflowY: "auto", ...style }}
112
+ >
113
+ <div
114
+ {...slot("grid", {
115
+ base: {
116
+ display: "grid",
117
+ gridTemplateColumns: `repeat(auto-fill, minmax(${minMonthWidth}px, 1fr))`,
118
+ },
119
+ })}
120
+ >
121
+ {months.map((month) => {
122
+ const weeks = buildMonthWeeks(month, weekStartsOn, { hiddenDays });
123
+ const monthLabel = format(month, "MMMM yyyy", { locale });
124
+ const titleContent = format(month, "MMMM", { locale });
125
+ return (
126
+ <div key={month.toISOString()} {...slot("month", { base: { padding: 8 } })}>
127
+ {onPressMonth ? (
128
+ <button
129
+ type="button"
130
+ onClick={() => onPressMonth(month)}
131
+ aria-label={monthLabel}
132
+ {...slot("monthTitle", {
133
+ base: {
134
+ display: "block",
135
+ border: "none",
136
+ background: "transparent",
137
+ padding: "0 0 4px",
138
+ cursor: "pointer",
139
+ font: "inherit",
140
+ textAlign: "left",
141
+ },
142
+ themed: {
143
+ fontSize: 13,
144
+ fontWeight: 700,
145
+ color: theme.todayBackground,
146
+ },
147
+ })}
148
+ >
149
+ {titleContent}
150
+ </button>
151
+ ) : (
152
+ <div
153
+ role="heading"
154
+ aria-level={3}
155
+ aria-label={monthLabel}
156
+ {...slot("monthTitle", {
157
+ base: { paddingBottom: 4 },
158
+ themed: { fontSize: 13, fontWeight: 700, color: theme.todayBackground },
159
+ })}
160
+ >
161
+ {titleContent}
162
+ </div>
163
+ )}
164
+ <div
165
+ {...slot("weekdays", {
166
+ base: { display: "grid", gridTemplateColumns: "repeat(7, 1fr)" },
167
+ })}
168
+ >
169
+ {weekdayLabels.map((label, i) => (
170
+ <div
171
+ key={i}
172
+ aria-hidden
173
+ {...slot("weekday", {
174
+ base: { textAlign: "center" },
175
+ themed: { fontSize: 9, fontWeight: 600, color: theme.textMuted },
176
+ })}
177
+ >
178
+ {label}
179
+ </div>
180
+ ))}
181
+ </div>
182
+ <div role="grid" aria-label={monthLabel}>
183
+ {weeks.map((week) => (
184
+ <div
185
+ key={week[0].toISOString()}
186
+ role="row"
187
+ {...slot("week", {
188
+ base: { display: "grid", gridTemplateColumns: "repeat(7, 1fr)" },
189
+ })}
190
+ >
191
+ {week.map((day) => {
192
+ // Adjacent-month days keep the grid shape but stay blank,
193
+ // like the mini months of other year views.
194
+ if (day.getMonth() !== month.getMonth()) {
195
+ return (
196
+ <div
197
+ key={day.toISOString()}
198
+ role="gridcell"
199
+ aria-hidden
200
+ style={{ aspectRatio: "1" }}
201
+ />
202
+ );
203
+ }
204
+ const isHighlighted = activeDate
205
+ ? isSameCalendarDay(day, activeDate)
206
+ : getIsToday(day);
207
+ const hasEvents = eventDays.has(startOfDay(day).toISOString());
208
+ const label = `${format(day, "EEEE, d LLLL yyyy", { locale })}${
209
+ getIsToday(day) ? ", today" : ""
210
+ }${hasEvents ? ", has events" : ""}`;
211
+ const daySlot = slot("day", {
212
+ base: {
213
+ position: "relative",
214
+ aspectRatio: "1",
215
+ display: "flex",
216
+ alignItems: "center",
217
+ justifyContent: "center",
218
+ padding: 0,
219
+ border: "none",
220
+ background: "transparent",
221
+ font: "inherit",
222
+ cursor: onPressDay ? "pointer" : "default",
223
+ boxSizing: "border-box",
224
+ },
225
+ themed: {
226
+ fontSize: 11,
227
+ borderRadius: "50%",
228
+ ...(isHighlighted
229
+ ? { background: theme.todayBackground, color: theme.todayText }
230
+ : { color: theme.text }),
231
+ },
232
+ });
233
+ const states = dataState({
234
+ // `data-today` states the fact; the activeDate highlight is
235
+ // visual only (themed), so the two can differ.
236
+ "data-today": getIsToday(day),
237
+ "data-events": hasEvents,
238
+ });
239
+ const dot = hasEvents ? (
240
+ <span
241
+ aria-hidden
242
+ {...slot("eventDot", {
243
+ base: {
244
+ position: "absolute",
245
+ bottom: 1,
246
+ left: "50%",
247
+ marginLeft: -1.5,
248
+ width: 3,
249
+ height: 3,
250
+ borderRadius: 2,
251
+ },
252
+ themed: {
253
+ background: isHighlighted ? theme.todayText : theme.todayBackground,
254
+ },
255
+ })}
256
+ />
257
+ ) : null;
258
+ return onPressDay ? (
259
+ <button
260
+ key={day.toISOString()}
261
+ type="button"
262
+ role="gridcell"
263
+ onClick={() => onPressDay(day)}
264
+ aria-label={label}
265
+ {...states}
266
+ {...daySlot}
267
+ >
268
+ {day.getDate()}
269
+ {dot}
270
+ </button>
271
+ ) : (
272
+ <div
273
+ key={day.toISOString()}
274
+ role="gridcell"
275
+ aria-label={label}
276
+ {...states}
277
+ {...daySlot}
278
+ >
279
+ {day.getDate()}
280
+ {dot}
281
+ </div>
282
+ );
283
+ })}
284
+ </div>
285
+ ))}
286
+ </div>
287
+ </div>
288
+ );
289
+ })}
290
+ </div>
291
+ </div>
292
+ );
293
+ }
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,
@@ -60,6 +61,7 @@ export { type ResolvedSlot, type SlotDefault, type SlotStyleProps } from "./slot
60
61
  export { type DomCalendarTheme, darkDomTheme, defaultDomTheme, mergeDomTheme } from "./theme";
61
62
  export {
62
63
  type BusinessHours,
64
+ type BusinessHoursBand,
63
65
  type CalendarEvent,
64
66
  type CalendarMode,
65
67
  type DateRange,
@@ -80,7 +82,9 @@ export {
80
82
  type RecurrenceFrequency,
81
83
  expandRecurringEvents,
82
84
  getViewDays,
85
+ backgroundBandsForDay,
83
86
  isAllDayEvent,
87
+ isBackgroundEvent,
84
88
  layoutDayEvents,
85
89
  daySelectionState,
86
90
  isDateSelectable,
@@ -88,6 +92,8 @@ export {
88
92
  isWithinDateRange,
89
93
  nextDateRange,
90
94
  useDateRange,
95
+ useEventSource,
96
+ useNow,
91
97
  useMonthGrid,
92
98
  buildMonthGrid,
93
99
  weekdayFormatToken,