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.
Files changed (73) hide show
  1. package/lib/module/Bars.js +269 -0
  2. package/lib/module/Carousel.js +103 -0
  3. package/lib/module/Display.js +317 -0
  4. package/lib/module/Feedback.js +386 -0
  5. package/lib/module/Forms2.js +454 -0
  6. package/lib/module/Message.js +88 -0
  7. package/lib/module/Overlays.js +328 -0
  8. package/lib/module/Pickers.js +243 -0
  9. package/lib/module/SegmentedControl.js +5 -1
  10. package/lib/module/Slider.js +6 -1
  11. package/lib/module/Switch.js +4 -1
  12. package/lib/module/TabBar.js +5 -1
  13. package/lib/module/Text.js +37 -0
  14. package/lib/module/ai.js +8 -3
  15. package/lib/module/index.js +79 -1
  16. package/lib/module/internal/GlassContainer.js +54 -0
  17. package/lib/module/internal/GlassView.js +31 -7
  18. package/lib/module/internal/a11y.js +94 -0
  19. package/lib/module/internal/fallback.js +19 -8
  20. package/lib/module/internal/glass-routing.js +7 -0
  21. package/lib/module/internal/haptics.js +54 -0
  22. package/lib/module/internal/motion.js +10 -1
  23. package/lib/module/internal/primitives.js +73 -3
  24. package/lib/module/internal/tokens.js +101 -1
  25. package/lib/typescript/Bars.d.ts +188 -0
  26. package/lib/typescript/Carousel.d.ts +47 -0
  27. package/lib/typescript/Display.d.ts +178 -0
  28. package/lib/typescript/Feedback.d.ts +196 -0
  29. package/lib/typescript/Forms2.d.ts +209 -0
  30. package/lib/typescript/Glass.d.ts +1 -1
  31. package/lib/typescript/Message.d.ts +71 -0
  32. package/lib/typescript/Overlays.d.ts +146 -0
  33. package/lib/typescript/Pickers.d.ts +105 -0
  34. package/lib/typescript/SegmentedControl.d.ts +2 -0
  35. package/lib/typescript/Slider.d.ts +2 -0
  36. package/lib/typescript/Switch.d.ts +2 -0
  37. package/lib/typescript/TabBar.d.ts +2 -0
  38. package/lib/typescript/Text.d.ts +28 -0
  39. package/lib/typescript/index.d.ts +11 -1
  40. package/lib/typescript/internal/GlassContainer.d.ts +37 -0
  41. package/lib/typescript/internal/GlassView.d.ts +18 -1
  42. package/lib/typescript/internal/a11y.d.ts +19 -0
  43. package/lib/typescript/internal/fallback.d.ts +5 -0
  44. package/lib/typescript/internal/glass-routing.d.ts +10 -0
  45. package/lib/typescript/internal/haptics.d.ts +7 -0
  46. package/lib/typescript/internal/motion.d.ts +4 -0
  47. package/lib/typescript/internal/primitives.d.ts +21 -0
  48. package/lib/typescript/internal/tokens.d.ts +114 -0
  49. package/package.json +2 -2
  50. package/src/Bars.tsx +797 -0
  51. package/src/Carousel.tsx +233 -0
  52. package/src/Display.tsx +778 -0
  53. package/src/Feedback.tsx +969 -0
  54. package/src/Forms2.tsx +1014 -0
  55. package/src/Message.tsx +219 -0
  56. package/src/Overlays.tsx +685 -0
  57. package/src/Pickers.tsx +590 -0
  58. package/src/SegmentedControl.tsx +6 -0
  59. package/src/Slider.tsx +7 -0
  60. package/src/Switch.tsx +5 -0
  61. package/src/TabBar.tsx +6 -0
  62. package/src/Text.tsx +83 -0
  63. package/src/ai.tsx +9 -3
  64. package/src/index.ts +142 -1
  65. package/src/internal/GlassContainer.tsx +82 -0
  66. package/src/internal/GlassView.tsx +37 -7
  67. package/src/internal/a11y.ts +105 -0
  68. package/src/internal/fallback.tsx +19 -2
  69. package/src/internal/glass-routing.ts +17 -0
  70. package/src/internal/haptics.ts +59 -0
  71. package/src/internal/motion.ts +10 -1
  72. package/src/internal/primitives.ts +84 -3
  73. package/src/internal/tokens.ts +110 -0
@@ -0,0 +1,328 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Drawer = exports.Sheet = exports.Menu = void 0;
4
+ exports.anchorPopover = anchorPopover;
5
+ exports.resolveSnapPoint = resolveSnapPoint;
6
+ exports.nearestSnapPoint = nearestSnapPoint;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const react_1 = require("react");
9
+ const Glass_1 = require("./Glass");
10
+ const tokens_1 = require("./internal/tokens");
11
+ const primitives_1 = require("./internal/primitives");
12
+ const motion_1 = require("./internal/motion");
13
+ const haptics_1 = require("./internal/haptics");
14
+ /**
15
+ * Registers Android's hardware back while an overlay is open.
16
+ *
17
+ * Shared by Menu, Sheet and Drawer so every overlay dismisses the same way a
18
+ * Dialog does: the back button closes the layer, not the screen behind it.
19
+ */
20
+ function useBackDismiss(open, onDismiss) {
21
+ (0, react_1.useEffect)(() => {
22
+ if (!open)
23
+ return;
24
+ const BackHandler = (0, primitives_1.primitives)().getBackHandler();
25
+ if (!BackHandler)
26
+ return;
27
+ const sub = BackHandler.addEventListener("hardwareBackPress", () => {
28
+ onDismiss();
29
+ return true;
30
+ });
31
+ return () => sub.remove();
32
+ }, [open, onDismiss]);
33
+ }
34
+ /**
35
+ * Keeps a popover inside the window.
36
+ *
37
+ * Called with the trigger's measured frame and the popover's own size; returns
38
+ * the offsets that bring it back on screen. A menu near the bottom edge flips
39
+ * above its trigger rather than hanging off the display.
40
+ */
41
+ function anchorPopover(trigger, popover, viewport, gap = 6) {
42
+ const below = trigger.y + trigger.height + gap;
43
+ const above = trigger.y - popover.height - gap;
44
+ const fitsBelow = below + popover.height <= viewport.height;
45
+ const flipped = !fitsBelow && above >= 0;
46
+ const top = Math.max(0, Math.min(flipped ? above : below, viewport.height - popover.height));
47
+ const left = Math.max(0, Math.min(trigger.x, viewport.width - popover.width));
48
+ return { left, top, flipped };
49
+ }
50
+ /**
51
+ * An anchored dropdown menu.
52
+ *
53
+ * A press outside or on an item closes it; Android's back button does too. The
54
+ * popover is a glass surface with `glassActive` rows, matching the way the
55
+ * library marks a tappable row rather than nesting another glass panel.
56
+ */
57
+ exports.Menu = (0, react_1.forwardRef)(function Menu({ trigger, items, open: openProp, onOpenChange, width = 220, accessibilityLabel = "Menu", style, isDark: isDarkProp, testID, ...props }, ref) {
58
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
59
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
60
+ const { View, Text: RNText, Pressable, Modal } = (0, primitives_1.primitives)();
61
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(false);
62
+ const open = openProp !== undefined ? openProp : internalOpen;
63
+ const setOpen = (0, react_1.useCallback)((next) => {
64
+ if (openProp === undefined)
65
+ setInternalOpen(next);
66
+ onOpenChange?.(next);
67
+ }, [openProp, onOpenChange]);
68
+ const dismiss = (0, react_1.useCallback)(() => setOpen(false), [setOpen]);
69
+ useBackDismiss(open, dismiss);
70
+ const { value: enter, animated } = (0, motion_1.useSpring)(open ? 1 : 0, "snappy");
71
+ const scale = animated ? enter.interpolate({ inputRange: [0, 1], outputRange: [0.96, 1] }) : 1;
72
+ const popover = ((0, jsx_runtime_1.jsx)(Glass_1.Glass, { effect: "regular", isDark: isDark, testID: testID ? `${testID}-popover` : undefined, style: { width, borderRadius: tokens_1.radii.md, paddingVertical: 4, overflow: "hidden", ...style }, children: items.map((item, index) => ((0, jsx_runtime_1.jsxs)(Pressable, { accessibilityRole: "button", accessibilityLabel: item.accessibilityLabel ?? item.label, accessibilityState: { disabled: item.disabled }, disabled: item.disabled, onPress: () => {
73
+ setOpen(false);
74
+ item.onPress();
75
+ }, style: { flexDirection: "row", alignItems: "center", gap: 10, paddingVertical: 10, paddingHorizontal: 12, opacity: item.disabled ? 0.38 : 1 }, children: [item.icon ? (0, jsx_runtime_1.jsx)(View, { children: item.icon }) : null, (0, jsx_runtime_1.jsx)(RNText, { numberOfLines: 1, style: { fontSize: 15, color: item.destructive ? theme.destructive : theme.foreground }, children: item.label })] }, `${item.label}-${index}`))) }));
76
+ return ((0, jsx_runtime_1.jsxs)(View, { ref: ref, style: { alignSelf: "flex-start" }, ...props, children: [(0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: accessibilityLabel, accessibilityState: { expanded: open }, onPress: () => setOpen(!open), children: trigger }), (0, jsx_runtime_1.jsx)(Modal, { visible: open, transparent: true, animationType: "fade", onRequestClose: dismiss, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Close menu", onPress: dismiss, style: { flex: 1, backgroundColor: theme.scrim }, children: (0, jsx_runtime_1.jsx)(View, { style: { flex: 1, alignItems: "center", justifyContent: "center", padding: 24 }, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "none", onPress: () => { }, children: animated ? ((0, jsx_runtime_1.jsx)(AnimatedScale, { scale: scale, children: popover })) : (popover) }) }) }) })] }));
77
+ });
78
+ /** Wraps a popover in the entrance scale, which must land on an Animated.View. */
79
+ function AnimatedScale({ scale, children }) {
80
+ const { Animated } = (0, primitives_1.primitives)();
81
+ return (0, jsx_runtime_1.jsx)(Animated.View, { style: { transform: [{ scale }] }, children: children });
82
+ }
83
+ /**
84
+ * A bottom sheet.
85
+ *
86
+ * The general-purpose container: it slides up on the springs token, dismisses
87
+ * on a scrim press and on Android back. ActionSheet remains the action-menu
88
+ * specialisation of this shape; Sheet is for arbitrary content — a form, a
89
+ * calendar, a detail panel.
90
+ */
91
+ /**
92
+ * Turns a snap point into a pixel height.
93
+ *
94
+ * A number under 1 is a fraction of the window; anything else is points. The
95
+ * result is clamped so a sheet can never be taller than the space it has.
96
+ */
97
+ function resolveSnapPoint(point, windowHeight) {
98
+ const height = point > 0 && point < 1 ? point * windowHeight : point;
99
+ return Math.max(0, Math.min(height, windowHeight));
100
+ }
101
+ /**
102
+ * Picks the snap point nearest to where the user let go.
103
+ *
104
+ * `height` is how tall the sheet currently is. Ties go to the smaller sheet,
105
+ * so a small flick does not jump the panel to full height.
106
+ */
107
+ function nearestSnapPoint(heights, height) {
108
+ let best = 0;
109
+ let bestDistance = Number.POSITIVE_INFINITY;
110
+ heights.forEach((candidate, index) => {
111
+ const distance = Math.abs(candidate - height);
112
+ if (distance < bestDistance) {
113
+ bestDistance = distance;
114
+ best = index;
115
+ }
116
+ });
117
+ return best;
118
+ }
119
+ exports.Sheet = (0, react_1.forwardRef)(function Sheet({ open: openProp, defaultOpen = false, onOpenChange, title, children, footer, dismissOnBackdropPress = true, showHandle = true, snapPoints, initialSnapIndex = 0, snapIndex: snapIndexProp, onSnapIndexChange, dismissOnSnapToBottom = false, draggable = true, haptics = true, style, isDark: isDarkProp, testID, ...props }, ref) {
120
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
121
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
122
+ const { View, Text: RNText, Pressable, Modal, PanResponder, useWindowDimensions } = (0, primitives_1.primitives)();
123
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(defaultOpen);
124
+ const open = openProp !== undefined ? openProp : internalOpen;
125
+ const setOpen = (0, react_1.useCallback)((next) => {
126
+ if (openProp === undefined)
127
+ setInternalOpen(next);
128
+ onOpenChange?.(next);
129
+ }, [openProp, onOpenChange]);
130
+ const dismiss = (0, react_1.useCallback)(() => setOpen(false), [setOpen]);
131
+ useBackDismiss(open, dismiss);
132
+ // Snap points in pixels, smallest first so index 0 is the shortest sheet.
133
+ const windowHeight = useWindowDimensions?.().height ?? 800;
134
+ const heights = (0, react_1.useMemo)(() => {
135
+ if (!snapPoints || snapPoints.length === 0)
136
+ return [];
137
+ return snapPoints.map((point) => resolveSnapPoint(point, windowHeight)).sort((a, b) => a - b);
138
+ }, [snapPoints, windowHeight]);
139
+ const [internalSnap, setInternalSnap] = (0, react_1.useState)(initialSnapIndex);
140
+ const snapIndex = snapIndexProp !== undefined ? snapIndexProp : internalSnap;
141
+ const activeHeight = heights.length > 0 ? heights[Math.min(snapIndex, heights.length - 1)] : null;
142
+ // Drag offset in pixels: 0 is resting at the active snap point, positive is
143
+ // dragged down. It resets to 0 after a release, because the settle either
144
+ // changes the snap index or dismisses.
145
+ const dragRef = (0, react_1.useRef)(0);
146
+ const [drag, setDrag] = (0, react_1.useState)(0);
147
+ const { value: rise, animated } = (0, motion_1.useSpring)(open ? 1 : 0, "gentle");
148
+ const slideIn = animated ? rise.interpolate({ inputRange: [0, 1], outputRange: [420, 0] }) : 0;
149
+ const heightsRef = (0, react_1.useRef)(heights);
150
+ heightsRef.current = heights;
151
+ const activeHeightRef = (0, react_1.useRef)(activeHeight);
152
+ activeHeightRef.current = activeHeight;
153
+ const snapIndexRef = (0, react_1.useRef)(snapIndex);
154
+ snapIndexRef.current = snapIndex;
155
+ const goToSnap = (0, react_1.useCallback)((index) => {
156
+ if (snapIndexProp === undefined)
157
+ setInternalSnap(index);
158
+ onSnapIndexChange?.(index);
159
+ if (haptics)
160
+ (0, haptics_1.hapticSelection)();
161
+ }, [snapIndexProp, onSnapIndexChange, haptics]);
162
+ /**
163
+ * The release decision.
164
+ *
165
+ * Below the lowest snap point the sheet dismisses when allowed, otherwise it
166
+ * snaps back; anywhere else it settles at the nearest point, with a fast
167
+ * downward flick counting as a step towards the smaller sheet.
168
+ */
169
+ const release = (0, react_1.useCallback)((dy, vy) => {
170
+ const list = heightsRef.current;
171
+ const current = list[snapIndexRef.current] ?? activeHeightRef.current ?? 0;
172
+ const restingAfterDrag = current - dy;
173
+ if (list.length === 0) {
174
+ // No snap points: a decisive downward drag dismisses.
175
+ if (dy > 120 || vy > 0.8)
176
+ dismiss();
177
+ return;
178
+ }
179
+ const lowest = list[0];
180
+ const belowLowest = restingAfterDrag < lowest - 40;
181
+ if (belowLowest && dismissOnSnapToBottom) {
182
+ dismiss();
183
+ return;
184
+ }
185
+ // A fast flick moves one step in its direction before distance decides.
186
+ let index = nearestSnapPoint(list, restingAfterDrag);
187
+ if (vy > 0.5)
188
+ index = Math.max(0, Math.min(index, snapIndexRef.current - 1));
189
+ if (vy < -0.5)
190
+ index = Math.max(index, Math.min(snapIndexRef.current + 1, list.length - 1));
191
+ if (index !== snapIndexRef.current)
192
+ goToSnap(index);
193
+ }, [dismiss, dismissOnSnapToBottom, goToSnap]);
194
+ const panResponder = (0, react_1.useMemo)(() => {
195
+ if (!draggable || !PanResponder?.create)
196
+ return null;
197
+ return PanResponder.create({
198
+ // Vertical drags belong to the sheet; horizontal ones are left alone.
199
+ onStartShouldSetPanResponder: () => false,
200
+ onMoveShouldSetPanResponder: (_event, gesture) => Math.abs(gesture.dy) > Math.abs(gesture.dx) && Math.abs(gesture.dy) > 6,
201
+ onPanResponderGrant: () => {
202
+ dragRef.current = 0;
203
+ },
204
+ onPanResponderMove: (_event, gesture) => {
205
+ // Dragging up past the tallest point has nowhere to go; clamp at 0.
206
+ const next = Math.max(-40, gesture.dy);
207
+ dragRef.current = next;
208
+ setDrag(next);
209
+ },
210
+ onPanResponderRelease: (_event, gesture) => {
211
+ release(gesture.dy, gesture.vy);
212
+ dragRef.current = 0;
213
+ setDrag(0);
214
+ },
215
+ onPanResponderTerminate: () => {
216
+ dragRef.current = 0;
217
+ setDrag(0);
218
+ },
219
+ onPanResponderTerminationRequest: () => false,
220
+ });
221
+ }, [draggable, PanResponder, release]);
222
+ // Reduce Motion is honoured by useSpring: when it is on, `animated` is false
223
+ // and the panel is already in position, so the sheet opens and snaps without
224
+ // a transition.
225
+ const translateY = drag !== 0 ? drag : animated ? slideIn : 0;
226
+ const panel = ((0, jsx_runtime_1.jsx)(SheetPanel, { title: title, footer: footer, showHandle: showHandle, isDark: isDark, testID: testID, style: style, height: activeHeight, children: children }));
227
+ return ((0, jsx_runtime_1.jsx)(Modal, { visible: open, transparent: true, animationType: "fade", onRequestClose: dismiss, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Close sheet", onPress: dismissOnBackdropPress ? dismiss : undefined, style: { flex: 1, justifyContent: "flex-end", backgroundColor: theme.scrim }, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "none", onPress: () => { }, style: { width: "100%" }, ...(panResponder ? panResponder.panHandlers : {}), children: animated || drag !== 0 ? ((0, jsx_runtime_1.jsx)(SheetRise, { translateY: translateY, children: panel })) : (panel) }) }) }));
228
+ });
229
+ function SheetRise({ translateY, children }) {
230
+ const { Animated } = (0, primitives_1.primitives)();
231
+ return (0, jsx_runtime_1.jsx)(Animated.View, { style: { transform: [{ translateY }] }, children: children });
232
+ }
233
+ /** The panel itself: a glass surface with a handle, a title and a footer. */
234
+ function SheetPanel({ title, footer, showHandle, isDark, testID, style, height, children, }) {
235
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
236
+ const { View, Text: RNText } = (0, primitives_1.primitives)();
237
+ return ((0, jsx_runtime_1.jsxs)(Glass_1.Glass, { effect: "regular", isDark: isDark, testID: testID, style: {
238
+ borderTopLeftRadius: tokens_1.radii.xl,
239
+ borderTopRightRadius: tokens_1.radii.xl,
240
+ borderBottomLeftRadius: 0,
241
+ borderBottomRightRadius: 0,
242
+ paddingBottom: 20,
243
+ // A snap point fixes the height; without one the panel is natural.
244
+ ...(height != null ? { height } : { maxHeight: 560 }),
245
+ ...style,
246
+ }, children: [showHandle ? ((0, jsx_runtime_1.jsx)(View, { style: { alignItems: "center", paddingTop: 8, paddingBottom: 4 }, children: (0, jsx_runtime_1.jsx)(View, { accessibilityElementsHidden: true,
247
+ // A capsule: the radius is half the handle's own height.
248
+ style: { width: 36, height: 5, borderRadius: 5 / 2, backgroundColor: theme.separator } }) })) : null, title ? ((0, jsx_runtime_1.jsx)(View, { style: { paddingHorizontal: 20, paddingVertical: 10 }, children: (0, jsx_runtime_1.jsx)(RNText, { accessibilityRole: "heading", style: { fontSize: 17, fontWeight: "600", color: theme.foreground }, children: title }) })) : null, (0, jsx_runtime_1.jsx)(View, { style: { paddingHorizontal: 20 }, children: children }), footer ? (0, jsx_runtime_1.jsx)(View, { style: { paddingHorizontal: 20, paddingTop: 12, gap: 8 }, children: footer }) : null] }));
249
+ }
250
+ /**
251
+ * A side navigation drawer.
252
+ *
253
+ * The same overlay discipline as Sheet — scrim, spring slide, Android back —
254
+ * but anchored to a side edge, with a square panel that runs the full height.
255
+ * The caller supplies the content, typically a List.
256
+ *
257
+ * Dragging the panel towards its own edge follows the finger; releasing past
258
+ * the halfway point, or with enough outward velocity, closes it, and a drag
259
+ * that stays inside springs it back open. Reduce Motion is honoured: with it
260
+ * on, the drawer appears and disappears without sliding.
261
+ */
262
+ exports.Drawer = (0, react_1.forwardRef)(function Drawer({ open: openProp, defaultOpen = false, onOpenChange, side = "left", width = 280, children, dismissOnBackdropPress = true, draggable = true, haptics = true, style, isDark: isDarkProp, testID, ...props }, ref) {
263
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
264
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
265
+ const { View, Pressable, Modal, PanResponder } = (0, primitives_1.primitives)();
266
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(defaultOpen);
267
+ const open = openProp !== undefined ? openProp : internalOpen;
268
+ const setOpen = (0, react_1.useCallback)((next) => {
269
+ if (openProp === undefined)
270
+ setInternalOpen(next);
271
+ onOpenChange?.(next);
272
+ }, [openProp, onOpenChange]);
273
+ const dismiss = (0, react_1.useCallback)(() => setOpen(false), [setOpen]);
274
+ useBackDismiss(open, dismiss);
275
+ const { value: slide, animated } = (0, motion_1.useSpring)(open ? 1 : 0, "gentle");
276
+ // Slide in from off-screen: negative from the left, positive from the right.
277
+ const slideIn = animated
278
+ ? slide.interpolate({ inputRange: [0, 1], outputRange: [side === "left" ? -width : width, 0] })
279
+ : 0;
280
+ // The drag is measured in the drawer's own closing direction, so a drag
281
+ // towards the anchored edge is always positive whichever side it is on.
282
+ const [drag, setDrag] = (0, react_1.useState)(0);
283
+ const panResponder = (0, react_1.useMemo)(() => {
284
+ if (!draggable || !PanResponder?.create)
285
+ return null;
286
+ const outward = side === "left" ? -1 : 1;
287
+ return PanResponder.create({
288
+ onStartShouldSetPanResponder: () => false,
289
+ onMoveShouldSetPanResponder: (_event, gesture) =>
290
+ // Only a horizontal drag towards the edge is ours; the other way has
291
+ // nowhere to go and should not fight content inside the panel.
292
+ Math.abs(gesture.dx) > Math.abs(gesture.dy) && gesture.dx * outward > 6,
293
+ onPanResponderMove: (_event, gesture) => {
294
+ setDrag(Math.max(0, gesture.dx * outward));
295
+ },
296
+ onPanResponderRelease: (_event, gesture) => {
297
+ const travelled = Math.max(0, gesture.dx * outward);
298
+ const thrown = gesture.vx * outward > 0.4;
299
+ setDrag(0);
300
+ if (travelled > width / 2 || thrown) {
301
+ if (haptics)
302
+ (0, haptics_1.hapticSelection)();
303
+ dismiss();
304
+ }
305
+ },
306
+ onPanResponderTerminate: () => setDrag(0),
307
+ onPanResponderTerminationRequest: () => false,
308
+ });
309
+ // `dismiss` is stable; the gesture only needs rebuilding when its shape
310
+ // (side or width) changes.
311
+ }, [draggable, PanResponder, side, width, haptics, dismiss]);
312
+ const translateX = drag !== 0 ? (side === "left" ? -drag : drag) : animated ? slideIn : 0;
313
+ const panel = ((0, jsx_runtime_1.jsx)(Glass_1.Glass, { effect: "regular", isDark: isDark, testID: testID, style: {
314
+ width,
315
+ height: "100%",
316
+ borderRadius: 0,
317
+ paddingTop: 56,
318
+ ...style,
319
+ }, children: children }));
320
+ return ((0, jsx_runtime_1.jsx)(Modal, { visible: open, transparent: true, animationType: "fade", onRequestClose: dismiss, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Close drawer", onPress: dismissOnBackdropPress ? dismiss : undefined, style: { flex: 1, backgroundColor: theme.scrim }, children: (0, jsx_runtime_1.jsx)(View, { style: {
321
+ flex: 1,
322
+ alignItems: side === "left" ? "flex-start" : "flex-end",
323
+ }, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "none", onPress: () => { }, style: { height: "100%" }, ...(panResponder ? panResponder.panHandlers : {}), children: animated || drag !== 0 ? (0, jsx_runtime_1.jsx)(DrawerSlide, { translateX: translateX, children: panel }) : panel }) }) }) }));
324
+ });
325
+ function DrawerSlide({ translateX, children }) {
326
+ const { Animated } = (0, primitives_1.primitives)();
327
+ return (0, jsx_runtime_1.jsx)(Animated.View, { style: { height: "100%", transform: [{ translateX }] }, children: children });
328
+ }
@@ -0,0 +1,243 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimePicker = exports.DatePicker = exports.Calendar = exports.MONTHS = exports.WEEKDAYS = void 0;
4
+ exports.startOfDay = startOfDay;
5
+ exports.sameDay = sameDay;
6
+ exports.monthGrid = monthGrid;
7
+ exports.parseTime = parseTime;
8
+ exports.formatTime = formatTime;
9
+ const jsx_runtime_1 = require("react/jsx-runtime");
10
+ const react_1 = require("react");
11
+ const Button_1 = require("./Button");
12
+ const Overlays_1 = require("./Overlays");
13
+ const tokens_1 = require("./internal/tokens");
14
+ const primitives_1 = require("./internal/primitives");
15
+ const motion_1 = require("./internal/motion");
16
+ // ---------------------------------------------------------------------------
17
+ // Date helpers
18
+ //
19
+ // Kept pure and exported so a caller (and the tests) can reason about the same
20
+ // calendar maths the grid uses. Dates are compared by day, never by instant,
21
+ // which is the bug that makes a picker select "yesterday" near midnight.
22
+ /** Midnight on the same calendar day, so two picks compare equal. */
23
+ function startOfDay(date) {
24
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate());
25
+ }
26
+ function sameDay(a, b) {
27
+ if (!a || !b)
28
+ return false;
29
+ return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate());
30
+ }
31
+ /**
32
+ * The six-row month grid.
33
+ *
34
+ * Always six rows so the calendar does not change height as months change,
35
+ * which would make the surrounding layout jump between February and March.
36
+ */
37
+ function monthGrid(year, month, weekStartsOn = 0) {
38
+ const first = new Date(year, month, 1);
39
+ // getDay() is 0 for Sunday; the offset is how many leading cells to add.
40
+ const offset = (first.getDay() - weekStartsOn + 7) % 7;
41
+ const cells = [];
42
+ for (let i = 0; i < 42; i += 1) {
43
+ const date = new Date(year, month, 1 - offset + i);
44
+ cells.push({
45
+ date,
46
+ outside: date.getMonth() !== month,
47
+ key: `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`,
48
+ });
49
+ }
50
+ return cells;
51
+ }
52
+ exports.WEEKDAYS = ["S", "M", "T", "W", "T", "F", "S"];
53
+ exports.MONTHS = [
54
+ "January",
55
+ "February",
56
+ "March",
57
+ "April",
58
+ "May",
59
+ "June",
60
+ "July",
61
+ "August",
62
+ "September",
63
+ "October",
64
+ "November",
65
+ "December",
66
+ ];
67
+ /**
68
+ * A month calendar, built from Glacia's own views.
69
+ *
70
+ * No native date picker anywhere: the grid, the weekday header and the month
71
+ * navigation are ours, so the calendar looks the same on iOS and Android and
72
+ * wears the library's tokens. The selected day is a `glassActive` fill, today
73
+ * carries a ring, and disabled days are dimmed.
74
+ */
75
+ exports.Calendar = (0, react_1.forwardRef)(function Calendar({ value, onChange, minDate, maxDate, initialMonth, weekStartsOn = 0, style, isDark: isDarkProp, testID, ...props }, ref) {
76
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
77
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
78
+ const { View, Text: RNText, Pressable } = (0, primitives_1.primitives)();
79
+ const anchor = value ?? initialMonth ?? new Date();
80
+ const [month, setMonth] = (0, react_1.useState)({ year: anchor.getFullYear(), month: anchor.getMonth() });
81
+ const cells = (0, react_1.useMemo)(() => monthGrid(month.year, month.month, weekStartsOn), [month, weekStartsOn]);
82
+ const today = startOfDay(new Date());
83
+ const isDisabled = (date) => {
84
+ const day = startOfDay(date);
85
+ if (minDate && day < startOfDay(minDate))
86
+ return true;
87
+ if (maxDate && day > startOfDay(maxDate))
88
+ return true;
89
+ return false;
90
+ };
91
+ const shift = (delta) => {
92
+ setMonth((current) => {
93
+ const next = new Date(current.year, current.month + delta, 1);
94
+ return { year: next.getFullYear(), month: next.getMonth() };
95
+ });
96
+ };
97
+ const cellSize = 38;
98
+ return ((0, jsx_runtime_1.jsxs)(View, { ref: ref, testID: testID, accessibilityRole: "none", style: [{ alignSelf: "stretch" }, style], ...props, children: [(0, jsx_runtime_1.jsxs)(View, { style: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingBottom: 8 }, children: [(0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Previous month", onPress: () => shift(-1), hitSlop: 8, style: { padding: 6 }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 17, color: theme.foreground }, children: "\u2039" }) }), (0, jsx_runtime_1.jsx)(RNText, { accessibilityRole: "heading", style: { fontSize: 16, fontWeight: "600", color: theme.foreground }, children: `${exports.MONTHS[month.month]} ${month.year}` }), (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Next month", onPress: () => shift(1), hitSlop: 8, style: { padding: 6 }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 17, color: theme.foreground }, children: "\u203A" }) })] }), (0, jsx_runtime_1.jsx)(View, { style: { flexDirection: "row" }, children: Array.from({ length: 7 }, (_, i) => ((0, jsx_runtime_1.jsx)(View, { style: { flex: 1, alignItems: "center", paddingVertical: 4 }, children: (0, jsx_runtime_1.jsx)(RNText, { accessibilityElementsHidden: true, style: { fontSize: 11, fontWeight: "600", color: theme.muted }, children: exports.WEEKDAYS[(i + weekStartsOn) % 7] }) }, i))) }), Array.from({ length: 6 }, (_, row) => ((0, jsx_runtime_1.jsx)(View, { style: { flexDirection: "row" }, children: cells.slice(row * 7, row * 7 + 7).map((cell) => {
99
+ const selected = sameDay(cell.date, value);
100
+ const isToday = sameDay(cell.date, today);
101
+ const disabled = isDisabled(cell.date);
102
+ return ((0, jsx_runtime_1.jsx)(View, { style: { flex: 1, alignItems: "center", paddingVertical: 2 }, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: cell.date.toDateString(), accessibilityState: { selected, disabled }, disabled: disabled, onPress: () => onChange?.(startOfDay(cell.date)), style: {
103
+ width: cellSize,
104
+ height: cellSize,
105
+ borderRadius: cellSize / 2,
106
+ alignItems: "center",
107
+ justifyContent: "center",
108
+ backgroundColor: selected ? theme.glassActive : "transparent",
109
+ borderWidth: isToday && !selected ? 0.5 : selected ? 1 : 0,
110
+ borderColor: isToday && !selected ? theme.fill : theme.edge,
111
+ opacity: disabled ? 0.28 : cell.outside ? 0.45 : 1,
112
+ }, children: (0, jsx_runtime_1.jsx)(RNText, { style: {
113
+ fontSize: 14,
114
+ fontWeight: selected ? "600" : "400",
115
+ color: selected ? theme.foreground : theme.foreground,
116
+ }, children: String(cell.date.getDate()) }) }) }, cell.key));
117
+ }) }, row)))] }));
118
+ });
119
+ /**
120
+ * A date picker: the Calendar presented in a Sheet.
121
+ *
122
+ * Browsing months does not commit anything — the pending date is local until
123
+ * the user confirms, so an accidental swipe of the calendar cannot change a
124
+ * saved value.
125
+ */
126
+ exports.DatePicker = (0, react_1.forwardRef)(function DatePicker({ open: openProp, defaultOpen = false, onOpenChange, value, onConfirm, onCancel, title = "Select date", confirmLabel = "Confirm", cancelLabel = "Cancel", minDate, maxDate, style, isDark: isDarkProp, testID, ...props }, ref) {
127
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
128
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(defaultOpen);
129
+ const open = openProp !== undefined ? openProp : internalOpen;
130
+ const [pending, setPending] = (0, react_1.useState)(value);
131
+ const setOpen = (next) => {
132
+ if (openProp === undefined)
133
+ setInternalOpen(next);
134
+ onOpenChange?.(next);
135
+ };
136
+ return ((0, jsx_runtime_1.jsx)(Overlays_1.Sheet, { ref: ref, open: open, onOpenChange: setOpen, title: title, isDark: isDark, testID: testID, style: style, footer: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "ghost", accessibilityLabel: cancelLabel, onPress: () => {
137
+ // Cancelling restores the committed value for the next open.
138
+ setPending(value);
139
+ onCancel?.();
140
+ setOpen(false);
141
+ }, children: cancelLabel }), (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "filled", accessibilityLabel: confirmLabel, disabled: pending === undefined, onPress: () => {
142
+ if (pending)
143
+ onConfirm?.(pending);
144
+ setOpen(false);
145
+ }, children: confirmLabel })] }), ...props, children: (0, jsx_runtime_1.jsx)(exports.Calendar, { value: pending, onChange: setPending, minDate: minDate, maxDate: maxDate, isDark: isDark, initialMonth: value }) }));
146
+ });
147
+ /** Splits "HH:MM" into parts, falling back to a sane default. */
148
+ function parseTime(value) {
149
+ const match = /^(\d{1,2}):(\d{2})$/.exec(value ?? "");
150
+ if (!match)
151
+ return { hour: 9, minute: 0 };
152
+ return {
153
+ hour: Math.min(Math.max(Number(match[1]), 0), 23),
154
+ minute: Math.min(Math.max(Number(match[2]), 0), 59),
155
+ };
156
+ }
157
+ /** Formats parts back to zero-padded "HH:MM". */
158
+ function formatTime(hour, minute) {
159
+ return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
160
+ }
161
+ /**
162
+ * A time picker: tappable hour/minute drums inside a Sheet.
163
+ *
164
+ * The drums are ordinary Pressables drawn by Glacia — not a native picker
165
+ * widget. Tapping a value selects it; the whole column scrolls in the Sheet.
166
+ */
167
+ exports.TimePicker = (0, react_1.forwardRef)(function TimePicker({ open: openProp, defaultOpen = false, onOpenChange, value, onConfirm, onCancel, use12Hour = false, title = "Select time", confirmLabel = "Confirm", cancelLabel = "Cancel", minuteStep = 1, style, isDark: isDarkProp, testID, ...props }, ref) {
168
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
169
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
170
+ const { View, Text: RNText, Pressable } = (0, primitives_1.primitives)();
171
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(defaultOpen);
172
+ const open = openProp !== undefined ? openProp : internalOpen;
173
+ const initial = parseTime(value);
174
+ const [hour, setHour] = (0, react_1.useState)(initial.hour);
175
+ const [minute, setMinute] = (0, react_1.useState)(initial.minute);
176
+ const setOpen = (next) => {
177
+ if (openProp === undefined)
178
+ setInternalOpen(next);
179
+ onOpenChange?.(next);
180
+ };
181
+ const hours = Array.from({ length: 24 }, (_, i) => i);
182
+ const minutes = Array.from({ length: Math.ceil(60 / minuteStep) }, (_, i) => i * minuteStep);
183
+ // In 12-hour mode the column shows 1–12; the stored hour stays 24-hour.
184
+ const displayHour = use12Hour ? (hour % 12 === 0 ? 12 : hour % 12) : hour;
185
+ const isPm = hour >= 12;
186
+ const column = (items, selected, onPick, label, pad = 2) => ((0, jsx_runtime_1.jsx)(View, { style: { height: 168, flex: 1 }, accessibilityRole: "none", accessibilityLabel: label, children: (0, jsx_runtime_1.jsx)(ScrollColumn, { items: items, selected: selected, onPick: onPick, isDark: isDark, pad: pad }) }));
187
+ return ((0, jsx_runtime_1.jsx)(Overlays_1.Sheet, { ref: ref, open: open, onOpenChange: setOpen, title: title, isDark: isDark, testID: testID, style: style, footer: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "ghost", accessibilityLabel: cancelLabel, onPress: () => {
188
+ setHour(initial.hour);
189
+ setMinute(initial.minute);
190
+ onCancel?.();
191
+ setOpen(false);
192
+ }, children: cancelLabel }), (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "filled", accessibilityLabel: confirmLabel, onPress: () => {
193
+ onConfirm?.(formatTime(hour, minute));
194
+ setOpen(false);
195
+ }, children: confirmLabel })] }), ...props, children: (0, jsx_runtime_1.jsxs)(View, { style: { flexDirection: "row", gap: 8, alignItems: "center" }, children: [column(use12Hour ? Array.from({ length: 12 }, (_, i) => i + 1) : hours, displayHour, (picked) => {
196
+ if (!use12Hour) {
197
+ setHour(picked);
198
+ return;
199
+ }
200
+ // Keep the current half of the day when the hour column changes.
201
+ setHour(picked % 12 === 0 ? (isPm ? 12 : 0) : isPm ? picked + 12 : picked);
202
+ }, "Hour"), (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 22, color: theme.muted }, children: ":" }), column(minutes, minute, setMinute, "Minute"), use12Hour && ((0, jsx_runtime_1.jsx)(View, { style: { flex: 1, gap: 6 }, children: ["AM", "PM"].map((label) => {
203
+ const active = label === "PM" ? isPm : !isPm;
204
+ return ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: label, accessibilityState: { selected: active }, onPress: () => {
205
+ const nextPm = label === "PM";
206
+ if (nextPm === isPm)
207
+ return;
208
+ setHour((current) => (nextPm ? (current % 12) + 12 : current % 12));
209
+ }, style: {
210
+ height: 34,
211
+ borderRadius: tokens_1.radii.sm,
212
+ alignItems: "center",
213
+ justifyContent: "center",
214
+ backgroundColor: active ? theme.glassActive : "transparent",
215
+ borderWidth: 0.5,
216
+ borderColor: active ? theme.edge : "transparent",
217
+ }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 14, fontWeight: active ? "600" : "500", color: active ? theme.foreground : theme.muted }, children: label }) }, label));
218
+ }) }))] }) }));
219
+ });
220
+ /** One scrollable column of the time drums. */
221
+ function ScrollColumn({ items, selected, onPick, isDark, pad, }) {
222
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
223
+ const { getScrollView } = (0, primitives_1.primitives)();
224
+ const ScrollView = getScrollView();
225
+ const { View, Text: RNText, Pressable } = (0, primitives_1.primitives)();
226
+ const rows = items.map((item) => {
227
+ const active = item === selected;
228
+ return ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: String(item).padStart(pad, "0"), accessibilityState: { selected: active }, onPress: () => onPick(item), style: {
229
+ height: 34,
230
+ alignItems: "center",
231
+ justifyContent: "center",
232
+ borderRadius: tokens_1.radii.sm,
233
+ backgroundColor: active ? theme.glassActive : "transparent",
234
+ }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 17, fontWeight: active ? "600" : "400", color: active ? theme.foreground : theme.muted }, children: String(item).padStart(pad, "0") }) }, item));
235
+ });
236
+ if (!ScrollView) {
237
+ // Off-device: no ScrollView, so the column is clipped to its height rather
238
+ // than truncated. Every value stays present and selectable, which is what
239
+ // the real drum shows once it can scroll.
240
+ return (0, jsx_runtime_1.jsx)(View, { style: { gap: 2, overflow: "hidden", height: 168 }, children: rows });
241
+ }
242
+ return ((0, jsx_runtime_1.jsx)(ScrollView, { showsVerticalScrollIndicator: false, style: { flex: 1 }, children: (0, jsx_runtime_1.jsx)(View, { style: { gap: 2 }, children: rows }) }));
243
+ }
@@ -7,6 +7,7 @@ const Glass_1 = require("./Glass");
7
7
  const tokens_1 = require("./internal/tokens");
8
8
  const primitives_1 = require("./internal/primitives");
9
9
  const motion_1 = require("./internal/motion");
10
+ const haptics_1 = require("./internal/haptics");
10
11
  const SEGMENT_HEIGHT = 30;
11
12
  /**
12
13
  * A row of mutually exclusive segments on one glass container.
@@ -15,7 +16,7 @@ const SEGMENT_HEIGHT = 30;
15
16
  * The pill's colours come from the theme tokens, so it reads correctly in both
16
17
  * light and dark instead of assuming a white-on-light palette.
17
18
  */
18
- exports.SegmentedControl = (0, react_1.forwardRef)(function SegmentedControl({ values, selectedIndex: selectedProp, defaultIndex = 0, onValueChange, style, isDark: isDarkProp, testID, ...props }, ref) {
19
+ exports.SegmentedControl = (0, react_1.forwardRef)(function SegmentedControl({ values, selectedIndex: selectedProp, defaultIndex = 0, onValueChange, haptics = true, style, isDark: isDarkProp, testID, ...props }, ref) {
19
20
  const isDark = (0, motion_1.useIsDark)(isDarkProp);
20
21
  const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
21
22
  const { View, Text, Pressable, Animated } = (0, primitives_1.primitives)();
@@ -26,6 +27,9 @@ exports.SegmentedControl = (0, react_1.forwardRef)(function SegmentedControl({ v
26
27
  // the first segment, values.length - 1 the last.
27
28
  const { value: slide, animated } = (0, motion_1.useSpring)(safeIndex, "snappy");
28
29
  const handleSelect = (index) => {
30
+ // Only a change ticks: re-pressing the current segment is not a selection.
31
+ if (haptics && index !== safeIndex)
32
+ (0, haptics_1.hapticSelection)();
29
33
  if (selectedProp === undefined)
30
34
  setInternalIndex(index);
31
35
  const val = values[index];
@@ -8,6 +8,7 @@ const Glass_1 = require("./Glass");
8
8
  const tokens_1 = require("./internal/tokens");
9
9
  const primitives_2 = require("./internal/primitives");
10
10
  const motion_1 = require("./internal/motion");
11
+ const haptics_1 = require("./internal/haptics");
11
12
  const TRACK_HEIGHT = 8;
12
13
  const THUMB = 22;
13
14
  const CONTAINER_HEIGHT = 40;
@@ -32,7 +33,7 @@ function quantize(value, min, max, step) {
32
33
  * sliders are not used, so the control looks and behaves the same on every
33
34
  * platform; only the glass material changes.
34
35
  */
35
- exports.Slider = (0, react_1.forwardRef)(function Slider({ value: valueProp, defaultValue = 50, min = 0, max = 100, step = 1, onValueChange, disabled = false, accessibilityLabel, style, isDark: isDarkProp, testID, ...props }, ref) {
36
+ exports.Slider = (0, react_1.forwardRef)(function Slider({ value: valueProp, defaultValue = 50, min = 0, max = 100, step = 1, onValueChange, disabled = false, haptics = true, accessibilityLabel, style, isDark: isDarkProp, testID, ...props }, ref) {
36
37
  const isDark = (0, motion_1.useIsDark)(isDarkProp);
37
38
  const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
38
39
  const { View, Pressable, PanResponder } = (0, primitives_2.primitives)();
@@ -55,8 +56,12 @@ exports.Slider = (0, react_1.forwardRef)(function Slider({ value: valueProp, def
55
56
  return quantize(min + ratio * (max - min), min, max, step);
56
57
  }, [max, min, step]);
57
58
  const report = (next) => {
59
+ // Only a real step change ticks: a drag that stays inside one step is not
60
+ // a selection, and reporting the same value twice must not buzz twice.
58
61
  if (next === valueRef.current)
59
62
  return;
63
+ if (haptics)
64
+ (0, haptics_1.hapticSelection)();
60
65
  valueRef.current = next;
61
66
  if (valueProp === undefined)
62
67
  setInternalValue(next);