@super-calendar/dom 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 +48 -0
- package/dist/index.d.mts +47 -26
- package/dist/index.d.ts +47 -26
- package/dist/index.js +1264 -452
- package/dist/index.mjs +1265 -453
- package/package.json +2 -2
- package/src/Calendar.tsx +13 -0
- package/src/DateRangePicker.tsx +60 -6
- package/src/EventActions.tsx +470 -0
- package/src/InertDiv.ts +7 -0
- package/src/MonthList.tsx +64 -38
- package/src/MonthView.tsx +14 -2
- package/src/ResourceTimeline.tsx +438 -98
- package/src/TimeGrid.tsx +305 -35
- 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.16.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.16.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@legendapp/list": ">=3",
|
package/src/Calendar.tsx
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
startOfYear,
|
|
14
14
|
} from "date-fns";
|
|
15
15
|
import {
|
|
16
|
+
type ComponentType,
|
|
16
17
|
type CSSProperties,
|
|
17
18
|
type ForwardedRef,
|
|
18
19
|
forwardRef,
|
|
@@ -29,6 +30,7 @@ import {
|
|
|
29
30
|
type BusinessHours,
|
|
30
31
|
type BusinessHoursBand,
|
|
31
32
|
type CalendarEvent,
|
|
33
|
+
type CreateGhostArgs,
|
|
32
34
|
type DateRange,
|
|
33
35
|
type DateSelectionConstraints,
|
|
34
36
|
type EventAccessibilityLabeler,
|
|
@@ -191,6 +193,11 @@ export interface CalendarProps<T = unknown>
|
|
|
191
193
|
* dragged (`start` at midnight of the first, `end` at midnight after the last).
|
|
192
194
|
*/
|
|
193
195
|
onCreateEvent?: (start: Date, end: Date) => void;
|
|
196
|
+
/**
|
|
197
|
+
* A component that draws content inside the time-grid drag-to-create preview;
|
|
198
|
+
* see `TimeGrid`'s prop of the same name. Applies to the week/day grid only.
|
|
199
|
+
*/
|
|
200
|
+
renderCreateGhost?: ComponentType<CreateGhostArgs>;
|
|
194
201
|
/** Fires when an event drag begins. */
|
|
195
202
|
onDragStart?: (event: CalendarEvent<T>) => void;
|
|
196
203
|
/**
|
|
@@ -199,6 +206,8 @@ export interface CalendarProps<T = unknown>
|
|
|
199
206
|
* days dragged so the time of day and duration are kept.
|
|
200
207
|
*/
|
|
201
208
|
onDragEvent?: (event: CalendarEvent<T>, start: Date, end: Date) => void | boolean;
|
|
209
|
+
/** Stable, unique event identity for the web editing dialog across data refreshes. */
|
|
210
|
+
getEventId?: (event: CalendarEvent<T>) => string | number;
|
|
202
211
|
/** Tap a day's column header. */
|
|
203
212
|
onPressDateHeader?: (day: Date) => void;
|
|
204
213
|
/** Custom time-grid event renderer. */
|
|
@@ -373,8 +382,10 @@ function CalendarInner<T = unknown>(
|
|
|
373
382
|
touchDragThreshold,
|
|
374
383
|
onPressCell,
|
|
375
384
|
onCreateEvent,
|
|
385
|
+
renderCreateGhost,
|
|
376
386
|
onDragStart,
|
|
377
387
|
onDragEvent,
|
|
388
|
+
getEventId,
|
|
378
389
|
onPressDateHeader,
|
|
379
390
|
renderTimeEvent,
|
|
380
391
|
hourComponent,
|
|
@@ -582,8 +593,10 @@ function CalendarInner<T = unknown>(
|
|
|
582
593
|
onPressEvent={onPressEvent}
|
|
583
594
|
onPressCell={onPressCell}
|
|
584
595
|
onCreateEvent={onCreateEvent}
|
|
596
|
+
renderCreateGhost={renderCreateGhost}
|
|
585
597
|
onDragStart={onDragStart}
|
|
586
598
|
onDragEvent={onDragEvent}
|
|
599
|
+
getEventId={getEventId}
|
|
587
600
|
onChangeDate={onChangeDate}
|
|
588
601
|
onPressDateHeader={onPressDateHeader}
|
|
589
602
|
renderEvent={renderTimeEvent}
|
package/src/DateRangePicker.tsx
CHANGED
|
@@ -29,7 +29,7 @@ interface PickerBaseProps
|
|
|
29
29
|
height?: number;
|
|
30
30
|
/** Placeholder shown on the trigger when nothing is selected. */
|
|
31
31
|
placeholder?: string;
|
|
32
|
-
/** Disable the
|
|
32
|
+
/** Disable the field and close its popover. Re-enabling leaves the popover closed. */
|
|
33
33
|
disabled?: boolean;
|
|
34
34
|
/** Theme overrides for the calendar. */
|
|
35
35
|
theme?: Partial<DomCalendarTheme>;
|
|
@@ -61,18 +61,34 @@ const popoverDefault: CSSProperties = {
|
|
|
61
61
|
minWidth: 300,
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
+
// Where keyboard focus lands when the popover opens: the picked (or today's)
|
|
65
|
+
// day, else that month's roving tab stop, else an enabled day in that month. Unavailable days
|
|
66
|
+
// stay focusable for arrow navigation but are skipped as a starting point.
|
|
67
|
+
function initialPickerFocus(dialog: HTMLElement, day: string): HTMLElement | null {
|
|
68
|
+
const usable = ':not([aria-disabled="true"]):not([data-outside])';
|
|
69
|
+
return (
|
|
70
|
+
dialog.querySelector<HTMLElement>(`[data-day="${day}"]${usable}`) ??
|
|
71
|
+
dialog.querySelector<HTMLElement>(`[data-day^="${day.slice(0, 7)}"][tabindex="0"]${usable}`) ??
|
|
72
|
+
dialog.querySelector<HTMLElement>(`[data-day^="${day.slice(0, 7)}"]${usable}`)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
64
76
|
/**
|
|
65
77
|
* The shared popover shell: a trigger button and a calendar panel that closes on
|
|
66
78
|
* outside-click and Escape, and returns focus to the trigger. `renderCalendar`
|
|
67
79
|
* gets a close callback so a single-date pick can dismiss immediately.
|
|
68
80
|
*/
|
|
69
|
-
function usePopover() {
|
|
81
|
+
function usePopover(disabled: boolean | undefined) {
|
|
70
82
|
const [open, setOpen] = useState(false);
|
|
71
83
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
72
84
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
73
85
|
|
|
74
86
|
useEffect(() => {
|
|
75
87
|
if (!open) return;
|
|
88
|
+
if (disabled) {
|
|
89
|
+
setOpen(false);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
76
92
|
const onDown = (e: MouseEvent) => {
|
|
77
93
|
if (!rootRef.current?.contains(e.target as Node)) setOpen(false);
|
|
78
94
|
};
|
|
@@ -88,9 +104,9 @@ function usePopover() {
|
|
|
88
104
|
document.removeEventListener("mousedown", onDown);
|
|
89
105
|
document.removeEventListener("keydown", onKey);
|
|
90
106
|
};
|
|
91
|
-
}, [open]);
|
|
107
|
+
}, [open, disabled]);
|
|
92
108
|
|
|
93
|
-
return { open, setOpen, rootRef, triggerRef };
|
|
109
|
+
return { open: open && !disabled, setOpen, rootRef, triggerRef };
|
|
94
110
|
}
|
|
95
111
|
|
|
96
112
|
interface ShellProps {
|
|
@@ -107,6 +123,8 @@ interface ShellProps {
|
|
|
107
123
|
className?: string;
|
|
108
124
|
style?: CSSProperties;
|
|
109
125
|
ariaLabel: string;
|
|
126
|
+
/** The day (`yyyy-MM-dd`) to focus when the popover opens. */
|
|
127
|
+
focusDay: string;
|
|
110
128
|
children: ReactNode;
|
|
111
129
|
}
|
|
112
130
|
|
|
@@ -124,9 +142,39 @@ function PickerShell({
|
|
|
124
142
|
className,
|
|
125
143
|
style,
|
|
126
144
|
ariaLabel,
|
|
145
|
+
focusDay,
|
|
127
146
|
children,
|
|
128
147
|
}: ShellProps): ReactElement {
|
|
129
148
|
const dialogId = useId();
|
|
149
|
+
const dialogRef = useRef<HTMLDivElement>(null);
|
|
150
|
+
const focusDayRef = useRef(focusDay);
|
|
151
|
+
focusDayRef.current = focusDay;
|
|
152
|
+
// Move focus into the popover once per opening (not on every re-render), so
|
|
153
|
+
// keyboard users land on a date. Wait for the intended month to mount instead
|
|
154
|
+
// of focusing a neighbouring virtualized row that happens to render first.
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (!open) return;
|
|
157
|
+
const dialog = dialogRef.current;
|
|
158
|
+
if (!dialog) return;
|
|
159
|
+
const place = () => {
|
|
160
|
+
if (dialog.contains(document.activeElement)) {
|
|
161
|
+
observer.disconnect();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const target = initialPickerFocus(dialog, focusDayRef.current);
|
|
165
|
+
const monthMounted = dialog.querySelector(
|
|
166
|
+
`[data-day^="${focusDayRef.current.slice(0, 7)}"]:not([data-outside])`,
|
|
167
|
+
);
|
|
168
|
+
if (target || monthMounted) {
|
|
169
|
+
(target ?? dialog).focus();
|
|
170
|
+
observer.disconnect();
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const observer = new MutationObserver(place);
|
|
174
|
+
observer.observe(dialog, { childList: true, subtree: true });
|
|
175
|
+
place();
|
|
176
|
+
return () => observer.disconnect();
|
|
177
|
+
}, [open]);
|
|
130
178
|
return (
|
|
131
179
|
<div
|
|
132
180
|
ref={rootRef}
|
|
@@ -161,8 +209,10 @@ function PickerShell({
|
|
|
161
209
|
</button>
|
|
162
210
|
{open ? (
|
|
163
211
|
<div
|
|
212
|
+
ref={dialogRef}
|
|
164
213
|
id={dialogId}
|
|
165
214
|
role="dialog"
|
|
215
|
+
tabIndex={-1}
|
|
166
216
|
aria-label={ariaLabel}
|
|
167
217
|
{...slot("popover", {
|
|
168
218
|
base: popoverDefault,
|
|
@@ -216,7 +266,7 @@ export function DatePicker({
|
|
|
216
266
|
}: DatePickerProps): ReactElement {
|
|
217
267
|
const theme = mergeDomTheme(themeOverrides);
|
|
218
268
|
const slot = createSlots<DatePickerSlot | MonthListSlot>({ classNames, styles });
|
|
219
|
-
const { open, setOpen, rootRef, triggerRef } = usePopover();
|
|
269
|
+
const { open, setOpen, rootRef, triggerRef } = usePopover(disabled);
|
|
220
270
|
|
|
221
271
|
return (
|
|
222
272
|
<PickerShell
|
|
@@ -233,8 +283,10 @@ export function DatePicker({
|
|
|
233
283
|
className={className}
|
|
234
284
|
style={style}
|
|
235
285
|
ariaLabel="Choose a date"
|
|
286
|
+
focusDay={format(value ?? new Date(), "yyyy-MM-dd")}
|
|
236
287
|
>
|
|
237
288
|
<MonthList
|
|
289
|
+
theme={themeOverrides}
|
|
238
290
|
date={value ?? new Date()}
|
|
239
291
|
weekStartsOn={weekStartsOn}
|
|
240
292
|
selectedDates={value ? [value] : []}
|
|
@@ -299,7 +351,7 @@ export function DateRangePicker({
|
|
|
299
351
|
}: DateRangePickerProps): ReactElement {
|
|
300
352
|
const theme = mergeDomTheme(themeOverrides);
|
|
301
353
|
const slot = createSlots<DatePickerSlot | MonthListSlot>({ classNames, styles });
|
|
302
|
-
const { open, setOpen, rootRef, triggerRef } = usePopover();
|
|
354
|
+
const { open, setOpen, rootRef, triggerRef } = usePopover(disabled);
|
|
303
355
|
const constraints: DateSelectionConstraints = { minDate, maxDate, isDateDisabled };
|
|
304
356
|
|
|
305
357
|
return (
|
|
@@ -317,8 +369,10 @@ export function DateRangePicker({
|
|
|
317
369
|
className={className}
|
|
318
370
|
style={style}
|
|
319
371
|
ariaLabel="Choose a date range"
|
|
372
|
+
focusDay={format(value?.start ?? new Date(), "yyyy-MM-dd")}
|
|
320
373
|
>
|
|
321
374
|
<MonthList
|
|
375
|
+
theme={themeOverrides}
|
|
322
376
|
date={value?.start ?? new Date()}
|
|
323
377
|
weekStartsOn={weekStartsOn}
|
|
324
378
|
selectedRange={value ?? undefined}
|
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import { type ReactElement, type RefObject, useEffect, useId, useRef, useState } from "react";
|
|
2
|
+
import { format } from "date-fns";
|
|
3
|
+
import type { ResolvedSlot, SlotDefault } from "./slots";
|
|
4
|
+
import type { DomCalendarTheme } from "./theme";
|
|
5
|
+
|
|
6
|
+
/** Styleable parts of the built-in move and resize actions. */
|
|
7
|
+
export type EventActionsSlot =
|
|
8
|
+
| "eventActionsTrigger"
|
|
9
|
+
| "eventActionsList"
|
|
10
|
+
| "eventActions"
|
|
11
|
+
| "eventAction";
|
|
12
|
+
|
|
13
|
+
/** One move or resize the actions dialog offers. */
|
|
14
|
+
export interface EventEditAction {
|
|
15
|
+
id: string;
|
|
16
|
+
label: string;
|
|
17
|
+
/** False when the change is refused before reaching `onDragEvent` (e.g. below one step). */
|
|
18
|
+
available: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function moveFocus(event: { key: string; preventDefault(): void }, container: HTMLElement | null) {
|
|
22
|
+
const buttons = [...(container?.querySelectorAll<HTMLElement>("button") ?? [])];
|
|
23
|
+
const index = buttons.indexOf(document.activeElement as HTMLElement);
|
|
24
|
+
const target =
|
|
25
|
+
event.key === "ArrowDown"
|
|
26
|
+
? buttons[(index + 1) % buttons.length]
|
|
27
|
+
: event.key === "ArrowUp"
|
|
28
|
+
? buttons[index < 0 ? buttons.length - 1 : (index - 1 + buttons.length) % buttons.length]
|
|
29
|
+
: event.key === "Home"
|
|
30
|
+
? buttons[0]
|
|
31
|
+
: event.key === "End"
|
|
32
|
+
? buttons[buttons.length - 1]
|
|
33
|
+
: undefined;
|
|
34
|
+
if (!target) return;
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
target.focus();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type SlotFn = (name: EventActionsSlot, defaults?: SlotDefault) => ResolvedSlot;
|
|
40
|
+
|
|
41
|
+
export function describeEventDates(event: { start: Date; end: Date }): string {
|
|
42
|
+
const start = format(event.start, "yyyy-MM-dd");
|
|
43
|
+
const end = format(event.end, "yyyy-MM-dd");
|
|
44
|
+
return start === end ? start : `${start} - ${end}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Open/close state for one component's actions dialog. `commit` applies an
|
|
49
|
+
* action through the component's drag path and returns a matcher for the event
|
|
50
|
+
* the consumer will render in its place, or null when the change was refused.
|
|
51
|
+
* The dialog follows that replacement so repeated steps keep working, and
|
|
52
|
+
* closes when its event disappears or can no longer be edited.
|
|
53
|
+
*/
|
|
54
|
+
export function useEventActions<E extends object>(
|
|
55
|
+
events: readonly E[],
|
|
56
|
+
actionsFor: (event: E) => EventEditAction[],
|
|
57
|
+
commit: (event: E, actionId: string) => ((candidate: E) => boolean) | null,
|
|
58
|
+
getEventId?: (event: E) => string | number,
|
|
59
|
+
) {
|
|
60
|
+
const [openEvent, setOpenEvent] = useState<E | null>(null);
|
|
61
|
+
const [rejections, setRejections] = useState(0);
|
|
62
|
+
const identity = useRef<{ id: string | number } | null>(null);
|
|
63
|
+
// Read by handlers that can run before the next render (see `current`).
|
|
64
|
+
const openRef = useRef<E | null>(null);
|
|
65
|
+
const show = (event: E | null) => {
|
|
66
|
+
openRef.current = event;
|
|
67
|
+
setOpenEvent(event);
|
|
68
|
+
};
|
|
69
|
+
const triggers = useRef(new WeakMap<E, HTMLElement>());
|
|
70
|
+
const dialogRef = useRef<HTMLDialogElement | null>(null);
|
|
71
|
+
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
72
|
+
const pending = useRef<{
|
|
73
|
+
from: E;
|
|
74
|
+
events: readonly E[];
|
|
75
|
+
matches: (candidate: E) => boolean;
|
|
76
|
+
} | null>(null);
|
|
77
|
+
|
|
78
|
+
// The open event as the consumer renders it now: itself, or the event that
|
|
79
|
+
// replaced it after an accepted change. Handlers can run after that update
|
|
80
|
+
// renders but before the dialog follows it, so they resolve it here too.
|
|
81
|
+
const current = (): E | undefined => {
|
|
82
|
+
const event = openRef.current;
|
|
83
|
+
if (!event) return undefined;
|
|
84
|
+
if (identity.current) {
|
|
85
|
+
if (!getEventId) return undefined;
|
|
86
|
+
const id = identity.current.id;
|
|
87
|
+
const matches = events.filter((candidate) => getEventId(candidate) === id);
|
|
88
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
89
|
+
}
|
|
90
|
+
if (getEventId) return undefined;
|
|
91
|
+
if (events.includes(event)) return event;
|
|
92
|
+
const committed = pending.current;
|
|
93
|
+
if (committed?.from !== event) return undefined;
|
|
94
|
+
// Without stable IDs, replacing other event objects makes identity ambiguous.
|
|
95
|
+
if (committed.events.some((previous) => previous !== event && !events.includes(previous)))
|
|
96
|
+
return undefined;
|
|
97
|
+
// A matching event that already existed is not the consumer's replacement.
|
|
98
|
+
const replacements = events.filter(
|
|
99
|
+
(candidate) => !committed.events.includes(candidate) && committed.matches(candidate),
|
|
100
|
+
);
|
|
101
|
+
return replacements.length === 1 ? replacements[0] : undefined;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const close = (restoreFocus: boolean, fallbackToRoot = false) => {
|
|
105
|
+
const event = openRef.current;
|
|
106
|
+
if (!event) return;
|
|
107
|
+
const shown = current() ?? event;
|
|
108
|
+
pending.current = null;
|
|
109
|
+
const dialog = dialogRef.current;
|
|
110
|
+
// Close before moving focus: the page behind a modal dialog is inert.
|
|
111
|
+
if (dialog?.open) {
|
|
112
|
+
if (typeof dialog.close === "function") dialog.close();
|
|
113
|
+
else dialog.removeAttribute("open");
|
|
114
|
+
}
|
|
115
|
+
if (restoreFocus) {
|
|
116
|
+
// Back to the button that opened the dialog, or, when its event is gone
|
|
117
|
+
// (moved off the page, removed), to the first control in the calendar.
|
|
118
|
+
const trigger = triggers.current.get(shown);
|
|
119
|
+
(fallbackToRoot
|
|
120
|
+
? rootRef.current
|
|
121
|
+
: trigger?.isConnected
|
|
122
|
+
? trigger
|
|
123
|
+
: ([
|
|
124
|
+
...(rootRef.current?.querySelectorAll<HTMLElement>(
|
|
125
|
+
'button:not(:disabled), [tabindex="0"]',
|
|
126
|
+
) ?? []),
|
|
127
|
+
].find((element) => !dialog?.contains(element)) ?? rootRef.current)
|
|
128
|
+
)?.focus();
|
|
129
|
+
}
|
|
130
|
+
show(null);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (!openRef.current) return;
|
|
135
|
+
const next = current();
|
|
136
|
+
// Once the events change after a commit, that change has settled.
|
|
137
|
+
if (pending.current && pending.current.events !== events) pending.current = null;
|
|
138
|
+
if (next && actionsFor(next).length > 0) {
|
|
139
|
+
if (next !== openEvent) show(next);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
close(
|
|
143
|
+
document.activeElement === document.body ||
|
|
144
|
+
!!dialogRef.current?.contains(document.activeElement),
|
|
145
|
+
true,
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const run = (actionId: string) => {
|
|
150
|
+
const event = current();
|
|
151
|
+
if (!event) return;
|
|
152
|
+
openRef.current = event;
|
|
153
|
+
pending.current = null;
|
|
154
|
+
const matches = commit(event, actionId);
|
|
155
|
+
setRejections((count) => (matches ? 0 : count + 1));
|
|
156
|
+
if (matches) pending.current = { from: event, events, matches };
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const select = (event: E, trigger: HTMLElement) => {
|
|
160
|
+
const id = getEventId?.(event);
|
|
161
|
+
identity.current = typeof id === "string" || typeof id === "number" ? { id } : null;
|
|
162
|
+
setRejections(0);
|
|
163
|
+
triggers.current.set(event, trigger);
|
|
164
|
+
show(event);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
return { openEvent, close, run, select, dialogRef, rootRef, rejections };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function EventActionsMenu<E extends object>({
|
|
171
|
+
events,
|
|
172
|
+
label,
|
|
173
|
+
labelFor,
|
|
174
|
+
descriptionFor,
|
|
175
|
+
onSelect,
|
|
176
|
+
rootRef,
|
|
177
|
+
slot,
|
|
178
|
+
theme,
|
|
179
|
+
}: {
|
|
180
|
+
events: readonly E[];
|
|
181
|
+
label: string;
|
|
182
|
+
labelFor: (event: E) => string;
|
|
183
|
+
descriptionFor: (event: E) => string;
|
|
184
|
+
onSelect: (event: E, trigger: HTMLElement) => void;
|
|
185
|
+
rootRef: RefObject<HTMLElement | null>;
|
|
186
|
+
slot: SlotFn;
|
|
187
|
+
theme: DomCalendarTheme;
|
|
188
|
+
}): ReactElement | null {
|
|
189
|
+
const [expanded, setExpanded] = useState(false);
|
|
190
|
+
const trigger = useRef<HTMLButtonElement>(null);
|
|
191
|
+
const listRef = useRef<HTMLDivElement>(null);
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
if (expanded) listRef.current?.querySelector<HTMLElement>("button")?.focus();
|
|
194
|
+
}, [expanded]);
|
|
195
|
+
const id = useId();
|
|
196
|
+
const focused = useRef<HTMLElement | null>(null);
|
|
197
|
+
const keys = useRef(new WeakMap<E, number>());
|
|
198
|
+
const nextKey = useRef(0);
|
|
199
|
+
const keyFor = (event: E) => {
|
|
200
|
+
let key = keys.current.get(event);
|
|
201
|
+
if (key === undefined) {
|
|
202
|
+
key = nextKey.current++;
|
|
203
|
+
keys.current.set(event, key);
|
|
204
|
+
}
|
|
205
|
+
return key;
|
|
206
|
+
};
|
|
207
|
+
useEffect(() => {
|
|
208
|
+
const previous = focused.current;
|
|
209
|
+
if (!previous || document.activeElement !== document.body) return;
|
|
210
|
+
(previous.isConnected ? previous : (trigger.current ?? rootRef.current))?.focus();
|
|
211
|
+
});
|
|
212
|
+
if (!events.length) return null;
|
|
213
|
+
return (
|
|
214
|
+
<div
|
|
215
|
+
onFocusCapture={(e) => {
|
|
216
|
+
focused.current = e.target as HTMLElement;
|
|
217
|
+
}}
|
|
218
|
+
onBlurCapture={(e) => {
|
|
219
|
+
if (e.relatedTarget && !e.currentTarget.contains(e.relatedTarget as Node))
|
|
220
|
+
focused.current = null;
|
|
221
|
+
}}
|
|
222
|
+
onKeyDown={(e) => {
|
|
223
|
+
if (!expanded) return;
|
|
224
|
+
if (e.key !== "Escape") {
|
|
225
|
+
moveFocus(e, listRef.current);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
e.preventDefault();
|
|
229
|
+
e.stopPropagation();
|
|
230
|
+
setExpanded(false);
|
|
231
|
+
trigger.current?.focus();
|
|
232
|
+
}}
|
|
233
|
+
>
|
|
234
|
+
<button
|
|
235
|
+
ref={trigger}
|
|
236
|
+
id={id}
|
|
237
|
+
type="button"
|
|
238
|
+
aria-expanded={expanded}
|
|
239
|
+
aria-controls={expanded ? id + "-list" : undefined}
|
|
240
|
+
onClick={() => setExpanded(!expanded)}
|
|
241
|
+
{...slot("eventActionsTrigger", {
|
|
242
|
+
base: { minHeight: 32, font: "inherit", cursor: "pointer" },
|
|
243
|
+
themed: {
|
|
244
|
+
margin: "4px 0",
|
|
245
|
+
padding: "4px 10px",
|
|
246
|
+
border: `1px solid ${theme.gridLine}`,
|
|
247
|
+
borderRadius: 6,
|
|
248
|
+
background: theme.surface,
|
|
249
|
+
color: theme.text,
|
|
250
|
+
},
|
|
251
|
+
})}
|
|
252
|
+
>
|
|
253
|
+
{label}
|
|
254
|
+
</button>
|
|
255
|
+
{expanded ? (
|
|
256
|
+
<div
|
|
257
|
+
ref={listRef}
|
|
258
|
+
id={id + "-list"}
|
|
259
|
+
role="group"
|
|
260
|
+
aria-labelledby={id}
|
|
261
|
+
{...slot("eventActionsList", {
|
|
262
|
+
base: {
|
|
263
|
+
display: "flex",
|
|
264
|
+
flexDirection: "column",
|
|
265
|
+
gap: 4,
|
|
266
|
+
maxHeight: "min(20rem, 50vh)",
|
|
267
|
+
overflowY: "auto",
|
|
268
|
+
},
|
|
269
|
+
themed: {
|
|
270
|
+
padding: 4,
|
|
271
|
+
background: theme.surface,
|
|
272
|
+
border: `1px solid ${theme.gridLine}`,
|
|
273
|
+
borderRadius: 6,
|
|
274
|
+
},
|
|
275
|
+
})}
|
|
276
|
+
>
|
|
277
|
+
{events.map((event, index) => (
|
|
278
|
+
<button
|
|
279
|
+
key={keyFor(event)}
|
|
280
|
+
type="button"
|
|
281
|
+
aria-haspopup="dialog"
|
|
282
|
+
aria-label={labelFor(event)}
|
|
283
|
+
aria-describedby={id + "-" + index}
|
|
284
|
+
onClick={() => {
|
|
285
|
+
setExpanded(false);
|
|
286
|
+
focused.current = null;
|
|
287
|
+
onSelect(event, trigger.current!);
|
|
288
|
+
}}
|
|
289
|
+
{...slot("eventAction", {
|
|
290
|
+
base: {
|
|
291
|
+
minHeight: 32,
|
|
292
|
+
flexShrink: 0,
|
|
293
|
+
overflowWrap: "anywhere",
|
|
294
|
+
font: "inherit",
|
|
295
|
+
cursor: "pointer",
|
|
296
|
+
textAlign: "left",
|
|
297
|
+
},
|
|
298
|
+
themed: {
|
|
299
|
+
padding: "6px 10px",
|
|
300
|
+
border: `1px solid ${theme.gridLine}`,
|
|
301
|
+
borderRadius: 6,
|
|
302
|
+
background: theme.surface,
|
|
303
|
+
color: theme.text,
|
|
304
|
+
},
|
|
305
|
+
})}
|
|
306
|
+
>
|
|
307
|
+
<span>{labelFor(event)}</span>
|
|
308
|
+
<small id={id + "-" + index} style={{ display: "block" }}>
|
|
309
|
+
{descriptionFor(event)}
|
|
310
|
+
</small>
|
|
311
|
+
</button>
|
|
312
|
+
))}
|
|
313
|
+
</div>
|
|
314
|
+
) : null}
|
|
315
|
+
</div>
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** The modal dialog listing an event's move and resize actions. */
|
|
320
|
+
export function EventActionsDialog({
|
|
321
|
+
dialogRef,
|
|
322
|
+
title,
|
|
323
|
+
description,
|
|
324
|
+
actions,
|
|
325
|
+
closeLabel,
|
|
326
|
+
rejectedLabel,
|
|
327
|
+
rejections,
|
|
328
|
+
onAction,
|
|
329
|
+
onClose,
|
|
330
|
+
slot,
|
|
331
|
+
theme,
|
|
332
|
+
}: {
|
|
333
|
+
dialogRef: RefObject<HTMLDialogElement | null>;
|
|
334
|
+
title: string;
|
|
335
|
+
description: string;
|
|
336
|
+
actions: EventEditAction[];
|
|
337
|
+
closeLabel: string;
|
|
338
|
+
rejectedLabel: string;
|
|
339
|
+
rejections: number;
|
|
340
|
+
onAction: (actionId: string) => void;
|
|
341
|
+
onClose: () => void;
|
|
342
|
+
slot: SlotFn;
|
|
343
|
+
theme: DomCalendarTheme;
|
|
344
|
+
}): ReactElement {
|
|
345
|
+
const titleId = useId();
|
|
346
|
+
const descriptionId = useId();
|
|
347
|
+
|
|
348
|
+
useEffect(() => {
|
|
349
|
+
const dialog = dialogRef.current;
|
|
350
|
+
if (!dialog || dialog.open) return;
|
|
351
|
+
// jsdom has no showModal; the open attribute shows it there.
|
|
352
|
+
if (typeof dialog.showModal === "function") dialog.showModal();
|
|
353
|
+
else dialog.setAttribute("open", "");
|
|
354
|
+
}, [dialogRef]);
|
|
355
|
+
|
|
356
|
+
// Keep focus inside: land on the first action when opening, and again if the
|
|
357
|
+
// focused action goes away when the dialog follows a moved event.
|
|
358
|
+
useEffect(() => {
|
|
359
|
+
const dialog = dialogRef.current;
|
|
360
|
+
if (dialog && !dialog.contains(document.activeElement))
|
|
361
|
+
dialog.querySelector<HTMLElement>("button")?.focus();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const buttonSlot = (available: boolean) =>
|
|
365
|
+
slot("eventAction", {
|
|
366
|
+
base: {
|
|
367
|
+
display: "block",
|
|
368
|
+
width: "100%",
|
|
369
|
+
minHeight: 32,
|
|
370
|
+
boxSizing: "border-box",
|
|
371
|
+
textAlign: "left",
|
|
372
|
+
font: "inherit",
|
|
373
|
+
cursor: available ? "pointer" : "default",
|
|
374
|
+
},
|
|
375
|
+
themed: {
|
|
376
|
+
padding: "6px 10px",
|
|
377
|
+
border: `1px solid ${theme.gridLine}`,
|
|
378
|
+
borderRadius: 8,
|
|
379
|
+
background: theme.surface,
|
|
380
|
+
color: theme.text,
|
|
381
|
+
fontSize: 13,
|
|
382
|
+
opacity: available ? 1 : 0.5,
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
return (
|
|
387
|
+
<dialog
|
|
388
|
+
ref={dialogRef}
|
|
389
|
+
aria-labelledby={titleId}
|
|
390
|
+
aria-describedby={descriptionId}
|
|
391
|
+
// Other close requests (e.g. a platform back gesture) close it natively;
|
|
392
|
+
// sync the state and restore focus.
|
|
393
|
+
onClose={() => onClose()}
|
|
394
|
+
onCancel={(e) => {
|
|
395
|
+
e.preventDefault();
|
|
396
|
+
onClose();
|
|
397
|
+
}}
|
|
398
|
+
// A click on the backdrop targets the dialog itself (the content fills it).
|
|
399
|
+
onClick={(e) => {
|
|
400
|
+
if (e.target === e.currentTarget) onClose();
|
|
401
|
+
}}
|
|
402
|
+
// Arrow keys step through the actions, alongside Tab (which some
|
|
403
|
+
// platforms limit to form fields and links).
|
|
404
|
+
onKeyDown={(e) => {
|
|
405
|
+
if (e.key === "PageUp" || e.key === "PageDown") e.stopPropagation();
|
|
406
|
+
if (e.key === "Escape") {
|
|
407
|
+
e.preventDefault();
|
|
408
|
+
e.stopPropagation();
|
|
409
|
+
onClose();
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
moveFocus(e, e.currentTarget);
|
|
413
|
+
}}
|
|
414
|
+
{...slot("eventActions", {
|
|
415
|
+
base: {
|
|
416
|
+
margin: "auto",
|
|
417
|
+
overflowWrap: "anywhere",
|
|
418
|
+
padding: 0,
|
|
419
|
+
width: "max-content",
|
|
420
|
+
maxWidth: "min(22rem, calc(100vw - 32px))",
|
|
421
|
+
maxHeight: "calc(100vh - 32px)",
|
|
422
|
+
overflowY: "auto",
|
|
423
|
+
boxSizing: "border-box",
|
|
424
|
+
},
|
|
425
|
+
themed: {
|
|
426
|
+
fontFamily: theme.fontFamily,
|
|
427
|
+
background: theme.surface,
|
|
428
|
+
color: theme.text,
|
|
429
|
+
border: `1px solid ${theme.gridLine}`,
|
|
430
|
+
borderRadius: 12,
|
|
431
|
+
boxShadow: "0 8px 24px rgba(0,0,0,0.18)",
|
|
432
|
+
},
|
|
433
|
+
})}
|
|
434
|
+
>
|
|
435
|
+
<div style={{ display: "flex", flexDirection: "column", gap: 6, padding: 12 }}>
|
|
436
|
+
<p id={titleId} style={{ margin: 0, fontWeight: 600, fontSize: 14 }}>
|
|
437
|
+
{title}
|
|
438
|
+
</p>
|
|
439
|
+
{/* Announces the event's new time after an accepted change. */}
|
|
440
|
+
<p
|
|
441
|
+
id={descriptionId}
|
|
442
|
+
aria-live="polite"
|
|
443
|
+
style={{ margin: "0 0 4px", fontSize: 12, color: theme.textMuted }}
|
|
444
|
+
>
|
|
445
|
+
{description}
|
|
446
|
+
</p>
|
|
447
|
+
{actions.map((action) => (
|
|
448
|
+
<button
|
|
449
|
+
key={action.id}
|
|
450
|
+
type="button"
|
|
451
|
+
data-action={action.id}
|
|
452
|
+
aria-disabled={action.available ? undefined : true}
|
|
453
|
+
onClick={() => {
|
|
454
|
+
if (action.available) onAction(action.id);
|
|
455
|
+
}}
|
|
456
|
+
{...buttonSlot(action.available)}
|
|
457
|
+
>
|
|
458
|
+
{action.label}
|
|
459
|
+
</button>
|
|
460
|
+
))}
|
|
461
|
+
<div role="status" aria-atomic="true" style={{ fontSize: 13 }}>
|
|
462
|
+
{rejections > 0 ? <span key={rejections}>{rejectedLabel}</span> : null}
|
|
463
|
+
</div>
|
|
464
|
+
<button type="button" data-action="close" onClick={() => onClose()} {...buttonSlot(true)}>
|
|
465
|
+
{closeLabel}
|
|
466
|
+
</button>
|
|
467
|
+
</div>
|
|
468
|
+
</dialog>
|
|
469
|
+
);
|
|
470
|
+
}
|