glacia-native 0.4.0 → 0.5.1
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/lib/module/Bars.js +269 -0
- package/lib/module/Carousel.js +103 -0
- package/lib/module/Display.js +317 -0
- package/lib/module/Feedback.js +386 -0
- package/lib/module/Forms2.js +454 -0
- package/lib/module/Message.js +88 -0
- package/lib/module/Overlays.js +328 -0
- package/lib/module/Pickers.js +243 -0
- package/lib/module/SegmentedControl.js +5 -1
- package/lib/module/Slider.js +6 -1
- package/lib/module/Switch.js +4 -1
- package/lib/module/TabBar.js +5 -1
- package/lib/module/Text.js +37 -0
- package/lib/module/ai.js +8 -3
- package/lib/module/index.js +79 -1
- package/lib/module/internal/GlassContainer.js +54 -0
- package/lib/module/internal/GlassView.js +31 -7
- package/lib/module/internal/a11y.js +94 -0
- package/lib/module/internal/fallback.js +19 -8
- package/lib/module/internal/glass-routing.js +7 -0
- package/lib/module/internal/haptics.js +54 -0
- package/lib/module/internal/motion.js +10 -1
- package/lib/module/internal/primitives.js +73 -3
- package/lib/module/internal/tokens.js +101 -1
- package/lib/typescript/Bars.d.ts +188 -0
- package/lib/typescript/Carousel.d.ts +47 -0
- package/lib/typescript/Display.d.ts +178 -0
- package/lib/typescript/Feedback.d.ts +196 -0
- package/lib/typescript/Forms2.d.ts +209 -0
- package/lib/typescript/Glass.d.ts +1 -1
- package/lib/typescript/Message.d.ts +71 -0
- package/lib/typescript/Overlays.d.ts +146 -0
- package/lib/typescript/Pickers.d.ts +105 -0
- package/lib/typescript/SegmentedControl.d.ts +2 -0
- package/lib/typescript/Slider.d.ts +2 -0
- package/lib/typescript/Switch.d.ts +2 -0
- package/lib/typescript/TabBar.d.ts +2 -0
- package/lib/typescript/Text.d.ts +28 -0
- package/lib/typescript/index.d.ts +11 -1
- package/lib/typescript/internal/GlassContainer.d.ts +37 -0
- package/lib/typescript/internal/GlassView.d.ts +18 -1
- package/lib/typescript/internal/a11y.d.ts +19 -0
- package/lib/typescript/internal/fallback.d.ts +5 -0
- package/lib/typescript/internal/glass-routing.d.ts +10 -0
- package/lib/typescript/internal/haptics.d.ts +7 -0
- package/lib/typescript/internal/motion.d.ts +4 -0
- package/lib/typescript/internal/primitives.d.ts +21 -0
- package/lib/typescript/internal/tokens.d.ts +114 -0
- package/package.json +2 -2
- package/src/Bars.tsx +797 -0
- package/src/Carousel.tsx +233 -0
- package/src/Display.tsx +778 -0
- package/src/Feedback.tsx +969 -0
- package/src/Forms2.tsx +1014 -0
- package/src/Message.tsx +219 -0
- package/src/Overlays.tsx +685 -0
- package/src/Pickers.tsx +590 -0
- package/src/SegmentedControl.tsx +6 -0
- package/src/Slider.tsx +7 -0
- package/src/Switch.tsx +5 -0
- package/src/TabBar.tsx +6 -0
- package/src/Text.tsx +83 -0
- package/src/ai.tsx +9 -3
- package/src/index.ts +142 -1
- package/src/internal/GlassContainer.tsx +82 -0
- package/src/internal/GlassView.tsx +37 -7
- package/src/internal/a11y.ts +105 -0
- package/src/internal/fallback.tsx +19 -2
- package/src/internal/glass-routing.ts +17 -0
- package/src/internal/haptics.ts +59 -0
- package/src/internal/motion.ts +10 -1
- package/src/internal/primitives.ts +84 -3
- package/src/internal/tokens.ts +110 -0
package/src/Pickers.tsx
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
import { forwardRef, useMemo, useState } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
4
|
+
import { Glass } from "./Glass";
|
|
5
|
+
import { Button } from "./Button";
|
|
6
|
+
import { Sheet } from "./Overlays";
|
|
7
|
+
import { colors, radii } from "./internal/tokens";
|
|
8
|
+
import { primitives } from "./internal/primitives";
|
|
9
|
+
import { useIsDark } from "./internal/motion";
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Date helpers
|
|
13
|
+
//
|
|
14
|
+
// Kept pure and exported so a caller (and the tests) can reason about the same
|
|
15
|
+
// calendar maths the grid uses. Dates are compared by day, never by instant,
|
|
16
|
+
// which is the bug that makes a picker select "yesterday" near midnight.
|
|
17
|
+
|
|
18
|
+
/** Midnight on the same calendar day, so two picks compare equal. */
|
|
19
|
+
export function startOfDay(date: Date): Date {
|
|
20
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function sameDay(a: Date | undefined, b: Date | undefined): boolean {
|
|
24
|
+
if (!a || !b) return false;
|
|
25
|
+
return (
|
|
26
|
+
a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface CalendarCell {
|
|
31
|
+
date: Date;
|
|
32
|
+
/** True for days outside the displayed month, shown to fill the grid. */
|
|
33
|
+
outside: boolean;
|
|
34
|
+
key: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The six-row month grid.
|
|
39
|
+
*
|
|
40
|
+
* Always six rows so the calendar does not change height as months change,
|
|
41
|
+
* which would make the surrounding layout jump between February and March.
|
|
42
|
+
*/
|
|
43
|
+
export function monthGrid(year: number, month: number, weekStartsOn: 0 | 1 = 0): CalendarCell[] {
|
|
44
|
+
const first = new Date(year, month, 1);
|
|
45
|
+
// getDay() is 0 for Sunday; the offset is how many leading cells to add.
|
|
46
|
+
const offset = (first.getDay() - weekStartsOn + 7) % 7;
|
|
47
|
+
const cells: CalendarCell[] = [];
|
|
48
|
+
for (let i = 0; i < 42; i += 1) {
|
|
49
|
+
const date = new Date(year, month, 1 - offset + i);
|
|
50
|
+
cells.push({
|
|
51
|
+
date,
|
|
52
|
+
outside: date.getMonth() !== month,
|
|
53
|
+
key: `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return cells;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const WEEKDAYS = ["S", "M", "T", "W", "T", "F", "S"];
|
|
60
|
+
|
|
61
|
+
export const MONTHS = [
|
|
62
|
+
"January",
|
|
63
|
+
"February",
|
|
64
|
+
"March",
|
|
65
|
+
"April",
|
|
66
|
+
"May",
|
|
67
|
+
"June",
|
|
68
|
+
"July",
|
|
69
|
+
"August",
|
|
70
|
+
"September",
|
|
71
|
+
"October",
|
|
72
|
+
"November",
|
|
73
|
+
"December",
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Calendar
|
|
78
|
+
|
|
79
|
+
export interface CalendarProps {
|
|
80
|
+
/** Controlled selection. */
|
|
81
|
+
value?: Date;
|
|
82
|
+
/** Fires when a day is tapped. */
|
|
83
|
+
onChange?: (date: Date) => void;
|
|
84
|
+
/** Days before this are dimmed and cannot be picked. */
|
|
85
|
+
minDate?: Date;
|
|
86
|
+
/** Days after this are dimmed and cannot be picked. */
|
|
87
|
+
maxDate?: Date;
|
|
88
|
+
/** Which month to open on when there is no value. */
|
|
89
|
+
initialMonth?: Date;
|
|
90
|
+
/** 0 starts the week on Sunday, 1 on Monday. */
|
|
91
|
+
weekStartsOn?: 0 | 1;
|
|
92
|
+
style?: StyleProp<ViewStyle>;
|
|
93
|
+
isDark?: boolean;
|
|
94
|
+
testID?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A month calendar, built from Glacia's own views.
|
|
99
|
+
*
|
|
100
|
+
* No native date picker anywhere: the grid, the weekday header and the month
|
|
101
|
+
* navigation are ours, so the calendar looks the same on iOS and Android and
|
|
102
|
+
* wears the library's tokens. The selected day is a `glassActive` fill, today
|
|
103
|
+
* carries a ring, and disabled days are dimmed.
|
|
104
|
+
*/
|
|
105
|
+
export const Calendar = forwardRef<any, CalendarProps>(function Calendar(
|
|
106
|
+
{
|
|
107
|
+
value,
|
|
108
|
+
onChange,
|
|
109
|
+
minDate,
|
|
110
|
+
maxDate,
|
|
111
|
+
initialMonth,
|
|
112
|
+
weekStartsOn = 0,
|
|
113
|
+
style,
|
|
114
|
+
isDark: isDarkProp,
|
|
115
|
+
testID,
|
|
116
|
+
...props
|
|
117
|
+
},
|
|
118
|
+
ref,
|
|
119
|
+
) {
|
|
120
|
+
const isDark = useIsDark(isDarkProp);
|
|
121
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
122
|
+
const { View, Text: RNText, Pressable } = primitives();
|
|
123
|
+
|
|
124
|
+
const anchor = value ?? initialMonth ?? new Date();
|
|
125
|
+
const [month, setMonth] = useState({ year: anchor.getFullYear(), month: anchor.getMonth() });
|
|
126
|
+
|
|
127
|
+
const cells = useMemo(() => monthGrid(month.year, month.month, weekStartsOn), [month, weekStartsOn]);
|
|
128
|
+
const today = startOfDay(new Date());
|
|
129
|
+
|
|
130
|
+
const isDisabled = (date: Date) => {
|
|
131
|
+
const day = startOfDay(date);
|
|
132
|
+
if (minDate && day < startOfDay(minDate)) return true;
|
|
133
|
+
if (maxDate && day > startOfDay(maxDate)) return true;
|
|
134
|
+
return false;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const shift = (delta: number) => {
|
|
138
|
+
setMonth((current) => {
|
|
139
|
+
const next = new Date(current.year, current.month + delta, 1);
|
|
140
|
+
return { year: next.getFullYear(), month: next.getMonth() };
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const cellSize = 38;
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<View
|
|
148
|
+
ref={ref}
|
|
149
|
+
testID={testID}
|
|
150
|
+
accessibilityRole="none"
|
|
151
|
+
style={[{ alignSelf: "stretch" }, style as ViewStyle]}
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
{/* Month navigation. */}
|
|
155
|
+
<View style={{ flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingBottom: 8 }}>
|
|
156
|
+
<Pressable
|
|
157
|
+
accessibilityRole="button"
|
|
158
|
+
accessibilityLabel="Previous month"
|
|
159
|
+
onPress={() => shift(-1)}
|
|
160
|
+
hitSlop={8}
|
|
161
|
+
style={{ padding: 6 }}
|
|
162
|
+
>
|
|
163
|
+
<RNText style={{ fontSize: 17, color: theme.foreground }}>{"\u2039"}</RNText>
|
|
164
|
+
</Pressable>
|
|
165
|
+
<RNText accessibilityRole="heading" style={{ fontSize: 16, fontWeight: "600", color: theme.foreground }}>
|
|
166
|
+
{`${MONTHS[month.month]} ${month.year}`}
|
|
167
|
+
</RNText>
|
|
168
|
+
<Pressable
|
|
169
|
+
accessibilityRole="button"
|
|
170
|
+
accessibilityLabel="Next month"
|
|
171
|
+
onPress={() => shift(1)}
|
|
172
|
+
hitSlop={8}
|
|
173
|
+
style={{ padding: 6 }}
|
|
174
|
+
>
|
|
175
|
+
<RNText style={{ fontSize: 17, color: theme.foreground }}>{"\u203A"}</RNText>
|
|
176
|
+
</Pressable>
|
|
177
|
+
</View>
|
|
178
|
+
|
|
179
|
+
{/* Weekday header, derived from the same weekStartsOn as the grid. */}
|
|
180
|
+
<View style={{ flexDirection: "row" }}>
|
|
181
|
+
{Array.from({ length: 7 }, (_, i) => (
|
|
182
|
+
<View key={i} style={{ flex: 1, alignItems: "center", paddingVertical: 4 }}>
|
|
183
|
+
<RNText
|
|
184
|
+
accessibilityElementsHidden
|
|
185
|
+
style={{ fontSize: 11, fontWeight: "600", color: theme.muted }}
|
|
186
|
+
>
|
|
187
|
+
{WEEKDAYS[(i + weekStartsOn) % 7]}
|
|
188
|
+
</RNText>
|
|
189
|
+
</View>
|
|
190
|
+
))}
|
|
191
|
+
</View>
|
|
192
|
+
|
|
193
|
+
{/* The grid: six rows of seven. */}
|
|
194
|
+
{Array.from({ length: 6 }, (_, row) => (
|
|
195
|
+
<View key={row} style={{ flexDirection: "row" }}>
|
|
196
|
+
{cells.slice(row * 7, row * 7 + 7).map((cell) => {
|
|
197
|
+
const selected = sameDay(cell.date, value);
|
|
198
|
+
const isToday = sameDay(cell.date, today);
|
|
199
|
+
const disabled = isDisabled(cell.date);
|
|
200
|
+
return (
|
|
201
|
+
<View key={cell.key} style={{ flex: 1, alignItems: "center", paddingVertical: 2 }}>
|
|
202
|
+
<Pressable
|
|
203
|
+
accessibilityRole="button"
|
|
204
|
+
accessibilityLabel={cell.date.toDateString()}
|
|
205
|
+
accessibilityState={{ selected, disabled }}
|
|
206
|
+
disabled={disabled}
|
|
207
|
+
onPress={() => onChange?.(startOfDay(cell.date))}
|
|
208
|
+
style={{
|
|
209
|
+
width: cellSize,
|
|
210
|
+
height: cellSize,
|
|
211
|
+
borderRadius: cellSize / 2,
|
|
212
|
+
alignItems: "center",
|
|
213
|
+
justifyContent: "center",
|
|
214
|
+
backgroundColor: selected ? theme.glassActive : "transparent",
|
|
215
|
+
borderWidth: isToday && !selected ? 0.5 : selected ? 1 : 0,
|
|
216
|
+
borderColor: isToday && !selected ? theme.fill : theme.edge,
|
|
217
|
+
opacity: disabled ? 0.28 : cell.outside ? 0.45 : 1,
|
|
218
|
+
}}
|
|
219
|
+
>
|
|
220
|
+
<RNText
|
|
221
|
+
style={{
|
|
222
|
+
fontSize: 14,
|
|
223
|
+
fontWeight: selected ? "600" : "400",
|
|
224
|
+
color: selected ? theme.foreground : theme.foreground,
|
|
225
|
+
}}
|
|
226
|
+
>
|
|
227
|
+
{String(cell.date.getDate())}
|
|
228
|
+
</RNText>
|
|
229
|
+
</Pressable>
|
|
230
|
+
</View>
|
|
231
|
+
);
|
|
232
|
+
})}
|
|
233
|
+
</View>
|
|
234
|
+
))}
|
|
235
|
+
</View>
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
// DatePicker
|
|
241
|
+
|
|
242
|
+
export interface DatePickerProps {
|
|
243
|
+
/** Controlled visibility. Omit and the picker keeps its own. */
|
|
244
|
+
open?: boolean;
|
|
245
|
+
defaultOpen?: boolean;
|
|
246
|
+
onOpenChange?: (open: boolean) => void;
|
|
247
|
+
/** The committed date. */
|
|
248
|
+
value?: Date;
|
|
249
|
+
/** Fires only when the user confirms, never on a mere browse. */
|
|
250
|
+
onConfirm?: (date: Date) => void;
|
|
251
|
+
onCancel?: () => void;
|
|
252
|
+
title?: string;
|
|
253
|
+
confirmLabel?: string;
|
|
254
|
+
cancelLabel?: string;
|
|
255
|
+
minDate?: Date;
|
|
256
|
+
maxDate?: Date;
|
|
257
|
+
style?: StyleProp<ViewStyle>;
|
|
258
|
+
isDark?: boolean;
|
|
259
|
+
testID?: string;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* A date picker: the Calendar presented in a Sheet.
|
|
264
|
+
*
|
|
265
|
+
* Browsing months does not commit anything — the pending date is local until
|
|
266
|
+
* the user confirms, so an accidental swipe of the calendar cannot change a
|
|
267
|
+
* saved value.
|
|
268
|
+
*/
|
|
269
|
+
export const DatePicker = forwardRef<any, DatePickerProps>(function DatePicker(
|
|
270
|
+
{
|
|
271
|
+
open: openProp,
|
|
272
|
+
defaultOpen = false,
|
|
273
|
+
onOpenChange,
|
|
274
|
+
value,
|
|
275
|
+
onConfirm,
|
|
276
|
+
onCancel,
|
|
277
|
+
title = "Select date",
|
|
278
|
+
confirmLabel = "Confirm",
|
|
279
|
+
cancelLabel = "Cancel",
|
|
280
|
+
minDate,
|
|
281
|
+
maxDate,
|
|
282
|
+
style,
|
|
283
|
+
isDark: isDarkProp,
|
|
284
|
+
testID,
|
|
285
|
+
...props
|
|
286
|
+
},
|
|
287
|
+
ref,
|
|
288
|
+
) {
|
|
289
|
+
const isDark = useIsDark(isDarkProp);
|
|
290
|
+
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
291
|
+
const open = openProp !== undefined ? openProp : internalOpen;
|
|
292
|
+
|
|
293
|
+
const [pending, setPending] = useState<Date | undefined>(value);
|
|
294
|
+
|
|
295
|
+
const setOpen = (next: boolean) => {
|
|
296
|
+
if (openProp === undefined) setInternalOpen(next);
|
|
297
|
+
onOpenChange?.(next);
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
return (
|
|
301
|
+
<Sheet
|
|
302
|
+
ref={ref}
|
|
303
|
+
open={open}
|
|
304
|
+
onOpenChange={setOpen}
|
|
305
|
+
title={title}
|
|
306
|
+
isDark={isDark}
|
|
307
|
+
testID={testID}
|
|
308
|
+
style={style}
|
|
309
|
+
footer={
|
|
310
|
+
<>
|
|
311
|
+
<Button
|
|
312
|
+
variant="ghost"
|
|
313
|
+
accessibilityLabel={cancelLabel}
|
|
314
|
+
onPress={() => {
|
|
315
|
+
// Cancelling restores the committed value for the next open.
|
|
316
|
+
setPending(value);
|
|
317
|
+
onCancel?.();
|
|
318
|
+
setOpen(false);
|
|
319
|
+
}}
|
|
320
|
+
>
|
|
321
|
+
{cancelLabel}
|
|
322
|
+
</Button>
|
|
323
|
+
<Button
|
|
324
|
+
variant="filled"
|
|
325
|
+
accessibilityLabel={confirmLabel}
|
|
326
|
+
disabled={pending === undefined}
|
|
327
|
+
onPress={() => {
|
|
328
|
+
if (pending) onConfirm?.(pending);
|
|
329
|
+
setOpen(false);
|
|
330
|
+
}}
|
|
331
|
+
>
|
|
332
|
+
{confirmLabel}
|
|
333
|
+
</Button>
|
|
334
|
+
</>
|
|
335
|
+
}
|
|
336
|
+
{...props}
|
|
337
|
+
>
|
|
338
|
+
<Calendar
|
|
339
|
+
value={pending}
|
|
340
|
+
onChange={setPending}
|
|
341
|
+
minDate={minDate}
|
|
342
|
+
maxDate={maxDate}
|
|
343
|
+
isDark={isDark}
|
|
344
|
+
initialMonth={value}
|
|
345
|
+
/>
|
|
346
|
+
</Sheet>
|
|
347
|
+
);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// ---------------------------------------------------------------------------
|
|
351
|
+
// TimePicker
|
|
352
|
+
|
|
353
|
+
export interface TimePickerProps {
|
|
354
|
+
open?: boolean;
|
|
355
|
+
defaultOpen?: boolean;
|
|
356
|
+
onOpenChange?: (open: boolean) => void;
|
|
357
|
+
/** The committed time, as "HH:MM" (24-hour). */
|
|
358
|
+
value?: string;
|
|
359
|
+
/** Fires only on confirm. */
|
|
360
|
+
onConfirm?: (time: string) => void;
|
|
361
|
+
onCancel?: () => void;
|
|
362
|
+
/** Show an AM/PM column and present times in 12-hour form. */
|
|
363
|
+
use12Hour?: boolean;
|
|
364
|
+
title?: string;
|
|
365
|
+
confirmLabel?: string;
|
|
366
|
+
cancelLabel?: string;
|
|
367
|
+
minuteStep?: number;
|
|
368
|
+
style?: StyleProp<ViewStyle>;
|
|
369
|
+
isDark?: boolean;
|
|
370
|
+
testID?: string;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Splits "HH:MM" into parts, falling back to a sane default. */
|
|
374
|
+
export function parseTime(value: string | undefined): { hour: number; minute: number } {
|
|
375
|
+
const match = /^(\d{1,2}):(\d{2})$/.exec(value ?? "");
|
|
376
|
+
if (!match) return { hour: 9, minute: 0 };
|
|
377
|
+
return {
|
|
378
|
+
hour: Math.min(Math.max(Number(match[1]), 0), 23),
|
|
379
|
+
minute: Math.min(Math.max(Number(match[2]), 0), 59),
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Formats parts back to zero-padded "HH:MM". */
|
|
384
|
+
export function formatTime(hour: number, minute: number): string {
|
|
385
|
+
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* A time picker: tappable hour/minute drums inside a Sheet.
|
|
390
|
+
*
|
|
391
|
+
* The drums are ordinary Pressables drawn by Glacia — not a native picker
|
|
392
|
+
* widget. Tapping a value selects it; the whole column scrolls in the Sheet.
|
|
393
|
+
*/
|
|
394
|
+
export const TimePicker = forwardRef<any, TimePickerProps>(function TimePicker(
|
|
395
|
+
{
|
|
396
|
+
open: openProp,
|
|
397
|
+
defaultOpen = false,
|
|
398
|
+
onOpenChange,
|
|
399
|
+
value,
|
|
400
|
+
onConfirm,
|
|
401
|
+
onCancel,
|
|
402
|
+
use12Hour = false,
|
|
403
|
+
title = "Select time",
|
|
404
|
+
confirmLabel = "Confirm",
|
|
405
|
+
cancelLabel = "Cancel",
|
|
406
|
+
minuteStep = 1,
|
|
407
|
+
style,
|
|
408
|
+
isDark: isDarkProp,
|
|
409
|
+
testID,
|
|
410
|
+
...props
|
|
411
|
+
},
|
|
412
|
+
ref,
|
|
413
|
+
) {
|
|
414
|
+
const isDark = useIsDark(isDarkProp);
|
|
415
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
416
|
+
const { View, Text: RNText, Pressable } = primitives();
|
|
417
|
+
|
|
418
|
+
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
419
|
+
const open = openProp !== undefined ? openProp : internalOpen;
|
|
420
|
+
|
|
421
|
+
const initial = parseTime(value);
|
|
422
|
+
const [hour, setHour] = useState(initial.hour);
|
|
423
|
+
const [minute, setMinute] = useState(initial.minute);
|
|
424
|
+
|
|
425
|
+
const setOpen = (next: boolean) => {
|
|
426
|
+
if (openProp === undefined) setInternalOpen(next);
|
|
427
|
+
onOpenChange?.(next);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const hours = Array.from({ length: 24 }, (_, i) => i);
|
|
431
|
+
const minutes = Array.from({ length: Math.ceil(60 / minuteStep) }, (_, i) => i * minuteStep);
|
|
432
|
+
|
|
433
|
+
// In 12-hour mode the column shows 1–12; the stored hour stays 24-hour.
|
|
434
|
+
const displayHour = use12Hour ? (hour % 12 === 0 ? 12 : hour % 12) : hour;
|
|
435
|
+
const isPm = hour >= 12;
|
|
436
|
+
|
|
437
|
+
const column = (items: number[], selected: number, onPick: (value: number) => void, label: string, pad = 2) => (
|
|
438
|
+
<View style={{ height: 168, flex: 1 }} accessibilityRole="none" accessibilityLabel={label}>
|
|
439
|
+
<ScrollColumn
|
|
440
|
+
items={items}
|
|
441
|
+
selected={selected}
|
|
442
|
+
onPick={onPick}
|
|
443
|
+
isDark={isDark}
|
|
444
|
+
pad={pad}
|
|
445
|
+
/>
|
|
446
|
+
</View>
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
return (
|
|
450
|
+
<Sheet
|
|
451
|
+
ref={ref}
|
|
452
|
+
open={open}
|
|
453
|
+
onOpenChange={setOpen}
|
|
454
|
+
title={title}
|
|
455
|
+
isDark={isDark}
|
|
456
|
+
testID={testID}
|
|
457
|
+
style={style}
|
|
458
|
+
footer={
|
|
459
|
+
<>
|
|
460
|
+
<Button
|
|
461
|
+
variant="ghost"
|
|
462
|
+
accessibilityLabel={cancelLabel}
|
|
463
|
+
onPress={() => {
|
|
464
|
+
setHour(initial.hour);
|
|
465
|
+
setMinute(initial.minute);
|
|
466
|
+
onCancel?.();
|
|
467
|
+
setOpen(false);
|
|
468
|
+
}}
|
|
469
|
+
>
|
|
470
|
+
{cancelLabel}
|
|
471
|
+
</Button>
|
|
472
|
+
<Button
|
|
473
|
+
variant="filled"
|
|
474
|
+
accessibilityLabel={confirmLabel}
|
|
475
|
+
onPress={() => {
|
|
476
|
+
onConfirm?.(formatTime(hour, minute));
|
|
477
|
+
setOpen(false);
|
|
478
|
+
}}
|
|
479
|
+
>
|
|
480
|
+
{confirmLabel}
|
|
481
|
+
</Button>
|
|
482
|
+
</>
|
|
483
|
+
}
|
|
484
|
+
{...props}
|
|
485
|
+
>
|
|
486
|
+
<View style={{ flexDirection: "row", gap: 8, alignItems: "center" }}>
|
|
487
|
+
{column(use12Hour ? Array.from({ length: 12 }, (_, i) => i + 1) : hours, displayHour, (picked) => {
|
|
488
|
+
if (!use12Hour) {
|
|
489
|
+
setHour(picked);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
// Keep the current half of the day when the hour column changes.
|
|
493
|
+
setHour(picked % 12 === 0 ? (isPm ? 12 : 0) : isPm ? picked + 12 : picked);
|
|
494
|
+
}, "Hour")}
|
|
495
|
+
<RNText style={{ fontSize: 22, color: theme.muted }}>{":"}</RNText>
|
|
496
|
+
{column(minutes, minute, setMinute, "Minute")}
|
|
497
|
+
{use12Hour && (
|
|
498
|
+
<View style={{ flex: 1, gap: 6 }}>
|
|
499
|
+
{["AM", "PM"].map((label) => {
|
|
500
|
+
const active = label === "PM" ? isPm : !isPm;
|
|
501
|
+
return (
|
|
502
|
+
<Pressable
|
|
503
|
+
key={label}
|
|
504
|
+
accessibilityRole="button"
|
|
505
|
+
accessibilityLabel={label}
|
|
506
|
+
accessibilityState={{ selected: active }}
|
|
507
|
+
onPress={() => {
|
|
508
|
+
const nextPm = label === "PM";
|
|
509
|
+
if (nextPm === isPm) return;
|
|
510
|
+
setHour((current) => (nextPm ? (current % 12) + 12 : current % 12));
|
|
511
|
+
}}
|
|
512
|
+
style={{
|
|
513
|
+
height: 34,
|
|
514
|
+
borderRadius: radii.sm,
|
|
515
|
+
alignItems: "center",
|
|
516
|
+
justifyContent: "center",
|
|
517
|
+
backgroundColor: active ? theme.glassActive : "transparent",
|
|
518
|
+
borderWidth: 0.5,
|
|
519
|
+
borderColor: active ? theme.edge : "transparent",
|
|
520
|
+
}}
|
|
521
|
+
>
|
|
522
|
+
<RNText style={{ fontSize: 14, fontWeight: active ? "600" : "500", color: active ? theme.foreground : theme.muted }}>
|
|
523
|
+
{label}
|
|
524
|
+
</RNText>
|
|
525
|
+
</Pressable>
|
|
526
|
+
);
|
|
527
|
+
})}
|
|
528
|
+
</View>
|
|
529
|
+
)}
|
|
530
|
+
</View>
|
|
531
|
+
</Sheet>
|
|
532
|
+
);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
/** One scrollable column of the time drums. */
|
|
536
|
+
function ScrollColumn({
|
|
537
|
+
items,
|
|
538
|
+
selected,
|
|
539
|
+
onPick,
|
|
540
|
+
isDark,
|
|
541
|
+
pad,
|
|
542
|
+
}: {
|
|
543
|
+
items: number[];
|
|
544
|
+
selected: number;
|
|
545
|
+
onPick: (value: number) => void;
|
|
546
|
+
isDark: boolean;
|
|
547
|
+
pad: number;
|
|
548
|
+
}) {
|
|
549
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
550
|
+
const { getScrollView } = primitives();
|
|
551
|
+
const ScrollView = getScrollView();
|
|
552
|
+
const { View, Text: RNText, Pressable } = primitives();
|
|
553
|
+
|
|
554
|
+
const rows = items.map((item) => {
|
|
555
|
+
const active = item === selected;
|
|
556
|
+
return (
|
|
557
|
+
<Pressable
|
|
558
|
+
key={item}
|
|
559
|
+
accessibilityRole="button"
|
|
560
|
+
accessibilityLabel={String(item).padStart(pad, "0")}
|
|
561
|
+
accessibilityState={{ selected: active }}
|
|
562
|
+
onPress={() => onPick(item)}
|
|
563
|
+
style={{
|
|
564
|
+
height: 34,
|
|
565
|
+
alignItems: "center",
|
|
566
|
+
justifyContent: "center",
|
|
567
|
+
borderRadius: radii.sm,
|
|
568
|
+
backgroundColor: active ? theme.glassActive : "transparent",
|
|
569
|
+
}}
|
|
570
|
+
>
|
|
571
|
+
<RNText style={{ fontSize: 17, fontWeight: active ? "600" : "400", color: active ? theme.foreground : theme.muted }}>
|
|
572
|
+
{String(item).padStart(pad, "0")}
|
|
573
|
+
</RNText>
|
|
574
|
+
</Pressable>
|
|
575
|
+
);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
if (!ScrollView) {
|
|
579
|
+
// Off-device: no ScrollView, so the column is clipped to its height rather
|
|
580
|
+
// than truncated. Every value stays present and selectable, which is what
|
|
581
|
+
// the real drum shows once it can scroll.
|
|
582
|
+
return <View style={{ gap: 2, overflow: "hidden", height: 168 }}>{rows}</View>;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return (
|
|
586
|
+
<ScrollView showsVerticalScrollIndicator={false} style={{ flex: 1 }}>
|
|
587
|
+
<View style={{ gap: 2 }}>{rows}</View>
|
|
588
|
+
</ScrollView>
|
|
589
|
+
);
|
|
590
|
+
}
|
package/src/SegmentedControl.tsx
CHANGED
|
@@ -5,12 +5,15 @@ import { Glass } from "./Glass";
|
|
|
5
5
|
import { colors, radii } from "./internal/tokens";
|
|
6
6
|
import { primitives } from "./internal/primitives";
|
|
7
7
|
import { useIsDark, useSpring } from "./internal/motion";
|
|
8
|
+
import { hapticSelection } from "./internal/haptics";
|
|
8
9
|
|
|
9
10
|
export interface SegmentedControlProps {
|
|
10
11
|
values: string[];
|
|
11
12
|
selectedIndex?: number;
|
|
12
13
|
defaultIndex?: number;
|
|
13
14
|
onValueChange?: (value: string, index: number) => void;
|
|
15
|
+
/** One selection tick when the segment changes. `false` opts out. */
|
|
16
|
+
haptics?: boolean;
|
|
14
17
|
style?: StyleProp<ViewStyle>;
|
|
15
18
|
isDark?: boolean;
|
|
16
19
|
/** Passed through to the control so it is testable. */
|
|
@@ -32,6 +35,7 @@ export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function
|
|
|
32
35
|
selectedIndex: selectedProp,
|
|
33
36
|
defaultIndex = 0,
|
|
34
37
|
onValueChange,
|
|
38
|
+
haptics = true,
|
|
35
39
|
style,
|
|
36
40
|
isDark: isDarkProp,
|
|
37
41
|
testID,
|
|
@@ -52,6 +56,8 @@ export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function
|
|
|
52
56
|
const { value: slide, animated } = useSpring(safeIndex, "snappy");
|
|
53
57
|
|
|
54
58
|
const handleSelect = (index: number) => {
|
|
59
|
+
// Only a change ticks: re-pressing the current segment is not a selection.
|
|
60
|
+
if (haptics && index !== safeIndex) hapticSelection();
|
|
55
61
|
if (selectedProp === undefined) setInternalIndex(index);
|
|
56
62
|
const val = values[index];
|
|
57
63
|
if (val !== undefined) onValueChange?.(val, index);
|
package/src/Slider.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import { Glass } from "./Glass";
|
|
|
6
6
|
import { colors } from "./internal/tokens";
|
|
7
7
|
import { primitives } from "./internal/primitives";
|
|
8
8
|
import { useIsDark } from "./internal/motion";
|
|
9
|
+
import { hapticSelection } from "./internal/haptics";
|
|
9
10
|
|
|
10
11
|
export interface SliderProps {
|
|
11
12
|
value?: number;
|
|
@@ -15,6 +16,8 @@ export interface SliderProps {
|
|
|
15
16
|
step?: number;
|
|
16
17
|
onValueChange?: (value: number) => void;
|
|
17
18
|
disabled?: boolean;
|
|
19
|
+
/** One selection tick each time the stepped value changes while dragging. */
|
|
20
|
+
haptics?: boolean;
|
|
18
21
|
accessibilityLabel: string;
|
|
19
22
|
style?: StyleProp<ViewStyle>;
|
|
20
23
|
isDark?: boolean;
|
|
@@ -55,6 +58,7 @@ export const Slider = forwardRef<any, SliderProps>(function Slider(
|
|
|
55
58
|
step = 1,
|
|
56
59
|
onValueChange,
|
|
57
60
|
disabled = false,
|
|
61
|
+
haptics = true,
|
|
58
62
|
accessibilityLabel,
|
|
59
63
|
style,
|
|
60
64
|
isDark: isDarkProp,
|
|
@@ -93,7 +97,10 @@ export const Slider = forwardRef<any, SliderProps>(function Slider(
|
|
|
93
97
|
);
|
|
94
98
|
|
|
95
99
|
const report = (next: number) => {
|
|
100
|
+
// Only a real step change ticks: a drag that stays inside one step is not
|
|
101
|
+
// a selection, and reporting the same value twice must not buzz twice.
|
|
96
102
|
if (next === valueRef.current) return;
|
|
103
|
+
if (haptics) hapticSelection();
|
|
97
104
|
valueRef.current = next;
|
|
98
105
|
if (valueProp === undefined) setInternalValue(next);
|
|
99
106
|
onValueChange?.(next);
|
package/src/Switch.tsx
CHANGED
|
@@ -5,11 +5,14 @@ import { Glass } from "./Glass";
|
|
|
5
5
|
import { colors } from "./internal/tokens";
|
|
6
6
|
import { primitives } from "./internal/primitives";
|
|
7
7
|
import { useIsDark, useSpring } from "./internal/motion";
|
|
8
|
+
import { hapticSelection } from "./internal/haptics";
|
|
8
9
|
|
|
9
10
|
export interface SwitchProps {
|
|
10
11
|
checked?: boolean;
|
|
11
12
|
defaultChecked?: boolean;
|
|
12
13
|
onCheckedChange?: (checked: boolean) => void;
|
|
14
|
+
/** One selection tick when it toggles. `false` opts out. */
|
|
15
|
+
haptics?: boolean;
|
|
13
16
|
disabled?: boolean;
|
|
14
17
|
accessibilityLabel: string;
|
|
15
18
|
style?: StyleProp<ViewStyle>;
|
|
@@ -37,6 +40,7 @@ export const Switch = forwardRef<any, SwitchProps>(function Switch(
|
|
|
37
40
|
defaultChecked = false,
|
|
38
41
|
onCheckedChange,
|
|
39
42
|
disabled = false,
|
|
43
|
+
haptics = true,
|
|
40
44
|
accessibilityLabel,
|
|
41
45
|
style,
|
|
42
46
|
isDark: isDarkProp,
|
|
@@ -57,6 +61,7 @@ export const Switch = forwardRef<any, SwitchProps>(function Switch(
|
|
|
57
61
|
const toggle = () => {
|
|
58
62
|
if (disabled) return;
|
|
59
63
|
const next = !isChecked;
|
|
64
|
+
if (haptics) hapticSelection();
|
|
60
65
|
if (checkedProp === undefined) setInternalChecked(next);
|
|
61
66
|
onCheckedChange?.(next);
|
|
62
67
|
};
|