react-native-hilal 0.1.0 → 0.2.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.
Files changed (57) hide show
  1. package/README.md +119 -40
  2. package/lib/module/calendar/Appear.js +75 -0
  3. package/lib/module/calendar/Appear.js.map +1 -0
  4. package/lib/module/calendar/DayCell.js +4 -1
  5. package/lib/module/calendar/DayCell.js.map +1 -1
  6. package/lib/module/calendar/HilalCalendar.js +341 -64
  7. package/lib/module/calendar/HilalCalendar.js.map +1 -1
  8. package/lib/module/calendar/PickerGrid.js +66 -0
  9. package/lib/module/calendar/PickerGrid.js.map +1 -0
  10. package/lib/module/calendar/month.js +135 -22
  11. package/lib/module/calendar/month.js.map +1 -1
  12. package/lib/module/calendar/styles.js +219 -6
  13. package/lib/module/calendar/styles.js.map +1 -1
  14. package/lib/module/calendar/theme.js +3 -3
  15. package/lib/module/calendar/theme.js.map +1 -1
  16. package/lib/module/core/convert.js +52 -21
  17. package/lib/module/core/convert.js.map +1 -1
  18. package/lib/module/core/index.js.map +1 -1
  19. package/lib/module/core/locale.js +5 -2
  20. package/lib/module/core/locale.js.map +1 -1
  21. package/lib/typescript/src/calendar/Appear.d.ts +22 -0
  22. package/lib/typescript/src/calendar/Appear.d.ts.map +1 -0
  23. package/lib/typescript/src/calendar/DayCell.d.ts +3 -1
  24. package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -1
  25. package/lib/typescript/src/calendar/HilalCalendar.d.ts +1 -1
  26. package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -1
  27. package/lib/typescript/src/calendar/PickerGrid.d.ts +28 -0
  28. package/lib/typescript/src/calendar/PickerGrid.d.ts.map +1 -0
  29. package/lib/typescript/src/calendar/index.d.ts +1 -1
  30. package/lib/typescript/src/calendar/index.d.ts.map +1 -1
  31. package/lib/typescript/src/calendar/month.d.ts +30 -7
  32. package/lib/typescript/src/calendar/month.d.ts.map +1 -1
  33. package/lib/typescript/src/calendar/styles.d.ts +200 -113
  34. package/lib/typescript/src/calendar/styles.d.ts.map +1 -1
  35. package/lib/typescript/src/calendar/theme.d.ts +21 -1
  36. package/lib/typescript/src/calendar/theme.d.ts.map +1 -1
  37. package/lib/typescript/src/calendar/types.d.ts +106 -2
  38. package/lib/typescript/src/calendar/types.d.ts.map +1 -1
  39. package/lib/typescript/src/core/convert.d.ts +38 -15
  40. package/lib/typescript/src/core/convert.d.ts.map +1 -1
  41. package/lib/typescript/src/core/index.d.ts +1 -1
  42. package/lib/typescript/src/core/index.d.ts.map +1 -1
  43. package/lib/typescript/src/core/locale.d.ts +1 -1
  44. package/lib/typescript/src/core/locale.d.ts.map +1 -1
  45. package/package.json +1 -1
  46. package/src/calendar/Appear.tsx +94 -0
  47. package/src/calendar/DayCell.tsx +6 -0
  48. package/src/calendar/HilalCalendar.tsx +406 -77
  49. package/src/calendar/PickerGrid.tsx +99 -0
  50. package/src/calendar/index.ts +6 -1
  51. package/src/calendar/month.ts +174 -23
  52. package/src/calendar/styles.ts +235 -5
  53. package/src/calendar/theme.ts +23 -3
  54. package/src/calendar/types.ts +109 -2
  55. package/src/core/convert.ts +87 -23
  56. package/src/core/index.ts +1 -0
  57. package/src/core/locale.ts +4 -1
@@ -7,7 +7,7 @@ import {
7
7
  View,
8
8
  } from 'react-native';
9
9
 
10
- import { hijriOfEpochDay } from '../core/convert';
10
+ import { checkAdjustment, hijriOfEpochDay } from '../core/convert';
11
11
  import {
12
12
  civilFromDays,
13
13
  epochDayOfLocalDate,
@@ -18,56 +18,88 @@ import { formatGregorian, formatHijri } from '../core/format';
18
18
  import {
19
19
  defaultDigits,
20
20
  formatNumber,
21
+ getMonthNames,
21
22
  getWeekdayNames,
22
23
  type CalendarType,
23
24
  type Digits,
24
25
  type Locale,
25
26
  } from '../core/locale';
27
+ import { Appear, useReduceMotion } from './Appear';
26
28
  import { Chevron } from './Chevron';
27
29
  import { DayCell } from './DayCell';
28
30
  import {
29
31
  addMonths,
30
32
  buildMonthGrid,
31
33
  clampDay,
34
+ dateInCalendar,
32
35
  monthContaining,
33
36
  monthOverlaps,
37
+ monthsOfYear,
34
38
  monthStart,
35
39
  monthTitle,
40
+ pageOverlaps,
36
41
  secondaryTitle,
37
42
  selectableRange,
43
+ yearOverlaps,
44
+ yearPageStart,
45
+ yearsOnPage,
46
+ YEARS_PER_PAGE,
47
+ yearsSecondaryTitle,
38
48
  type CalendarMonth,
39
49
  type DayRange,
40
50
  } from './month';
41
- import { createStyles } from './styles';
51
+ import { PickerGrid, type PickerGridItem } from './PickerGrid';
52
+ import {
53
+ createStyles,
54
+ MAX_CELL_FONT_SCALE,
55
+ MAX_HEADER_FONT_SCALE,
56
+ withSlots,
57
+ } from './styles';
42
58
  import { resolveTheme } from './theme';
43
- import type { HilalCalendarProps, SelectedDate } from './types';
44
-
45
- const LABELS: Record<
46
- Locale,
47
- {
48
- hijri: string;
49
- gregorian: string;
50
- previous: string;
51
- next: string;
52
- separator: string;
53
- }
54
- > = {
59
+ import type { HilalCalendarProps, HilalLabels, SelectedDate } from './types';
60
+
61
+ const LABELS: Record<Locale, Required<HilalLabels> & { separator: string }> = {
55
62
  ar: {
56
63
  hijri: 'هجري',
57
64
  gregorian: 'ميلادي',
58
- previous: 'الشهر السابق',
59
- next: 'الشهر التالي',
65
+ today: 'اليوم',
66
+ thisMonth: 'هذا الشهر',
67
+ thisYear: 'هذه السنة',
68
+ previousMonth: 'الشهر السابق',
69
+ nextMonth: 'الشهر التالي',
70
+ previousYear: 'السنة السابقة',
71
+ nextYear: 'السنة التالية',
72
+ previousYears: 'السنوات السابقة',
73
+ nextYears: 'السنوات التالية',
74
+ chooseMonth: 'اختر الشهر والسنة',
75
+ chooseYear: 'اختر السنة',
60
76
  separator: '، ',
61
77
  },
62
78
  en: {
63
79
  hijri: 'Hijri',
64
80
  gregorian: 'Gregorian',
65
- previous: 'Previous month',
66
- next: 'Next month',
81
+ today: 'Today',
82
+ thisMonth: 'This month',
83
+ thisYear: 'This year',
84
+ previousMonth: 'Previous month',
85
+ nextMonth: 'Next month',
86
+ previousYear: 'Previous year',
87
+ nextYear: 'Next year',
88
+ previousYears: 'Previous years',
89
+ nextYears: 'Next years',
90
+ chooseMonth: 'Choose month and year',
91
+ chooseYear: 'Choose year',
67
92
  separator: ', ',
68
93
  },
69
94
  };
70
95
 
96
+ // More below than above, so it never meets the toggle's.
97
+ const TITLE_HIT_SLOP = { top: 8, bottom: 10, left: 6, right: 6 };
98
+ const TOGGLE_HIT_SLOP = { top: 8, bottom: 8 };
99
+
100
+ /** What the grid shows: the days of a month, or the month or year picker. */
101
+ type View_ = 'days' | 'months' | 'years';
102
+
71
103
  function epochDayOf(date: Date | undefined) {
72
104
  return date instanceof Date && !Number.isNaN(date.getTime())
73
105
  ? epochDayOfLocalDate(date)
@@ -77,9 +109,10 @@ function epochDayOf(date: Date | undefined) {
77
109
  function selectedDateOf(
78
110
  epochDay: number,
79
111
  locale: Locale,
80
- digits: Digits
112
+ digits: Digits,
113
+ adjustment: number
81
114
  ): SelectedDate {
82
- const hijri = hijriOfEpochDay(epochDay);
115
+ const hijri = hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment);
83
116
  return {
84
117
  gregorian: localDateOfEpochDay(epochDay),
85
118
  hijri,
@@ -106,11 +139,13 @@ function visibleMonth(
106
139
  preferred: CalendarMonth,
107
140
  calendar: CalendarType,
108
141
  range: DayRange,
109
- fallbackDay: number
142
+ fallbackDay: number,
143
+ adjustment: number
110
144
  ) {
111
- return preferred.calendar === calendar && monthOverlaps(preferred, range)
145
+ return preferred.calendar === calendar &&
146
+ monthOverlaps(preferred, range, adjustment)
112
147
  ? preferred
113
- : monthContaining(clampDay(fallbackDay, range), calendar);
148
+ : monthContaining(clampDay(fallbackDay, range), calendar, adjustment);
114
149
  }
115
150
 
116
151
  /**
@@ -132,12 +167,40 @@ export function HilalCalendar({
132
167
  minDate,
133
168
  maxDate,
134
169
  isDateDisabled,
170
+ hijriAdjustment = 0,
171
+ showMonthYearPicker = true,
172
+ weekdayFormat = 'short',
135
173
  theme: themeProp = 'auto',
174
+ styles: stylesProp,
175
+ labels: labelsProp,
136
176
  style,
137
177
  dayStyle,
178
+ maxFontSizeMultiplier,
138
179
  }: HilalCalendarProps) {
180
+ const adjustment = checkAdjustment(hijriAdjustment, 'HilalCalendar');
139
181
  const digits = digitsProp ?? defaultDigits(locale);
140
- const labels = LABELS[locale];
182
+ // Compared by value, so an inline object doesn't recompute every day.
183
+ const labelsKey = JSON.stringify(labelsProp);
184
+ const labels = useMemo(
185
+ () => {
186
+ const merged = { ...LABELS[locale] };
187
+ for (const [key, text] of Object.entries(labelsProp ?? {})) {
188
+ // A label left undefined keeps the default.
189
+ if (text !== undefined) {
190
+ merged[key as keyof HilalLabels] = text;
191
+ }
192
+ }
193
+ return merged;
194
+ },
195
+ // eslint-disable-next-line react-hooks/exhaustive-deps
196
+ [locale, labelsKey]
197
+ );
198
+ const cellFontScale = maxFontSizeMultiplier ?? MAX_CELL_FONT_SCALE;
199
+ const headerFontScale = maxFontSizeMultiplier ?? MAX_HEADER_FONT_SCALE;
200
+ // Today, this month and this year are only marked by a ring; screen
201
+ // readers hear it at the end of the label instead.
202
+ const withCurrent = (label: string, current: boolean, text: string) =>
203
+ current ? `${label}${labels.separator}${text}` : label;
141
204
  // React Native Web leaves I18nManager.isRTL undefined, not false.
142
205
  const appRTL = I18nManager.isRTL === true;
143
206
  const rtl =
@@ -155,7 +218,13 @@ export function HilalCalendar({
155
218
  // eslint-disable-next-line react-hooks/exhaustive-deps
156
219
  [themeKey, colorScheme]
157
220
  );
158
- const styles = useMemo(() => createStyles(theme), [theme]);
221
+ // An inline `styles` prop is compared by value too.
222
+ const stylesKey = JSON.stringify(stylesProp);
223
+ const { styles, dayStyles } = useMemo(
224
+ () => withSlots(createStyles(theme), stylesProp),
225
+ // eslint-disable-next-line react-hooks/exhaustive-deps
226
+ [theme, stylesKey]
227
+ );
159
228
  // Same for an inline dayStyle: a new object every render would re-render
160
229
  // every day cell.
161
230
  const dayStyleKey = JSON.stringify(dayStyle);
@@ -165,8 +234,8 @@ export function HilalCalendar({
165
234
  const minDay = epochDayOf(minDate);
166
235
  const maxDay = epochDayOf(maxDate);
167
236
  const range = useMemo(
168
- () => selectableRange(minDay, maxDay),
169
- [minDay, maxDay]
237
+ () => selectableRange(minDay, maxDay, adjustment),
238
+ [minDay, maxDay, adjustment]
170
239
  );
171
240
  const today = epochDayOfLocalDate(new Date());
172
241
 
@@ -180,18 +249,47 @@ export function HilalCalendar({
180
249
  const [calendar, setCalendar] = useState(calendarProp);
181
250
  const [lastCalendarProp, setLastCalendarProp] = useState(calendarProp);
182
251
  const [month, setMonth] = useState(() =>
183
- monthContaining(clampDay(selectedDay ?? today, range), calendarProp)
252
+ monthContaining(
253
+ clampDay(selectedDay ?? today, range),
254
+ calendarProp,
255
+ adjustment
256
+ )
184
257
  );
185
258
  const [lastSelectedDay, setLastSelectedDay] = useState(selectedDay);
186
259
 
260
+ // The month and year picker: which grid is shown, the year whose months
261
+ // are listed, and the first year of the page of years.
262
+ const [view, setView] = useState<View_>('days');
263
+ const [pickerYear, setPickerYear] = useState(0);
264
+ const [pageStart, setPageStart] = useState(0);
265
+ const mode: View_ = showMonthYearPicker ? view : 'days';
266
+ // The grids fade in when switching views, but not when the calendar
267
+ // first appears: the days only animate once the picker has been open.
268
+ const reduceMotion = useReduceMotion();
269
+ const [pickerOpened, setPickerOpened] = useState(false);
270
+ if (mode !== 'days' && !pickerOpened) {
271
+ setPickerOpened(true);
272
+ }
273
+ // The height of the days, which the picker takes too so that the
274
+ // calendar doesn't jump in size when switching.
275
+ const [daysHeight, setDaysHeight] = useState<number>();
276
+
187
277
  const switchTo = (next: CalendarType, current: CalendarMonth) => {
188
278
  setCalendar(next);
279
+ setView('days');
189
280
  // Keep the selected day in view, or else the month being looked at.
190
- const anchor = selectedDay ?? clampDay(monthStart(current), range);
191
- setMonth(monthContaining(clampDay(anchor, range), next));
281
+ const anchor =
282
+ selectedDay ?? clampDay(monthStart(current, adjustment), range);
283
+ setMonth(monthContaining(clampDay(anchor, range), next, adjustment));
192
284
  };
193
285
 
194
- const shown = visibleMonth(month, calendar, range, selectedDay ?? today);
286
+ const shown = visibleMonth(
287
+ month,
288
+ calendar,
289
+ range,
290
+ selectedDay ?? today,
291
+ adjustment
292
+ );
195
293
 
196
294
  // Adjust state while rendering when props change (React's recommended
197
295
  // alternative to effects for derived state).
@@ -206,7 +304,7 @@ export function HilalCalendar({
206
304
  selectedDay >= range.min &&
207
305
  selectedDay <= range.max
208
306
  ) {
209
- const containing = monthContaining(selectedDay, calendar);
307
+ const containing = monthContaining(selectedDay, calendar, adjustment);
210
308
  if (containing.year !== shown.year || containing.month !== shown.month) {
211
309
  setMonth(containing);
212
310
  }
@@ -214,10 +312,10 @@ export function HilalCalendar({
214
312
  }
215
313
 
216
314
  const grid = useMemo(
217
- () => buildMonthGrid(shown, firstDayOfWeek),
315
+ () => buildMonthGrid(shown, firstDayOfWeek, adjustment),
218
316
  // `shown` is a new object on every render; its fields identify it.
219
317
  // eslint-disable-next-line react-hooks/exhaustive-deps
220
- [shown.calendar, shown.year, shown.month, firstDayOfWeek]
318
+ [shown.calendar, shown.year, shown.month, firstDayOfWeek, adjustment]
221
319
  );
222
320
 
223
321
  // Everything about a day except `selected` and `today`, so that changing
@@ -235,7 +333,9 @@ export function HilalCalendar({
235
333
  !inSupportedRange ||
236
334
  epochDay < range.min ||
237
335
  epochDay > range.max ||
238
- (isDateDisabled?.(selectedDateOf(epochDay, locale, digits)) ??
336
+ (isDateDisabled?.(
337
+ selectedDateOf(epochDay, locale, digits, adjustment)
338
+ ) ??
239
339
  false);
240
340
  const weekday = getWeekdayNames(locale)[weekdayOfEpochDay(epochDay)];
241
341
  const gregorianText = formatGregorian(civilFromDays(epochDay), {
@@ -244,7 +344,10 @@ export function HilalCalendar({
244
344
  });
245
345
  const spoken = inSupportedRange
246
346
  ? [
247
- formatHijri(hijriOfEpochDay(epochDay), { locale, digits }),
347
+ formatHijri(
348
+ hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment),
349
+ { locale, digits }
350
+ ),
248
351
  gregorianText,
249
352
  ]
250
353
  : [gregorianText];
@@ -272,6 +375,7 @@ export function HilalCalendar({
272
375
  digits,
273
376
  showSecondaryDate,
274
377
  labels,
378
+ adjustment,
275
379
  ]
276
380
  );
277
381
 
@@ -279,7 +383,7 @@ export function HilalCalendar({
279
383
  if (value === undefined) {
280
384
  setInternalDay(epochDay);
281
385
  }
282
- onChange?.(selectedDateOf(epochDay, locale, digits));
386
+ onChange?.(selectedDateOf(epochDay, locale, digits, adjustment));
283
387
  });
284
388
 
285
389
  const toggleCalendar = (next: CalendarType) => {
@@ -290,39 +394,190 @@ export function HilalCalendar({
290
394
  onCalendarChange?.(next);
291
395
  };
292
396
 
293
- const previous = addMonths(shown, -1);
294
- const next = addMonths(shown, 1);
295
- const canGoBack = monthOverlaps(previous, range);
296
- const canGoForward = monthOverlaps(next, range);
397
+ // Today's month and year in the shown calendar, for the picker's ring.
398
+ const todayInCalendar = safeDateInCalendar(today, calendar, adjustment);
399
+
400
+ // What the arrows do depends on the grid: months, years, or pages.
401
+ let arrows: Record<
402
+ 'previous' | 'next',
403
+ { enabled: boolean; label: string; onPress: () => void }
404
+ >;
405
+ if (mode === 'months') {
406
+ arrows = {
407
+ previous: {
408
+ enabled: yearOverlaps(calendar, pickerYear - 1, range, adjustment),
409
+ label: labels.previousYear,
410
+ onPress: () => setPickerYear(pickerYear - 1),
411
+ },
412
+ next: {
413
+ enabled: yearOverlaps(calendar, pickerYear + 1, range, adjustment),
414
+ label: labels.nextYear,
415
+ onPress: () => setPickerYear(pickerYear + 1),
416
+ },
417
+ };
418
+ } else if (mode === 'years') {
419
+ const before = pageStart - YEARS_PER_PAGE;
420
+ const after = pageStart + YEARS_PER_PAGE;
421
+ arrows = {
422
+ previous: {
423
+ enabled: pageOverlaps(calendar, before, range, adjustment),
424
+ label: labels.previousYears,
425
+ onPress: () => setPageStart(before),
426
+ },
427
+ next: {
428
+ enabled: pageOverlaps(calendar, after, range, adjustment),
429
+ label: labels.nextYears,
430
+ onPress: () => setPageStart(after),
431
+ },
432
+ };
433
+ } else {
434
+ const previous = addMonths(shown, -1);
435
+ const next = addMonths(shown, 1);
436
+ arrows = {
437
+ previous: {
438
+ enabled: monthOverlaps(previous, range, adjustment),
439
+ label: labels.previousMonth,
440
+ onPress: () => setMonth(previous),
441
+ },
442
+ next: {
443
+ enabled: monthOverlaps(next, range, adjustment),
444
+ label: labels.nextMonth,
445
+ onPress: () => setMonth(next),
446
+ },
447
+ };
448
+ }
297
449
 
298
- const weekdayNames = getWeekdayNames(locale, 'short');
450
+ const weekdayNames = getWeekdayNames(locale, weekdayFormat);
299
451
  const weekdays = Array.from(
300
452
  { length: 7 },
301
453
  (_, i) => weekdayNames[(firstDayOfWeek + i) % 7]!
302
454
  );
303
455
 
304
456
  const renderArrow = (kind: 'previous' | 'next') => {
305
- const enabled = kind === 'previous' ? canGoBack : canGoForward;
457
+ const { enabled, label, onPress } = arrows[kind];
306
458
  // Arrows point the way the months move: outward in reading order.
307
459
  const pointsLeft = (kind === 'previous') !== rtl;
308
460
  return (
309
461
  <Pressable
310
462
  accessibilityRole="button"
311
- accessibilityLabel={labels[kind]}
463
+ accessibilityLabel={label}
312
464
  accessibilityState={{ disabled: !enabled }}
313
465
  disabled={!enabled}
314
466
  hitSlop={8}
315
- onPress={() => setMonth(kind === 'previous' ? previous : next)}
467
+ onPress={onPress}
316
468
  style={({ pressed }) => [styles.arrow, pressed && styles.arrowPressed]}
317
469
  >
318
470
  <Chevron
319
471
  direction={pointsLeft ? 'left' : 'right'}
320
- color={enabled ? theme.text : theme.disabled}
472
+ color={enabled ? (theme.arrow ?? theme.text) : theme.disabled}
321
473
  />
322
474
  </Pressable>
323
475
  );
324
476
  };
325
477
 
478
+ const year = (y: number) => formatNumber(y, digits);
479
+
480
+ // The header title, and what pressing it opens.
481
+ let title: string;
482
+ let subtitle: string;
483
+ let titleAction: { label: string; onPress: () => void } | undefined;
484
+ if (mode === 'months') {
485
+ title = year(pickerYear);
486
+ subtitle = yearsSecondaryTitle(
487
+ calendar,
488
+ pickerYear,
489
+ pickerYear,
490
+ digits,
491
+ adjustment
492
+ );
493
+ titleAction = {
494
+ label: labels.chooseYear,
495
+ onPress: () => {
496
+ setPageStart(yearPageStart(pickerYear));
497
+ setView('years');
498
+ },
499
+ };
500
+ } else if (mode === 'years') {
501
+ const last = pageStart + YEARS_PER_PAGE - 1;
502
+ title = `${year(pageStart)} – ${year(last)}`;
503
+ subtitle = yearsSecondaryTitle(
504
+ calendar,
505
+ pageStart,
506
+ last,
507
+ digits,
508
+ adjustment
509
+ );
510
+ } else {
511
+ title = monthTitle(shown, locale, digits);
512
+ subtitle = secondaryTitle(shown, locale, digits, adjustment);
513
+ if (showMonthYearPicker) {
514
+ titleAction = {
515
+ label: labels.chooseMonth,
516
+ onPress: () => {
517
+ setPickerYear(shown.year);
518
+ setView('months');
519
+ },
520
+ };
521
+ }
522
+ }
523
+
524
+ const titleText = (
525
+ <Text
526
+ accessibilityRole="header"
527
+ maxFontSizeMultiplier={headerFontScale}
528
+ style={titleAction ? [styles.title, styles.titleInButton] : styles.title}
529
+ >
530
+ {title}
531
+ </Text>
532
+ );
533
+
534
+ let pickerItems: PickerGridItem[] | undefined;
535
+ if (mode === 'months') {
536
+ const names = getMonthNames(calendar, locale);
537
+ pickerItems = monthsOfYear(calendar, pickerYear, range, adjustment).map(
538
+ ({ value: m, disabled }) => {
539
+ const current =
540
+ todayInCalendar?.year === pickerYear && todayInCalendar.month === m;
541
+ return {
542
+ key: m,
543
+ label: names[m - 1]!,
544
+ accessibilityLabel: withCurrent(
545
+ `${names[m - 1]} ${year(pickerYear)}`,
546
+ current,
547
+ labels.thisMonth
548
+ ),
549
+ selected: pickerYear === shown.year && m === shown.month,
550
+ current,
551
+ disabled,
552
+ };
553
+ }
554
+ );
555
+ } else if (mode === 'years') {
556
+ pickerItems = yearsOnPage(calendar, pageStart, range, adjustment).map(
557
+ ({ value: y, disabled }) => {
558
+ const current = todayInCalendar?.year === y;
559
+ return {
560
+ key: y,
561
+ label: year(y),
562
+ accessibilityLabel: withCurrent(year(y), current, labels.thisYear),
563
+ selected: y === pickerYear,
564
+ current,
565
+ disabled,
566
+ };
567
+ }
568
+ );
569
+ }
570
+
571
+ const pick = (key: number) => {
572
+ if (mode === 'months') {
573
+ setMonth({ calendar, year: pickerYear, month: key });
574
+ setView('days');
575
+ } else {
576
+ setPickerYear(key);
577
+ setView('months');
578
+ }
579
+ };
580
+
326
581
  return (
327
582
  <View style={[styles.container, style]}>
328
583
  {showCalendarToggle && (
@@ -332,6 +587,8 @@ export function HilalCalendar({
332
587
  return (
333
588
  <Pressable
334
589
  key={option}
590
+ // 45 pt tall to the finger; the toggle itself stays slim.
591
+ hitSlop={TOGGLE_HIT_SLOP}
335
592
  accessibilityRole="button"
336
593
  accessibilityState={{ selected: active }}
337
594
  onPress={() => toggleCalendar(option)}
@@ -341,6 +598,7 @@ export function HilalCalendar({
341
598
  ]}
342
599
  >
343
600
  <Text
601
+ maxFontSizeMultiplier={headerFontScale}
344
602
  style={[styles.toggleText, active && styles.toggleTextActive]}
345
603
  >
346
604
  {labels[option]}
@@ -357,43 +615,114 @@ export function HilalCalendar({
357
615
  >
358
616
  {renderArrow('previous')}
359
617
  <View style={styles.titles}>
360
- <Text accessibilityRole="header" style={styles.title}>
361
- {monthTitle(shown, locale, digits)}
362
- </Text>
363
- <Text style={styles.subtitle}>
364
- {secondaryTitle(shown, locale, digits)}
618
+ {titleAction ? (
619
+ <Pressable
620
+ accessibilityRole="button"
621
+ accessibilityLabel={`${titleAction.label}${labels.separator}${title}`}
622
+ // 44 pt tall to the finger, like the arrows.
623
+ hitSlop={TITLE_HIT_SLOP}
624
+ onPress={titleAction.onPress}
625
+ style={({ pressed }) => [
626
+ styles.titleButton,
627
+ { flexDirection: row },
628
+ pressed && styles.titleButtonPressed,
629
+ ]}
630
+ >
631
+ <View style={styles.titleTint} />
632
+ {titleText}
633
+ </Pressable>
634
+ ) : mode === 'years' ? (
635
+ // As tall as the title button of the other views, so the
636
+ // header doesn't jump when the years open.
637
+ <View style={styles.titleStatic}>{titleText}</View>
638
+ ) : (
639
+ titleText
640
+ )}
641
+ <Text maxFontSizeMultiplier={headerFontScale} style={styles.subtitle}>
642
+ {subtitle}
365
643
  </Text>
366
644
  </View>
367
645
  {renderArrow('next')}
368
646
  </View>
369
647
 
370
- <View style={[styles.weekdays, { flexDirection: row }]}>
371
- {weekdays.map((name) => (
372
- <Text key={name} style={styles.weekday}>
373
- {name}
374
- </Text>
375
- ))}
376
- </View>
648
+ {pickerItems ? (
649
+ <Appear
650
+ key={mode}
651
+ animate={!reduceMotion}
652
+ style={daysHeight !== undefined && { minHeight: daysHeight }}
653
+ >
654
+ <PickerGrid
655
+ items={pickerItems}
656
+ styles={styles}
657
+ row={row}
658
+ fill={daysHeight !== undefined}
659
+ maxFontScale={cellFontScale}
660
+ onSelect={pick}
661
+ />
662
+ </Appear>
663
+ ) : (
664
+ <Appear
665
+ key="days"
666
+ animate={pickerOpened && !reduceMotion}
667
+ onLayout={(event) => setDaysHeight(event.nativeEvent.layout.height)}
668
+ >
669
+ <View style={[styles.weekdays, { flexDirection: row }]}>
670
+ {/* Keyed by position: narrow names repeat (S, T). */}
671
+ {weekdays.map((name, i) => (
672
+ <Text
673
+ key={i}
674
+ // Shrinks back to fit its column when the phone's text is
675
+ // large.
676
+ numberOfLines={1}
677
+ adjustsFontSizeToFit
678
+ maxFontSizeMultiplier={cellFontScale}
679
+ style={styles.weekday}
680
+ >
681
+ {name}
682
+ </Text>
683
+ ))}
684
+ </View>
377
685
 
378
- {cells.map((week, w) => (
379
- <View key={w} style={[styles.week, { flexDirection: row }]}>
380
- {week.map((cell, d) =>
381
- cell === null ? (
382
- <View key={`empty-${d}`} style={styles.slot} />
383
- ) : (
384
- <DayCell
385
- key={cell.epochDay}
386
- {...cell}
387
- selected={cell.epochDay === selectedDay}
388
- today={cell.epochDay === today}
389
- styles={styles}
390
- dayStyle={stableDayStyle}
391
- onSelect={onSelect}
392
- />
393
- )
394
- )}
395
- </View>
396
- ))}
686
+ {cells.map((week, w) => (
687
+ <View key={w} style={[styles.week, { flexDirection: row }]}>
688
+ {week.map((cell, d) =>
689
+ cell === null ? (
690
+ <View key={`empty-${d}`} style={styles.slot} />
691
+ ) : (
692
+ <DayCell
693
+ key={cell.epochDay}
694
+ {...cell}
695
+ accessibilityLabel={withCurrent(
696
+ cell.accessibilityLabel,
697
+ cell.epochDay === today,
698
+ labels.today
699
+ )}
700
+ selected={cell.epochDay === selectedDay}
701
+ today={cell.epochDay === today}
702
+ styles={dayStyles}
703
+ dayStyle={stableDayStyle}
704
+ maxFontScale={cellFontScale}
705
+ onSelect={onSelect}
706
+ />
707
+ )
708
+ )}
709
+ </View>
710
+ ))}
711
+ </Appear>
712
+ )}
397
713
  </View>
398
714
  );
399
715
  }
716
+
717
+ /** A day's date in a calendar, or undefined outside the supported range. */
718
+ function safeDateInCalendar(
719
+ epochDay: number,
720
+ calendar: CalendarType,
721
+ adjustment: number
722
+ ) {
723
+ try {
724
+ return dateInCalendar(epochDay, calendar, adjustment);
725
+ } catch {
726
+ return undefined;
727
+ }
728
+ }