@vvedantb/native-ui 0.24.2 → 0.25.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.
@@ -0,0 +1,186 @@
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, on the screen colour with a shadow, 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
+ // The screen colour behind the copy: a reply without a bubble would show the dimmed original through it.
98
+ backgroundColor: tokens.color.background[scheme],
99
+ borderRadius: tokens.radius.card,
100
+ ...shadow,
101
+ }}
102
+ >
103
+ {preview}
104
+ </View>
105
+ {menu.reactions ? (
106
+ <View
107
+ accessibilityRole="toolbar"
108
+ style={{
109
+ position: "absolute",
110
+ top: layout.pillTop,
111
+ ...side(pillWidth),
112
+ height: MESSAGE_MENU.pillHeight,
113
+ borderRadius: MESSAGE_MENU.pillHeight / 2,
114
+ backgroundColor: surface,
115
+ ...shadow,
116
+ }}
117
+ className="flex-row items-center px-2"
118
+ >
119
+ {menu.reactions.items.map((item) => {
120
+ const selected = item.selected === true;
121
+ return (
122
+ <Pressable
123
+ key={item.key}
124
+ testID={`${testID}-reaction-${item.key}`}
125
+ accessibilityRole="button"
126
+ accessibilityLabel={item.label}
127
+ accessibilityState={{ selected }}
128
+ onPress={() => run(() => menu.reactions?.onPress(item.key))}
129
+ className="size-11 items-center justify-center rounded-full active:opacity-70"
130
+ style={{ marginHorizontal: 2, backgroundColor: selected ? withAlpha(palette.primary[scheme], 0.12) : "transparent" }}
131
+ >
132
+ {item.render(selected)}
133
+ </Pressable>
134
+ );
135
+ })}
136
+ </View>
137
+ ) : null}
138
+ <View
139
+ accessibilityRole="menu"
140
+ style={{
141
+ position: "absolute",
142
+ top: layout.menuTop,
143
+ ...side(MESSAGE_MENU.menuWidth),
144
+ width: MESSAGE_MENU.menuWidth,
145
+ maxWidth: window.width - 2 * MESSAGE_MENU.edge,
146
+ borderRadius: tokens.radius.card,
147
+ backgroundColor: surface,
148
+ overflow: "hidden",
149
+ ...shadow,
150
+ }}
151
+ >
152
+ {menu.actions.map((action, index) => {
153
+ const Icon = action.icon;
154
+ const color = action.destructive ? palette.danger[scheme] : tokens.color.foreground[scheme];
155
+ return (
156
+ <Pressable
157
+ key={action.key}
158
+ testID={`${testID}-action-${action.key}`}
159
+ accessibilityRole="menuitem"
160
+ accessibilityLabel={action.label}
161
+ accessibilityState={{ disabled: action.disabled === true }}
162
+ disabled={action.disabled === true}
163
+ onPress={() => run(action.onPress)}
164
+ // A static style: with a `className`, NativeWind drops a style function.
165
+ className="flex-row items-center gap-3 px-4 active:opacity-70"
166
+ style={[
167
+ { minHeight: rowHeight, opacity: action.disabled ? 0.5 : 1 },
168
+ index > 0 && { borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: tokens.color.separator[scheme] },
169
+ ]}
170
+ >
171
+ <Text
172
+ numberOfLines={1}
173
+ className={cn(body, "min-w-0 flex-1 font-instrument-sans")}
174
+ style={[sizeStyle(body), typeStyle("medium"), { color }]}
175
+ >
176
+ {action.label}
177
+ </Text>
178
+ {Icon ? <Icon size={largerText ? 22 : 20} color={color} /> : null}
179
+ </Pressable>
180
+ );
181
+ })}
182
+ </View>
183
+ </View>
184
+ </Modal>
185
+ );
186
+ }
@@ -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
+ }
@@ -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,27 @@ 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) and a long press opens the
110
+ * `className`, `style` and view props): a 350 ms hold dims 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
+ /**
119
+ * A held message dims a little. It does not shrink: the hold menu measures the
120
+ * message while it is held, and a scaled box would draw the lifted copy smaller
121
+ * (and could wrap its text differently).
122
+ */
123
+ const HELD_STYLE = { opacity: 0.9 };
124
+
107
125
  export function Message({
108
126
  from,
109
127
  className,
110
128
  style,
111
129
  onLongPress,
130
+ menu,
112
131
  actions,
113
132
  actionSheetTitle,
114
133
  highlighted = false,
@@ -118,11 +137,21 @@ export function Message({
118
137
  }: MessageProps) {
119
138
  const user = from === "user";
120
139
  const [sheetOpen, setSheetOpen] = useState(false);
140
+ const [pressing, setPressing] = useState(false);
141
+ // The open context menu: where the message was and what it offers, read at the hold.
142
+ const [held, setHeld] = useState<{ anchor: MessageMenuRect; menu: MessageMenu } | null>(null);
143
+ // Measured at the hold: the menu draws the message again where it is on screen.
144
+ const pressable = useRef<View>(null);
121
145
  const hasActions = actions != null && actions.length > 0;
122
- const longPressable = onLongPress != null || hasActions;
146
+ const longPressable = onLongPress != null || hasActions || menu != null;
123
147
  const longPress = () => {
148
+ // A medium tap on a hold, as iMessage; the sheet adds its own selection tick.
149
+ impactHaptic("medium");
124
150
  onLongPress?.();
125
- if (hasActions) setSheetOpen(true);
151
+ if (menu) {
152
+ const built = menu();
153
+ pressable.current?.measureInWindow((x, y, width, height) => setHeld({ anchor: { x, y, width, height }, menu: built }));
154
+ } else if (hasActions) setSheetOpen(true);
126
155
  };
127
156
  const layout: ViewProps["style"] = [
128
157
  { alignSelf: user ? "flex-end" : "flex-start", alignItems: user ? "flex-end" : "stretch" },
@@ -141,10 +170,16 @@ export function Message({
141
170
  {longPressable ? (
142
171
  <>
143
172
  <Pressable
173
+ ref={pressable}
144
174
  accessible={false}
145
175
  className={merged}
146
- style={layout}
176
+ // While held it dims a little: the feedback before the menu opens. A state, not a
177
+ // style function: with a `className`, NativeWind drops a style function, layout and all.
178
+ style={[layout, pressing ? HELD_STYLE : null]}
179
+ onPressIn={() => setPressing(true)}
180
+ onPressOut={() => setPressing(false)}
147
181
  onLongPress={longPress}
182
+ delayLongPress={HOLD_DELAY_MS}
148
183
  // TalkBack's actions menu reaches the same sheet as a long press.
149
184
  accessibilityActions={[{ name: "longpress", label: actionSheetTitle ?? MESSAGE_ACTIONS_TITLE }]}
150
185
  onAccessibilityAction={(event) => {
@@ -154,7 +189,22 @@ export function Message({
154
189
  >
155
190
  {parts}
156
191
  </Pressable>
157
- {hasActions ? (
192
+ {menu ? (
193
+ <MessageContextMenu
194
+ open={held !== null}
195
+ onClose={() => setHeld(null)}
196
+ anchor={held?.anchor ?? null}
197
+ align={user ? "end" : "start"}
198
+ preview={
199
+ <MessageContext.Provider value={from}>
200
+ <View className={merged} style={[layout, { maxWidth: "100%", width: "100%" }]}>
201
+ {parts}
202
+ </View>
203
+ </MessageContext.Provider>
204
+ }
205
+ menu={held?.menu ?? { actions: [] }}
206
+ />
207
+ ) : hasActions ? (
158
208
  <MessageActionSheet
159
209
  open={sheetOpen}
160
210
  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";