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.
- package/README.md +119 -40
- package/lib/module/calendar/Appear.js +75 -0
- package/lib/module/calendar/Appear.js.map +1 -0
- package/lib/module/calendar/DayCell.js +4 -1
- package/lib/module/calendar/DayCell.js.map +1 -1
- package/lib/module/calendar/HilalCalendar.js +341 -64
- package/lib/module/calendar/HilalCalendar.js.map +1 -1
- package/lib/module/calendar/PickerGrid.js +66 -0
- package/lib/module/calendar/PickerGrid.js.map +1 -0
- package/lib/module/calendar/month.js +135 -22
- package/lib/module/calendar/month.js.map +1 -1
- package/lib/module/calendar/styles.js +219 -6
- package/lib/module/calendar/styles.js.map +1 -1
- package/lib/module/calendar/theme.js +3 -3
- package/lib/module/calendar/theme.js.map +1 -1
- package/lib/module/core/convert.js +52 -21
- package/lib/module/core/convert.js.map +1 -1
- package/lib/module/core/index.js.map +1 -1
- package/lib/module/core/locale.js +5 -2
- package/lib/module/core/locale.js.map +1 -1
- package/lib/typescript/src/calendar/Appear.d.ts +22 -0
- package/lib/typescript/src/calendar/Appear.d.ts.map +1 -0
- package/lib/typescript/src/calendar/DayCell.d.ts +3 -1
- package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -1
- package/lib/typescript/src/calendar/HilalCalendar.d.ts +1 -1
- package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -1
- package/lib/typescript/src/calendar/PickerGrid.d.ts +28 -0
- package/lib/typescript/src/calendar/PickerGrid.d.ts.map +1 -0
- package/lib/typescript/src/calendar/index.d.ts +1 -1
- package/lib/typescript/src/calendar/index.d.ts.map +1 -1
- package/lib/typescript/src/calendar/month.d.ts +30 -7
- package/lib/typescript/src/calendar/month.d.ts.map +1 -1
- package/lib/typescript/src/calendar/styles.d.ts +200 -113
- package/lib/typescript/src/calendar/styles.d.ts.map +1 -1
- package/lib/typescript/src/calendar/theme.d.ts +21 -1
- package/lib/typescript/src/calendar/theme.d.ts.map +1 -1
- package/lib/typescript/src/calendar/types.d.ts +106 -2
- package/lib/typescript/src/calendar/types.d.ts.map +1 -1
- package/lib/typescript/src/core/convert.d.ts +38 -15
- package/lib/typescript/src/core/convert.d.ts.map +1 -1
- package/lib/typescript/src/core/index.d.ts +1 -1
- package/lib/typescript/src/core/index.d.ts.map +1 -1
- package/lib/typescript/src/core/locale.d.ts +1 -1
- package/lib/typescript/src/core/locale.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/calendar/Appear.tsx +94 -0
- package/src/calendar/DayCell.tsx +6 -0
- package/src/calendar/HilalCalendar.tsx +406 -77
- package/src/calendar/PickerGrid.tsx +99 -0
- package/src/calendar/index.ts +6 -1
- package/src/calendar/month.ts +174 -23
- package/src/calendar/styles.ts +235 -5
- package/src/calendar/theme.ts +23 -3
- package/src/calendar/types.ts +109 -2
- package/src/core/convert.ts +87 -23
- package/src/core/index.ts +1 -0
- package/src/core/locale.ts +4 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Pressable, Text, View } from 'react-native';
|
|
2
|
+
|
|
3
|
+
import type { CalendarStyles } from './styles';
|
|
4
|
+
|
|
5
|
+
export type PickerGridItem = {
|
|
6
|
+
key: number;
|
|
7
|
+
label: string;
|
|
8
|
+
accessibilityLabel: string;
|
|
9
|
+
selected: boolean;
|
|
10
|
+
/** The current month or year (today's), drawn with a ring. */
|
|
11
|
+
current: boolean;
|
|
12
|
+
disabled: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type PickerGridProps = {
|
|
16
|
+
items: PickerGridItem[];
|
|
17
|
+
styles: CalendarStyles;
|
|
18
|
+
/** 'row' or 'row-reverse', the same as the day grid. */
|
|
19
|
+
row: 'row' | 'row-reverse';
|
|
20
|
+
/** Grow the rows to fill the parent's height. */
|
|
21
|
+
fill: boolean;
|
|
22
|
+
/** maxFontSizeMultiplier of the texts. */
|
|
23
|
+
maxFontScale: number;
|
|
24
|
+
onSelect: (key: number) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const COLUMNS = 3;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Lets names wrap only at spaces: "Rabi' al-Awwal" breaks into "Rabi'" and
|
|
31
|
+
* "al‑Awwal", never after the hyphen. U+2011 looks like a hyphen.
|
|
32
|
+
*/
|
|
33
|
+
function noBreakAtHyphens(label: string) {
|
|
34
|
+
return label.replace(/-/g, '\u2011');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The month or year picker: items in rows of three, each a button. Only
|
|
39
|
+
* rendered while the picker is open.
|
|
40
|
+
*/
|
|
41
|
+
export function PickerGrid({
|
|
42
|
+
items,
|
|
43
|
+
styles,
|
|
44
|
+
row,
|
|
45
|
+
fill,
|
|
46
|
+
maxFontScale,
|
|
47
|
+
onSelect,
|
|
48
|
+
}: PickerGridProps) {
|
|
49
|
+
const rows: PickerGridItem[][] = [];
|
|
50
|
+
for (let i = 0; i < items.length; i += COLUMNS) {
|
|
51
|
+
rows.push(items.slice(i, i + COLUMNS));
|
|
52
|
+
}
|
|
53
|
+
return (
|
|
54
|
+
<View style={[styles.picker, fill && styles.grow]}>
|
|
55
|
+
{rows.map((rowItems, r) => (
|
|
56
|
+
<View key={r} style={[{ flexDirection: row }, fill && styles.grow]}>
|
|
57
|
+
{rowItems.map((item) => (
|
|
58
|
+
<View key={item.key} style={styles.pickerSlot}>
|
|
59
|
+
<Pressable
|
|
60
|
+
accessibilityRole="button"
|
|
61
|
+
accessibilityLabel={item.accessibilityLabel}
|
|
62
|
+
accessibilityState={{
|
|
63
|
+
selected: item.selected,
|
|
64
|
+
disabled: item.disabled,
|
|
65
|
+
}}
|
|
66
|
+
disabled={item.disabled}
|
|
67
|
+
onPress={() => onSelect(item.key)}
|
|
68
|
+
style={({ pressed }) => [
|
|
69
|
+
styles.pickerItem,
|
|
70
|
+
fill && styles.grow,
|
|
71
|
+
item.current && styles.today,
|
|
72
|
+
item.selected && styles.selected,
|
|
73
|
+
pressed && !item.selected && styles.pressed,
|
|
74
|
+
]}
|
|
75
|
+
>
|
|
76
|
+
{!item.selected && <View style={styles.pickerItemTint} />}
|
|
77
|
+
{/* Long names (جمادى الآخرة, Jumada al-Akhirah) wrap to a
|
|
78
|
+
second line on narrow phones instead of being cut. */}
|
|
79
|
+
<Text
|
|
80
|
+
numberOfLines={2}
|
|
81
|
+
adjustsFontSizeToFit
|
|
82
|
+
minimumFontScale={0.8}
|
|
83
|
+
maxFontSizeMultiplier={maxFontScale}
|
|
84
|
+
style={[
|
|
85
|
+
styles.pickerText,
|
|
86
|
+
item.disabled && styles.disabledText,
|
|
87
|
+
item.selected && styles.selectedText,
|
|
88
|
+
]}
|
|
89
|
+
>
|
|
90
|
+
{noBreakAtHyphens(item.label)}
|
|
91
|
+
</Text>
|
|
92
|
+
</Pressable>
|
|
93
|
+
</View>
|
|
94
|
+
))}
|
|
95
|
+
</View>
|
|
96
|
+
))}
|
|
97
|
+
</View>
|
|
98
|
+
);
|
|
99
|
+
}
|
package/src/calendar/index.ts
CHANGED
package/src/calendar/month.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
getHijriMonthLength,
|
|
10
10
|
hijriOfEpochDay,
|
|
11
11
|
LAST_DAY,
|
|
12
|
+
supportedEpochDays,
|
|
12
13
|
} from '../core/convert';
|
|
13
14
|
import { civilFromDays, daysFromCivil, weekdayOfEpochDay } from '../core/days';
|
|
14
15
|
import {
|
|
@@ -47,14 +48,23 @@ export type DayRange = { min: number; max: number };
|
|
|
47
48
|
/** The days the calendar can ever show: the supported Hijri range. */
|
|
48
49
|
export const SUPPORTED_DAYS: DayRange = { min: FIRST_DAY, max: LAST_DAY };
|
|
49
50
|
|
|
51
|
+
/*
|
|
52
|
+
* `adjustment` below is the Hijri adjustment in days (see
|
|
53
|
+
* HijriConversionOptions). It moves which Gregorian days carry which
|
|
54
|
+
* Hijri date, and so the supported days; Gregorian dates never change.
|
|
55
|
+
*/
|
|
56
|
+
|
|
50
57
|
export function otherCalendar(calendar: CalendarType): CalendarType {
|
|
51
58
|
return calendar === 'hijri' ? 'gregorian' : 'hijri';
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
/** Epoch day of the first day of a month. */
|
|
55
|
-
export function monthStart(
|
|
62
|
+
export function monthStart(
|
|
63
|
+
{ calendar, year, month }: CalendarMonth,
|
|
64
|
+
adjustment = 0
|
|
65
|
+
) {
|
|
56
66
|
return calendar === 'hijri'
|
|
57
|
-
? epochDayOfHijri({ year, month, day: 1 }, 'monthStart')
|
|
67
|
+
? epochDayOfHijri({ year, month, day: 1 }, 'monthStart', adjustment)
|
|
58
68
|
: daysFromCivil(year, month, 1);
|
|
59
69
|
}
|
|
60
70
|
|
|
@@ -68,9 +78,13 @@ export function monthLength(m: CalendarMonth) {
|
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
/** Year, month and day of an epoch day in either calendar. */
|
|
71
|
-
export function dateInCalendar(
|
|
81
|
+
export function dateInCalendar(
|
|
82
|
+
epochDay: number,
|
|
83
|
+
calendar: CalendarType,
|
|
84
|
+
adjustment = 0
|
|
85
|
+
) {
|
|
72
86
|
return calendar === 'hijri'
|
|
73
|
-
? hijriOfEpochDay(epochDay)
|
|
87
|
+
? hijriOfEpochDay(epochDay, 'dateInCalendar', adjustment)
|
|
74
88
|
: civilFromDays(epochDay);
|
|
75
89
|
}
|
|
76
90
|
|
|
@@ -80,11 +94,13 @@ export function dateInCalendar(epochDay: number, calendar: CalendarType) {
|
|
|
80
94
|
*/
|
|
81
95
|
export function monthContaining(
|
|
82
96
|
epochDay: number,
|
|
83
|
-
calendar: CalendarType
|
|
97
|
+
calendar: CalendarType,
|
|
98
|
+
adjustment = 0
|
|
84
99
|
): CalendarMonth {
|
|
85
100
|
const { year, month } = dateInCalendar(
|
|
86
|
-
clampDay(epochDay,
|
|
87
|
-
calendar
|
|
101
|
+
clampDay(epochDay, supportedEpochDays(adjustment)),
|
|
102
|
+
calendar,
|
|
103
|
+
adjustment
|
|
88
104
|
);
|
|
89
105
|
return { calendar, year, month };
|
|
90
106
|
}
|
|
@@ -108,16 +124,22 @@ export function clampDay(epochDay: number, range: DayRange) {
|
|
|
108
124
|
*/
|
|
109
125
|
export function selectableRange(
|
|
110
126
|
minDay: number | undefined,
|
|
111
|
-
maxDay: number | undefined
|
|
127
|
+
maxDay: number | undefined,
|
|
128
|
+
adjustment = 0
|
|
112
129
|
): DayRange {
|
|
130
|
+
const supported = supportedEpochDays(adjustment);
|
|
113
131
|
return {
|
|
114
|
-
min: Math.max(minDay ??
|
|
115
|
-
max: Math.min(maxDay ??
|
|
132
|
+
min: Math.max(minDay ?? supported.min, supported.min),
|
|
133
|
+
max: Math.min(maxDay ?? supported.max, supported.max),
|
|
116
134
|
};
|
|
117
135
|
}
|
|
118
136
|
|
|
119
137
|
/** Whether any day of month `m` falls inside `range`. */
|
|
120
|
-
export function monthOverlaps(
|
|
138
|
+
export function monthOverlaps(
|
|
139
|
+
m: CalendarMonth,
|
|
140
|
+
range: DayRange,
|
|
141
|
+
adjustment = 0
|
|
142
|
+
) {
|
|
121
143
|
if (m.calendar === 'hijri') {
|
|
122
144
|
// Hijri months outside the table have no start day at all.
|
|
123
145
|
const { min, max } = monthRangeOfSupportedHijri();
|
|
@@ -126,7 +148,7 @@ export function monthOverlaps(m: CalendarMonth, range: DayRange) {
|
|
|
126
148
|
return false;
|
|
127
149
|
}
|
|
128
150
|
}
|
|
129
|
-
const start = monthStart(m);
|
|
151
|
+
const start = monthStart(m, adjustment);
|
|
130
152
|
const end = start + monthLength(m) - 1;
|
|
131
153
|
return start <= range.max && end >= range.min;
|
|
132
154
|
}
|
|
@@ -146,9 +168,11 @@ function monthRangeOfSupportedHijri() {
|
|
|
146
168
|
*/
|
|
147
169
|
export function buildMonthGrid(
|
|
148
170
|
m: CalendarMonth,
|
|
149
|
-
firstDayOfWeek: number
|
|
171
|
+
firstDayOfWeek: number,
|
|
172
|
+
adjustment = 0
|
|
150
173
|
): MonthGrid {
|
|
151
|
-
const start = monthStart(m);
|
|
174
|
+
const start = monthStart(m, adjustment);
|
|
175
|
+
const supported = supportedEpochDays(adjustment);
|
|
152
176
|
const length = monthLength(m);
|
|
153
177
|
const secondary = otherCalendar(m.calendar);
|
|
154
178
|
const leading = (weekdayOfEpochDay(start) - firstDayOfWeek + 7) % 7;
|
|
@@ -156,12 +180,13 @@ export function buildMonthGrid(
|
|
|
156
180
|
const slots: Array<DayCell | null> = Array(leading).fill(null);
|
|
157
181
|
for (let day = 1; day <= length; day++) {
|
|
158
182
|
const epochDay = start + day - 1;
|
|
159
|
-
const inRange =
|
|
160
|
-
epochDay >= SUPPORTED_DAYS.min && epochDay <= SUPPORTED_DAYS.max;
|
|
183
|
+
const inRange = epochDay >= supported.min && epochDay <= supported.max;
|
|
161
184
|
slots.push({
|
|
162
185
|
epochDay,
|
|
163
186
|
day,
|
|
164
|
-
secondaryDay: inRange
|
|
187
|
+
secondaryDay: inRange
|
|
188
|
+
? dateInCalendar(epochDay, secondary, adjustment).day
|
|
189
|
+
: null,
|
|
165
190
|
});
|
|
166
191
|
}
|
|
167
192
|
while (slots.length % 7 !== 0) {
|
|
@@ -190,14 +215,16 @@ export function monthTitle(m: CalendarMonth, locale: Locale, digits: Digits) {
|
|
|
190
215
|
export function secondaryTitle(
|
|
191
216
|
m: CalendarMonth,
|
|
192
217
|
locale: Locale,
|
|
193
|
-
digits: Digits
|
|
218
|
+
digits: Digits,
|
|
219
|
+
adjustment = 0
|
|
194
220
|
) {
|
|
195
|
-
const start = monthStart(m);
|
|
196
|
-
const
|
|
197
|
-
const
|
|
221
|
+
const start = monthStart(m, adjustment);
|
|
222
|
+
const supported = supportedEpochDays(adjustment);
|
|
223
|
+
const first = clampDay(start, supported);
|
|
224
|
+
const last = clampDay(start + monthLength(m) - 1, supported);
|
|
198
225
|
const calendar = otherCalendar(m.calendar);
|
|
199
|
-
const from = dateInCalendar(first, calendar);
|
|
200
|
-
const to = dateInCalendar(last, calendar);
|
|
226
|
+
const from = dateInCalendar(first, calendar, adjustment);
|
|
227
|
+
const to = dateInCalendar(last, calendar, adjustment);
|
|
201
228
|
const names = getMonthNames(calendar, locale);
|
|
202
229
|
const fromName = names[from.month - 1];
|
|
203
230
|
const toName = names[to.month - 1];
|
|
@@ -211,3 +238,127 @@ export function secondaryTitle(
|
|
|
211
238
|
}
|
|
212
239
|
return `${fromName} ${year(from.year)} – ${toName} ${year(to.year)}`;
|
|
213
240
|
}
|
|
241
|
+
|
|
242
|
+
/*
|
|
243
|
+
* Month and year picker. Opened from the header: a grid of the 12 months
|
|
244
|
+
* of a year, and a grid of years, YEARS_PER_PAGE at a time.
|
|
245
|
+
*/
|
|
246
|
+
|
|
247
|
+
/** Years shown on one page of the year picker. */
|
|
248
|
+
export const YEARS_PER_PAGE = 12;
|
|
249
|
+
|
|
250
|
+
/** A month (1-12) or a year in the picker, and whether it can be picked. */
|
|
251
|
+
export type PickerItem = { value: number; disabled: boolean };
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* First and last epoch day of years `fromYear` to `toYear` of a calendar,
|
|
255
|
+
* limited to the supported range, or null when none of it is supported.
|
|
256
|
+
*/
|
|
257
|
+
function yearsSpan(
|
|
258
|
+
calendar: CalendarType,
|
|
259
|
+
fromYear: number,
|
|
260
|
+
toYear: number,
|
|
261
|
+
adjustment: number
|
|
262
|
+
): DayRange | null {
|
|
263
|
+
const supported = supportedEpochDays(adjustment);
|
|
264
|
+
let start: number;
|
|
265
|
+
let end: number;
|
|
266
|
+
if (calendar === 'hijri') {
|
|
267
|
+
const first = Math.max(fromYear, hijriOfEpochDay(FIRST_DAY).year);
|
|
268
|
+
const last = Math.min(toYear, hijriOfEpochDay(LAST_DAY).year);
|
|
269
|
+
if (first > last) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
const lastMonth: CalendarMonth = { calendar, year: last, month: 12 };
|
|
273
|
+
start = monthStart({ calendar, year: first, month: 1 }, adjustment);
|
|
274
|
+
end = monthStart(lastMonth, adjustment) + monthLength(lastMonth) - 1;
|
|
275
|
+
} else {
|
|
276
|
+
start = daysFromCivil(fromYear, 1, 1);
|
|
277
|
+
end = daysFromCivil(toYear + 1, 1, 1) - 1;
|
|
278
|
+
}
|
|
279
|
+
const min = Math.max(start, supported.min);
|
|
280
|
+
const max = Math.min(end, supported.max);
|
|
281
|
+
return min <= max ? { min, max } : null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Whether any day of `year` falls inside `range`. */
|
|
285
|
+
export function yearOverlaps(
|
|
286
|
+
calendar: CalendarType,
|
|
287
|
+
year: number,
|
|
288
|
+
range: DayRange,
|
|
289
|
+
adjustment = 0
|
|
290
|
+
) {
|
|
291
|
+
const span = yearsSpan(calendar, year, year, adjustment);
|
|
292
|
+
return span !== null && span.min <= range.max && span.max >= range.min;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The 12 months of `year`, disabled when none of their days is in `range`. */
|
|
296
|
+
export function monthsOfYear(
|
|
297
|
+
calendar: CalendarType,
|
|
298
|
+
year: number,
|
|
299
|
+
range: DayRange,
|
|
300
|
+
adjustment = 0
|
|
301
|
+
): PickerItem[] {
|
|
302
|
+
return Array.from({ length: 12 }, (_, i) => ({
|
|
303
|
+
value: i + 1,
|
|
304
|
+
disabled: !monthOverlaps(
|
|
305
|
+
{ calendar, year, month: i + 1 },
|
|
306
|
+
range,
|
|
307
|
+
adjustment
|
|
308
|
+
),
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** First year of the year-picker page that contains `year`. */
|
|
313
|
+
export function yearPageStart(year: number) {
|
|
314
|
+
return Math.floor(year / YEARS_PER_PAGE) * YEARS_PER_PAGE;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The years of the page starting at `pageStart`. */
|
|
318
|
+
export function yearsOnPage(
|
|
319
|
+
calendar: CalendarType,
|
|
320
|
+
pageStart: number,
|
|
321
|
+
range: DayRange,
|
|
322
|
+
adjustment = 0
|
|
323
|
+
): PickerItem[] {
|
|
324
|
+
return Array.from({ length: YEARS_PER_PAGE }, (_, i) => ({
|
|
325
|
+
value: pageStart + i,
|
|
326
|
+
disabled: !yearOverlaps(calendar, pageStart + i, range, adjustment),
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Whether the page starting at `pageStart` has any year that can be picked. */
|
|
331
|
+
export function pageOverlaps(
|
|
332
|
+
calendar: CalendarType,
|
|
333
|
+
pageStart: number,
|
|
334
|
+
range: DayRange,
|
|
335
|
+
adjustment = 0
|
|
336
|
+
) {
|
|
337
|
+
return yearsOnPage(calendar, pageStart, range, adjustment).some(
|
|
338
|
+
(year) => !year.disabled
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* The years of the other calendar that years `fromYear` to `toYear`
|
|
344
|
+
* overlap, for example "٢٠٢٣ – ٢٠٢٤" for 1445 AH, or a single year. Only
|
|
345
|
+
* days in the supported range count.
|
|
346
|
+
*/
|
|
347
|
+
export function yearsSecondaryTitle(
|
|
348
|
+
calendar: CalendarType,
|
|
349
|
+
fromYear: number,
|
|
350
|
+
toYear: number,
|
|
351
|
+
digits: Digits,
|
|
352
|
+
adjustment = 0
|
|
353
|
+
) {
|
|
354
|
+
const span = yearsSpan(calendar, fromYear, toYear, adjustment);
|
|
355
|
+
if (span === null) {
|
|
356
|
+
return '';
|
|
357
|
+
}
|
|
358
|
+
const other = otherCalendar(calendar);
|
|
359
|
+
const from = dateInCalendar(span.min, other, adjustment).year;
|
|
360
|
+
const to = dateInCalendar(span.max, other, adjustment).year;
|
|
361
|
+
return from === to
|
|
362
|
+
? formatNumber(from, digits)
|
|
363
|
+
: `${formatNumber(from, digits)} – ${formatNumber(to, digits)}`;
|
|
364
|
+
}
|
package/src/calendar/styles.ts
CHANGED
|
@@ -1,11 +1,171 @@
|
|
|
1
1
|
import { StyleSheet } from 'react-native';
|
|
2
2
|
|
|
3
3
|
import type { HilalTheme } from './theme';
|
|
4
|
+
import type { HilalStyles } from './types';
|
|
5
|
+
|
|
6
|
+
/** Fills the parent; left/right are both 0, so RTL mirroring can't move it. */
|
|
7
|
+
const fill = {
|
|
8
|
+
bottom: 0,
|
|
9
|
+
left: 0,
|
|
10
|
+
position: 'absolute',
|
|
11
|
+
right: 0,
|
|
12
|
+
top: 0,
|
|
13
|
+
} as const;
|
|
4
14
|
|
|
5
15
|
export type CalendarStyles = ReturnType<typeof createStyles>;
|
|
6
16
|
|
|
17
|
+
/**
|
|
18
|
+
* How far the phone's text size setting may grow the text of day cells,
|
|
19
|
+
* weekday names and picker buttons, which have a fixed size: up to 1.3 a
|
|
20
|
+
* day number and the date under it still fit a 48 pt cell.
|
|
21
|
+
*/
|
|
22
|
+
export const MAX_CELL_FONT_SCALE = 1.3;
|
|
23
|
+
/** The same for the header and the toggle, which have room to grow. */
|
|
24
|
+
export const MAX_HEADER_FONT_SCALE = 2;
|
|
25
|
+
|
|
26
|
+
/** The text styles, which take the theme's fonts and font scale. */
|
|
27
|
+
const TEXT_STYLES = [
|
|
28
|
+
'toggleText',
|
|
29
|
+
'title',
|
|
30
|
+
'subtitle',
|
|
31
|
+
'weekday',
|
|
32
|
+
'dayText',
|
|
33
|
+
'secondaryText',
|
|
34
|
+
'pickerText',
|
|
35
|
+
// Only a weight, for the bold font.
|
|
36
|
+
'selectedText',
|
|
37
|
+
'todayText',
|
|
38
|
+
] as const;
|
|
39
|
+
|
|
40
|
+
/** Text in columns of a fixed width: shrinks with the font scale only. */
|
|
41
|
+
const FIXED_WIDTH_TEXT = new Set<string>(['weekday', 'pickerText']);
|
|
42
|
+
|
|
43
|
+
type TextStyleObject = {
|
|
44
|
+
fontSize?: number;
|
|
45
|
+
lineHeight?: number;
|
|
46
|
+
fontWeight?: string;
|
|
47
|
+
fontFamily?: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
7
50
|
export function createStyles(theme: HilalTheme) {
|
|
8
|
-
return StyleSheet.create(
|
|
51
|
+
return StyleSheet.create(applyFonts(baseStyles(theme), theme));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Applies `fontFamily`, `fontFamilyBold` and `fontScale`. Only adds keys
|
|
56
|
+
* when the theme sets them, so a theme without fonts gives exactly the
|
|
57
|
+
* base styles.
|
|
58
|
+
*/
|
|
59
|
+
function applyFonts<T extends Record<string, object>>(
|
|
60
|
+
styles: T,
|
|
61
|
+
theme: HilalTheme
|
|
62
|
+
): T {
|
|
63
|
+
const scale = theme.fontScale ?? 1;
|
|
64
|
+
if (!(Number.isFinite(scale) && scale > 0)) {
|
|
65
|
+
throw new RangeError(
|
|
66
|
+
`HilalCalendar: theme.fontScale must be a positive number, got ${scale}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const { fontFamily, fontFamilyBold } = theme;
|
|
70
|
+
if (scale === 1 && !fontFamily && !fontFamilyBold) {
|
|
71
|
+
return styles;
|
|
72
|
+
}
|
|
73
|
+
const out: Record<string, object> = { ...styles };
|
|
74
|
+
for (const key of TEXT_STYLES) {
|
|
75
|
+
const style = { ...(styles[key] as TextStyleObject) };
|
|
76
|
+
// Weekday names (أربعاء خميس) only just fit seven columns on small
|
|
77
|
+
// phones, and long month names (Dhu al-Qi'dah) three, so these never
|
|
78
|
+
// grow.
|
|
79
|
+
const factor = FIXED_WIDTH_TEXT.has(key) ? Math.min(scale, 1) : scale;
|
|
80
|
+
if (factor !== 1) {
|
|
81
|
+
if (style.fontSize) style.fontSize = round(style.fontSize * factor);
|
|
82
|
+
if (style.lineHeight) style.lineHeight = round(style.lineHeight * factor);
|
|
83
|
+
}
|
|
84
|
+
const bold = style.fontWeight === '600' || style.fontWeight === '700';
|
|
85
|
+
if (bold && fontFamilyBold) {
|
|
86
|
+
// A bold font file plus a bold weight would be bolded twice.
|
|
87
|
+
style.fontFamily = fontFamilyBold;
|
|
88
|
+
style.fontWeight = 'normal';
|
|
89
|
+
} else if (fontFamily) {
|
|
90
|
+
style.fontFamily = fontFamily;
|
|
91
|
+
}
|
|
92
|
+
out[key] = style;
|
|
93
|
+
}
|
|
94
|
+
if (scale > 1) {
|
|
95
|
+
// Larger text needs larger day cells.
|
|
96
|
+
const day = styles.day as { height: number; maxWidth: number };
|
|
97
|
+
out.day = {
|
|
98
|
+
...day,
|
|
99
|
+
height: round(day.height * scale),
|
|
100
|
+
maxWidth: round(day.maxWidth * scale),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return out as T;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const round = (n: number) => Math.round(n * 10) / 10;
|
|
107
|
+
|
|
108
|
+
/** The calendar style each `styles` slot is merged into. */
|
|
109
|
+
const SLOTS = {
|
|
110
|
+
container: 'container',
|
|
111
|
+
toggle: 'toggle',
|
|
112
|
+
toggleOption: 'toggleOption',
|
|
113
|
+
toggleText: 'toggleText',
|
|
114
|
+
header: 'header',
|
|
115
|
+
title: 'title',
|
|
116
|
+
subtitle: 'subtitle',
|
|
117
|
+
arrow: 'arrow',
|
|
118
|
+
weekdayText: 'weekday',
|
|
119
|
+
day: 'day',
|
|
120
|
+
dayText: 'dayText',
|
|
121
|
+
secondaryDayText: 'secondaryText',
|
|
122
|
+
pickerItem: 'pickerItem',
|
|
123
|
+
pickerText: 'pickerText',
|
|
124
|
+
} as const;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The slots for day states. The picker shares these calendar styles, so
|
|
128
|
+
* they are merged into a copy used only by the day cells.
|
|
129
|
+
*/
|
|
130
|
+
const DAY_SLOTS = {
|
|
131
|
+
selectedDay: 'selected',
|
|
132
|
+
selectedDayText: 'selectedText',
|
|
133
|
+
today: 'today',
|
|
134
|
+
disabledDayText: 'disabledText',
|
|
135
|
+
todayDayText: 'todayText',
|
|
136
|
+
} as const;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Merges the `styles` prop over the calendar styles: `styles` for the
|
|
140
|
+
* calendar and the picker, `dayStyles` for the day cells. Slots that are
|
|
141
|
+
* not set leave the calendar styles as they are.
|
|
142
|
+
*/
|
|
143
|
+
export function withSlots(
|
|
144
|
+
styles: CalendarStyles,
|
|
145
|
+
slots: HilalStyles | undefined
|
|
146
|
+
): { styles: CalendarStyles; dayStyles: CalendarStyles } {
|
|
147
|
+
if (!slots) {
|
|
148
|
+
return { styles, dayStyles: styles };
|
|
149
|
+
}
|
|
150
|
+
const merge = (
|
|
151
|
+
target: CalendarStyles,
|
|
152
|
+
map: Record<string, keyof CalendarStyles>
|
|
153
|
+
) => {
|
|
154
|
+
const out: Record<string, unknown> = { ...target };
|
|
155
|
+
for (const [slot, key] of Object.entries(map)) {
|
|
156
|
+
const style = slots[slot as keyof HilalStyles];
|
|
157
|
+
if (style) {
|
|
158
|
+
out[key] = StyleSheet.flatten([target[key], style]);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out as CalendarStyles;
|
|
162
|
+
};
|
|
163
|
+
const merged = merge(styles, SLOTS);
|
|
164
|
+
return { styles: merged, dayStyles: merge(merged, DAY_SLOTS) };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function baseStyles(theme: HilalTheme) {
|
|
168
|
+
return {
|
|
9
169
|
container: {
|
|
10
170
|
backgroundColor: theme.background,
|
|
11
171
|
borderRadius: 20,
|
|
@@ -64,7 +224,7 @@ export function createStyles(theme: HilalTheme) {
|
|
|
64
224
|
backgroundColor: theme.border,
|
|
65
225
|
},
|
|
66
226
|
weekdays: {
|
|
67
|
-
marginBottom:
|
|
227
|
+
marginBottom: 8,
|
|
68
228
|
},
|
|
69
229
|
weekday: {
|
|
70
230
|
color: theme.textSecondary,
|
|
@@ -103,18 +263,27 @@ export function createStyles(theme: HilalTheme) {
|
|
|
103
263
|
pressed: {
|
|
104
264
|
backgroundColor: theme.border,
|
|
105
265
|
},
|
|
266
|
+
// Medium, so the selected day and today stand out in bold. Fixed line
|
|
267
|
+
// heights: fonts with tall lines (Noto Naskh, many custom fonts) would
|
|
268
|
+
// otherwise push the date under the number to the next row.
|
|
106
269
|
dayText: {
|
|
107
270
|
color: theme.text,
|
|
108
271
|
fontSize: 16,
|
|
109
|
-
fontWeight: '
|
|
272
|
+
fontWeight: '500',
|
|
273
|
+
lineHeight: 20,
|
|
110
274
|
},
|
|
111
275
|
secondaryText: {
|
|
112
276
|
color: theme.textSecondary,
|
|
113
|
-
fontSize:
|
|
277
|
+
fontSize: 11,
|
|
278
|
+
lineHeight: 14,
|
|
114
279
|
marginTop: 1,
|
|
115
280
|
},
|
|
116
281
|
selectedText: {
|
|
117
282
|
color: theme.onPrimary,
|
|
283
|
+
fontWeight: '700',
|
|
284
|
+
},
|
|
285
|
+
todayText: {
|
|
286
|
+
fontWeight: '700',
|
|
118
287
|
},
|
|
119
288
|
selectedSecondaryText: {
|
|
120
289
|
color: theme.onPrimary,
|
|
@@ -123,5 +292,66 @@ export function createStyles(theme: HilalTheme) {
|
|
|
123
292
|
disabledText: {
|
|
124
293
|
color: theme.disabled,
|
|
125
294
|
},
|
|
126
|
-
|
|
295
|
+
// Month and year picker.
|
|
296
|
+
titleButton: {
|
|
297
|
+
alignItems: 'center',
|
|
298
|
+
borderRadius: 10,
|
|
299
|
+
overflow: 'hidden',
|
|
300
|
+
paddingHorizontal: 10,
|
|
301
|
+
paddingVertical: 2,
|
|
302
|
+
},
|
|
303
|
+
// The title inside the button: centered and shrinkable when a long
|
|
304
|
+
// name wraps on a narrow phone.
|
|
305
|
+
titleInButton: {
|
|
306
|
+
flexShrink: 1,
|
|
307
|
+
textAlign: 'center',
|
|
308
|
+
},
|
|
309
|
+
// The title of the year picker, which is not a button.
|
|
310
|
+
titleStatic: {
|
|
311
|
+
paddingVertical: 2,
|
|
312
|
+
},
|
|
313
|
+
titleButtonPressed: {
|
|
314
|
+
backgroundColor: theme.border,
|
|
315
|
+
},
|
|
316
|
+
// Tints are layers with opacity, so any theme color format works.
|
|
317
|
+
titleTint: {
|
|
318
|
+
...fill,
|
|
319
|
+
backgroundColor: theme.primary,
|
|
320
|
+
opacity: 0.1,
|
|
321
|
+
},
|
|
322
|
+
picker: {
|
|
323
|
+
marginTop: 4,
|
|
324
|
+
},
|
|
325
|
+
pickerSlot: {
|
|
326
|
+
flex: 1,
|
|
327
|
+
padding: 3,
|
|
328
|
+
},
|
|
329
|
+
pickerItem: {
|
|
330
|
+
alignItems: 'center',
|
|
331
|
+
borderColor: 'transparent',
|
|
332
|
+
borderRadius: theme.radius,
|
|
333
|
+
borderWidth: 1.5,
|
|
334
|
+
justifyContent: 'center',
|
|
335
|
+
minHeight: 52,
|
|
336
|
+
overflow: 'hidden',
|
|
337
|
+
paddingHorizontal: 4,
|
|
338
|
+
paddingVertical: 6,
|
|
339
|
+
},
|
|
340
|
+
// Grows into the free height of its parent.
|
|
341
|
+
grow: {
|
|
342
|
+
flexGrow: 1,
|
|
343
|
+
},
|
|
344
|
+
pickerItemTint: {
|
|
345
|
+
...fill,
|
|
346
|
+
backgroundColor: theme.text,
|
|
347
|
+
opacity: 0.05,
|
|
348
|
+
},
|
|
349
|
+
pickerText: {
|
|
350
|
+
color: theme.text,
|
|
351
|
+
fontSize: 14,
|
|
352
|
+
fontWeight: '600',
|
|
353
|
+
lineHeight: 18,
|
|
354
|
+
textAlign: 'center',
|
|
355
|
+
},
|
|
356
|
+
} as const;
|
|
127
357
|
}
|