@plexui/react-native 0.2.4 → 0.2.6
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/dist/es/components/Badge/Badge.js +10 -0
- package/dist/es/components/DatePicker/Calendar.js +112 -0
- package/dist/es/components/DatePicker/DatePicker.js +335 -0
- package/dist/es/components/DatePicker/TimeDial.js +123 -0
- package/dist/es/components/DatePicker/Wheel.js +164 -0
- package/dist/es/components/DatePicker/dates.js +147 -0
- package/dist/es/components/Dialog/Dialog.js +21 -0
- package/dist/es/components/Icon/material.js +16 -0
- package/dist/es/components/PageControl/PageControl.js +49 -22
- package/dist/es/components/ScrollEdge/ScrollEdge.js +27 -0
- package/dist/es/components/TabBar/TabBar.js +16 -13
- package/dist/es/index.js +2 -0
- package/dist/es/internal/BarBackdrop.js +54 -0
- package/dist/types/components/Badge/Badge.d.ts +1 -1
- package/dist/types/components/DatePicker/Calendar.d.ts +29 -0
- package/dist/types/components/DatePicker/DatePicker.d.ts +57 -0
- package/dist/types/components/DatePicker/TimeDial.d.ts +20 -0
- package/dist/types/components/DatePicker/Wheel.d.ts +40 -0
- package/dist/types/components/DatePicker/dates.d.ts +45 -0
- package/dist/types/components/Dialog/Dialog.d.ts +18 -0
- package/dist/types/components/Icon/material.d.ts +6 -0
- package/dist/types/components/PageControl/PageControl.d.ts +12 -1
- package/dist/types/components/ScrollEdge/ScrollEdge.d.ts +19 -0
- package/dist/types/components/TabBar/TabBar.d.ts +14 -4
- package/dist/types/index.d.ts +3 -1
- package/dist/types/internal/BarBackdrop.d.ts +9 -0
- package/package.json +1 -1
- package/src/components/Badge/Badge.tsx +9 -1
- package/src/components/DatePicker/Calendar.tsx +227 -0
- package/src/components/DatePicker/DatePicker.tsx +608 -0
- package/src/components/DatePicker/TimeDial.tsx +201 -0
- package/src/components/DatePicker/Wheel.tsx +247 -0
- package/src/components/DatePicker/dates.ts +161 -0
- package/src/components/Dialog/Dialog.tsx +46 -0
- package/src/components/Icon/material.ts +18 -0
- package/src/components/PageControl/PageControl.tsx +71 -24
- package/src/components/ScrollEdge/ScrollEdge.tsx +48 -0
- package/src/components/TabBar/TabBar.tsx +33 -15
- package/src/index.ts +3 -0
- package/src/internal/BarBackdrop.tsx +73 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { useMemo, useState } from "react"
|
|
2
|
+
import { Platform, Pressable, StyleSheet, View } from "react-native"
|
|
3
|
+
import ChevronLeftLg from "../../icons/plex/ChevronLeftLg"
|
|
4
|
+
import ChevronRightLg from "../../icons/plex/ChevronRightLg"
|
|
5
|
+
import { isRTL } from "../../internal/direction"
|
|
6
|
+
import { withAlpha } from "../../theme/material"
|
|
7
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
8
|
+
import { MaterialChevronLeft, MaterialChevronRight } from "../Icon/material"
|
|
9
|
+
import { Text } from "../Text/Text"
|
|
10
|
+
import { dayOutside, formatFullDay, formatMonth, monthOf, monthWeeks, sameDay, weekdayLabels } from "./dates"
|
|
11
|
+
|
|
12
|
+
export type CalendarProps = {
|
|
13
|
+
/** The chosen day. */
|
|
14
|
+
value: Date | null
|
|
15
|
+
/** Called with the day a person taps (its time is midnight). */
|
|
16
|
+
onSelect: (day: Date) => void
|
|
17
|
+
min?: Date
|
|
18
|
+
max?: Date
|
|
19
|
+
locale?: string
|
|
20
|
+
weekStartsOn: number
|
|
21
|
+
/** The calendar's width: seven columns across it (44 at the least on iOS). */
|
|
22
|
+
width: number
|
|
23
|
+
/** Takes no presses (an inline calendar of a disabled picker). */
|
|
24
|
+
disabled?: boolean
|
|
25
|
+
/** Labels of the month arrows, read by a screen reader. */
|
|
26
|
+
previousLabel?: string
|
|
27
|
+
nextLabel?: string
|
|
28
|
+
testID?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** iOS: the corner's inset around the grid of a compact picker's calendar (the web's `--calendar-inner-padding`). */
|
|
32
|
+
export const IOS_CALENDAR_INSET = 10
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The month grid of DatePicker. On iOS the phone calendar of @plexui/ui (Calendar.module.css under 768px): the month's
|
|
36
|
+
* title Semibold 17 at the row's text inset, the two arrows as 44 targets at the end, the weekdays Semibold 13 in
|
|
37
|
+
* capitals in the tertiary label, the days Regular 17 in 44 cells side by side and row on row, the chosen day white on
|
|
38
|
+
* a 44 circle of the text's color, today a 4 dot under its digits, days out of range at 40%. On Android Material 3's
|
|
39
|
+
* date picker: the month at the start in Medium 14 with the arrows at the end, the weekdays' letters, the days in 40dp
|
|
40
|
+
* circles, the chosen one filled with the primary color and today ringed in it.
|
|
41
|
+
*/
|
|
42
|
+
export function Calendar({ value, onSelect, min, max, locale, weekStartsOn, width, disabled: off = false, previousLabel = "Previous month", nextLabel = "Next month", testID }: CalendarProps) {
|
|
43
|
+
const theme = useTheme()
|
|
44
|
+
const android = theme.isAndroid
|
|
45
|
+
const [month, setMonth] = useState(() => monthOf(value ?? new Date()))
|
|
46
|
+
// The value moved to another month from outside (a wheel, a reset): the calendar turns to it
|
|
47
|
+
const [shownFor, setShownFor] = useState(value?.getTime() ?? null)
|
|
48
|
+
if ((value?.getTime() ?? null) !== shownFor) {
|
|
49
|
+
setShownFor(value?.getTime() ?? null)
|
|
50
|
+
if (value != null && monthOf(value).getTime() !== month.getTime()) setMonth(monthOf(value))
|
|
51
|
+
}
|
|
52
|
+
const weeks = useMemo(() => monthWeeks(month, weekStartsOn), [month, weekStartsOn])
|
|
53
|
+
const labels = useMemo(() => weekdayLabels(locale, weekStartsOn, android ? "narrow" : "short"), [locale, weekStartsOn, android])
|
|
54
|
+
const today = new Date()
|
|
55
|
+
// The arrows stop at the months that hold no day in range
|
|
56
|
+
const canPrevious = min == null || monthOf(month, -1).getTime() >= monthOf(min).getTime()
|
|
57
|
+
const canNext = max == null || monthOf(month, 1).getTime() <= monthOf(max).getTime()
|
|
58
|
+
const rtl = isRTL()
|
|
59
|
+
const cell = android ? Math.min(48, width / 7) : Math.max(44, width / 7)
|
|
60
|
+
const m = theme.material
|
|
61
|
+
const text = android ? m.onSurface : theme.colors.text
|
|
62
|
+
const arrowColor = android ? m.onSurfaceVariant : text
|
|
63
|
+
const arrow = (direction: -1 | 1) => {
|
|
64
|
+
const enabled = !off && (direction < 0 ? canPrevious : canNext)
|
|
65
|
+
// In a right-to-left layout the earlier month is on the right, its arrow pointing right
|
|
66
|
+
const pointsLeft = (direction < 0) !== rtl
|
|
67
|
+
const Icon = android ? (pointsLeft ? MaterialChevronLeft : MaterialChevronRight) : pointsLeft ? ChevronLeftLg : ChevronRightLg
|
|
68
|
+
return (
|
|
69
|
+
<Pressable
|
|
70
|
+
accessibilityRole="button"
|
|
71
|
+
accessibilityLabel={direction < 0 ? previousLabel : nextLabel}
|
|
72
|
+
accessibilityState={{ disabled: !enabled }}
|
|
73
|
+
disabled={!enabled}
|
|
74
|
+
onPress={() => setMonth((current) => monthOf(current, direction))}
|
|
75
|
+
testID={testID ? `${testID}-${direction < 0 ? "previous" : "next"}` : undefined}
|
|
76
|
+
style={({ pressed }) => [
|
|
77
|
+
android ? styles.androidArrow : styles.arrow,
|
|
78
|
+
pressed ? { backgroundColor: android ? withAlpha(m.onSurfaceVariant, 0.1) : theme.color("--button-background-color-active", withAlpha(text, 0.1)) } : null,
|
|
79
|
+
{ opacity: enabled ? 1 : android ? 0.38 : 0.4 },
|
|
80
|
+
]}
|
|
81
|
+
>
|
|
82
|
+
<Icon size={24} color={arrowColor} />
|
|
83
|
+
</Pressable>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
return (
|
|
87
|
+
<View style={{ width }} testID={testID}>
|
|
88
|
+
<View style={[styles.header, android ? styles.androidHeader : { height: 44 }]}>
|
|
89
|
+
<Text
|
|
90
|
+
accessibilityRole="header"
|
|
91
|
+
accessibilityLiveRegion="polite"
|
|
92
|
+
numberOfLines={1}
|
|
93
|
+
color={android ? m.onSurfaceVariant : text}
|
|
94
|
+
style={[
|
|
95
|
+
styles.title,
|
|
96
|
+
android
|
|
97
|
+
? [styles.androidTitle, theme.font("medium")]
|
|
98
|
+
: [{ fontSize: 17, lineHeight: 22, paddingStart: Math.max(0, theme.length("--field-row-padding-inline", 16) - IOS_CALENDAR_INSET) }, theme.font("semibold")],
|
|
99
|
+
]}
|
|
100
|
+
>
|
|
101
|
+
{formatMonth(month, locale)}
|
|
102
|
+
</Text>
|
|
103
|
+
{arrow(-1)}
|
|
104
|
+
{arrow(1)}
|
|
105
|
+
</View>
|
|
106
|
+
<View style={[styles.week, android ? { height: 48 } : styles.weekdays, android ? null : { marginBottom: 3 }]} importantForAccessibility="no-hide-descendants" accessibilityElementsHidden>
|
|
107
|
+
{labels.map(({ label, name }) => (
|
|
108
|
+
<Text
|
|
109
|
+
key={name}
|
|
110
|
+
color={android ? m.onSurface : theme.colors.labelTertiary}
|
|
111
|
+
style={[
|
|
112
|
+
{ width: cell, textAlign: "center" },
|
|
113
|
+
android ? styles.androidWeekday : [styles.weekday, theme.font("semibold")],
|
|
114
|
+
]}
|
|
115
|
+
>
|
|
116
|
+
{android ? label : label.toUpperCase()}
|
|
117
|
+
</Text>
|
|
118
|
+
))}
|
|
119
|
+
</View>
|
|
120
|
+
{weeks.map((week, row) => (
|
|
121
|
+
<View key={row} style={styles.week}>
|
|
122
|
+
{week.map((day, column) =>
|
|
123
|
+
day == null ? (
|
|
124
|
+
<View key={column} style={{ width: cell, height: android ? 48 : 44 }} />
|
|
125
|
+
) : (
|
|
126
|
+
<Day
|
|
127
|
+
key={column}
|
|
128
|
+
day={day}
|
|
129
|
+
cell={cell}
|
|
130
|
+
chosen={sameDay(day, value)}
|
|
131
|
+
isToday={sameDay(day, today)}
|
|
132
|
+
disabled={dayOutside(day, min, max)}
|
|
133
|
+
locked={off}
|
|
134
|
+
locale={locale}
|
|
135
|
+
onSelect={onSelect}
|
|
136
|
+
testID={testID ? `${testID}-day-${day.getDate()}` : undefined}
|
|
137
|
+
/>
|
|
138
|
+
),
|
|
139
|
+
)}
|
|
140
|
+
</View>
|
|
141
|
+
))}
|
|
142
|
+
</View>
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
type DayProps = {
|
|
147
|
+
locked?: boolean
|
|
148
|
+
day: Date
|
|
149
|
+
cell: number
|
|
150
|
+
chosen: boolean
|
|
151
|
+
isToday: boolean
|
|
152
|
+
disabled: boolean
|
|
153
|
+
locale?: string
|
|
154
|
+
onSelect: (day: Date) => void
|
|
155
|
+
testID?: string
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function Day({ day, cell, chosen, isToday, disabled, locked, locale, onSelect, testID }: DayProps) {
|
|
159
|
+
const theme = useTheme()
|
|
160
|
+
const android = theme.isAndroid
|
|
161
|
+
const m = theme.material
|
|
162
|
+
const ink = theme.colors.text
|
|
163
|
+
const size = android ? 40 : 44
|
|
164
|
+
const digitColor = android
|
|
165
|
+
? chosen
|
|
166
|
+
? m.onPrimary
|
|
167
|
+
: disabled
|
|
168
|
+
? withAlpha(m.onSurface, 0.38)
|
|
169
|
+
: isToday
|
|
170
|
+
? m.primary
|
|
171
|
+
: m.onSurface
|
|
172
|
+
: chosen
|
|
173
|
+
? theme.colors.textInverse
|
|
174
|
+
: ink
|
|
175
|
+
return (
|
|
176
|
+
<Pressable
|
|
177
|
+
accessibilityRole="button"
|
|
178
|
+
accessibilityLabel={formatFullDay(day, locale)}
|
|
179
|
+
accessibilityState={{ selected: chosen, disabled: disabled || locked }}
|
|
180
|
+
disabled={disabled || locked}
|
|
181
|
+
onPress={() => onSelect(day)}
|
|
182
|
+
testID={testID}
|
|
183
|
+
style={[styles.cell, { width: cell, height: android ? 48 : 44 }]}
|
|
184
|
+
>
|
|
185
|
+
{({ pressed }) => (
|
|
186
|
+
<View
|
|
187
|
+
style={[
|
|
188
|
+
styles.circle,
|
|
189
|
+
{ width: size, height: size, borderRadius: size / 2 },
|
|
190
|
+
chosen
|
|
191
|
+
? { backgroundColor: android ? m.primary : ink }
|
|
192
|
+
: android
|
|
193
|
+
? [isToday ? { borderWidth: 1, borderColor: m.primary } : null, pressed ? { backgroundColor: withAlpha(m.onSurface, 0.1) } : null]
|
|
194
|
+
: // iOS: the web's hover ring, shown while the finger is on the day
|
|
195
|
+
pressed
|
|
196
|
+
? { borderWidth: 1, borderColor: ink }
|
|
197
|
+
: null,
|
|
198
|
+
!android && disabled ? { opacity: 0.4 } : null,
|
|
199
|
+
]}
|
|
200
|
+
>
|
|
201
|
+
<Text color={digitColor} style={android ? styles.androidDigit : styles.digit}>
|
|
202
|
+
{day.getDate()}
|
|
203
|
+
</Text>
|
|
204
|
+
{isToday && !android ? <View style={[styles.dot, { backgroundColor: chosen ? theme.colors.textInverse : ink }]} /> : null}
|
|
205
|
+
</View>
|
|
206
|
+
)}
|
|
207
|
+
</Pressable>
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const styles = StyleSheet.create({
|
|
212
|
+
header: { flexDirection: "row", alignItems: "center" },
|
|
213
|
+
androidHeader: { height: 56, paddingStart: 12, paddingEnd: 0 },
|
|
214
|
+
title: { flex: 1 },
|
|
215
|
+
androidTitle: { fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
|
|
216
|
+
arrow: { width: 44, height: 44, borderRadius: 22, alignItems: "center", justifyContent: "center" },
|
|
217
|
+
androidArrow: { width: 48, height: 48, borderRadius: 24, alignItems: "center", justifyContent: "center" },
|
|
218
|
+
week: { flexDirection: "row", alignItems: "center", justifyContent: "center" },
|
|
219
|
+
weekdays: { height: 20 },
|
|
220
|
+
weekday: { fontSize: 13, lineHeight: 18 },
|
|
221
|
+
androidWeekday: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
|
|
222
|
+
cell: { alignItems: "center", justifyContent: "center" },
|
|
223
|
+
circle: { alignItems: "center", justifyContent: "center" },
|
|
224
|
+
digit: { fontSize: 17, lineHeight: 22, ...(Platform.OS === "web" ? ({ userSelect: "none" } as object) : null) },
|
|
225
|
+
androidDigit: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5, ...(Platform.OS === "web" ? ({ userSelect: "none" } as object) : null) },
|
|
226
|
+
dot: { position: "absolute", bottom: 8, width: 4, height: 4, borderRadius: 2 },
|
|
227
|
+
})
|