@vvedantb/native-ui 0.24.2 → 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/MenuSheet.jsx +164 -0
- package/dist/ReactionRow.jsx +84 -0
- package/dist/ai/index.js +2 -0
- 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/package.json +1 -1
- 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 +2 -0
- 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
|
@@ -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";
|