glacia-native 0.5.0 → 0.5.2
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/LICENSE +29 -4
- package/README.md +27 -0
- package/lib/module/Bars.js +103 -9
- package/lib/module/Button.js +11 -8
- package/lib/module/Card.js +2 -2
- package/lib/module/Dialog.js +16 -17
- package/lib/module/Display.js +86 -8
- package/lib/module/Feedback.js +244 -21
- package/lib/module/Form.js +29 -33
- package/lib/module/Forms2.js +257 -13
- package/lib/module/Glass.js +2 -2
- package/lib/module/Input.js +12 -10
- package/lib/module/Message.js +84 -0
- package/lib/module/Overlays.js +195 -17
- package/lib/module/Pickers.js +16 -16
- package/lib/module/SegmentedControl.js +24 -11
- package/lib/module/Slider.js +24 -10
- package/lib/module/Switch.js +25 -10
- package/lib/module/TabBar.js +23 -13
- package/lib/module/Table.js +4 -3
- package/lib/module/Tabs.js +3 -2
- package/lib/module/ai.js +26 -18
- package/lib/module/charts/BarChart.js +11 -3
- package/lib/module/index.js +23 -2
- package/lib/module/internal/GlassContainer.js +54 -0
- package/lib/module/internal/GlassView.js +10 -2
- package/lib/module/internal/a11y.js +94 -0
- package/lib/module/internal/blur.js +78 -0
- package/lib/module/internal/fallback.js +120 -19
- package/lib/module/internal/glass-routing.js +7 -0
- package/lib/module/internal/glow.js +36 -0
- package/lib/module/internal/haptics.js +54 -0
- package/lib/module/internal/lens.js +70 -0
- package/lib/module/internal/material.js +64 -0
- package/lib/module/internal/motion.js +10 -1
- package/lib/module/internal/primitives.js +70 -4
- package/lib/module/internal/rim.js +35 -0
- package/lib/module/internal/tokens.js +80 -2
- package/lib/typescript/Bars.d.ts +67 -0
- package/lib/typescript/Display.d.ts +59 -0
- package/lib/typescript/Feedback.d.ts +58 -0
- package/lib/typescript/Forms2.d.ts +81 -0
- package/lib/typescript/Message.d.ts +67 -0
- package/lib/typescript/Overlays.d.ts +41 -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/index.d.ts +9 -6
- package/lib/typescript/internal/GlassContainer.d.ts +37 -0
- package/lib/typescript/internal/GlassView.d.ts +21 -0
- package/lib/typescript/internal/a11y.d.ts +19 -0
- package/lib/typescript/internal/blur.d.ts +24 -0
- package/lib/typescript/internal/fallback.d.ts +32 -2
- package/lib/typescript/internal/glass-routing.d.ts +10 -0
- package/lib/typescript/internal/glow.d.ts +11 -0
- package/lib/typescript/internal/haptics.d.ts +7 -0
- package/lib/typescript/internal/lens.d.ts +47 -0
- package/lib/typescript/internal/material.d.ts +27 -0
- package/lib/typescript/internal/motion.d.ts +4 -0
- package/lib/typescript/internal/primitives.d.ts +23 -0
- package/lib/typescript/internal/rim.d.ts +16 -0
- package/lib/typescript/internal/tokens.d.ts +89 -0
- package/package.json +12 -3
- package/src/Bars.tsx +239 -2
- package/src/Button.tsx +16 -9
- package/src/Card.tsx +2 -2
- package/src/Dialog.tsx +37 -26
- package/src/Display.tsx +237 -6
- package/src/Feedback.tsx +448 -35
- package/src/Form.tsx +60 -40
- package/src/Forms2.tsx +455 -8
- package/src/Glass.tsx +4 -0
- package/src/Input.tsx +15 -11
- package/src/Message.tsx +218 -0
- package/src/Overlays.tsx +269 -39
- package/src/Pickers.tsx +11 -6
- package/src/SegmentedControl.tsx +39 -9
- package/src/Slider.tsx +26 -10
- package/src/Switch.tsx +29 -10
- package/src/TabBar.tsx +39 -12
- package/src/Table.tsx +4 -3
- package/src/Tabs.tsx +5 -0
- package/src/ai.tsx +37 -23
- package/src/charts/BarChart.tsx +41 -3
- package/src/index.ts +43 -1
- package/src/internal/GlassContainer.tsx +82 -0
- package/src/internal/GlassView.tsx +25 -0
- package/src/internal/a11y.ts +105 -0
- package/src/internal/blur.ts +90 -0
- package/src/internal/fallback.tsx +170 -15
- package/src/internal/glass-routing.ts +17 -0
- package/src/internal/glow.tsx +62 -0
- package/src/internal/haptics.ts +59 -0
- package/src/internal/lens.tsx +105 -0
- package/src/internal/material.ts +92 -0
- package/src/internal/motion.ts +10 -1
- package/src/internal/primitives.ts +83 -4
- package/src/internal/rim.tsx +79 -0
- package/src/internal/tokens.ts +82 -1
package/src/Message.tsx
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { forwardRef, type ReactNode } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
4
|
+
import { Avatar } from "./Display";
|
|
5
|
+
import { Glass } from "./Glass";
|
|
6
|
+
import { colors, radii } from "./internal/tokens";
|
|
7
|
+
import { primitives } from "./internal/primitives";
|
|
8
|
+
import { useIsDark } from "./internal/motion";
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// MessageBubble
|
|
12
|
+
|
|
13
|
+
export interface MessageBubbleProps {
|
|
14
|
+
/** Whether the message is the device owner's (right, tinted) or not (left). */
|
|
15
|
+
outgoing?: boolean;
|
|
16
|
+
children?: ReactNode;
|
|
17
|
+
style?: StyleProp<ViewStyle>;
|
|
18
|
+
isDark?: boolean;
|
|
19
|
+
testID?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* One message's bubble.
|
|
24
|
+
*
|
|
25
|
+
* The bubble is a glass surface like the rest of the library (panel profile),
|
|
26
|
+
* so it carries the same blurred backdrop, lit rim and shadow as a card. The
|
|
27
|
+
* outgoing direction is tinted with `glassActive` — the theme's selected
|
|
28
|
+
* tint — and the incoming one uses the plain glass token, which is what keeps
|
|
29
|
+
* the two directions distinguishable without a second material.
|
|
30
|
+
*/
|
|
31
|
+
export const MessageBubble = forwardRef<any, MessageBubbleProps>(function MessageBubble(
|
|
32
|
+
{ outgoing = false, children, style, isDark: isDarkProp, testID, ...props },
|
|
33
|
+
ref,
|
|
34
|
+
) {
|
|
35
|
+
const isDark = useIsDark(isDarkProp);
|
|
36
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
37
|
+
const { View } = primitives();
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<Glass
|
|
41
|
+
ref={ref}
|
|
42
|
+
testID={testID}
|
|
43
|
+
surface="panel"
|
|
44
|
+
isDark={isDark}
|
|
45
|
+
tintColor={outgoing ? theme.glassActive : undefined}
|
|
46
|
+
style={{
|
|
47
|
+
alignSelf: outgoing ? "flex-end" : "flex-start",
|
|
48
|
+
maxWidth: "82%",
|
|
49
|
+
paddingHorizontal: 14,
|
|
50
|
+
paddingVertical: 10,
|
|
51
|
+
borderRadius: radii.lg,
|
|
52
|
+
...(style as ViewStyle),
|
|
53
|
+
}}
|
|
54
|
+
{...props}
|
|
55
|
+
>
|
|
56
|
+
{children}
|
|
57
|
+
</Glass>
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// MessageAttachment
|
|
63
|
+
|
|
64
|
+
export interface MessageAttachmentProps {
|
|
65
|
+
/** The file's name, shown and used as the accessible label. */
|
|
66
|
+
name: string;
|
|
67
|
+
/** A human-readable size, e.g. "1.2 MB". */
|
|
68
|
+
size?: string;
|
|
69
|
+
/** A leading node, typically an SVG icon. */
|
|
70
|
+
icon?: ReactNode;
|
|
71
|
+
/** Makes the chip tappable, e.g. to open the file. */
|
|
72
|
+
onPress?: () => void;
|
|
73
|
+
disabled?: boolean;
|
|
74
|
+
style?: StyleProp<ViewStyle>;
|
|
75
|
+
isDark?: boolean;
|
|
76
|
+
testID?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A file chip for a message.
|
|
81
|
+
*
|
|
82
|
+
* A flat, bordered chip rather than a glass one, for the same reason the
|
|
83
|
+
* bubbles are flat: it sits inside a bubble or beside one, and glass inside
|
|
84
|
+
* glass is never allowed. The size is a caller-formatted string — byte
|
|
85
|
+
* formatting belongs to the app's locale, not to a component library.
|
|
86
|
+
*/
|
|
87
|
+
export const MessageAttachment = forwardRef<any, MessageAttachmentProps>(
|
|
88
|
+
function MessageAttachment(
|
|
89
|
+
{ name, size, icon, onPress, disabled = false, style, isDark: isDarkProp, testID, ...props },
|
|
90
|
+
ref,
|
|
91
|
+
) {
|
|
92
|
+
const isDark = useIsDark(isDarkProp);
|
|
93
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
94
|
+
const { View, Text, Pressable } = primitives();
|
|
95
|
+
|
|
96
|
+
const body = (
|
|
97
|
+
<>
|
|
98
|
+
{icon ? <View style={{ marginRight: 8 }}>{icon}</View> : null}
|
|
99
|
+
<View style={{ flexShrink: 1 }}>
|
|
100
|
+
<Text numberOfLines={1} style={{ fontSize: 14, color: theme.foreground }}>
|
|
101
|
+
{name}
|
|
102
|
+
</Text>
|
|
103
|
+
{size ? (
|
|
104
|
+
<Text style={{ fontSize: 11, color: theme.muted }}>{size}</Text>
|
|
105
|
+
) : null}
|
|
106
|
+
</View>
|
|
107
|
+
</>
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const chipStyle: ViewStyle = {
|
|
111
|
+
flexDirection: "row",
|
|
112
|
+
alignItems: "center",
|
|
113
|
+
paddingHorizontal: 12,
|
|
114
|
+
paddingVertical: 8,
|
|
115
|
+
borderRadius: radii.md,
|
|
116
|
+
backgroundColor: theme.wash,
|
|
117
|
+
borderWidth: 0.5,
|
|
118
|
+
borderColor: theme.separator,
|
|
119
|
+
opacity: disabled ? 0.38 : 1,
|
|
120
|
+
...(style as ViewStyle),
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
if (!onPress) {
|
|
124
|
+
return (
|
|
125
|
+
<View ref={ref} testID={testID} accessibilityRole="text" accessibilityLabel={size ? `${name}, ${size}` : name} style={chipStyle} {...props}>
|
|
126
|
+
{body}
|
|
127
|
+
</View>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<Pressable
|
|
133
|
+
ref={ref}
|
|
134
|
+
testID={testID}
|
|
135
|
+
accessibilityRole="button"
|
|
136
|
+
accessibilityLabel={size ? `${name}, ${size}` : name}
|
|
137
|
+
accessibilityState={{ disabled }}
|
|
138
|
+
disabled={disabled}
|
|
139
|
+
onPress={onPress}
|
|
140
|
+
style={chipStyle}
|
|
141
|
+
{...props}
|
|
142
|
+
>
|
|
143
|
+
{body}
|
|
144
|
+
</Pressable>
|
|
145
|
+
);
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// ---------------------------------------------------------------------------
|
|
150
|
+
// Message
|
|
151
|
+
|
|
152
|
+
export interface MessageProps {
|
|
153
|
+
/** The sender's name, shown above the content. */
|
|
154
|
+
name?: string;
|
|
155
|
+
/** A time or timestamp, shown beside the name. */
|
|
156
|
+
time?: string;
|
|
157
|
+
/** Draws an Avatar showing `name`'s initials beside the content. */
|
|
158
|
+
avatar?: boolean;
|
|
159
|
+
/** The bubble direction. */
|
|
160
|
+
outgoing?: boolean;
|
|
161
|
+
/** The message's content: text, or a bubble, or a bubble with attachments. */
|
|
162
|
+
children?: ReactNode;
|
|
163
|
+
style?: StyleProp<ViewStyle>;
|
|
164
|
+
isDark?: boolean;
|
|
165
|
+
testID?: string;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A whole message: who sent it, when, and what they said.
|
|
170
|
+
*
|
|
171
|
+
* The row every chat screen builds by hand — an avatar, a name and time line,
|
|
172
|
+
* and the content under it. Grouping it means the alignment, spacing and
|
|
173
|
+
* accessibility grouping are decided once: the row is announced as a single
|
|
174
|
+
* message (name, time, content) rather than as three unrelated pieces of text.
|
|
175
|
+
*/
|
|
176
|
+
export const Message = forwardRef<any, MessageProps>(function Message(
|
|
177
|
+
{ name, time, avatar = false, outgoing = false, children, style, isDark: isDarkProp, testID, ...props },
|
|
178
|
+
ref,
|
|
179
|
+
) {
|
|
180
|
+
const isDark = useIsDark(isDarkProp);
|
|
181
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
182
|
+
const { View, Text } = primitives();
|
|
183
|
+
|
|
184
|
+
const meta = [name, time].filter(Boolean).join(", ");
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<View
|
|
188
|
+
ref={ref}
|
|
189
|
+
testID={testID}
|
|
190
|
+
accessibilityRole="text"
|
|
191
|
+
accessibilityLabel={meta || undefined}
|
|
192
|
+
style={{
|
|
193
|
+
flexDirection: outgoing ? "row-reverse" : "row",
|
|
194
|
+
alignItems: "flex-end",
|
|
195
|
+
gap: 8,
|
|
196
|
+
paddingHorizontal: 4,
|
|
197
|
+
paddingVertical: 4,
|
|
198
|
+
...(style as ViewStyle),
|
|
199
|
+
}}
|
|
200
|
+
{...props}
|
|
201
|
+
>
|
|
202
|
+
{avatar ? (
|
|
203
|
+
<Avatar name={name ?? ""} size="sm" isDark={isDark} />
|
|
204
|
+
) : null}
|
|
205
|
+
<View style={{ flexShrink: 1, alignItems: outgoing ? "flex-end" : "flex-start", gap: 2 }}>
|
|
206
|
+
{name || time ? (
|
|
207
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 6 }}>
|
|
208
|
+
{name ? (
|
|
209
|
+
<Text style={{ fontSize: 12, fontWeight: "600", color: theme.foreground }}>{name}</Text>
|
|
210
|
+
) : null}
|
|
211
|
+
{time ? <Text style={{ fontSize: 11, color: theme.muted }}>{time}</Text> : null}
|
|
212
|
+
</View>
|
|
213
|
+
) : null}
|
|
214
|
+
{children}
|
|
215
|
+
</View>
|
|
216
|
+
</View>
|
|
217
|
+
);
|
|
218
|
+
});
|
package/src/Overlays.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { forwardRef, useCallback, useEffect, useState, type ReactNode } from "react";
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
|
|
2
2
|
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
3
|
|
|
4
4
|
import { Glass } from "./Glass";
|
|
@@ -6,6 +6,7 @@ import { Text } from "./Text";
|
|
|
6
6
|
import { colors, radii } from "./internal/tokens";
|
|
7
7
|
import { primitives } from "./internal/primitives";
|
|
8
8
|
import { useIsDark, useSpring } from "./internal/motion";
|
|
9
|
+
import { hapticSelection } from "./internal/haptics";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Registers Android's hardware back while an overlay is open.
|
|
@@ -122,9 +123,21 @@ export const Menu = forwardRef<any, MenuProps>(function Menu(
|
|
|
122
123
|
const popover = (
|
|
123
124
|
<Glass
|
|
124
125
|
effect="regular"
|
|
126
|
+
surface="overlay"
|
|
125
127
|
isDark={isDark}
|
|
126
128
|
testID={testID ? `${testID}-popover` : undefined}
|
|
127
|
-
style={{
|
|
129
|
+
style={{
|
|
130
|
+
width,
|
|
131
|
+
borderRadius: radii.lg,
|
|
132
|
+
paddingVertical: 4,
|
|
133
|
+
// The web menu is deliberately near-solid so its labels stay legible
|
|
134
|
+
// over any backdrop, and it floats on a real shadow.
|
|
135
|
+
backgroundColor: theme.surface,
|
|
136
|
+
shadowOpacity: 0.12,
|
|
137
|
+
shadowRadius: 40,
|
|
138
|
+
shadowOffset: { width: 0, height: 12 },
|
|
139
|
+
...(style as ViewStyle),
|
|
140
|
+
}}
|
|
128
141
|
>
|
|
129
142
|
{items.map((item, index) => (
|
|
130
143
|
<Pressable
|
|
@@ -137,7 +150,7 @@ export const Menu = forwardRef<any, MenuProps>(function Menu(
|
|
|
137
150
|
setOpen(false);
|
|
138
151
|
item.onPress();
|
|
139
152
|
}}
|
|
140
|
-
style={{ flexDirection: "row", alignItems: "center", gap: 10, paddingVertical:
|
|
153
|
+
style={{ flexDirection: "row", alignItems: "center", gap: 10, minHeight: 40, paddingVertical: 8, paddingHorizontal: 12, opacity: item.disabled ? 0.38 : 1 }}
|
|
141
154
|
>
|
|
142
155
|
{item.icon ? <View>{item.icon}</View> : null}
|
|
143
156
|
<RNText
|
|
@@ -206,6 +219,24 @@ export interface SheetProps {
|
|
|
206
219
|
dismissOnBackdropPress?: boolean;
|
|
207
220
|
/** The drag handle at the top of the panel. */
|
|
208
221
|
showHandle?: boolean;
|
|
222
|
+
/**
|
|
223
|
+
* Resting heights the sheet can settle at, smallest first. A number is
|
|
224
|
+
* points; a fraction below 1 is a share of the window height. With one
|
|
225
|
+
* point the sheet opens there; with several it can be dragged between them.
|
|
226
|
+
* Omitted, the sheet uses its own natural height.
|
|
227
|
+
*/
|
|
228
|
+
snapPoints?: number[];
|
|
229
|
+
/** Which snap point to open at. Defaults to the first. */
|
|
230
|
+
initialSnapIndex?: number;
|
|
231
|
+
/** Controlled snap point. Omit and the sheet tracks its own. */
|
|
232
|
+
snapIndex?: number;
|
|
233
|
+
onSnapIndexChange?: (index: number) => void;
|
|
234
|
+
/** Dragging below the lowest point dismisses instead of springing back. */
|
|
235
|
+
dismissOnSnapToBottom?: boolean;
|
|
236
|
+
/** Turns the drag gesture off, leaving press-and-back dismissal only. */
|
|
237
|
+
draggable?: boolean;
|
|
238
|
+
/** Opt out of the selection tick on snap changes. */
|
|
239
|
+
haptics?: boolean;
|
|
209
240
|
style?: StyleProp<ViewStyle>;
|
|
210
241
|
isDark?: boolean;
|
|
211
242
|
testID?: string;
|
|
@@ -219,6 +250,36 @@ export interface SheetProps {
|
|
|
219
250
|
* specialisation of this shape; Sheet is for arbitrary content — a form, a
|
|
220
251
|
* calendar, a detail panel.
|
|
221
252
|
*/
|
|
253
|
+
/**
|
|
254
|
+
* Turns a snap point into a pixel height.
|
|
255
|
+
*
|
|
256
|
+
* A number under 1 is a fraction of the window; anything else is points. The
|
|
257
|
+
* result is clamped so a sheet can never be taller than the space it has.
|
|
258
|
+
*/
|
|
259
|
+
export function resolveSnapPoint(point: number, windowHeight: number): number {
|
|
260
|
+
const height = point > 0 && point < 1 ? point * windowHeight : point;
|
|
261
|
+
return Math.max(0, Math.min(height, windowHeight));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Picks the snap point nearest to where the user let go.
|
|
266
|
+
*
|
|
267
|
+
* `height` is how tall the sheet currently is. Ties go to the smaller sheet,
|
|
268
|
+
* so a small flick does not jump the panel to full height.
|
|
269
|
+
*/
|
|
270
|
+
export function nearestSnapPoint(heights: number[], height: number): number {
|
|
271
|
+
let best = 0;
|
|
272
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
273
|
+
heights.forEach((candidate, index) => {
|
|
274
|
+
const distance = Math.abs(candidate - height);
|
|
275
|
+
if (distance < bestDistance) {
|
|
276
|
+
bestDistance = distance;
|
|
277
|
+
best = index;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
return best;
|
|
281
|
+
}
|
|
282
|
+
|
|
222
283
|
export const Sheet = forwardRef<any, SheetProps>(function Sheet(
|
|
223
284
|
{
|
|
224
285
|
open: openProp,
|
|
@@ -229,6 +290,13 @@ export const Sheet = forwardRef<any, SheetProps>(function Sheet(
|
|
|
229
290
|
footer,
|
|
230
291
|
dismissOnBackdropPress = true,
|
|
231
292
|
showHandle = true,
|
|
293
|
+
snapPoints,
|
|
294
|
+
initialSnapIndex = 0,
|
|
295
|
+
snapIndex: snapIndexProp,
|
|
296
|
+
onSnapIndexChange,
|
|
297
|
+
dismissOnSnapToBottom = false,
|
|
298
|
+
draggable = true,
|
|
299
|
+
haptics = true,
|
|
232
300
|
style,
|
|
233
301
|
isDark: isDarkProp,
|
|
234
302
|
testID,
|
|
@@ -238,7 +306,7 @@ export const Sheet = forwardRef<any, SheetProps>(function Sheet(
|
|
|
238
306
|
) {
|
|
239
307
|
const isDark = useIsDark(isDarkProp);
|
|
240
308
|
const theme = isDark ? colors.dark : colors.light;
|
|
241
|
-
const { View, Text: RNText, Pressable, Modal } = primitives();
|
|
309
|
+
const { View, Text: RNText, Pressable, Modal, PanResponder, useWindowDimensions } = primitives();
|
|
242
310
|
|
|
243
311
|
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
244
312
|
const open = openProp !== undefined ? openProp : internalOpen;
|
|
@@ -254,9 +322,125 @@ export const Sheet = forwardRef<any, SheetProps>(function Sheet(
|
|
|
254
322
|
const dismiss = useCallback(() => setOpen(false), [setOpen]);
|
|
255
323
|
useBackDismiss(open, dismiss);
|
|
256
324
|
|
|
257
|
-
//
|
|
325
|
+
// Snap points in pixels, smallest first so index 0 is the shortest sheet.
|
|
326
|
+
const windowHeight = useWindowDimensions?.().height ?? 800;
|
|
327
|
+
const heights = useMemo(() => {
|
|
328
|
+
if (!snapPoints || snapPoints.length === 0) return [];
|
|
329
|
+
return snapPoints.map((point) => resolveSnapPoint(point, windowHeight)).sort((a, b) => a - b);
|
|
330
|
+
}, [snapPoints, windowHeight]);
|
|
331
|
+
|
|
332
|
+
const [internalSnap, setInternalSnap] = useState(initialSnapIndex);
|
|
333
|
+
const snapIndex = snapIndexProp !== undefined ? snapIndexProp : internalSnap;
|
|
334
|
+
const activeHeight = heights.length > 0 ? heights[Math.min(snapIndex, heights.length - 1)]! : null;
|
|
335
|
+
|
|
336
|
+
// Drag offset in pixels: 0 is resting at the active snap point, positive is
|
|
337
|
+
// dragged down. It resets to 0 after a release, because the settle either
|
|
338
|
+
// changes the snap index or dismisses.
|
|
339
|
+
const dragRef = useRef(0);
|
|
340
|
+
const [drag, setDrag] = useState(0);
|
|
341
|
+
|
|
258
342
|
const { value: rise, animated } = useSpring(open ? 1 : 0, "gentle");
|
|
259
|
-
const
|
|
343
|
+
const slideIn = animated ? rise.interpolate({ inputRange: [0, 1], outputRange: [420, 0] }) : 0;
|
|
344
|
+
|
|
345
|
+
const heightsRef = useRef(heights);
|
|
346
|
+
heightsRef.current = heights;
|
|
347
|
+
const activeHeightRef = useRef(activeHeight);
|
|
348
|
+
activeHeightRef.current = activeHeight;
|
|
349
|
+
const snapIndexRef = useRef(snapIndex);
|
|
350
|
+
snapIndexRef.current = snapIndex;
|
|
351
|
+
|
|
352
|
+
const goToSnap = useCallback(
|
|
353
|
+
(index: number) => {
|
|
354
|
+
if (snapIndexProp === undefined) setInternalSnap(index);
|
|
355
|
+
onSnapIndexChange?.(index);
|
|
356
|
+
if (haptics) hapticSelection();
|
|
357
|
+
},
|
|
358
|
+
[snapIndexProp, onSnapIndexChange, haptics],
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The release decision.
|
|
363
|
+
*
|
|
364
|
+
* Below the lowest snap point the sheet dismisses when allowed, otherwise it
|
|
365
|
+
* snaps back; anywhere else it settles at the nearest point, with a fast
|
|
366
|
+
* downward flick counting as a step towards the smaller sheet.
|
|
367
|
+
*/
|
|
368
|
+
const release = useCallback(
|
|
369
|
+
(dy: number, vy: number) => {
|
|
370
|
+
const list = heightsRef.current;
|
|
371
|
+
const current = list[snapIndexRef.current] ?? activeHeightRef.current ?? 0;
|
|
372
|
+
const restingAfterDrag = current - dy;
|
|
373
|
+
|
|
374
|
+
if (list.length === 0) {
|
|
375
|
+
// No snap points: a decisive downward drag dismisses.
|
|
376
|
+
if (dy > 120 || vy > 0.8) dismiss();
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const lowest = list[0]!;
|
|
381
|
+
const belowLowest = restingAfterDrag < lowest - 40;
|
|
382
|
+
if (belowLowest && dismissOnSnapToBottom) {
|
|
383
|
+
dismiss();
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// A fast flick moves one step in its direction before distance decides.
|
|
388
|
+
let index = nearestSnapPoint(list, restingAfterDrag);
|
|
389
|
+
if (vy > 0.5) index = Math.max(0, Math.min(index, snapIndexRef.current - 1));
|
|
390
|
+
if (vy < -0.5) index = Math.max(index, Math.min(snapIndexRef.current + 1, list.length - 1));
|
|
391
|
+
|
|
392
|
+
if (index !== snapIndexRef.current) goToSnap(index);
|
|
393
|
+
},
|
|
394
|
+
[dismiss, dismissOnSnapToBottom, goToSnap],
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
const panResponder = useMemo(() => {
|
|
398
|
+
if (!draggable || !PanResponder?.create) return null;
|
|
399
|
+
return PanResponder.create({
|
|
400
|
+
// Vertical drags belong to the sheet; horizontal ones are left alone.
|
|
401
|
+
onStartShouldSetPanResponder: () => false,
|
|
402
|
+
onMoveShouldSetPanResponder: (_event: any, gesture: any) =>
|
|
403
|
+
Math.abs(gesture.dy) > Math.abs(gesture.dx) && Math.abs(gesture.dy) > 6,
|
|
404
|
+
onPanResponderGrant: () => {
|
|
405
|
+
dragRef.current = 0;
|
|
406
|
+
},
|
|
407
|
+
onPanResponderMove: (_event: any, gesture: any) => {
|
|
408
|
+
// Dragging up past the tallest point has nowhere to go; clamp at 0.
|
|
409
|
+
const next = Math.max(-40, gesture.dy);
|
|
410
|
+
dragRef.current = next;
|
|
411
|
+
setDrag(next);
|
|
412
|
+
},
|
|
413
|
+
onPanResponderRelease: (_event: any, gesture: any) => {
|
|
414
|
+
release(gesture.dy, gesture.vy);
|
|
415
|
+
dragRef.current = 0;
|
|
416
|
+
setDrag(0);
|
|
417
|
+
},
|
|
418
|
+
onPanResponderTerminate: () => {
|
|
419
|
+
dragRef.current = 0;
|
|
420
|
+
setDrag(0);
|
|
421
|
+
},
|
|
422
|
+
onPanResponderTerminationRequest: () => false,
|
|
423
|
+
});
|
|
424
|
+
}, [draggable, PanResponder, release]);
|
|
425
|
+
|
|
426
|
+
// Reduce Motion is honoured by useSpring: when it is on, `animated` is false
|
|
427
|
+
// and the panel is already in position, so the sheet opens and snaps without
|
|
428
|
+
// a transition.
|
|
429
|
+
const translateY = drag !== 0 ? drag : animated ? slideIn : 0;
|
|
430
|
+
|
|
431
|
+
const panel = (
|
|
432
|
+
<SheetPanel
|
|
433
|
+
title={title}
|
|
434
|
+
footer={footer}
|
|
435
|
+
showHandle={showHandle}
|
|
436
|
+
isDark={isDark}
|
|
437
|
+
testID={testID}
|
|
438
|
+
style={style}
|
|
439
|
+
height={activeHeight}
|
|
440
|
+
>
|
|
441
|
+
{children}
|
|
442
|
+
</SheetPanel>
|
|
443
|
+
);
|
|
260
444
|
|
|
261
445
|
return (
|
|
262
446
|
<Modal visible={open} transparent animationType="fade" onRequestClose={dismiss}>
|
|
@@ -266,31 +450,16 @@ export const Sheet = forwardRef<any, SheetProps>(function Sheet(
|
|
|
266
450
|
onPress={dismissOnBackdropPress ? dismiss : undefined}
|
|
267
451
|
style={{ flex: 1, justifyContent: "flex-end", backgroundColor: theme.scrim }}
|
|
268
452
|
>
|
|
269
|
-
<Pressable
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
testID={testID}
|
|
278
|
-
style={style}
|
|
279
|
-
>
|
|
280
|
-
{children}
|
|
281
|
-
</SheetPanel>
|
|
282
|
-
</SheetRise>
|
|
453
|
+
<Pressable
|
|
454
|
+
accessibilityRole="none"
|
|
455
|
+
onPress={() => {}}
|
|
456
|
+
style={{ width: "100%" }}
|
|
457
|
+
{...(panResponder ? panResponder.panHandlers : {})}
|
|
458
|
+
>
|
|
459
|
+
{animated || drag !== 0 ? (
|
|
460
|
+
<SheetRise translateY={translateY}>{panel}</SheetRise>
|
|
283
461
|
) : (
|
|
284
|
-
|
|
285
|
-
title={title}
|
|
286
|
-
footer={footer}
|
|
287
|
-
showHandle={showHandle}
|
|
288
|
-
isDark={isDark}
|
|
289
|
-
testID={testID}
|
|
290
|
-
style={style}
|
|
291
|
-
>
|
|
292
|
-
{children}
|
|
293
|
-
</SheetPanel>
|
|
462
|
+
panel
|
|
294
463
|
)}
|
|
295
464
|
</Pressable>
|
|
296
465
|
</Pressable>
|
|
@@ -311,6 +480,7 @@ function SheetPanel({
|
|
|
311
480
|
isDark,
|
|
312
481
|
testID,
|
|
313
482
|
style,
|
|
483
|
+
height,
|
|
314
484
|
children,
|
|
315
485
|
}: {
|
|
316
486
|
title?: string;
|
|
@@ -319,6 +489,8 @@ function SheetPanel({
|
|
|
319
489
|
isDark: boolean;
|
|
320
490
|
testID?: string;
|
|
321
491
|
style?: StyleProp<ViewStyle>;
|
|
492
|
+
/** The snap point's height, or null for the panel's natural height. */
|
|
493
|
+
height?: number | null;
|
|
322
494
|
children?: ReactNode;
|
|
323
495
|
}) {
|
|
324
496
|
const theme = isDark ? colors.dark : colors.light;
|
|
@@ -327,6 +499,7 @@ function SheetPanel({
|
|
|
327
499
|
return (
|
|
328
500
|
<Glass
|
|
329
501
|
effect="regular"
|
|
502
|
+
surface="overlay"
|
|
330
503
|
isDark={isDark}
|
|
331
504
|
testID={testID}
|
|
332
505
|
style={{
|
|
@@ -334,8 +507,14 @@ function SheetPanel({
|
|
|
334
507
|
borderTopRightRadius: radii.xl,
|
|
335
508
|
borderBottomLeftRadius: 0,
|
|
336
509
|
borderBottomRightRadius: 0,
|
|
337
|
-
paddingBottom:
|
|
338
|
-
|
|
510
|
+
paddingBottom: 24,
|
|
511
|
+
// The web sheet carries a deep, soft shadow so it reads as a layer
|
|
512
|
+
// above the page rather than a rectangle painted on it.
|
|
513
|
+
shadowOpacity: 0.5,
|
|
514
|
+
shadowRadius: 40,
|
|
515
|
+
shadowOffset: { width: 0, height: 0 },
|
|
516
|
+
// A snap point fixes the height; without one the panel is natural.
|
|
517
|
+
...(height != null ? { height } : { maxHeight: 560 }),
|
|
339
518
|
...(style as ViewStyle),
|
|
340
519
|
}}
|
|
341
520
|
>
|
|
@@ -349,14 +528,14 @@ function SheetPanel({
|
|
|
349
528
|
</View>
|
|
350
529
|
) : null}
|
|
351
530
|
{title ? (
|
|
352
|
-
<View style={{ paddingHorizontal:
|
|
531
|
+
<View style={{ paddingHorizontal: 24, paddingVertical: 10 }}>
|
|
353
532
|
<RNText accessibilityRole="heading" style={{ fontSize: 17, fontWeight: "600", color: theme.foreground }}>
|
|
354
533
|
{title}
|
|
355
534
|
</RNText>
|
|
356
535
|
</View>
|
|
357
536
|
) : null}
|
|
358
|
-
<View style={{ paddingHorizontal:
|
|
359
|
-
{footer ? <View style={{ paddingHorizontal:
|
|
537
|
+
<View style={{ paddingHorizontal: 24 }}>{children}</View>
|
|
538
|
+
{footer ? <View style={{ paddingHorizontal: 24, paddingTop: 12, gap: 8 }}>{footer}</View> : null}
|
|
360
539
|
</Glass>
|
|
361
540
|
);
|
|
362
541
|
}
|
|
@@ -376,6 +555,10 @@ export interface DrawerProps {
|
|
|
376
555
|
children?: ReactNode;
|
|
377
556
|
/** Tapping the scrim dismisses. */
|
|
378
557
|
dismissOnBackdropPress?: boolean;
|
|
558
|
+
/** Turns the drag-to-close gesture off. */
|
|
559
|
+
draggable?: boolean;
|
|
560
|
+
/** Opt out of the selection tick when the drawer is thrown closed. */
|
|
561
|
+
haptics?: boolean;
|
|
379
562
|
style?: StyleProp<ViewStyle>;
|
|
380
563
|
isDark?: boolean;
|
|
381
564
|
testID?: string;
|
|
@@ -387,6 +570,11 @@ export interface DrawerProps {
|
|
|
387
570
|
* The same overlay discipline as Sheet — scrim, spring slide, Android back —
|
|
388
571
|
* but anchored to a side edge, with a square panel that runs the full height.
|
|
389
572
|
* The caller supplies the content, typically a List.
|
|
573
|
+
*
|
|
574
|
+
* Dragging the panel towards its own edge follows the finger; releasing past
|
|
575
|
+
* the halfway point, or with enough outward velocity, closes it, and a drag
|
|
576
|
+
* that stays inside springs it back open. Reduce Motion is honoured: with it
|
|
577
|
+
* on, the drawer appears and disappears without sliding.
|
|
390
578
|
*/
|
|
391
579
|
export const Drawer = forwardRef<any, DrawerProps>(function Drawer(
|
|
392
580
|
{
|
|
@@ -397,6 +585,8 @@ export const Drawer = forwardRef<any, DrawerProps>(function Drawer(
|
|
|
397
585
|
width = 280,
|
|
398
586
|
children,
|
|
399
587
|
dismissOnBackdropPress = true,
|
|
588
|
+
draggable = true,
|
|
589
|
+
haptics = true,
|
|
400
590
|
style,
|
|
401
591
|
isDark: isDarkProp,
|
|
402
592
|
testID,
|
|
@@ -406,7 +596,7 @@ export const Drawer = forwardRef<any, DrawerProps>(function Drawer(
|
|
|
406
596
|
) {
|
|
407
597
|
const isDark = useIsDark(isDarkProp);
|
|
408
598
|
const theme = isDark ? colors.dark : colors.light;
|
|
409
|
-
const { View, Pressable, Modal } = primitives();
|
|
599
|
+
const { View, Pressable, Modal, PanResponder } = primitives();
|
|
410
600
|
|
|
411
601
|
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
412
602
|
const open = openProp !== undefined ? openProp : internalOpen;
|
|
@@ -424,13 +614,48 @@ export const Drawer = forwardRef<any, DrawerProps>(function Drawer(
|
|
|
424
614
|
|
|
425
615
|
const { value: slide, animated } = useSpring(open ? 1 : 0, "gentle");
|
|
426
616
|
// Slide in from off-screen: negative from the left, positive from the right.
|
|
427
|
-
const
|
|
617
|
+
const slideIn = animated
|
|
428
618
|
? slide.interpolate({ inputRange: [0, 1], outputRange: [side === "left" ? -width : width, 0] })
|
|
429
619
|
: 0;
|
|
430
620
|
|
|
621
|
+
// The drag is measured in the drawer's own closing direction, so a drag
|
|
622
|
+
// towards the anchored edge is always positive whichever side it is on.
|
|
623
|
+
const [drag, setDrag] = useState(0);
|
|
624
|
+
|
|
625
|
+
const panResponder = useMemo(() => {
|
|
626
|
+
if (!draggable || !PanResponder?.create) return null;
|
|
627
|
+
const outward = side === "left" ? -1 : 1;
|
|
628
|
+
return PanResponder.create({
|
|
629
|
+
onStartShouldSetPanResponder: () => false,
|
|
630
|
+
onMoveShouldSetPanResponder: (_event: any, gesture: any) =>
|
|
631
|
+
// Only a horizontal drag towards the edge is ours; the other way has
|
|
632
|
+
// nowhere to go and should not fight content inside the panel.
|
|
633
|
+
Math.abs(gesture.dx) > Math.abs(gesture.dy) && gesture.dx * outward > 6,
|
|
634
|
+
onPanResponderMove: (_event: any, gesture: any) => {
|
|
635
|
+
setDrag(Math.max(0, gesture.dx * outward));
|
|
636
|
+
},
|
|
637
|
+
onPanResponderRelease: (_event: any, gesture: any) => {
|
|
638
|
+
const travelled = Math.max(0, gesture.dx * outward);
|
|
639
|
+
const thrown = gesture.vx * outward > 0.4;
|
|
640
|
+
setDrag(0);
|
|
641
|
+
if (travelled > width / 2 || thrown) {
|
|
642
|
+
if (haptics) hapticSelection();
|
|
643
|
+
dismiss();
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
onPanResponderTerminate: () => setDrag(0),
|
|
647
|
+
onPanResponderTerminationRequest: () => false,
|
|
648
|
+
});
|
|
649
|
+
// `dismiss` is stable; the gesture only needs rebuilding when its shape
|
|
650
|
+
// (side or width) changes.
|
|
651
|
+
}, [draggable, PanResponder, side, width, haptics, dismiss]);
|
|
652
|
+
|
|
653
|
+
const translateX = drag !== 0 ? (side === "left" ? -drag : drag) : animated ? slideIn : 0;
|
|
654
|
+
|
|
431
655
|
const panel = (
|
|
432
656
|
<Glass
|
|
433
657
|
effect="regular"
|
|
658
|
+
surface="overlay"
|
|
434
659
|
isDark={isDark}
|
|
435
660
|
testID={testID}
|
|
436
661
|
style={{
|
|
@@ -459,8 +684,13 @@ export const Drawer = forwardRef<any, DrawerProps>(function Drawer(
|
|
|
459
684
|
alignItems: side === "left" ? "flex-start" : "flex-end",
|
|
460
685
|
}}
|
|
461
686
|
>
|
|
462
|
-
<Pressable
|
|
463
|
-
|
|
687
|
+
<Pressable
|
|
688
|
+
accessibilityRole="none"
|
|
689
|
+
onPress={() => {}}
|
|
690
|
+
style={{ height: "100%" }}
|
|
691
|
+
{...(panResponder ? panResponder.panHandlers : {})}
|
|
692
|
+
>
|
|
693
|
+
{animated || drag !== 0 ? <DrawerSlide translateX={translateX}>{panel}</DrawerSlide> : panel}
|
|
464
694
|
</Pressable>
|
|
465
695
|
</View>
|
|
466
696
|
</Pressable>
|