react-native-hilal 0.1.0 → 0.3.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 (62) hide show
  1. package/README.md +165 -49
  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 +32 -5
  5. package/lib/module/calendar/DayCell.js.map +1 -1
  6. package/lib/module/calendar/HilalCalendar.js +434 -76
  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/range.js +103 -0
  13. package/lib/module/calendar/range.js.map +1 -0
  14. package/lib/module/calendar/styles.js +233 -7
  15. package/lib/module/calendar/styles.js.map +1 -1
  16. package/lib/module/calendar/theme.js +3 -3
  17. package/lib/module/calendar/theme.js.map +1 -1
  18. package/lib/module/core/convert.js +52 -21
  19. package/lib/module/core/convert.js.map +1 -1
  20. package/lib/module/core/index.js.map +1 -1
  21. package/lib/module/core/locale.js +5 -2
  22. package/lib/module/core/locale.js.map +1 -1
  23. package/lib/typescript/src/calendar/Appear.d.ts +22 -0
  24. package/lib/typescript/src/calendar/Appear.d.ts.map +1 -0
  25. package/lib/typescript/src/calendar/DayCell.d.ts +8 -1
  26. package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -1
  27. package/lib/typescript/src/calendar/HilalCalendar.d.ts +1 -1
  28. package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -1
  29. package/lib/typescript/src/calendar/PickerGrid.d.ts +28 -0
  30. package/lib/typescript/src/calendar/PickerGrid.d.ts.map +1 -0
  31. package/lib/typescript/src/calendar/index.d.ts +1 -1
  32. package/lib/typescript/src/calendar/index.d.ts.map +1 -1
  33. package/lib/typescript/src/calendar/month.d.ts +30 -7
  34. package/lib/typescript/src/calendar/month.d.ts.map +1 -1
  35. package/lib/typescript/src/calendar/range.d.ts +39 -0
  36. package/lib/typescript/src/calendar/range.d.ts.map +1 -0
  37. package/lib/typescript/src/calendar/styles.d.ts +207 -113
  38. package/lib/typescript/src/calendar/styles.d.ts.map +1 -1
  39. package/lib/typescript/src/calendar/theme.d.ts +21 -1
  40. package/lib/typescript/src/calendar/theme.d.ts.map +1 -1
  41. package/lib/typescript/src/calendar/types.d.ts +153 -2
  42. package/lib/typescript/src/calendar/types.d.ts.map +1 -1
  43. package/lib/typescript/src/core/convert.d.ts +38 -15
  44. package/lib/typescript/src/core/convert.d.ts.map +1 -1
  45. package/lib/typescript/src/core/index.d.ts +1 -1
  46. package/lib/typescript/src/core/index.d.ts.map +1 -1
  47. package/lib/typescript/src/core/locale.d.ts +1 -1
  48. package/lib/typescript/src/core/locale.d.ts.map +1 -1
  49. package/package.json +1 -1
  50. package/src/calendar/Appear.tsx +94 -0
  51. package/src/calendar/DayCell.tsx +41 -1
  52. package/src/calendar/HilalCalendar.tsx +541 -85
  53. package/src/calendar/PickerGrid.tsx +99 -0
  54. package/src/calendar/index.ts +8 -1
  55. package/src/calendar/month.ts +174 -23
  56. package/src/calendar/range.ts +114 -0
  57. package/src/calendar/styles.ts +249 -6
  58. package/src/calendar/theme.ts +23 -3
  59. package/src/calendar/types.ts +156 -2
  60. package/src/core/convert.ts +87 -23
  61. package/src/core/index.ts +1 -0
  62. package/src/core/locale.ts +4 -1
@@ -2,49 +2,121 @@
2
2
 
3
3
  import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
4
4
  import { I18nManager, Pressable, Text, useColorScheme, View } from 'react-native';
5
- import { hijriOfEpochDay } from "../core/convert.js";
5
+ import { checkAdjustment, hijriOfEpochDay } from "../core/convert.js";
6
6
  import { civilFromDays, epochDayOfLocalDate, localDateOfEpochDay, weekdayOfEpochDay } from "../core/days.js";
7
7
  import { formatGregorian, formatHijri } from "../core/format.js";
8
- import { defaultDigits, formatNumber, getWeekdayNames } from "../core/locale.js";
8
+ import { defaultDigits, formatNumber, getMonthNames, getWeekdayNames } from "../core/locale.js";
9
+ import { Appear, useReduceMotion } from "./Appear.js";
9
10
  import { Chevron } from "./Chevron.js";
10
11
  import { DayCell } from "./DayCell.js";
11
- import { addMonths, buildMonthGrid, clampDay, monthContaining, monthOverlaps, monthStart, monthTitle, secondaryTitle, selectableRange } from "./month.js";
12
- import { createStyles } from "./styles.js";
12
+ import { addMonths, buildMonthGrid, clampDay, dateInCalendar, monthContaining, monthOverlaps, monthsOfYear, monthStart, monthTitle, pageOverlaps, secondaryTitle, selectableRange, yearOverlaps, yearPageStart, yearsOnPage, YEARS_PER_PAGE, yearsSecondaryTitle } from "./month.js";
13
+ import { PickerGrid } from "./PickerGrid.js";
14
+ import { checkRangeLimits, nextRange, outsideRangeLimits, rangeLength, rangePosition } from "./range.js";
15
+ import { createStyles, MAX_CELL_FONT_SCALE, MAX_HEADER_FONT_SCALE, withSlots } from "./styles.js";
13
16
  import { resolveTheme } from "./theme.js";
14
17
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
15
18
  const LABELS = {
16
19
  ar: {
17
20
  hijri: 'هجري',
18
21
  gregorian: 'ميلادي',
19
- previous: 'الشهر السابق',
20
- next: 'الشهر التالي',
22
+ today: 'اليوم',
23
+ rangeStart: 'بداية الفترة',
24
+ rangeEnd: 'نهاية الفترة',
25
+ thisMonth: 'هذا الشهر',
26
+ thisYear: 'هذه السنة',
27
+ previousMonth: 'الشهر السابق',
28
+ nextMonth: 'الشهر التالي',
29
+ previousYear: 'السنة السابقة',
30
+ nextYear: 'السنة التالية',
31
+ previousYears: 'السنوات السابقة',
32
+ nextYears: 'السنوات التالية',
33
+ chooseMonth: 'اختر الشهر والسنة',
34
+ chooseYear: 'اختر السنة',
21
35
  separator: '، '
22
36
  },
23
37
  en: {
24
38
  hijri: 'Hijri',
25
39
  gregorian: 'Gregorian',
26
- previous: 'Previous month',
27
- next: 'Next month',
40
+ today: 'Today',
41
+ rangeStart: 'Start of range',
42
+ rangeEnd: 'End of range',
43
+ thisMonth: 'This month',
44
+ thisYear: 'This year',
45
+ previousMonth: 'Previous month',
46
+ nextMonth: 'Next month',
47
+ previousYear: 'Previous year',
48
+ nextYear: 'Next year',
49
+ previousYears: 'Previous years',
50
+ nextYears: 'Next years',
51
+ chooseMonth: 'Choose month and year',
52
+ chooseYear: 'Choose year',
28
53
  separator: ', '
29
54
  }
30
55
  };
56
+
57
+ // More below than above, so it never meets the toggle's.
58
+ const TITLE_HIT_SLOP = {
59
+ top: 8,
60
+ bottom: 10,
61
+ left: 6,
62
+ right: 6
63
+ };
64
+ const TOGGLE_HIT_SLOP = {
65
+ top: 8,
66
+ bottom: 8
67
+ };
68
+
69
+ /** What the grid shows: the days of a month, or the month or year picker. */
70
+
31
71
  function epochDayOf(date) {
32
72
  return date instanceof Date && !Number.isNaN(date.getTime()) ? epochDayOfLocalDate(date) : undefined;
33
73
  }
34
- function selectedDateOf(epochDay, locale, digits) {
35
- const hijri = hijriOfEpochDay(epochDay);
74
+
75
+ /** A DateRange as epoch days, its ends in order; undefined without a start. */
76
+ function rangeOfDates(dates) {
77
+ const start = epochDayOf(dates?.start);
78
+ if (start === undefined) {
79
+ return undefined;
80
+ }
81
+ const end = epochDayOf(dates?.end ?? undefined);
82
+ if (end === undefined) {
83
+ return {
84
+ start
85
+ };
86
+ }
87
+ return {
88
+ start: Math.min(start, end),
89
+ end: Math.max(start, end)
90
+ };
91
+ }
92
+
93
+ /** The days of a range drawn as selected: its two ends. */
94
+ function isRangeEnd(position) {
95
+ return position === 'start' || position === 'end' || position === 'single';
96
+ }
97
+ function selectedDateOf(epochDay, locale, digits, adjustment) {
98
+ const hijri = hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment);
99
+ // Formatted on first read: isDateDisabled runs for every day of a
100
+ // range and rarely reads the texts. Enumerable getters, so the object
101
+ // still spreads and serializes like plain data.
102
+ let gregorianText;
103
+ let hijriText;
36
104
  return {
37
105
  gregorian: localDateOfEpochDay(epochDay),
38
106
  hijri,
39
107
  formatted: {
40
- gregorian: formatGregorian(civilFromDays(epochDay), {
41
- locale,
42
- digits
43
- }),
44
- hijri: formatHijri(hijri, {
45
- locale,
46
- digits
47
- })
108
+ get gregorian() {
109
+ return gregorianText ??= formatGregorian(civilFromDays(epochDay), {
110
+ locale,
111
+ digits
112
+ });
113
+ },
114
+ get hijri() {
115
+ return hijriText ??= formatHijri(hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment), {
116
+ locale,
117
+ digits
118
+ });
119
+ }
48
120
  }
49
121
  };
50
122
  }
@@ -59,8 +131,8 @@ function useStableCallback(fn) {
59
131
  }
60
132
 
61
133
  /** The month to show: `preferred` if it can be shown, else a sensible one. */
62
- function visibleMonth(preferred, calendar, range, fallbackDay) {
63
- return preferred.calendar === calendar && monthOverlaps(preferred, range) ? preferred : monthContaining(clampDay(fallbackDay, range), calendar);
134
+ function visibleMonth(preferred, calendar, range, fallbackDay, adjustment) {
135
+ return preferred.calendar === calendar && monthOverlaps(preferred, range, adjustment) ? preferred : monthContaining(clampDay(fallbackDay, range), calendar, adjustment);
64
136
  }
65
137
 
66
138
  /**
@@ -71,6 +143,12 @@ export function HilalCalendar({
71
143
  value,
72
144
  defaultValue,
73
145
  onChange,
146
+ selectionMode = 'single',
147
+ rangeValue,
148
+ defaultRangeValue,
149
+ onRangeChange,
150
+ minRangeDays,
151
+ maxRangeDays,
74
152
  calendar: calendarProp = 'hijri',
75
153
  onCalendarChange,
76
154
  showCalendarToggle = true,
@@ -82,12 +160,39 @@ export function HilalCalendar({
82
160
  minDate,
83
161
  maxDate,
84
162
  isDateDisabled,
163
+ hijriAdjustment = 0,
164
+ showMonthYearPicker = true,
165
+ weekdayFormat = 'short',
85
166
  theme: themeProp = 'auto',
167
+ styles: stylesProp,
168
+ labels: labelsProp,
86
169
  style,
87
- dayStyle
170
+ dayStyle,
171
+ maxFontSizeMultiplier
88
172
  }) {
173
+ const adjustment = checkAdjustment(hijriAdjustment, 'HilalCalendar');
89
174
  const digits = digitsProp ?? defaultDigits(locale);
90
- const labels = LABELS[locale];
175
+ // Compared by value, so an inline object doesn't recompute every day.
176
+ const labelsKey = JSON.stringify(labelsProp);
177
+ const labels = useMemo(() => {
178
+ const merged = {
179
+ ...LABELS[locale]
180
+ };
181
+ for (const [key, text] of Object.entries(labelsProp ?? {})) {
182
+ // A label left undefined keeps the default.
183
+ if (text !== undefined) {
184
+ merged[key] = text;
185
+ }
186
+ }
187
+ return merged;
188
+ },
189
+ // eslint-disable-next-line react-hooks/exhaustive-deps
190
+ [locale, labelsKey]);
191
+ const cellFontScale = maxFontSizeMultiplier ?? MAX_CELL_FONT_SCALE;
192
+ const headerFontScale = maxFontSizeMultiplier ?? MAX_HEADER_FONT_SCALE;
193
+ // Today, this month and this year are only marked by a ring; screen
194
+ // readers hear it at the end of the label instead.
195
+ const withCurrent = (label, current, text) => current ? `${label}${labels.separator}${text}` : label;
91
196
  // React Native Web leaves I18nManager.isRTL undefined, not false.
92
197
  const appRTL = I18nManager.isRTL === true;
93
198
  const rtl = direction === 'rtl' || direction === 'auto' && (appRTL || locale === 'ar');
@@ -99,7 +204,14 @@ export function HilalCalendar({
99
204
  // A partial theme is usually an inline object; compare it by value.
100
205
  // eslint-disable-next-line react-hooks/exhaustive-deps
101
206
  [themeKey, colorScheme]);
102
- const styles = useMemo(() => createStyles(theme), [theme]);
207
+ // An inline `styles` prop is compared by value too.
208
+ const stylesKey = JSON.stringify(stylesProp);
209
+ const {
210
+ styles,
211
+ dayStyles
212
+ } = useMemo(() => withSlots(createStyles(theme), stylesProp),
213
+ // eslint-disable-next-line react-hooks/exhaustive-deps
214
+ [theme, stylesKey]);
103
215
  // Same for an inline dayStyle: a new object every render would re-render
104
216
  // every day cell.
105
217
  const dayStyleKey = JSON.stringify(dayStyle);
@@ -107,25 +219,52 @@ export function HilalCalendar({
107
219
  const stableDayStyle = useMemo(() => dayStyle, [dayStyleKey]);
108
220
  const minDay = epochDayOf(minDate);
109
221
  const maxDay = epochDayOf(maxDate);
110
- const range = useMemo(() => selectableRange(minDay, maxDay), [minDay, maxDay]);
222
+ const range = useMemo(() => selectableRange(minDay, maxDay, adjustment), [minDay, maxDay, adjustment]);
111
223
  const today = epochDayOfLocalDate(new Date());
112
224
 
113
225
  // Selection: controlled through `value`, otherwise internal state.
114
226
  const [internalDay, setInternalDay] = useState(() => epochDayOf(defaultValue));
115
227
  const selectedDay = value !== undefined ? epochDayOf(value) : internalDay;
116
228
 
229
+ // Range selection: controlled through `rangeValue`, otherwise internal.
230
+ const rangeMode = selectionMode === 'range';
231
+ const rangeLimits = checkRangeLimits(minRangeDays, maxRangeDays);
232
+ const [internalRange, setInternalRange] = useState(() => rangeOfDates(defaultRangeValue));
233
+ const pickedRange = rangeValue !== undefined ? rangeOfDates(rangeValue ?? undefined) : internalRange;
234
+ // The day the calendar opens on and follows: the selected day, or the
235
+ // start of the range.
236
+ const anchorDay = rangeMode ? pickedRange?.start : selectedDay;
237
+
117
238
  // Calendar: internal state that follows the prop when the prop changes.
118
239
  const [calendar, setCalendar] = useState(calendarProp);
119
240
  const [lastCalendarProp, setLastCalendarProp] = useState(calendarProp);
120
- const [month, setMonth] = useState(() => monthContaining(clampDay(selectedDay ?? today, range), calendarProp));
121
- const [lastSelectedDay, setLastSelectedDay] = useState(selectedDay);
241
+ const [month, setMonth] = useState(() => monthContaining(clampDay(anchorDay ?? today, range), calendarProp, adjustment));
242
+ const [lastAnchorDay, setLastAnchorDay] = useState(anchorDay);
243
+
244
+ // The month and year picker: which grid is shown, the year whose months
245
+ // are listed, and the first year of the page of years.
246
+ const [view, setView] = useState('days');
247
+ const [pickerYear, setPickerYear] = useState(0);
248
+ const [pageStart, setPageStart] = useState(0);
249
+ const mode = showMonthYearPicker ? view : 'days';
250
+ // The grids fade in when switching views, but not when the calendar
251
+ // first appears: the days only animate once the picker has been open.
252
+ const reduceMotion = useReduceMotion();
253
+ const [pickerOpened, setPickerOpened] = useState(false);
254
+ if (mode !== 'days' && !pickerOpened) {
255
+ setPickerOpened(true);
256
+ }
257
+ // The height of the days, which the picker takes too so that the
258
+ // calendar doesn't jump in size when switching.
259
+ const [daysHeight, setDaysHeight] = useState();
122
260
  const switchTo = (next, current) => {
123
261
  setCalendar(next);
262
+ setView('days');
124
263
  // Keep the selected day in view, or else the month being looked at.
125
- const anchor = selectedDay ?? clampDay(monthStart(current), range);
126
- setMonth(monthContaining(clampDay(anchor, range), next));
264
+ const anchor = anchorDay ?? clampDay(monthStart(current, adjustment), range);
265
+ setMonth(monthContaining(clampDay(anchor, range), next, adjustment));
127
266
  };
128
- const shown = visibleMonth(month, calendar, range, selectedDay ?? today);
267
+ const shown = visibleMonth(month, calendar, range, anchorDay ?? today, adjustment);
129
268
 
130
269
  // Adjust state while rendering when props change (React's recommended
131
270
  // alternative to effects for derived state).
@@ -133,19 +272,19 @@ export function HilalCalendar({
133
272
  setLastCalendarProp(calendarProp);
134
273
  switchTo(calendarProp, shown);
135
274
  }
136
- if (selectedDay !== lastSelectedDay) {
137
- setLastSelectedDay(selectedDay);
138
- if (selectedDay !== undefined && selectedDay >= range.min && selectedDay <= range.max) {
139
- const containing = monthContaining(selectedDay, calendar);
275
+ if (anchorDay !== lastAnchorDay) {
276
+ setLastAnchorDay(anchorDay);
277
+ if (anchorDay !== undefined && anchorDay >= range.min && anchorDay <= range.max) {
278
+ const containing = monthContaining(anchorDay, calendar, adjustment);
140
279
  if (containing.year !== shown.year || containing.month !== shown.month) {
141
280
  setMonth(containing);
142
281
  }
143
282
  }
144
283
  }
145
- const grid = useMemo(() => buildMonthGrid(shown, firstDayOfWeek),
284
+ const grid = useMemo(() => buildMonthGrid(shown, firstDayOfWeek, adjustment),
146
285
  // `shown` is a new object on every render; its fields identify it.
147
286
  // eslint-disable-next-line react-hooks/exhaustive-deps
148
- [shown.calendar, shown.year, shown.month, firstDayOfWeek]);
287
+ [shown.calendar, shown.year, shown.month, firstDayOfWeek, adjustment]);
149
288
 
150
289
  // Everything about a day except `selected` and `today`, so that changing
151
290
  // the selection doesn't recompute labels or call isDateDisabled.
@@ -159,13 +298,13 @@ export function HilalCalendar({
159
298
  secondaryDay
160
299
  } = cell;
161
300
  const inSupportedRange = secondaryDay !== null;
162
- const disabled = !inSupportedRange || epochDay < range.min || epochDay > range.max || (isDateDisabled?.(selectedDateOf(epochDay, locale, digits)) ?? false);
301
+ const disabled = !inSupportedRange || epochDay < range.min || epochDay > range.max || (isDateDisabled?.(selectedDateOf(epochDay, locale, digits, adjustment)) ?? false);
163
302
  const weekday = getWeekdayNames(locale)[weekdayOfEpochDay(epochDay)];
164
303
  const gregorianText = formatGregorian(civilFromDays(epochDay), {
165
304
  locale,
166
305
  digits
167
306
  });
168
- const spoken = inSupportedRange ? [formatHijri(hijriOfEpochDay(epochDay), {
307
+ const spoken = inSupportedRange ? [formatHijri(hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment), {
169
308
  locale,
170
309
  digits
171
310
  }), gregorianText] : [gregorianText];
@@ -179,12 +318,25 @@ export function HilalCalendar({
179
318
  accessibilityLabel: [weekday, ...spoken].join(labels.separator),
180
319
  disabled
181
320
  };
182
- })), [grid, shown.calendar, range, isDateDisabled, locale, digits, showSecondaryDate, labels]);
321
+ })), [grid, shown.calendar, range, isDateDisabled, locale, digits, showSecondaryDate, labels, adjustment]);
183
322
  const onSelect = useStableCallback(epochDay => {
323
+ if (rangeMode) {
324
+ const dateOf = day => selectedDateOf(day, locale, digits, adjustment);
325
+ const next = nextRange(pickedRange, epochDay, isDateDisabled && (day => isDateDisabled(dateOf(day))), rangeLimits);
326
+ if (rangeValue === undefined) {
327
+ setInternalRange(next);
328
+ }
329
+ onRangeChange?.({
330
+ start: dateOf(next.start),
331
+ end: next.end === undefined ? null : dateOf(next.end),
332
+ days: rangeLength(next)
333
+ });
334
+ return;
335
+ }
184
336
  if (value === undefined) {
185
337
  setInternalDay(epochDay);
186
338
  }
187
- onChange?.(selectedDateOf(epochDay, locale, digits));
339
+ onChange?.(selectedDateOf(epochDay, locale, digits, adjustment));
188
340
  });
189
341
  const toggleCalendar = next => {
190
342
  if (next === calendar) {
@@ -193,36 +345,181 @@ export function HilalCalendar({
193
345
  switchTo(next, shown);
194
346
  onCalendarChange?.(next);
195
347
  };
196
- const previous = addMonths(shown, -1);
197
- const next = addMonths(shown, 1);
198
- const canGoBack = monthOverlaps(previous, range);
199
- const canGoForward = monthOverlaps(next, range);
200
- const weekdayNames = getWeekdayNames(locale, 'short');
348
+
349
+ // A day's spoken label: its date, then today and the ends of a range.
350
+ const dayLabel = cell => {
351
+ const position = rangeMode ? rangePosition(pickedRange, cell.epochDay) : null;
352
+ const label = withCurrent(cell.accessibilityLabel, cell.epochDay === today, labels.today);
353
+ if (position === 'start' || position === 'single') {
354
+ return `${label}${labels.separator}${labels.rangeStart}`;
355
+ }
356
+ return position === 'end' ? `${label}${labels.separator}${labels.rangeEnd}` : label;
357
+ };
358
+
359
+ // Today's month and year in the shown calendar, for the picker's ring.
360
+ const todayInCalendar = safeDateInCalendar(today, calendar, adjustment);
361
+
362
+ // What the arrows do depends on the grid: months, years, or pages.
363
+ let arrows;
364
+ if (mode === 'months') {
365
+ arrows = {
366
+ previous: {
367
+ enabled: yearOverlaps(calendar, pickerYear - 1, range, adjustment),
368
+ label: labels.previousYear,
369
+ onPress: () => setPickerYear(pickerYear - 1)
370
+ },
371
+ next: {
372
+ enabled: yearOverlaps(calendar, pickerYear + 1, range, adjustment),
373
+ label: labels.nextYear,
374
+ onPress: () => setPickerYear(pickerYear + 1)
375
+ }
376
+ };
377
+ } else if (mode === 'years') {
378
+ const before = pageStart - YEARS_PER_PAGE;
379
+ const after = pageStart + YEARS_PER_PAGE;
380
+ arrows = {
381
+ previous: {
382
+ enabled: pageOverlaps(calendar, before, range, adjustment),
383
+ label: labels.previousYears,
384
+ onPress: () => setPageStart(before)
385
+ },
386
+ next: {
387
+ enabled: pageOverlaps(calendar, after, range, adjustment),
388
+ label: labels.nextYears,
389
+ onPress: () => setPageStart(after)
390
+ }
391
+ };
392
+ } else {
393
+ const previous = addMonths(shown, -1);
394
+ const next = addMonths(shown, 1);
395
+ arrows = {
396
+ previous: {
397
+ enabled: monthOverlaps(previous, range, adjustment),
398
+ label: labels.previousMonth,
399
+ onPress: () => setMonth(previous)
400
+ },
401
+ next: {
402
+ enabled: monthOverlaps(next, range, adjustment),
403
+ label: labels.nextMonth,
404
+ onPress: () => setMonth(next)
405
+ }
406
+ };
407
+ }
408
+ const weekdayNames = getWeekdayNames(locale, weekdayFormat);
201
409
  const weekdays = Array.from({
202
410
  length: 7
203
411
  }, (_, i) => weekdayNames[(firstDayOfWeek + i) % 7]);
204
412
  const renderArrow = kind => {
205
- const enabled = kind === 'previous' ? canGoBack : canGoForward;
413
+ const {
414
+ enabled,
415
+ label,
416
+ onPress
417
+ } = arrows[kind];
206
418
  // Arrows point the way the months move: outward in reading order.
207
419
  const pointsLeft = kind === 'previous' !== rtl;
208
420
  return /*#__PURE__*/_jsx(Pressable, {
209
421
  accessibilityRole: "button",
210
- accessibilityLabel: labels[kind],
422
+ accessibilityLabel: label,
211
423
  accessibilityState: {
212
424
  disabled: !enabled
213
425
  },
214
426
  disabled: !enabled,
215
427
  hitSlop: 8,
216
- onPress: () => setMonth(kind === 'previous' ? previous : next),
428
+ onPress: onPress,
217
429
  style: ({
218
430
  pressed
219
431
  }) => [styles.arrow, pressed && styles.arrowPressed],
220
432
  children: /*#__PURE__*/_jsx(Chevron, {
221
433
  direction: pointsLeft ? 'left' : 'right',
222
- color: enabled ? theme.text : theme.disabled
434
+ color: enabled ? theme.arrow ?? theme.text : theme.disabled
223
435
  })
224
436
  });
225
437
  };
438
+ const year = y => formatNumber(y, digits);
439
+
440
+ // The header title, and what pressing it opens.
441
+ let title;
442
+ let subtitle;
443
+ let titleAction;
444
+ if (mode === 'months') {
445
+ title = year(pickerYear);
446
+ subtitle = yearsSecondaryTitle(calendar, pickerYear, pickerYear, digits, adjustment);
447
+ titleAction = {
448
+ label: labels.chooseYear,
449
+ onPress: () => {
450
+ setPageStart(yearPageStart(pickerYear));
451
+ setView('years');
452
+ }
453
+ };
454
+ } else if (mode === 'years') {
455
+ const last = pageStart + YEARS_PER_PAGE - 1;
456
+ title = `${year(pageStart)} – ${year(last)}`;
457
+ subtitle = yearsSecondaryTitle(calendar, pageStart, last, digits, adjustment);
458
+ } else {
459
+ title = monthTitle(shown, locale, digits);
460
+ subtitle = secondaryTitle(shown, locale, digits, adjustment);
461
+ if (showMonthYearPicker) {
462
+ titleAction = {
463
+ label: labels.chooseMonth,
464
+ onPress: () => {
465
+ setPickerYear(shown.year);
466
+ setView('months');
467
+ }
468
+ };
469
+ }
470
+ }
471
+ const titleText = /*#__PURE__*/_jsx(Text, {
472
+ accessibilityRole: "header",
473
+ maxFontSizeMultiplier: headerFontScale,
474
+ style: titleAction ? [styles.title, styles.titleInButton] : styles.title,
475
+ children: title
476
+ });
477
+ let pickerItems;
478
+ if (mode === 'months') {
479
+ const names = getMonthNames(calendar, locale);
480
+ pickerItems = monthsOfYear(calendar, pickerYear, range, adjustment).map(({
481
+ value: m,
482
+ disabled
483
+ }) => {
484
+ const current = todayInCalendar?.year === pickerYear && todayInCalendar.month === m;
485
+ return {
486
+ key: m,
487
+ label: names[m - 1],
488
+ accessibilityLabel: withCurrent(`${names[m - 1]} ${year(pickerYear)}`, current, labels.thisMonth),
489
+ selected: pickerYear === shown.year && m === shown.month,
490
+ current,
491
+ disabled
492
+ };
493
+ });
494
+ } else if (mode === 'years') {
495
+ pickerItems = yearsOnPage(calendar, pageStart, range, adjustment).map(({
496
+ value: y,
497
+ disabled
498
+ }) => {
499
+ const current = todayInCalendar?.year === y;
500
+ return {
501
+ key: y,
502
+ label: year(y),
503
+ accessibilityLabel: withCurrent(year(y), current, labels.thisYear),
504
+ selected: y === pickerYear,
505
+ current,
506
+ disabled
507
+ };
508
+ });
509
+ }
510
+ const pick = key => {
511
+ if (mode === 'months') {
512
+ setMonth({
513
+ calendar,
514
+ year: pickerYear,
515
+ month: key
516
+ });
517
+ setView('days');
518
+ } else {
519
+ setPickerYear(key);
520
+ setView('months');
521
+ }
522
+ };
226
523
  return /*#__PURE__*/_jsxs(View, {
227
524
  style: [styles.container, style],
228
525
  children: [showCalendarToggle && /*#__PURE__*/_jsx(View, {
@@ -232,6 +529,8 @@ export function HilalCalendar({
232
529
  children: ['hijri', 'gregorian'].map(option => {
233
530
  const active = option === calendar;
234
531
  return /*#__PURE__*/_jsx(Pressable, {
532
+ // 45 pt tall to the finger; the toggle itself stays slim.
533
+ hitSlop: TOGGLE_HIT_SLOP,
235
534
  accessibilityRole: "button",
236
535
  accessibilityState: {
237
536
  selected: active
@@ -239,6 +538,7 @@ export function HilalCalendar({
239
538
  onPress: () => toggleCalendar(option),
240
539
  style: [styles.toggleOption, active && styles.toggleOptionActive],
241
540
  children: /*#__PURE__*/_jsx(Text, {
541
+ maxFontSizeMultiplier: headerFontScale,
242
542
  style: [styles.toggleText, active && styles.toggleTextActive],
243
543
  children: labels[option]
244
544
  })
@@ -251,38 +551,96 @@ export function HilalCalendar({
251
551
  }],
252
552
  children: [renderArrow('previous'), /*#__PURE__*/_jsxs(View, {
253
553
  style: styles.titles,
254
- children: [/*#__PURE__*/_jsx(Text, {
255
- accessibilityRole: "header",
256
- style: styles.title,
257
- children: monthTitle(shown, locale, digits)
258
- }), /*#__PURE__*/_jsx(Text, {
554
+ children: [titleAction ? /*#__PURE__*/_jsxs(Pressable, {
555
+ accessibilityRole: "button",
556
+ accessibilityLabel: `${titleAction.label}${labels.separator}${title}`
557
+ // 44 pt tall to the finger, like the arrows.
558
+ ,
559
+ hitSlop: TITLE_HIT_SLOP,
560
+ onPress: titleAction.onPress,
561
+ style: ({
562
+ pressed
563
+ }) => [styles.titleButton, {
564
+ flexDirection: row
565
+ }, pressed && styles.titleButtonPressed],
566
+ children: [/*#__PURE__*/_jsx(View, {
567
+ style: styles.titleTint
568
+ }), titleText]
569
+ }) : mode === 'years' ?
570
+ /*#__PURE__*/
571
+ // As tall as the title button of the other views, so the
572
+ // header doesn't jump when the years open.
573
+ _jsx(View, {
574
+ style: styles.titleStatic,
575
+ children: titleText
576
+ }) : titleText, /*#__PURE__*/_jsx(Text, {
577
+ maxFontSizeMultiplier: headerFontScale,
259
578
  style: styles.subtitle,
260
- children: secondaryTitle(shown, locale, digits)
579
+ children: subtitle
261
580
  })]
262
581
  }), renderArrow('next')]
263
- }), /*#__PURE__*/_jsx(View, {
264
- style: [styles.weekdays, {
265
- flexDirection: row
266
- }],
267
- children: weekdays.map(name => /*#__PURE__*/_jsx(Text, {
268
- style: styles.weekday,
269
- children: name
270
- }, name))
271
- }), cells.map((week, w) => /*#__PURE__*/_jsx(View, {
272
- style: [styles.week, {
273
- flexDirection: row
274
- }],
275
- children: week.map((cell, d) => cell === null ? /*#__PURE__*/_jsx(View, {
276
- style: styles.slot
277
- }, `empty-${d}`) : /*#__PURE__*/_jsx(DayCell, {
278
- ...cell,
279
- selected: cell.epochDay === selectedDay,
280
- today: cell.epochDay === today,
582
+ }), pickerItems ? /*#__PURE__*/_jsx(Appear, {
583
+ animate: !reduceMotion,
584
+ style: daysHeight !== undefined && {
585
+ minHeight: daysHeight
586
+ },
587
+ children: /*#__PURE__*/_jsx(PickerGrid, {
588
+ items: pickerItems,
281
589
  styles: styles,
282
- dayStyle: stableDayStyle,
283
- onSelect: onSelect
284
- }, cell.epochDay))
285
- }, w))]
590
+ row: row,
591
+ fill: daysHeight !== undefined,
592
+ maxFontScale: cellFontScale,
593
+ onSelect: pick
594
+ })
595
+ }, mode) : /*#__PURE__*/_jsxs(Appear, {
596
+ animate: pickerOpened && !reduceMotion,
597
+ onLayout: event => setDaysHeight(event.nativeEvent.layout.height),
598
+ children: [/*#__PURE__*/_jsx(View, {
599
+ style: [styles.weekdays, {
600
+ flexDirection: row
601
+ }],
602
+ children: weekdays.map((name, i) => /*#__PURE__*/_jsx(Text, {
603
+ // Shrinks back to fit its column when the phone's text is
604
+ // large.
605
+ numberOfLines: 1,
606
+ adjustsFontSizeToFit: true,
607
+ maxFontSizeMultiplier: cellFontScale,
608
+ style: styles.weekday,
609
+ children: name
610
+ }, i))
611
+ }), cells.map((week, w) => /*#__PURE__*/_jsx(View, {
612
+ style: [styles.week, {
613
+ flexDirection: row
614
+ }],
615
+ children: week.map((cell, d) => cell === null ? /*#__PURE__*/_jsx(View, {
616
+ style: styles.slot
617
+ }, `empty-${d}`) : /*#__PURE__*/_jsx(DayCell, {
618
+ ...cell,
619
+ accessibilityLabel: dayLabel(cell)
620
+ // After the start, days that would make the range too
621
+ // short or too long can't be pressed.
622
+ ,
623
+ disabled: cell.disabled || rangeMode && outsideRangeLimits(pickedRange, cell.epochDay, rangeLimits),
624
+ selected: rangeMode ? isRangeEnd(rangePosition(pickedRange, cell.epochDay)) : cell.epochDay === selectedDay,
625
+ rangePosition: rangeMode ? rangePosition(pickedRange, cell.epochDay) : null,
626
+ nextSide: row === 'row' ? 'end' : 'start',
627
+ today: cell.epochDay === today,
628
+ styles: dayStyles,
629
+ dayStyle: stableDayStyle,
630
+ maxFontScale: cellFontScale,
631
+ onSelect: onSelect
632
+ }, cell.epochDay))
633
+ }, w))]
634
+ }, "days")]
286
635
  });
287
636
  }
637
+
638
+ /** A day's date in a calendar, or undefined outside the supported range. */
639
+ function safeDateInCalendar(epochDay, calendar, adjustment) {
640
+ try {
641
+ return dateInCalendar(epochDay, calendar, adjustment);
642
+ } catch {
643
+ return undefined;
644
+ }
645
+ }
288
646
  //# sourceMappingURL=HilalCalendar.js.map