@vvedantb/native-ui 0.24.1 → 0.25.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.
- package/dist/DropdownMenu.jsx +245 -116
- package/dist/MenuSheet.jsx +164 -0
- package/dist/ReactionRow.jsx +84 -0
- package/dist/ai/index.js +2 -0
- package/dist/ai/message-action-sheet.jsx +182 -202
- package/dist/ai/message-context-menu.jsx +386 -0
- package/dist/ai/message-menu-layout.js +46 -0
- package/dist/ai/message.jsx +113 -69
- package/dist/entries/primitives.js +2 -0
- package/dist/index.js +2 -0
- package/dist/motion.js +31 -0
- package/dist/primitives/overlay.jsx +162 -101
- package/package.json +1 -1
- package/src/DropdownMenu.tsx +71 -16
- package/src/MenuSheet.tsx +80 -0
- package/src/ReactionRow.tsx +70 -0
- package/src/ai/index.ts +3 -0
- package/src/ai/message-action-sheet.tsx +92 -97
- package/src/ai/message-context-menu.tsx +183 -0
- package/src/ai/message-menu-layout.ts +44 -0
- package/src/ai/message.tsx +52 -6
- package/src/entries/primitives.ts +4 -0
- package/src/index.ts +4 -0
- package/src/motion.ts +15 -0
- package/src/primitives/overlay.tsx +48 -8
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, type ComponentType } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { Pressable, StyleSheet, Text, View } from "react-native";
|
|
3
3
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
4
4
|
import type { IconProps } from "@tabler/icons-react-native";
|
|
5
5
|
import { cn } from "../cn";
|
|
@@ -8,8 +8,9 @@ import { selectionHaptic } from "../haptics";
|
|
|
8
8
|
import { palette } from "../palette";
|
|
9
9
|
import { useLargerText } from "../theme";
|
|
10
10
|
import { tokens } from "../tokens";
|
|
11
|
-
import { useReduceMotion } from "../useReduceMotion";
|
|
12
11
|
import { useScheme } from "../useScheme";
|
|
12
|
+
import { OverlayModal, OverlayRoot } from "../primitives/overlay";
|
|
13
|
+
import { SheetDragView } from "../primitives/sheet-drag";
|
|
13
14
|
import { chatTextSize } from "./chat-text";
|
|
14
15
|
|
|
15
16
|
/** One row of `MessageActionSheet`. */
|
|
@@ -19,6 +20,8 @@ export type MessageSheetAction = {
|
|
|
19
20
|
icon?: ComponentType<IconProps>;
|
|
20
21
|
/** Draws the row in the danger colour, such as Delete. */
|
|
21
22
|
destructive?: boolean;
|
|
23
|
+
/** Dims the row and ignores presses (the context menu's rows; the sheet leaves a disabled action out). */
|
|
24
|
+
disabled?: boolean;
|
|
22
25
|
onPress: () => void;
|
|
23
26
|
};
|
|
24
27
|
|
|
@@ -39,8 +42,9 @@ export type MessageActionSheetProps = {
|
|
|
39
42
|
|
|
40
43
|
/**
|
|
41
44
|
* The sheet that opens on a long press of a chat message: a bottom sheet in
|
|
42
|
-
* an RN `Modal
|
|
43
|
-
* and a Cancel row. It
|
|
45
|
+
* the kit's `OverlayModal` (an RN `Modal`, no bottom-sheet library) with one row per action
|
|
46
|
+
* and a Cancel row. It slides up and down as `Sheet` does (a drag down closes it too; with
|
|
47
|
+
* Reduce Motion on it appears and leaves in place). It closes on the backdrop, Cancel, Android back and after
|
|
44
48
|
* an action. A `selectionHaptic()` fires when it opens (`expo-haptics` is an
|
|
45
49
|
* optional peer). Row testIDs are `{testID}-action-{key}`.
|
|
46
50
|
*/
|
|
@@ -55,7 +59,6 @@ export function MessageActionSheet({
|
|
|
55
59
|
const scheme = useScheme();
|
|
56
60
|
const largerText = useLargerText();
|
|
57
61
|
const insets = useSafeAreaInsets();
|
|
58
|
-
const reduceMotion = useReduceMotion();
|
|
59
62
|
|
|
60
63
|
useEffect(() => {
|
|
61
64
|
if (open) selectionHaptic();
|
|
@@ -66,103 +69,95 @@ export function MessageActionSheet({
|
|
|
66
69
|
const minHeight = largerText ? 56 : MIN_TOUCH_TARGET + 4;
|
|
67
70
|
|
|
68
71
|
return (
|
|
69
|
-
<
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
onRequestClose={onClose}
|
|
72
|
+
<OverlayRoot
|
|
73
|
+
open={open}
|
|
74
|
+
onOpenChange={(next) => {
|
|
75
|
+
if (!next) onClose();
|
|
76
|
+
}}
|
|
75
77
|
>
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
hitSlop={touchSlop(MIN_TOUCH_TARGET)}
|
|
82
|
-
style={[StyleSheet.absoluteFill, { backgroundColor: palette.backdrop[scheme] }]}
|
|
83
|
-
/>
|
|
84
|
-
<View pointerEvents="box-none" style={styles.anchor}>
|
|
85
|
-
<View
|
|
86
|
-
testID={testID}
|
|
87
|
-
accessibilityViewIsModal
|
|
88
|
-
accessibilityLabel={title}
|
|
89
|
-
onAccessibilityEscape={onClose}
|
|
90
|
-
className="w-full self-center px-2"
|
|
91
|
-
style={{ maxWidth: 480, paddingBottom: insets.bottom + 8 }}
|
|
92
|
-
>
|
|
78
|
+
{/* The kit's bottom-sheet overlay: it slides with `SHEET_SPRING`, the backdrop
|
|
79
|
+
follows its position and a drag down closes it. The part name keeps
|
|
80
|
+
the backdrop testID `{testID}-backdrop`. */}
|
|
81
|
+
<OverlayModal part={testID} placement="bottom" dismissible>
|
|
82
|
+
<SheetDragView enabled>
|
|
93
83
|
<View
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
testID={testID}
|
|
85
|
+
accessibilityViewIsModal
|
|
86
|
+
accessibilityLabel={title}
|
|
87
|
+
onAccessibilityEscape={onClose}
|
|
88
|
+
className="w-full self-center px-2"
|
|
89
|
+
style={{ maxWidth: 480, paddingBottom: insets.bottom + 8 }}
|
|
96
90
|
>
|
|
97
|
-
<
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
className={cn(caption, "px-4 pb-1 pt-3 font-instrument-sans-medium")}
|
|
101
|
-
style={[sizeStyle(caption), typeStyle("medium"), textStyle("muted", scheme)]}
|
|
91
|
+
<View
|
|
92
|
+
className="overflow-hidden"
|
|
93
|
+
style={{ backgroundColor: tokens.color.surface[scheme], borderRadius: tokens.radius.card }}
|
|
102
94
|
>
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
action.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
{
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
95
|
+
<Text
|
|
96
|
+
accessibilityRole="header"
|
|
97
|
+
numberOfLines={1}
|
|
98
|
+
className={cn(caption, "px-4 pb-1 pt-3 font-instrument-sans-medium")}
|
|
99
|
+
style={[sizeStyle(caption), typeStyle("medium"), textStyle("muted", scheme)]}
|
|
100
|
+
>
|
|
101
|
+
{title}
|
|
102
|
+
</Text>
|
|
103
|
+
{actions.map((action, index) => {
|
|
104
|
+
const Icon = action.icon;
|
|
105
|
+
const color = action.destructive ? palette.danger[scheme] : tokens.color.foreground[scheme];
|
|
106
|
+
return (
|
|
107
|
+
<Pressable
|
|
108
|
+
key={action.key}
|
|
109
|
+
testID={`${testID}-action-${action.key}`}
|
|
110
|
+
accessibilityRole="button"
|
|
111
|
+
accessibilityLabel={action.label}
|
|
112
|
+
hitSlop={touchSlop(MIN_TOUCH_TARGET)}
|
|
113
|
+
onPress={() => {
|
|
114
|
+
onClose();
|
|
115
|
+
action.onPress();
|
|
116
|
+
}}
|
|
117
|
+
className="flex-row items-center gap-3 px-4"
|
|
118
|
+
style={[
|
|
119
|
+
{ minHeight },
|
|
120
|
+
index > 0 && {
|
|
121
|
+
borderTopWidth: StyleSheet.hairlineWidth,
|
|
122
|
+
borderTopColor: tokens.color.separator[scheme],
|
|
123
|
+
},
|
|
124
|
+
]}
|
|
133
125
|
>
|
|
134
|
-
{
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
126
|
+
{Icon ? <Icon size={largerText ? 22 : 20} color={color} /> : null}
|
|
127
|
+
<Text
|
|
128
|
+
numberOfLines={1}
|
|
129
|
+
className={cn(body, "min-w-0 flex-1 font-instrument-sans-medium")}
|
|
130
|
+
style={[sizeStyle(body), typeStyle("medium"), { color }]}
|
|
131
|
+
>
|
|
132
|
+
{action.label}
|
|
133
|
+
</Text>
|
|
134
|
+
</Pressable>
|
|
135
|
+
);
|
|
136
|
+
})}
|
|
137
|
+
</View>
|
|
138
|
+
<Pressable
|
|
139
|
+
testID={`${testID}-cancel`}
|
|
140
|
+
accessibilityRole="button"
|
|
141
|
+
accessibilityLabel={cancelLabel}
|
|
142
|
+
onPress={onClose}
|
|
143
|
+
hitSlop={touchSlop(MIN_TOUCH_TARGET)}
|
|
144
|
+
className="mt-2 items-center justify-center"
|
|
145
|
+
style={{
|
|
146
|
+
minHeight,
|
|
147
|
+
backgroundColor: tokens.color.surface[scheme],
|
|
148
|
+
borderRadius: tokens.radius.card,
|
|
149
|
+
}}
|
|
156
150
|
>
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
151
|
+
<Text
|
|
152
|
+
className={cn(body, "font-instrument-sans-semibold")}
|
|
153
|
+
style={[sizeStyle(body), typeStyle("semibold"), textStyle("foreground", scheme)]}
|
|
154
|
+
>
|
|
155
|
+
{cancelLabel}
|
|
156
|
+
</Text>
|
|
157
|
+
</Pressable>
|
|
158
|
+
</View>
|
|
159
|
+
</SheetDragView>
|
|
160
|
+
</OverlayModal>
|
|
161
|
+
</OverlayRoot>
|
|
163
162
|
);
|
|
164
163
|
}
|
|
165
|
-
|
|
166
|
-
const styles = StyleSheet.create({
|
|
167
|
-
anchor: { flex: 1, justifyContent: "flex-end" },
|
|
168
|
-
});
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { Modal, Pressable, StyleSheet, Text, View, useWindowDimensions } from "react-native";
|
|
3
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
4
|
+
import { cn } from "../cn";
|
|
5
|
+
import { sizeStyle, typeStyle } from "../controlStyles";
|
|
6
|
+
import { palette, withAlpha } from "../palette";
|
|
7
|
+
import { useLargerText } from "../theme";
|
|
8
|
+
import { tokens } from "../tokens";
|
|
9
|
+
import { useReduceMotion } from "../useReduceMotion";
|
|
10
|
+
import { useScheme } from "../useScheme";
|
|
11
|
+
import { chatTextSize } from "./chat-text";
|
|
12
|
+
import { MESSAGE_MENU, messageContextMenuLayout, type MessageMenu, type MessageMenuRect } from "./message-menu-layout";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The iMessage hold menu for a chat message: the chat fades under a frosted
|
|
16
|
+
* wash of the screen colour, the held message stays in place,
|
|
17
|
+
* lifted, with the quick reactions in a pill above it and the actions in a
|
|
18
|
+
* card below it, label first and icon last. It fades in and out as one (the
|
|
19
|
+
* Modal's own fade; with Reduce Motion on, it appears at once). A tap outside, Android back
|
|
20
|
+
* or the VoiceOver escape closes it; an action or a reaction closes it, then
|
|
21
|
+
* runs. `Message` opens it with `menu`.
|
|
22
|
+
*/
|
|
23
|
+
export function MessageContextMenu({
|
|
24
|
+
open,
|
|
25
|
+
onClose,
|
|
26
|
+
anchor,
|
|
27
|
+
align,
|
|
28
|
+
preview,
|
|
29
|
+
menu,
|
|
30
|
+
testID = "message-context-menu",
|
|
31
|
+
}: {
|
|
32
|
+
open: boolean;
|
|
33
|
+
onClose: () => void;
|
|
34
|
+
/** The held message's box; the menu stays shut without one. */
|
|
35
|
+
anchor: MessageMenuRect | null;
|
|
36
|
+
/** Which side the pill and the card line up with: the message's (`end` for the user's). */
|
|
37
|
+
align: "start" | "end";
|
|
38
|
+
/** The message, drawn again over the blur. */
|
|
39
|
+
preview: ReactNode;
|
|
40
|
+
menu: MessageMenu;
|
|
41
|
+
testID?: string;
|
|
42
|
+
}) {
|
|
43
|
+
const scheme = useScheme();
|
|
44
|
+
const window = useWindowDimensions();
|
|
45
|
+
const insets = useSafeAreaInsets();
|
|
46
|
+
const largerText = useLargerText();
|
|
47
|
+
const reduceMotion = useReduceMotion();
|
|
48
|
+
const rowHeight = largerText ? 56 : 48;
|
|
49
|
+
const body = chatTextSize("body", largerText);
|
|
50
|
+
if (!open || anchor === null) return null;
|
|
51
|
+
|
|
52
|
+
const layout = messageContextMenuLayout({
|
|
53
|
+
anchor,
|
|
54
|
+
window,
|
|
55
|
+
insets,
|
|
56
|
+
menuHeight: menu.actions.length * rowHeight,
|
|
57
|
+
hasReactions: menu.reactions !== undefined,
|
|
58
|
+
});
|
|
59
|
+
const run = (action: () => void) => {
|
|
60
|
+
onClose();
|
|
61
|
+
action();
|
|
62
|
+
};
|
|
63
|
+
// The pill and the card line up with the message's side, kept on screen.
|
|
64
|
+
const side = (width: number) =>
|
|
65
|
+
align === "end"
|
|
66
|
+
? { left: Math.max(MESSAGE_MENU.edge, Math.min(anchor.x + anchor.width, window.width - MESSAGE_MENU.edge) - width) }
|
|
67
|
+
: { left: Math.max(MESSAGE_MENU.edge, Math.min(anchor.x, window.width - MESSAGE_MENU.edge - width)) };
|
|
68
|
+
const surface = tokens.color.surface[scheme];
|
|
69
|
+
const shadow = { shadowColor: "#000", shadowOpacity: 0.18, shadowRadius: 16, shadowOffset: { width: 0, height: 6 }, elevation: 8 };
|
|
70
|
+
const pillWidth = Math.min(window.width - 2 * MESSAGE_MENU.edge, (menu.reactions?.items.length ?? 0) * 48 + 16);
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
// The Modal's own fade: Reanimated entering animations do not run inside a Modal on Android.
|
|
74
|
+
<Modal visible transparent animationType={reduceMotion ? "none" : "fade"} statusBarTranslucent onRequestClose={onClose}>
|
|
75
|
+
<View style={{ flex: 1 }} testID={testID} accessibilityViewIsModal onAccessibilityEscape={onClose}>
|
|
76
|
+
<View style={StyleSheet.absoluteFill}>
|
|
77
|
+
<Pressable
|
|
78
|
+
testID={`${testID}-backdrop`}
|
|
79
|
+
accessibilityRole="button"
|
|
80
|
+
accessibilityLabel="Close menu"
|
|
81
|
+
onPress={onClose}
|
|
82
|
+
// A frosted wash of the screen colour over the chat, as iMessage's blur reads: the `ai`
|
|
83
|
+
// entry reaches no optional peer, so it draws no `expo-blur`.
|
|
84
|
+
style={[StyleSheet.absoluteFill, { backgroundColor: withAlpha(tokens.color.background[scheme], 0.85) }]}
|
|
85
|
+
/>
|
|
86
|
+
</View>
|
|
87
|
+
{/* The held message, lifted: in place, a shadow under it, clipped when it is taller than the room. */}
|
|
88
|
+
<View
|
|
89
|
+
pointerEvents="none"
|
|
90
|
+
style={{
|
|
91
|
+
position: "absolute",
|
|
92
|
+
top: layout.previewTop,
|
|
93
|
+
left: anchor.x,
|
|
94
|
+
width: anchor.width,
|
|
95
|
+
height: layout.previewHeight,
|
|
96
|
+
overflow: "hidden",
|
|
97
|
+
...shadow,
|
|
98
|
+
}}
|
|
99
|
+
>
|
|
100
|
+
{preview}
|
|
101
|
+
</View>
|
|
102
|
+
{menu.reactions ? (
|
|
103
|
+
<View
|
|
104
|
+
accessibilityRole="toolbar"
|
|
105
|
+
style={{
|
|
106
|
+
position: "absolute",
|
|
107
|
+
top: layout.pillTop,
|
|
108
|
+
...side(pillWidth),
|
|
109
|
+
height: MESSAGE_MENU.pillHeight,
|
|
110
|
+
borderRadius: MESSAGE_MENU.pillHeight / 2,
|
|
111
|
+
backgroundColor: surface,
|
|
112
|
+
...shadow,
|
|
113
|
+
}}
|
|
114
|
+
className="flex-row items-center px-2"
|
|
115
|
+
>
|
|
116
|
+
{menu.reactions.items.map((item) => {
|
|
117
|
+
const selected = item.selected === true;
|
|
118
|
+
return (
|
|
119
|
+
<Pressable
|
|
120
|
+
key={item.key}
|
|
121
|
+
testID={`${testID}-reaction-${item.key}`}
|
|
122
|
+
accessibilityRole="button"
|
|
123
|
+
accessibilityLabel={item.label}
|
|
124
|
+
accessibilityState={{ selected }}
|
|
125
|
+
onPress={() => run(() => menu.reactions?.onPress(item.key))}
|
|
126
|
+
className="size-11 items-center justify-center rounded-full active:opacity-70"
|
|
127
|
+
style={{ marginHorizontal: 2, backgroundColor: selected ? withAlpha(palette.primary[scheme], 0.12) : "transparent" }}
|
|
128
|
+
>
|
|
129
|
+
{item.render(selected)}
|
|
130
|
+
</Pressable>
|
|
131
|
+
);
|
|
132
|
+
})}
|
|
133
|
+
</View>
|
|
134
|
+
) : null}
|
|
135
|
+
<View
|
|
136
|
+
accessibilityRole="menu"
|
|
137
|
+
style={{
|
|
138
|
+
position: "absolute",
|
|
139
|
+
top: layout.menuTop,
|
|
140
|
+
...side(MESSAGE_MENU.menuWidth),
|
|
141
|
+
width: MESSAGE_MENU.menuWidth,
|
|
142
|
+
maxWidth: window.width - 2 * MESSAGE_MENU.edge,
|
|
143
|
+
borderRadius: tokens.radius.card,
|
|
144
|
+
backgroundColor: surface,
|
|
145
|
+
overflow: "hidden",
|
|
146
|
+
...shadow,
|
|
147
|
+
}}
|
|
148
|
+
>
|
|
149
|
+
{menu.actions.map((action, index) => {
|
|
150
|
+
const Icon = action.icon;
|
|
151
|
+
const color = action.destructive ? palette.danger[scheme] : tokens.color.foreground[scheme];
|
|
152
|
+
return (
|
|
153
|
+
<Pressable
|
|
154
|
+
key={action.key}
|
|
155
|
+
testID={`${testID}-action-${action.key}`}
|
|
156
|
+
accessibilityRole="menuitem"
|
|
157
|
+
accessibilityLabel={action.label}
|
|
158
|
+
accessibilityState={{ disabled: action.disabled === true }}
|
|
159
|
+
disabled={action.disabled === true}
|
|
160
|
+
onPress={() => run(action.onPress)}
|
|
161
|
+
// A static style: with a `className`, NativeWind drops a style function.
|
|
162
|
+
className="flex-row items-center gap-3 px-4 active:opacity-70"
|
|
163
|
+
style={[
|
|
164
|
+
{ minHeight: rowHeight, opacity: action.disabled ? 0.5 : 1 },
|
|
165
|
+
index > 0 && { borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: tokens.color.separator[scheme] },
|
|
166
|
+
]}
|
|
167
|
+
>
|
|
168
|
+
<Text
|
|
169
|
+
numberOfLines={1}
|
|
170
|
+
className={cn(body, "min-w-0 flex-1 font-instrument-sans")}
|
|
171
|
+
style={[sizeStyle(body), typeStyle("medium"), { color }]}
|
|
172
|
+
>
|
|
173
|
+
{action.label}
|
|
174
|
+
</Text>
|
|
175
|
+
{Icon ? <Icon size={largerText ? 22 : 20} color={color} /> : null}
|
|
176
|
+
</Pressable>
|
|
177
|
+
);
|
|
178
|
+
})}
|
|
179
|
+
</View>
|
|
180
|
+
</View>
|
|
181
|
+
</Modal>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ReactionRowItem } from "../ReactionRow";
|
|
2
|
+
import type { MessageSheetAction } from "./message-action-sheet";
|
|
3
|
+
|
|
4
|
+
/** A view's box in window coordinates, from `measureInWindow`. */
|
|
5
|
+
export type MessageMenuRect = { x: number; y: number; width: number; height: number };
|
|
6
|
+
|
|
7
|
+
/** What a held message offers: quick reactions above it, actions below it. */
|
|
8
|
+
export type MessageMenu = {
|
|
9
|
+
reactions?: { items: readonly ReactionRowItem[]; onPress: (key: string) => void };
|
|
10
|
+
actions: MessageSheetAction[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** The pill's height, the gap between the parts, the menu's width and the margin from the screen edges. */
|
|
14
|
+
export const MESSAGE_MENU = { pillHeight: 52, gap: 8, menuWidth: 250, edge: 12, maxPreviewRatio: 0.4 } as const;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Where the parts go: the message stays where it was held when the pill fits
|
|
18
|
+
* above it and the menu below it; else it moves just enough, and a message
|
|
19
|
+
* taller than `maxPreviewRatio` of the screen shows its top part only.
|
|
20
|
+
*/
|
|
21
|
+
export function messageContextMenuLayout({
|
|
22
|
+
anchor,
|
|
23
|
+
window,
|
|
24
|
+
insets,
|
|
25
|
+
menuHeight,
|
|
26
|
+
hasReactions,
|
|
27
|
+
}: {
|
|
28
|
+
anchor: MessageMenuRect;
|
|
29
|
+
window: { width: number; height: number };
|
|
30
|
+
insets: { top: number; bottom: number };
|
|
31
|
+
menuHeight: number;
|
|
32
|
+
hasReactions: boolean;
|
|
33
|
+
}): { previewTop: number; previewHeight: number; pillTop: number; menuTop: number } {
|
|
34
|
+
const { pillHeight, gap, edge, maxPreviewRatio } = MESSAGE_MENU;
|
|
35
|
+
const above = hasReactions ? pillHeight + gap : 0;
|
|
36
|
+
const below = gap + menuHeight;
|
|
37
|
+
const top = insets.top + edge;
|
|
38
|
+
const bottom = window.height - insets.bottom - edge;
|
|
39
|
+
const previewHeight = Math.max(0, Math.min(anchor.height, window.height * maxPreviewRatio, bottom - top - above - below));
|
|
40
|
+
const minTop = top + above;
|
|
41
|
+
const maxTop = bottom - below - previewHeight;
|
|
42
|
+
const previewTop = Math.min(Math.max(anchor.y, minTop), Math.max(minTop, maxTop));
|
|
43
|
+
return { previewTop, previewHeight, pillTop: previewTop - above, menuTop: previewTop + previewHeight + gap };
|
|
44
|
+
}
|
package/src/ai/message.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useContext, useEffect, useState, type ComponentType, type ReactNode } from "react";
|
|
1
|
+
import { createContext, useContext, useEffect, useRef, useState, type ComponentType, type ReactNode } from "react";
|
|
2
2
|
import { Pressable, StyleSheet, Text, View, type ViewProps } from "react-native";
|
|
3
3
|
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated";
|
|
4
4
|
import {
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type IconProps,
|
|
11
11
|
} from "@tabler/icons-react-native";
|
|
12
12
|
import { cn } from "../cn";
|
|
13
|
+
import { impactHaptic } from "../haptics";
|
|
13
14
|
import { sizeStyle, typeStyle } from "../controlStyles";
|
|
14
15
|
import { palette, withAlpha } from "../palette";
|
|
15
16
|
import { useLargerText } from "../theme";
|
|
@@ -19,6 +20,8 @@ import { useScheme } from "../useScheme";
|
|
|
19
20
|
import { META_TEXT_STYLE, chatTextSize } from "./chat-text";
|
|
20
21
|
import { ChatIconButton, type ChatIconButtonSize } from "./icon-button";
|
|
21
22
|
import { MESSAGE_ACTIONS_TITLE, MessageActionSheet, type MessageSheetAction } from "./message-action-sheet";
|
|
23
|
+
import { MessageContextMenu } from "./message-context-menu";
|
|
24
|
+
import type { MessageMenu, MessageMenuRect } from "./message-menu-layout";
|
|
22
25
|
import { ChatTextColorContext, ResponseText } from "./response-text";
|
|
23
26
|
|
|
24
27
|
export type MessageRole = "user" | "assistant" | "system";
|
|
@@ -33,6 +36,11 @@ export type MessageProps = ViewProps & {
|
|
|
33
36
|
* the message is a plain `View`, as before.
|
|
34
37
|
*/
|
|
35
38
|
onLongPress?: () => void;
|
|
39
|
+
/**
|
|
40
|
+
* The iMessage hold menu (`MessageContextMenu`): built when the message is
|
|
41
|
+
* held, so a long list builds none up front. Takes over from `actions`.
|
|
42
|
+
*/
|
|
43
|
+
menu?: () => MessageMenu;
|
|
36
44
|
/**
|
|
37
45
|
* Actions for a `MessageActionSheet` that opens on a long press, such as
|
|
38
46
|
* Reply and Copy. `onLongPress` also runs, before the sheet opens.
|
|
@@ -99,16 +107,23 @@ function MessageHighlight() {
|
|
|
99
107
|
* at the start, its parts full width.
|
|
100
108
|
*
|
|
101
109
|
* With `onLongPress` or `actions` the message is a `Pressable` (same layout,
|
|
102
|
-
* `className`, `style` and view props)
|
|
110
|
+
* `className`, `style` and view props): a 350 ms hold shrinks it a little,
|
|
111
|
+
* fires a medium haptic, runs `onLongPress` and opens the
|
|
103
112
|
* `MessageActionSheet`; TalkBack reaches it as the `longpress` action. It stays
|
|
104
113
|
* a non-accessible container so its buttons and links keep their own focus:
|
|
105
114
|
* also offer the actions in a `MessageActionsBar` for screen reader users. `highlighted` draws the quote-jump highlight.
|
|
106
115
|
*/
|
|
116
|
+
/** How long a hold takes, in ms: shorter than React Native's 500, as iMessage. */
|
|
117
|
+
const HOLD_DELAY_MS = 350;
|
|
118
|
+
/** A held message: a little smaller and dimmer. */
|
|
119
|
+
const HELD_STYLE = { transform: [{ scale: 0.98 }], opacity: 0.9 };
|
|
120
|
+
|
|
107
121
|
export function Message({
|
|
108
122
|
from,
|
|
109
123
|
className,
|
|
110
124
|
style,
|
|
111
125
|
onLongPress,
|
|
126
|
+
menu,
|
|
112
127
|
actions,
|
|
113
128
|
actionSheetTitle,
|
|
114
129
|
highlighted = false,
|
|
@@ -118,11 +133,21 @@ export function Message({
|
|
|
118
133
|
}: MessageProps) {
|
|
119
134
|
const user = from === "user";
|
|
120
135
|
const [sheetOpen, setSheetOpen] = useState(false);
|
|
136
|
+
const [pressing, setPressing] = useState(false);
|
|
137
|
+
// The open context menu: where the message was and what it offers, read at the hold.
|
|
138
|
+
const [held, setHeld] = useState<{ anchor: MessageMenuRect; menu: MessageMenu } | null>(null);
|
|
139
|
+
// Measured at the hold: the menu draws the message again where it is on screen.
|
|
140
|
+
const pressable = useRef<View>(null);
|
|
121
141
|
const hasActions = actions != null && actions.length > 0;
|
|
122
|
-
const longPressable = onLongPress != null || hasActions;
|
|
142
|
+
const longPressable = onLongPress != null || hasActions || menu != null;
|
|
123
143
|
const longPress = () => {
|
|
144
|
+
// A medium tap on a hold, as iMessage; the sheet adds its own selection tick.
|
|
145
|
+
impactHaptic("medium");
|
|
124
146
|
onLongPress?.();
|
|
125
|
-
if (
|
|
147
|
+
if (menu) {
|
|
148
|
+
const built = menu();
|
|
149
|
+
pressable.current?.measureInWindow((x, y, width, height) => setHeld({ anchor: { x, y, width, height }, menu: built }));
|
|
150
|
+
} else if (hasActions) setSheetOpen(true);
|
|
126
151
|
};
|
|
127
152
|
const layout: ViewProps["style"] = [
|
|
128
153
|
{ alignSelf: user ? "flex-end" : "flex-start", alignItems: user ? "flex-end" : "stretch" },
|
|
@@ -141,10 +166,16 @@ export function Message({
|
|
|
141
166
|
{longPressable ? (
|
|
142
167
|
<>
|
|
143
168
|
<Pressable
|
|
169
|
+
ref={pressable}
|
|
144
170
|
accessible={false}
|
|
145
171
|
className={merged}
|
|
146
|
-
|
|
172
|
+
// While held it shrinks a little and dims: the feedback before the menu opens. A state, not a
|
|
173
|
+
// style function: with a `className`, NativeWind drops a style function, layout and all.
|
|
174
|
+
style={[layout, pressing ? HELD_STYLE : null]}
|
|
175
|
+
onPressIn={() => setPressing(true)}
|
|
176
|
+
onPressOut={() => setPressing(false)}
|
|
147
177
|
onLongPress={longPress}
|
|
178
|
+
delayLongPress={HOLD_DELAY_MS}
|
|
148
179
|
// TalkBack's actions menu reaches the same sheet as a long press.
|
|
149
180
|
accessibilityActions={[{ name: "longpress", label: actionSheetTitle ?? MESSAGE_ACTIONS_TITLE }]}
|
|
150
181
|
onAccessibilityAction={(event) => {
|
|
@@ -154,7 +185,22 @@ export function Message({
|
|
|
154
185
|
>
|
|
155
186
|
{parts}
|
|
156
187
|
</Pressable>
|
|
157
|
-
{
|
|
188
|
+
{menu ? (
|
|
189
|
+
<MessageContextMenu
|
|
190
|
+
open={held !== null}
|
|
191
|
+
onClose={() => setHeld(null)}
|
|
192
|
+
anchor={held?.anchor ?? null}
|
|
193
|
+
align={user ? "end" : "start"}
|
|
194
|
+
preview={
|
|
195
|
+
<MessageContext.Provider value={from}>
|
|
196
|
+
<View className={merged} style={[layout, { maxWidth: "100%", width: "100%" }]}>
|
|
197
|
+
{parts}
|
|
198
|
+
</View>
|
|
199
|
+
</MessageContext.Provider>
|
|
200
|
+
}
|
|
201
|
+
menu={held?.menu ?? { actions: [] }}
|
|
202
|
+
/>
|
|
203
|
+
) : hasActions ? (
|
|
158
204
|
<MessageActionSheet
|
|
159
205
|
open={sheetOpen}
|
|
160
206
|
onClose={() => setSheetOpen(false)}
|
|
@@ -35,6 +35,10 @@ export { SectionLabel } from "../SectionLabel";
|
|
|
35
35
|
export type { SectionLabelProps } from "../SectionLabel";
|
|
36
36
|
export { SheetRow } from "../SheetRow";
|
|
37
37
|
export type { SheetRowProps } from "../SheetRow";
|
|
38
|
+
export { MenuSheet, SubmenuRow, MenuDivider } from "../MenuSheet";
|
|
39
|
+
export type { MenuSheetProps, SubmenuRowProps } from "../MenuSheet";
|
|
40
|
+
export { ReactionRow } from "../ReactionRow";
|
|
41
|
+
export type { ReactionRowItem, ReactionRowProps } from "../ReactionRow";
|
|
38
42
|
export { PageIndicator } from "../PageIndicator";
|
|
39
43
|
export type { PageIndicatorProps } from "../PageIndicator";
|
|
40
44
|
export { IconButton } from "../IconButton";
|
package/src/index.ts
CHANGED
|
@@ -72,6 +72,10 @@ export { SectionLabel } from "./SectionLabel";
|
|
|
72
72
|
export type { SectionLabelProps } from "./SectionLabel";
|
|
73
73
|
export { SheetRow } from "./SheetRow";
|
|
74
74
|
export type { SheetRowProps } from "./SheetRow";
|
|
75
|
+
export { MenuSheet, SubmenuRow, MenuDivider } from "./MenuSheet";
|
|
76
|
+
export type { MenuSheetProps, SubmenuRowProps } from "./MenuSheet";
|
|
77
|
+
export { ReactionRow } from "./ReactionRow";
|
|
78
|
+
export type { ReactionRowItem, ReactionRowProps } from "./ReactionRow";
|
|
75
79
|
export { PageIndicator } from "./PageIndicator";
|
|
76
80
|
export type { PageIndicatorProps } from "./PageIndicator";
|
|
77
81
|
export { IconButton } from "./IconButton";
|
package/src/motion.ts
CHANGED
|
@@ -31,3 +31,18 @@ export const SHEET_SNAP_SPRING = {
|
|
|
31
31
|
export function usePressFeedback(): string {
|
|
32
32
|
return useReduceMotion() ? "active:opacity-80" : "active:scale-[0.97]";
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
/** Start scale of a popover or dialog panel as it opens: never from 0, as the `animate-expo` rules say. */
|
|
36
|
+
export const PANEL_SCALE_FROM = 0.96;
|
|
37
|
+
|
|
38
|
+
/** A menu panel opening from its trigger: opacity and scale together. */
|
|
39
|
+
export const POPOVER_ENTER = { duration: 150, easing: EASE_OUT, reduceMotion: ReduceMotion.System } as const;
|
|
40
|
+
|
|
41
|
+
/** A menu panel leaving: a quick fade only. */
|
|
42
|
+
export const POPOVER_EXIT = { duration: 100, easing: EASE_OUT, reduceMotion: ReduceMotion.System } as const;
|
|
43
|
+
|
|
44
|
+
/** A centred dialog opening: the backdrop fades while the panel scales and fades in. */
|
|
45
|
+
export const DIALOG_ENTER = { duration: 200, easing: EASE_OUT, reduceMotion: ReduceMotion.System } as const;
|
|
46
|
+
|
|
47
|
+
/** A centred dialog closing. */
|
|
48
|
+
export const DIALOG_EXIT = { duration: 150, easing: EASE_OUT, reduceMotion: ReduceMotion.System } as const;
|