-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCalendar.tsx
More file actions
566 lines (533 loc) · 17.2 KB
/
Calendar.tsx
File metadata and controls
566 lines (533 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {ActionButton, Header, HeaderContext, Heading, HeadingContext, pressScale} from './';
import {
Calendar as AriaCalendar,
CalendarCell as AriaCalendarCell,
CalendarGrid as AriaCalendarGrid,
CalendarHeaderCell as AriaCalendarHeaderCell,
CalendarProps as AriaCalendarProps,
ButtonProps,
CalendarCellProps,
CalendarCellRenderProps,
CalendarGridBody,
CalendarGridHeader,
CalendarHeaderCellProps,
CalendarState,
CalendarStateContext,
ContextValue,
DateValue,
Provider,
RangeCalendarState,
RangeCalendarStateContext,
Text
} from 'react-aria-components';
import {AriaCalendarGridProps} from '@react-aria/calendar';
import {baseColor, focusRing, lightDark, style} from '../style' with {type: 'macro'};
import {
CalendarDate,
getDayOfWeek,
startOfMonth
} from '@internationalized/date';
import ChevronLeftIcon from '../s2wf-icons/S2_Icon_ChevronLeft_20_N.svg';
import ChevronRightIcon from '../s2wf-icons/S2_Icon_ChevronRight_20_N.svg';
import {forwardRefType, GlobalDOMAttributes} from '@react-types/shared';
import {getAllowedOverrides, StyleProps} from './style-utils' with {type: 'macro'};
import {helpTextStyles} from './Field';
// @ts-ignore
import intlMessages from '../intl/*.json';
import React, {createContext, CSSProperties, ForwardedRef, forwardRef, Fragment, PropsWithChildren, ReactElement, ReactNode, useContext, useMemo, useRef} from 'react';
import {useDateFormatter, useLocale, useLocalizedStringFormatter} from '@react-aria/i18n';
import {useSpectrumContextProps} from './useSpectrumContextProps';
export interface CalendarProps<T extends DateValue>
extends Omit<AriaCalendarProps<T>, 'visibleDuration' | 'style' | 'className' | 'styles' | 'children' | keyof GlobalDOMAttributes>,
StyleProps {
/**
* The error message to display when the calendar is invalid.
*/
errorMessage?: ReactNode,
/**
* The number of months to display at once.
* @default 1
*/
visibleMonths?: number
}
export const CalendarContext = createContext<ContextValue<Partial<CalendarProps<any>>, HTMLDivElement>>(null);
const calendarStyles = style({
display: 'flex',
flexDirection: 'column',
gap: 24,
width: 'fit',
disableTapHighlight: true
}, getAllowedOverrides());
const headerStyles = style({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
width: 'full'
});
const headingStyles = style({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
margin: 0,
width: 'full'
});
const titleStyles = style({
font: 'title-lg',
textAlign: 'center',
flexGrow: 1,
flexShrink: 0,
flexBasis: '0%',
minWidth: 0
});
const headerCellStyles = style({
font: 'title-sm',
cursor: 'default',
textAlign: 'center',
paddingStart: {
default: 4,
':first-child': 0
},
paddingEnd: {
default: 4,
':last-child': 0
},
paddingBottom: 12
});
const cellStyles = style({
outlineStyle: 'none',
'--cell-gap': {
type: 'paddingStart',
value: 4
},
paddingStart: {
default: 4,
isFirstChild: 0
},
paddingEnd: {
default: 4,
isLastChild: 0
},
paddingTop: {
default: 2,
isFirstWeek: 0
},
paddingBottom: 2,
position: 'relative',
width: 32,
height: 32,
display: {
default: 'flex',
isOutsideMonth: 'none'
},
alignItems: 'center',
justifyContent: 'center'
});
const cellInnerStyles = style<CalendarCellRenderProps & {selectionMode: 'single' | 'range'}>({
...focusRing(),
transition: {
default: 'default',
forcedColors: 'none'
},
outlineOffset: {
default: -2,
isToday: 2,
isSelected: {
selectionMode: {
single: 2,
range: -2
}
},
isSelectionStart: 2,
isSelectionEnd: 2
},
position: 'relative',
font: 'body-sm',
cursor: 'default',
width: 'full',
height: 32,
borderRadius: 'full',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
forcedColorAdjust: 'none',
backgroundColor: {
default: 'transparent',
isHovered: 'gray-100',
isPressed: 'gray-100',
isDisabled: 'transparent',
isToday: {
default: baseColor('gray-300'),
isDisabled: 'disabled'
},
isSelected: {
selectionMode: {
single: {
default: lightDark('accent-900', 'accent-700'),
isHovered: lightDark('accent-1000', 'accent-600'),
isPressed: lightDark('accent-1000', 'accent-600'),
isFocusVisible: lightDark('accent-1000', 'accent-600'),
isDisabled: 'transparent'
},
range: {
isHovered: 'blue-500'
}
}
},
isSelectionStart: {
default: lightDark('accent-900', 'accent-700'),
isHovered: lightDark('accent-1000', 'accent-600'),
isPressed: lightDark('accent-1000', 'accent-600'),
isFocusVisible: lightDark('accent-1000', 'accent-600')
},
isSelectionEnd: {
default: lightDark('accent-900', 'accent-700'),
isHovered: lightDark('accent-1000', 'accent-600'),
isPressed: lightDark('accent-1000', 'accent-600'),
isFocusVisible: lightDark('accent-1000', 'accent-600')
},
isUnavailable: 'transparent',
forcedColors: {
default: 'transparent',
isToday: 'ButtonFace',
isHovered: 'Highlight',
isSelected: {
selectionMode: {
single: 'Highlight',
range: {
isHovered: 'Highlight'
}
}
},
isSelectionStart: 'Highlight',
isSelectionEnd: 'Highlight',
isUnavailable: 'transparent'
}
},
color: {
default: 'neutral',
isSelected: {
default: 'white',
selectionMode: {
range: 'neutral'
}
},
isSelectionStart: 'white',
isSelectionEnd: 'white',
isDisabled: 'disabled',
forcedColors: {
default: 'ButtonText',
isToday: 'ButtonFace',
isSelected: 'HighlightText',
isSelectionStart: 'HighlightText',
isSelectionEnd: 'HighlightText',
isDisabled: 'GrayText'
}
}
});
const unavailableStyles = style({
position: 'absolute',
top: 'calc(50% - 1px)',
left: 'calc(25% - 1px)',
right: 'calc(25% - 1px)',
height: 2,
transform: 'rotate(-16deg)',
borderRadius: 'full',
backgroundColor: '[currentColor]'
});
const selectionSpanStyles = style({
position: 'absolute',
zIndex: -1,
top: 0,
insetStart: 'calc(-1 * var(--selection-span) * (var(--cell-width) + var(--cell-gap) + var(--cell-gap)))',
insetEnd: 0,
bottom: 0,
borderWidth: 2,
borderStyle: 'dashed',
borderColor: {
default: 'blue-800', // focus-indicator-color
forcedColors: {
default: 'ButtonText'
}
},
borderStartRadius: 'full',
borderEndRadius: 'full',
backgroundColor: {
default: 'blue-subtle',
forcedColors: {
default: 'Highlight'
}
},
forcedColorAdjust: 'none'
});
/**
* Calendars display a grid of days in one or more months and allow users to select a single date.
*/
export const Calendar = /*#__PURE__*/ (forwardRef as forwardRefType)(function Calendar<T extends DateValue>(props: CalendarProps<T>, ref: ForwardedRef<HTMLDivElement>) {
[props, ref] = useSpectrumContextProps(props, ref, CalendarContext);
let {
visibleMonths = 1,
errorMessage,
UNSAFE_style,
UNSAFE_className,
styles,
...otherProps
} = props;
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
return (
<AriaCalendar
{...otherProps}
ref={ref}
visibleDuration={{months: visibleMonths}}
style={UNSAFE_style}
className={(UNSAFE_className || '') + calendarStyles(null, styles)}>
{({isInvalid, isDisabled}) => {
return (
<>
<Provider
values={[
[HeaderContext, null],
[HeadingContext, null]
]}>
<Header styles={headerStyles}>
<CalendarButton slot="previous"><ChevronLeftIcon /></CalendarButton>
<CalendarHeading />
<CalendarButton slot="next"><ChevronRightIcon /></CalendarButton>
</Header>
</Provider>
<div
className={style({
display: 'flex',
flexDirection: 'row',
gap: 24,
width: 'full',
alignItems: 'start'
})}>
{Array.from({length: visibleMonths}).map((_, i) => (
<CalendarGrid months={i} key={i} />
))}
</div>
{isInvalid && (
<Text slot="errorMessage" className={helpTextStyles({isInvalid, isDisabled, size: 'M'})}>
{errorMessage || stringFormatter.format('calendar.invalidSelection', {selectedCount: 1})}
</Text>
)}
</>
);
}}
</AriaCalendar>
);
});
export const CalendarGrid = (props: Omit<AriaCalendarGridProps, 'children'> & PropsWithChildren & {months: number}): ReactElement => {
// use isolation to start a new stacking context so that we can use zIndex -1 for the selection span.
return (
<AriaCalendarGrid
className={style({
borderCollapse: 'collapse',
borderSpacing: 0,
isolation: 'isolate'
})}
offset={{months: props.months}}>
<CalendarGridHeader>
{(day) => (
<CalendarHeaderCell>
{day}
</CalendarHeaderCell>
)}
</CalendarGridHeader>
<CalendarGridBody>
{(date) => (
<CalendarCell date={date} firstDayOfWeek={props.firstDayOfWeek} />
)}
</CalendarGridBody>
</AriaCalendarGrid>
);
};
// Ordinarily the heading is a formatted date range, ie January 2025 - February 2025.
// However, we want to show each month individually.
export const CalendarHeading = (): ReactElement => {
let calendarStateContext = useContext(CalendarStateContext);
let rangeCalendarStateContext = useContext(RangeCalendarStateContext);
let {visibleRange, timeZone} = calendarStateContext ?? rangeCalendarStateContext ?? {};
let currentMonth = visibleRange?.start ?? visibleRange?.end;
let monthFormatter = useDateFormatter({
month: 'long',
year: 'numeric',
era: currentMonth && currentMonth.calendar.identifier === 'gregory' && currentMonth.era === 'BC' ? 'short' : undefined,
calendar: visibleRange?.start.calendar.identifier,
timeZone
});
let months = useMemo(() => {
if (!visibleRange) {
return [];
}
let months: string[] = [];
for (let i = visibleRange.start; i.compare(visibleRange.end) <= 0; i = i.add({months: 1})) {
// TODO: account for the first week possibly overlapping, like with a custom 454 calendar.
// there has to be a better way to do this...
if (i.month === visibleRange.start.month) {
i = i.add({weeks: 1});
}
months.push(monthFormatter.format(i.toDate(timeZone!)));
}
return months;
}, [visibleRange, monthFormatter, timeZone]);
return (
<Heading styles={headingStyles}>
{months.map((month, i) => {
if (i === 0) {
return (
<Fragment key={month}>
<div className={titleStyles}>{month}</div>
</Fragment>
);
} else {
return (
<Fragment key={month}>
{/* Spacers to account for Next/Previous buttons and gap, spelled out to show the math */}
<div className={style({visibility: 'hidden', width: 32})} />
<div className={style({visibility: 'hidden', width: 24})} />
<div className={style({visibility: 'hidden', width: 32})} />
<div className={titleStyles}>{month}</div>
</Fragment>
);
}
})}
</Heading>
);
};
export const CalendarButton = (props: Omit<ButtonProps, 'children'> & {children: ReactNode}): ReactElement => {
let {direction} = useLocale();
return (
<div
className={
style({
scale: {
direction: {
rtl: -1
}
}
})({direction})
}>
<ActionButton
{...props}
isQuiet>
{props.children}
</ActionButton>
</div>
);
};
const CalendarHeaderCell = (props: Omit<CalendarHeaderCellProps, 'children'> & PropsWithChildren): ReactElement => {
return (
<AriaCalendarHeaderCell className={headerCellStyles}>
{props.children}
</AriaCalendarHeaderCell>
);
};
const CalendarCell = (props: Omit<CalendarCellProps, 'children'> & {firstDayOfWeek: 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | undefined}): ReactElement => {
let {locale} = useLocale();
let firstDayOfWeek = props.firstDayOfWeek;
// Calculate the day and week index based on the date.
let {dayIndex, weekIndex} = useWeekAndDayIndices(props.date, locale, firstDayOfWeek);
let calendarStateContext = useContext(CalendarStateContext);
let rangeCalendarStateContext = useContext(RangeCalendarStateContext);
let state = (calendarStateContext ?? rangeCalendarStateContext)!;
let isFirstWeek = weekIndex === 0;
let isFirstChild = dayIndex === 0;
let isLastChild = dayIndex === 6;
return (
<AriaCalendarCell
date={props.date}
className={(renderProps) => cellStyles({...renderProps, isFirstChild, isLastChild, isFirstWeek})}>
{(renderProps) => <CalendarCellInner {...props} weekIndex={weekIndex} dayIndex={dayIndex} state={state} isRangeSelection={!!rangeCalendarStateContext} renderProps={renderProps} />}
</AriaCalendarCell>
);
};
const CalendarCellInner = (props: Omit<CalendarCellProps, 'children'> & {isRangeSelection: boolean, state: CalendarState | RangeCalendarState, weekIndex: number, dayIndex: number, renderProps?: CalendarCellRenderProps, date: DateValue}): ReactElement => {
let {weekIndex, dayIndex, date, renderProps, state, isRangeSelection} = props;
let {getDatesInWeek} = state;
let ref = useRef<HTMLDivElement>(null);
let {isUnavailable, formattedDate, isSelected} = renderProps!;
let startDate = startOfMonth(date);
let datesInWeek = getDatesInWeek(weekIndex, startDate);
// Starting from the current day, find the first day before it in the current week that is not selected.
// Then, the span of selected days is the current day minus the first unselected day.
let firstUnselectedInRangeInWeek = datesInWeek.slice(0, dayIndex + 1).reverse().findIndex((date, i) => {
return date && i > 0 && (!state.isSelected(date) || date.month !== props.date.month);
});
let selectionSpan = -1;
if (firstUnselectedInRangeInWeek > -1 && isSelected) {
selectionSpan = firstUnselectedInRangeInWeek - 1;
} else if (isSelected) {
selectionSpan = dayIndex;
}
let prevDay = date.subtract({days: 1});
let nextDay = date.add({days: 1});
let isBackgroundStyleApplied = (
isSelected
&& isRangeSelection
&& (state.isSelected(prevDay)
|| (nextDay.month === date.month && state.isSelected(nextDay)))
);
return (
<div
className={style({
position: 'relative',
width: 32,
'--cell-width': {
type: 'width',
value: '[self(width)]'
}
})}>
<div
ref={ref}
style={pressScale(ref, {})(renderProps!)}
className={cellInnerStyles({...renderProps!, selectionMode: isRangeSelection ? 'range' : 'single'})}>
<div>
{formattedDate}
</div>
{isUnavailable && <div className={unavailableStyles} role="presentation" />}
</div>
{isBackgroundStyleApplied && <div style={{'--selection-span': selectionSpan} as CSSProperties} className={selectionSpanStyles} role="presentation" />}
</div>
);
};
type DayOfWeek = 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat';
/**
* Calculate the week index (0-based) and day index (0-based) for a given date within a month in a calendar.
* @param date - The date to calculate indices for.
* @param locale - The locale string (e.g., 'en-US', 'fr-FR', 'hi-IN-u-ca-indian').
* @param firstDayOfWeek - Optional override for the first day of the week ('sun', 'mon', 'tue', etc.).
* @returns Object with weekIndex and dayIndex.
*/
function useWeekAndDayIndices(
date: CalendarDate,
locale: string,
firstDayOfWeek?: DayOfWeek
) {
let {dayIndex, weekIndex} = useMemo(() => {
// Get the day index within the week (0-6)
const dayIndex = getDayOfWeek(date, locale, firstDayOfWeek);
const monthStart = startOfMonth(date);
// Calculate the week index by finding which week this date falls into
// within the month's calendar grid
const monthStartDayOfWeek = getDayOfWeek(monthStart, locale, firstDayOfWeek);
const dayOfMonth = date.day;
const weekIndex = Math.floor((dayOfMonth + monthStartDayOfWeek - 1) / 7);
return {
weekIndex,
dayIndex
};
}, [date, locale, firstDayOfWeek]);
return {dayIndex, weekIndex};
}