glacia-native 0.4.0 → 0.5.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.
@@ -0,0 +1,474 @@
1
+ import { forwardRef, useCallback, useEffect, useState, type ReactNode } from "react";
2
+ import type { StyleProp, ViewStyle } from "react-native";
3
+
4
+ import { Glass } from "./Glass";
5
+ import { Text } from "./Text";
6
+ import { colors, radii } from "./internal/tokens";
7
+ import { primitives } from "./internal/primitives";
8
+ import { useIsDark, useSpring } from "./internal/motion";
9
+
10
+ /**
11
+ * Registers Android's hardware back while an overlay is open.
12
+ *
13
+ * Shared by Menu, Sheet and Drawer so every overlay dismisses the same way a
14
+ * Dialog does: the back button closes the layer, not the screen behind it.
15
+ */
16
+ function useBackDismiss(open: boolean, onDismiss: () => void) {
17
+ useEffect(() => {
18
+ if (!open) return;
19
+ const BackHandler = primitives().getBackHandler();
20
+ if (!BackHandler) return;
21
+ const sub = BackHandler.addEventListener("hardwareBackPress", () => {
22
+ onDismiss();
23
+ return true;
24
+ });
25
+ return () => sub.remove();
26
+ }, [open, onDismiss]);
27
+ }
28
+
29
+ /**
30
+ * Keeps a popover inside the window.
31
+ *
32
+ * Called with the trigger's measured frame and the popover's own size; returns
33
+ * the offsets that bring it back on screen. A menu near the bottom edge flips
34
+ * above its trigger rather than hanging off the display.
35
+ */
36
+ export function anchorPopover(
37
+ trigger: { x: number; y: number; width: number; height: number },
38
+ popover: { width: number; height: number },
39
+ viewport: { width: number; height: number },
40
+ gap = 6,
41
+ ): { left: number; top: number; flipped: boolean } {
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
+ // ---------------------------------------------------------------------------
52
+ // Menu
53
+
54
+ export interface MenuItem {
55
+ label: string;
56
+ onPress: () => void;
57
+ icon?: ReactNode;
58
+ disabled?: boolean;
59
+ /** Renders in the destructive tone, for delete-and-similar. */
60
+ destructive?: boolean;
61
+ accessibilityLabel?: string;
62
+ }
63
+
64
+ export interface MenuProps {
65
+ /** The control that opens the menu. */
66
+ trigger: ReactNode;
67
+ items: MenuItem[];
68
+ /** Controlled open state. Omit and the menu keeps its own. */
69
+ open?: boolean;
70
+ onOpenChange?: (open: boolean) => void;
71
+ /** The menu's width. The list scrolls if the items exceed the screen. */
72
+ width?: number;
73
+ accessibilityLabel?: string;
74
+ style?: StyleProp<ViewStyle>;
75
+ isDark?: boolean;
76
+ testID?: string;
77
+ }
78
+
79
+ /**
80
+ * An anchored dropdown menu.
81
+ *
82
+ * A press outside or on an item closes it; Android's back button does too. The
83
+ * popover is a glass surface with `glassActive` rows, matching the way the
84
+ * library marks a tappable row rather than nesting another glass panel.
85
+ */
86
+ export const Menu = forwardRef<any, MenuProps>(function Menu(
87
+ {
88
+ trigger,
89
+ items,
90
+ open: openProp,
91
+ onOpenChange,
92
+ width = 220,
93
+ accessibilityLabel = "Menu",
94
+ style,
95
+ isDark: isDarkProp,
96
+ testID,
97
+ ...props
98
+ },
99
+ ref,
100
+ ) {
101
+ const isDark = useIsDark(isDarkProp);
102
+ const theme = isDark ? colors.dark : colors.light;
103
+ const { View, Text: RNText, Pressable, Modal } = primitives();
104
+
105
+ const [internalOpen, setInternalOpen] = useState(false);
106
+ const open = openProp !== undefined ? openProp : internalOpen;
107
+
108
+ const setOpen = useCallback(
109
+ (next: boolean) => {
110
+ if (openProp === undefined) setInternalOpen(next);
111
+ onOpenChange?.(next);
112
+ },
113
+ [openProp, onOpenChange],
114
+ );
115
+
116
+ const dismiss = useCallback(() => setOpen(false), [setOpen]);
117
+ useBackDismiss(open, dismiss);
118
+
119
+ const { value: enter, animated } = useSpring(open ? 1 : 0, "snappy");
120
+ const scale = animated ? enter.interpolate({ inputRange: [0, 1], outputRange: [0.96, 1] }) : 1;
121
+
122
+ const popover = (
123
+ <Glass
124
+ effect="regular"
125
+ isDark={isDark}
126
+ testID={testID ? `${testID}-popover` : undefined}
127
+ style={{ width, borderRadius: radii.md, paddingVertical: 4, overflow: "hidden", ...(style as ViewStyle) }}
128
+ >
129
+ {items.map((item, index) => (
130
+ <Pressable
131
+ key={`${item.label}-${index}`}
132
+ accessibilityRole="button"
133
+ accessibilityLabel={item.accessibilityLabel ?? item.label}
134
+ accessibilityState={{ disabled: item.disabled }}
135
+ disabled={item.disabled}
136
+ onPress={() => {
137
+ setOpen(false);
138
+ item.onPress();
139
+ }}
140
+ style={{ flexDirection: "row", alignItems: "center", gap: 10, paddingVertical: 10, paddingHorizontal: 12, opacity: item.disabled ? 0.38 : 1 }}
141
+ >
142
+ {item.icon ? <View>{item.icon}</View> : null}
143
+ <RNText
144
+ numberOfLines={1}
145
+ style={{ fontSize: 15, color: item.destructive ? theme.destructive : theme.foreground }}
146
+ >
147
+ {item.label}
148
+ </RNText>
149
+ </Pressable>
150
+ ))}
151
+ </Glass>
152
+ );
153
+
154
+ return (
155
+ <View ref={ref} style={{ alignSelf: "flex-start" }} {...props}>
156
+ <Pressable
157
+ accessibilityRole="button"
158
+ accessibilityLabel={accessibilityLabel}
159
+ accessibilityState={{ expanded: open }}
160
+ onPress={() => setOpen(!open)}
161
+ >
162
+ {trigger}
163
+ </Pressable>
164
+
165
+ <Modal visible={open} transparent animationType="fade" onRequestClose={dismiss}>
166
+ {/* The backdrop: any press outside the popover dismisses the menu. */}
167
+ <Pressable
168
+ accessibilityRole="button"
169
+ accessibilityLabel="Close menu"
170
+ onPress={dismiss}
171
+ style={{ flex: 1, backgroundColor: theme.scrim }}
172
+ >
173
+ <View style={{ flex: 1, alignItems: "center", justifyContent: "center", padding: 24 }}>
174
+ <Pressable accessibilityRole="none" onPress={() => {}}>
175
+ {animated ? (
176
+ <AnimatedScale scale={scale}>{popover}</AnimatedScale>
177
+ ) : (
178
+ popover
179
+ )}
180
+ </Pressable>
181
+ </View>
182
+ </Pressable>
183
+ </Modal>
184
+ </View>
185
+ );
186
+ });
187
+
188
+ /** Wraps a popover in the entrance scale, which must land on an Animated.View. */
189
+ function AnimatedScale({ scale, children }: { scale: any; children: ReactNode }) {
190
+ const { Animated } = primitives();
191
+ return <Animated.View style={{ transform: [{ scale }] }}>{children}</Animated.View>;
192
+ }
193
+
194
+ // ---------------------------------------------------------------------------
195
+ // Sheet
196
+
197
+ export interface SheetProps {
198
+ /** Controlled visibility. Omit and the sheet keeps its own. */
199
+ open?: boolean;
200
+ defaultOpen?: boolean;
201
+ onOpenChange?: (open: boolean) => void;
202
+ title?: string;
203
+ children?: ReactNode;
204
+ footer?: ReactNode;
205
+ /** Tapping the scrim dismisses. */
206
+ dismissOnBackdropPress?: boolean;
207
+ /** The drag handle at the top of the panel. */
208
+ showHandle?: boolean;
209
+ style?: StyleProp<ViewStyle>;
210
+ isDark?: boolean;
211
+ testID?: string;
212
+ }
213
+
214
+ /**
215
+ * A bottom sheet.
216
+ *
217
+ * The general-purpose container: it slides up on the springs token, dismisses
218
+ * on a scrim press and on Android back. ActionSheet remains the action-menu
219
+ * specialisation of this shape; Sheet is for arbitrary content — a form, a
220
+ * calendar, a detail panel.
221
+ */
222
+ export const Sheet = forwardRef<any, SheetProps>(function Sheet(
223
+ {
224
+ open: openProp,
225
+ defaultOpen = false,
226
+ onOpenChange,
227
+ title,
228
+ children,
229
+ footer,
230
+ dismissOnBackdropPress = true,
231
+ showHandle = true,
232
+ style,
233
+ isDark: isDarkProp,
234
+ testID,
235
+ ...props
236
+ },
237
+ ref,
238
+ ) {
239
+ const isDark = useIsDark(isDarkProp);
240
+ const theme = isDark ? colors.dark : colors.light;
241
+ const { View, Text: RNText, Pressable, Modal } = primitives();
242
+
243
+ const [internalOpen, setInternalOpen] = useState(defaultOpen);
244
+ const open = openProp !== undefined ? openProp : internalOpen;
245
+
246
+ const setOpen = useCallback(
247
+ (next: boolean) => {
248
+ if (openProp === undefined) setInternalOpen(next);
249
+ onOpenChange?.(next);
250
+ },
251
+ [openProp, onOpenChange],
252
+ );
253
+
254
+ const dismiss = useCallback(() => setOpen(false), [setOpen]);
255
+ useBackDismiss(open, dismiss);
256
+
257
+ // 0 is fully dismissed, 1 fully presented; the panel rides up this value.
258
+ const { value: rise, animated } = useSpring(open ? 1 : 0, "gentle");
259
+ const translateY = animated ? rise.interpolate({ inputRange: [0, 1], outputRange: [420, 0] }) : 0;
260
+
261
+ return (
262
+ <Modal visible={open} transparent animationType="fade" onRequestClose={dismiss}>
263
+ <Pressable
264
+ accessibilityRole="button"
265
+ accessibilityLabel="Close sheet"
266
+ onPress={dismissOnBackdropPress ? dismiss : undefined}
267
+ style={{ flex: 1, justifyContent: "flex-end", backgroundColor: theme.scrim }}
268
+ >
269
+ <Pressable accessibilityRole="none" onPress={() => {}} style={{ width: "100%" }}>
270
+ {animated ? (
271
+ <SheetRise translateY={translateY}>
272
+ <SheetPanel
273
+ title={title}
274
+ footer={footer}
275
+ showHandle={showHandle}
276
+ isDark={isDark}
277
+ testID={testID}
278
+ style={style}
279
+ >
280
+ {children}
281
+ </SheetPanel>
282
+ </SheetRise>
283
+ ) : (
284
+ <SheetPanel
285
+ title={title}
286
+ footer={footer}
287
+ showHandle={showHandle}
288
+ isDark={isDark}
289
+ testID={testID}
290
+ style={style}
291
+ >
292
+ {children}
293
+ </SheetPanel>
294
+ )}
295
+ </Pressable>
296
+ </Pressable>
297
+ </Modal>
298
+ );
299
+ });
300
+
301
+ function SheetRise({ translateY, children }: { translateY: any; children: ReactNode }) {
302
+ const { Animated } = primitives();
303
+ return <Animated.View style={{ transform: [{ translateY }] }}>{children}</Animated.View>;
304
+ }
305
+
306
+ /** The panel itself: a glass surface with a handle, a title and a footer. */
307
+ function SheetPanel({
308
+ title,
309
+ footer,
310
+ showHandle,
311
+ isDark,
312
+ testID,
313
+ style,
314
+ children,
315
+ }: {
316
+ title?: string;
317
+ footer?: ReactNode;
318
+ showHandle: boolean;
319
+ isDark: boolean;
320
+ testID?: string;
321
+ style?: StyleProp<ViewStyle>;
322
+ children?: ReactNode;
323
+ }) {
324
+ const theme = isDark ? colors.dark : colors.light;
325
+ const { View, Text: RNText } = primitives();
326
+
327
+ return (
328
+ <Glass
329
+ effect="regular"
330
+ isDark={isDark}
331
+ testID={testID}
332
+ style={{
333
+ borderTopLeftRadius: radii.xl,
334
+ borderTopRightRadius: radii.xl,
335
+ borderBottomLeftRadius: 0,
336
+ borderBottomRightRadius: 0,
337
+ paddingBottom: 20,
338
+ maxHeight: 560,
339
+ ...(style as ViewStyle),
340
+ }}
341
+ >
342
+ {showHandle ? (
343
+ <View style={{ alignItems: "center", paddingTop: 8, paddingBottom: 4 }}>
344
+ <View
345
+ accessibilityElementsHidden
346
+ // A capsule: the radius is half the handle's own height.
347
+ style={{ width: 36, height: 5, borderRadius: 5 / 2, backgroundColor: theme.separator }}
348
+ />
349
+ </View>
350
+ ) : null}
351
+ {title ? (
352
+ <View style={{ paddingHorizontal: 20, paddingVertical: 10 }}>
353
+ <RNText accessibilityRole="heading" style={{ fontSize: 17, fontWeight: "600", color: theme.foreground }}>
354
+ {title}
355
+ </RNText>
356
+ </View>
357
+ ) : null}
358
+ <View style={{ paddingHorizontal: 20 }}>{children}</View>
359
+ {footer ? <View style={{ paddingHorizontal: 20, paddingTop: 12, gap: 8 }}>{footer}</View> : null}
360
+ </Glass>
361
+ );
362
+ }
363
+
364
+ // ---------------------------------------------------------------------------
365
+ // Drawer
366
+
367
+ export interface DrawerProps {
368
+ /** Controlled visibility. Omit and the drawer keeps its own. */
369
+ open?: boolean;
370
+ defaultOpen?: boolean;
371
+ onOpenChange?: (open: boolean) => void;
372
+ /** The edge the panel slides in from. */
373
+ side?: "left" | "right";
374
+ /** Panel width. */
375
+ width?: number;
376
+ children?: ReactNode;
377
+ /** Tapping the scrim dismisses. */
378
+ dismissOnBackdropPress?: boolean;
379
+ style?: StyleProp<ViewStyle>;
380
+ isDark?: boolean;
381
+ testID?: string;
382
+ }
383
+
384
+ /**
385
+ * A side navigation drawer.
386
+ *
387
+ * The same overlay discipline as Sheet — scrim, spring slide, Android back —
388
+ * but anchored to a side edge, with a square panel that runs the full height.
389
+ * The caller supplies the content, typically a List.
390
+ */
391
+ export const Drawer = forwardRef<any, DrawerProps>(function Drawer(
392
+ {
393
+ open: openProp,
394
+ defaultOpen = false,
395
+ onOpenChange,
396
+ side = "left",
397
+ width = 280,
398
+ children,
399
+ dismissOnBackdropPress = true,
400
+ style,
401
+ isDark: isDarkProp,
402
+ testID,
403
+ ...props
404
+ },
405
+ ref,
406
+ ) {
407
+ const isDark = useIsDark(isDarkProp);
408
+ const theme = isDark ? colors.dark : colors.light;
409
+ const { View, Pressable, Modal } = primitives();
410
+
411
+ const [internalOpen, setInternalOpen] = useState(defaultOpen);
412
+ const open = openProp !== undefined ? openProp : internalOpen;
413
+
414
+ const setOpen = useCallback(
415
+ (next: boolean) => {
416
+ if (openProp === undefined) setInternalOpen(next);
417
+ onOpenChange?.(next);
418
+ },
419
+ [openProp, onOpenChange],
420
+ );
421
+
422
+ const dismiss = useCallback(() => setOpen(false), [setOpen]);
423
+ useBackDismiss(open, dismiss);
424
+
425
+ const { value: slide, animated } = useSpring(open ? 1 : 0, "gentle");
426
+ // Slide in from off-screen: negative from the left, positive from the right.
427
+ const translateX = animated
428
+ ? slide.interpolate({ inputRange: [0, 1], outputRange: [side === "left" ? -width : width, 0] })
429
+ : 0;
430
+
431
+ const panel = (
432
+ <Glass
433
+ effect="regular"
434
+ isDark={isDark}
435
+ testID={testID}
436
+ style={{
437
+ width,
438
+ height: "100%",
439
+ borderRadius: 0,
440
+ paddingTop: 56,
441
+ ...(style as ViewStyle),
442
+ }}
443
+ >
444
+ {children}
445
+ </Glass>
446
+ );
447
+
448
+ return (
449
+ <Modal visible={open} transparent animationType="fade" onRequestClose={dismiss}>
450
+ <Pressable
451
+ accessibilityRole="button"
452
+ accessibilityLabel="Close drawer"
453
+ onPress={dismissOnBackdropPress ? dismiss : undefined}
454
+ style={{ flex: 1, backgroundColor: theme.scrim }}
455
+ >
456
+ <View
457
+ style={{
458
+ flex: 1,
459
+ alignItems: side === "left" ? "flex-start" : "flex-end",
460
+ }}
461
+ >
462
+ <Pressable accessibilityRole="none" onPress={() => {}} style={{ height: "100%" }}>
463
+ {animated ? <DrawerSlide translateX={translateX}>{panel}</DrawerSlide> : panel}
464
+ </Pressable>
465
+ </View>
466
+ </Pressable>
467
+ </Modal>
468
+ );
469
+ });
470
+
471
+ function DrawerSlide({ translateX, children }: { translateX: any; children: ReactNode }) {
472
+ const { Animated } = primitives();
473
+ return <Animated.View style={{ height: "100%", transform: [{ translateX }] }}>{children}</Animated.View>;
474
+ }