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
@@ -1,4 +1,4 @@
1
- /** Colors and shape of the calendar. */
1
+ /** Colors, fonts and shape of the calendar. */
2
2
  export type HilalTheme = {
3
3
  /** Calendar background. */
4
4
  background: string;
@@ -18,6 +18,26 @@ export type HilalTheme = {
18
18
  border: string;
19
19
  /** Corner radius of a day cell. */
20
20
  radius: number;
21
+ /** Month arrows. Default: `text`. */
22
+ arrow?: string;
23
+ /**
24
+ * Font of all text, for example 'Cairo-Regular'. Default: the system
25
+ * font.
26
+ */
27
+ fontFamily?: string;
28
+ /**
29
+ * Font of the bold text (month title, selected day and today, toggle,
30
+ * weekdays, picker), for example 'Cairo-Bold'. When set, it is used as is,
31
+ * without a fontWeight on top. Default: `fontFamily` with a bold weight.
32
+ */
33
+ fontFamilyBold?: string;
34
+ /**
35
+ * Multiplies every font size, for example 1.15 for larger text. Day
36
+ * cells grow with it. Weekday names and the picker's month and year
37
+ * names shrink with it but never grow, so they still fit their columns
38
+ * on small phones. Default 1.
39
+ */
40
+ fontScale?: number;
21
41
  };
22
42
 
23
43
  export type ThemeProp = 'light' | 'dark' | 'auto' | Partial<HilalTheme>;
@@ -29,7 +49,7 @@ export const lightTheme: HilalTheme = {
29
49
  primary: '#0F766E',
30
50
  onPrimary: '#FFFFFF',
31
51
  today: '#0F766E',
32
- disabled: '#C5CAD1',
52
+ disabled: '#9EA5AF',
33
53
  border: '#E3E6EA',
34
54
  radius: 12,
35
55
  };
@@ -41,7 +61,7 @@ export const darkTheme: HilalTheme = {
41
61
  primary: '#2DD4BF',
42
62
  onPrimary: '#042F2C',
43
63
  today: '#2DD4BF',
44
- disabled: '#4A525C',
64
+ disabled: '#56606B',
45
65
  border: '#2A3038',
46
66
  radius: 12,
47
67
  };
@@ -1,4 +1,4 @@
1
- import type { StyleProp, ViewStyle } from 'react-native';
1
+ import type { StyleProp, TextStyle, ViewStyle } from 'react-native';
2
2
 
3
3
  import type { CalendarType, Digits, Locale } from '../core/locale';
4
4
  import type { HijriDate } from '../core/types';
@@ -13,6 +13,80 @@ export type SelectedDate = {
13
13
  formatted: { gregorian: string; hijri: string };
14
14
  };
15
15
 
16
+ type ViewSlot = StyleProp<ViewStyle>;
17
+ type TextSlot = StyleProp<TextStyle>;
18
+
19
+ /**
20
+ * Styles for parts of the calendar, merged over the theme's styles. Each
21
+ * is optional.
22
+ */
23
+ export type HilalStyles = {
24
+ /** The outer container (the same as the `style` prop). */
25
+ container?: ViewSlot;
26
+ /** The Hijri / Gregorian toggle. */
27
+ toggle?: ViewSlot;
28
+ /** Each option of the toggle. */
29
+ toggleOption?: ViewSlot;
30
+ toggleText?: TextSlot;
31
+ /** The row of arrows and titles. */
32
+ header?: ViewSlot;
33
+ /** The month title. */
34
+ title?: TextSlot;
35
+ /** The other calendar's months under the title. */
36
+ subtitle?: TextSlot;
37
+ /** Each arrow button. */
38
+ arrow?: ViewSlot;
39
+ /** Each weekday name. */
40
+ weekdayText?: TextSlot;
41
+ /** Each day cell (the pressable circle). */
42
+ day?: ViewSlot;
43
+ /** The day number. */
44
+ dayText?: TextSlot;
45
+ /** The other calendar's day under the day number. */
46
+ secondaryDayText?: TextSlot;
47
+ /** The selected day cell. */
48
+ selectedDay?: ViewSlot;
49
+ /** The selected day's number. */
50
+ selectedDayText?: TextSlot;
51
+ /** Today's day cell. */
52
+ today?: ViewSlot;
53
+ /** Today's day number, bold by default. */
54
+ todayDayText?: TextSlot;
55
+ /** Both texts of a disabled day. */
56
+ disabledDayText?: TextSlot;
57
+ /** Each month or year in the month and year picker. */
58
+ pickerItem?: ViewSlot;
59
+ pickerText?: TextSlot;
60
+ };
61
+
62
+ /**
63
+ * Texts of the calendar's buttons, read by screen readers too. Each is
64
+ * optional; the rest come from the locale.
65
+ */
66
+ export type HilalLabels = {
67
+ /** The toggle's Hijri option. */
68
+ hijri?: string;
69
+ /** The toggle's Gregorian option. */
70
+ gregorian?: string;
71
+ /** Read by screen readers after today's date. */
72
+ today?: string;
73
+ /** Read after the current month in the month picker. */
74
+ thisMonth?: string;
75
+ /** Read after the current year in the year picker. */
76
+ thisYear?: string;
77
+ /** Arrows, read by screen readers. */
78
+ previousMonth?: string;
79
+ nextMonth?: string;
80
+ previousYear?: string;
81
+ nextYear?: string;
82
+ previousYears?: string;
83
+ nextYears?: string;
84
+ /** The month title button, read by screen readers before the title. */
85
+ chooseMonth?: string;
86
+ /** The year title button in the month picker. */
87
+ chooseYear?: string;
88
+ };
89
+
16
90
  export type HilalCalendarProps = {
17
91
  /** Selected day (controlled). Only the local calendar day is used. */
18
92
  value?: Date;
@@ -41,6 +115,11 @@ export type HilalCalendarProps = {
41
115
  direction?: 'rtl' | 'ltr' | 'auto';
42
116
  /** 0 = Sunday (default), 1 = Monday, 6 = Saturday. */
43
117
  firstDayOfWeek?: 0 | 1 | 6;
118
+ /**
119
+ * Weekday names above the days: 'short' (default, أحد / Sun) or
120
+ * 'narrow' (ح / S). Full names don't fit seven columns on a phone.
121
+ */
122
+ weekdayFormat?: 'short' | 'narrow';
44
123
 
45
124
  /** Earliest selectable day. */
46
125
  minDate?: Date;
@@ -49,13 +128,41 @@ export type HilalCalendarProps = {
49
128
  /** Return true to disable a day. */
50
129
  isDateDisabled?: (date: SelectedDate) => boolean;
51
130
 
131
+ /**
132
+ * Shift the Hijri calendar by whole days, from -2 to 2, for places where
133
+ * the month started before or after Umm al-Qura. `1` makes every Hijri
134
+ * date one day later. Gregorian dates never change. Default 0.
135
+ */
136
+ hijriAdjustment?: number;
137
+ /**
138
+ * Pressing the month title opens a grid of months, and from there a
139
+ * grid of years, to jump far without paging month by month. Default
140
+ * true.
141
+ */
142
+ showMonthYearPicker?: boolean;
143
+
52
144
  /**
53
145
  * 'light', 'dark', 'auto' (default, follows the system color scheme),
54
146
  * or a partial theme merged over the 'auto' theme.
55
147
  */
56
148
  theme?: ThemeProp;
149
+ /** Texts of the toggle and buttons, see `HilalLabels`. */
150
+ labels?: HilalLabels;
151
+ /** Styles for parts of the calendar, see `HilalStyles`. */
152
+ styles?: HilalStyles;
57
153
  /** Style of the outer container. */
58
154
  style?: StyleProp<ViewStyle>;
59
- /** Style of every day cell (the pressable circle). */
155
+ /**
156
+ * Style of every day cell (the pressable circle). Unlike `styles.day`,
157
+ * it also wins over the selected and today styles.
158
+ */
60
159
  dayStyle?: StyleProp<ViewStyle>;
160
+ /**
161
+ * How far the phone's text size setting may grow the calendar's text,
162
+ * like React Native's prop of the same name: `1` turns it off, `0`
163
+ * removes the limit. By default, text in day cells, weekday names and
164
+ * picker buttons grows up to 1.3 times (as far as it fits a cell), and
165
+ * the header and toggle up to 2 times.
166
+ */
167
+ maxFontSizeMultiplier?: number;
61
168
  };
@@ -29,10 +29,50 @@ const MONTH_STARTS: readonly number[] = (() => {
29
29
  return starts;
30
30
  })();
31
31
 
32
- /** First and last epoch day of the supported range (both inclusive). */
32
+ /**
33
+ * First and last epoch day of the Umm al-Qura table (both inclusive). With
34
+ * a Hijri adjustment of `n` days, the supported Gregorian days move by
35
+ * `-n`: see `supportedEpochDays`.
36
+ */
33
37
  export const FIRST_DAY = MONTH_STARTS[0]!;
34
38
  export const LAST_DAY = MONTH_STARTS[MONTH_STARTS.length - 1]! - 1;
35
39
 
40
+ /** The largest Hijri adjustment accepted, in days, in either direction. */
41
+ export const MAX_HIJRI_ADJUSTMENT = 2;
42
+
43
+ export type HijriConversionOptions = {
44
+ /**
45
+ * Days to shift the Hijri calendar by, from -2 to 2. `1` makes every
46
+ * Hijri date one day later than Umm al-Qura (Umm al-Qura's 29 Sha'ban
47
+ * becomes 30 Sha'ban), for places where the month started a day
48
+ * earlier. Default 0.
49
+ */
50
+ adjustment?: number;
51
+ };
52
+
53
+ /**
54
+ * Checks a Hijri adjustment and returns it.
55
+ *
56
+ * @throws {RangeError} unless it is an integer from -2 to 2.
57
+ */
58
+ export function checkAdjustment(adjustment: number, caller: string) {
59
+ if (
60
+ !Number.isInteger(adjustment) ||
61
+ Math.abs(adjustment) > MAX_HIJRI_ADJUSTMENT
62
+ ) {
63
+ throw new RangeError(
64
+ `${caller}: the Hijri adjustment must be an integer from ` +
65
+ `-${MAX_HIJRI_ADJUSTMENT} to ${MAX_HIJRI_ADJUSTMENT}, got ${adjustment}`
66
+ );
67
+ }
68
+ return adjustment;
69
+ }
70
+
71
+ /** First and last epoch day that have a Hijri date, for an adjustment. */
72
+ export function supportedEpochDays(adjustment = 0) {
73
+ return { min: FIRST_DAY - adjustment, max: LAST_DAY - adjustment };
74
+ }
75
+
36
76
  function monthIndex(year: number, month: number) {
37
77
  return (year - UMALQURA_MIN_YEAR) * 12 + (month - 1);
38
78
  }
@@ -42,12 +82,13 @@ function formatIso({ year, month, day }: HijriDate) {
42
82
  return `${year}-${pad(month)}-${pad(day)}`;
43
83
  }
44
84
 
45
- function supportedRangeText() {
85
+ function supportedRangeText(adjustment: number) {
46
86
  const { min, max } = getSupportedRange();
87
+ const days = supportedEpochDays(adjustment);
47
88
  return (
48
89
  `${formatIso(min)} to ${formatIso(max)} AH ` +
49
- `(${formatIso(civilFromDays(FIRST_DAY))} to ` +
50
- `${formatIso(civilFromDays(LAST_DAY))})`
90
+ `(${formatIso(civilFromDays(days.min))} to ` +
91
+ `${formatIso(civilFromDays(days.max))})`
51
92
  );
52
93
  }
53
94
 
@@ -109,20 +150,25 @@ export function isValidHijri(hijri: HijriDate): boolean {
109
150
  }
110
151
 
111
152
  /**
112
- * Hijri date of an epoch day. Internal: the public entry point is
113
- * `toHijri`.
153
+ * Hijri date of an epoch day, shifted by `adjustment` days. Internal: the
154
+ * public entry point is `toHijri`.
114
155
  *
115
156
  * @throws {RangeError} if the day is outside the supported range.
116
157
  */
117
- export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
158
+ export function hijriOfEpochDay(
159
+ epochDay: number,
160
+ caller = 'toHijri',
161
+ adjustment = 0
162
+ ): HijriDate {
163
+ const tableDay = epochDay + adjustment;
118
164
  if (
119
165
  !Number.isInteger(epochDay) ||
120
- epochDay < FIRST_DAY ||
121
- epochDay > LAST_DAY
166
+ tableDay < FIRST_DAY ||
167
+ tableDay > LAST_DAY
122
168
  ) {
123
169
  throw new RangeError(
124
170
  `${caller}: ${formatIso(civilFromDays(epochDay))} is outside the ` +
125
- `supported range ${supportedRangeText()}`
171
+ `supported range ${supportedRangeText(adjustment)}`
126
172
  );
127
173
  }
128
174
 
@@ -131,7 +177,7 @@ export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
131
177
  let high = MONTH_STARTS.length - 2;
132
178
  while (low < high) {
133
179
  const mid = (low + high + 1) >> 1; // eslint-disable-line no-bitwise
134
- if (MONTH_STARTS[mid]! <= epochDay) {
180
+ if (MONTH_STARTS[mid]! <= tableDay) {
135
181
  low = mid;
136
182
  } else {
137
183
  high = mid - 1;
@@ -141,18 +187,22 @@ export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
141
187
  return {
142
188
  year: UMALQURA_MIN_YEAR + Math.floor(low / 12),
143
189
  month: (low % 12) + 1,
144
- day: epochDay - MONTH_STARTS[low]! + 1,
190
+ day: tableDay - MONTH_STARTS[low]! + 1,
145
191
  };
146
192
  }
147
193
 
148
194
  /**
149
- * Epoch day of a Hijri date. Internal: the public entry point is
150
- * `toGregorian`.
195
+ * Epoch day of a Hijri date, shifted by `adjustment` days. Internal: the
196
+ * public entry point is `toGregorian`.
151
197
  *
152
198
  * @throws {RangeError} if `hijri` is not a valid date in the supported
153
199
  * range.
154
200
  */
155
- export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
201
+ export function epochDayOfHijri(
202
+ hijri: HijriDate,
203
+ caller = 'toGregorian',
204
+ adjustment = 0
205
+ ) {
156
206
  if (!isValidHijri(hijri)) {
157
207
  const text =
158
208
  typeof hijri === 'object' && hijri !== null
@@ -160,10 +210,15 @@ export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
160
210
  : String(hijri);
161
211
  throw new RangeError(
162
212
  `${caller}: ${text} is not a valid Hijri date in the supported ` +
163
- `range ${supportedRangeText()}`
213
+ `range ${supportedRangeText(adjustment)}`
164
214
  );
165
215
  }
166
- return MONTH_STARTS[monthIndex(hijri.year, hijri.month)]! + hijri.day - 1;
216
+ return (
217
+ MONTH_STARTS[monthIndex(hijri.year, hijri.month)]! +
218
+ hijri.day -
219
+ 1 -
220
+ adjustment
221
+ );
167
222
  }
168
223
 
169
224
  /**
@@ -171,13 +226,18 @@ export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
171
226
  * day and the time zone offset are ignored: 23:59 and 00:00 on the same
172
227
  * local day give the same result.
173
228
  *
174
- * @throws {RangeError} if `date` is invalid or outside the supported range.
229
+ * @throws {RangeError} if `date` is invalid or outside the supported
230
+ * range, or the adjustment is not an integer from -2 to 2.
175
231
  */
176
- export function toHijri(date: Date): HijriDate {
232
+ export function toHijri(
233
+ date: Date,
234
+ options: HijriConversionOptions = {}
235
+ ): HijriDate {
236
+ const adjustment = checkAdjustment(options.adjustment ?? 0, 'toHijri');
177
237
  if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
178
238
  throw new RangeError('toHijri: expected a valid Date');
179
239
  }
180
- return hijriOfEpochDay(epochDayOfLocalDate(date));
240
+ return hijriOfEpochDay(epochDayOfLocalDate(date), 'toHijri', adjustment);
181
241
  }
182
242
 
183
243
  /**
@@ -185,8 +245,12 @@ export function toHijri(date: Date): HijriDate {
185
245
  * Gregorian day.
186
246
  *
187
247
  * @throws {RangeError} if `hijri` is not a valid date in the supported
188
- * range.
248
+ * range, or the adjustment is not an integer from -2 to 2.
189
249
  */
190
- export function toGregorian(hijri: HijriDate): Date {
191
- return localDateOfEpochDay(epochDayOfHijri(hijri));
250
+ export function toGregorian(
251
+ hijri: HijriDate,
252
+ options: HijriConversionOptions = {}
253
+ ): Date {
254
+ const adjustment = checkAdjustment(options.adjustment ?? 0, 'toGregorian');
255
+ return localDateOfEpochDay(epochDayOfHijri(hijri, 'toGregorian', adjustment));
192
256
  }
package/src/core/index.ts CHANGED
@@ -4,6 +4,7 @@ export {
4
4
  isValidHijri,
5
5
  toGregorian,
6
6
  toHijri,
7
+ type HijriConversionOptions,
7
8
  } from './convert';
8
9
  export { formatHijri, type FormatHijriOptions } from './format';
9
10
  export {
@@ -1,7 +1,7 @@
1
1
  export type Locale = 'ar' | 'en';
2
2
  export type Digits = 'arabic' | 'latin';
3
3
  export type CalendarType = 'hijri' | 'gregorian';
4
- export type WeekdayFormat = 'short' | 'long';
4
+ export type WeekdayFormat = 'short' | 'long' | 'narrow';
5
5
 
6
6
  const HIJRI_MONTHS: Record<Locale, readonly string[]> = {
7
7
  ar: [
@@ -80,6 +80,8 @@ const WEEKDAYS: Record<Locale, Record<WeekdayFormat, readonly string[]>> = {
80
80
  'السبت',
81
81
  ],
82
82
  short: ['أحد', 'اثنين', 'ثلاثاء', 'أربعاء', 'خميس', 'جمعة', 'سبت'],
83
+ // The same letters as ICU's narrow format.
84
+ narrow: ['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س'],
83
85
  },
84
86
  en: {
85
87
  long: [
@@ -92,6 +94,7 @@ const WEEKDAYS: Record<Locale, Record<WeekdayFormat, readonly string[]>> = {
92
94
  'Saturday',
93
95
  ],
94
96
  short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
97
+ narrow: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
95
98
  },
96
99
  };
97
100