@vvedantb/native-ui 0.24.1 → 0.24.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/dist/DropdownMenu.jsx +245 -116
- package/dist/ai/message-action-sheet.jsx +182 -202
- 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/ai/message-action-sheet.tsx +90 -97
- 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`. */
|
|
@@ -39,8 +40,9 @@ export type MessageActionSheetProps = {
|
|
|
39
40
|
|
|
40
41
|
/**
|
|
41
42
|
* 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
|
|
43
|
+
* the kit's `OverlayModal` (an RN `Modal`, no bottom-sheet library) with one row per action
|
|
44
|
+
* and a Cancel row. It slides up and down as `Sheet` does (a drag down closes it too; with
|
|
45
|
+
* Reduce Motion on it appears and leaves in place). It closes on the backdrop, Cancel, Android back and after
|
|
44
46
|
* an action. A `selectionHaptic()` fires when it opens (`expo-haptics` is an
|
|
45
47
|
* optional peer). Row testIDs are `{testID}-action-{key}`.
|
|
46
48
|
*/
|
|
@@ -55,7 +57,6 @@ export function MessageActionSheet({
|
|
|
55
57
|
const scheme = useScheme();
|
|
56
58
|
const largerText = useLargerText();
|
|
57
59
|
const insets = useSafeAreaInsets();
|
|
58
|
-
const reduceMotion = useReduceMotion();
|
|
59
60
|
|
|
60
61
|
useEffect(() => {
|
|
61
62
|
if (open) selectionHaptic();
|
|
@@ -66,103 +67,95 @@ export function MessageActionSheet({
|
|
|
66
67
|
const minHeight = largerText ? 56 : MIN_TOUCH_TARGET + 4;
|
|
67
68
|
|
|
68
69
|
return (
|
|
69
|
-
<
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
onRequestClose={onClose}
|
|
70
|
+
<OverlayRoot
|
|
71
|
+
open={open}
|
|
72
|
+
onOpenChange={(next) => {
|
|
73
|
+
if (!next) onClose();
|
|
74
|
+
}}
|
|
75
75
|
>
|
|
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
|
-
>
|
|
76
|
+
{/* The kit's bottom-sheet overlay: it slides with `SHEET_SPRING`, the backdrop
|
|
77
|
+
follows its position and a drag down closes it. The part name keeps
|
|
78
|
+
the backdrop testID `{testID}-backdrop`. */}
|
|
79
|
+
<OverlayModal part={testID} placement="bottom" dismissible>
|
|
80
|
+
<SheetDragView enabled>
|
|
93
81
|
<View
|
|
94
|
-
|
|
95
|
-
|
|
82
|
+
testID={testID}
|
|
83
|
+
accessibilityViewIsModal
|
|
84
|
+
accessibilityLabel={title}
|
|
85
|
+
onAccessibilityEscape={onClose}
|
|
86
|
+
className="w-full self-center px-2"
|
|
87
|
+
style={{ maxWidth: 480, paddingBottom: insets.bottom + 8 }}
|
|
96
88
|
>
|
|
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)]}
|
|
89
|
+
<View
|
|
90
|
+
className="overflow-hidden"
|
|
91
|
+
style={{ backgroundColor: tokens.color.surface[scheme], borderRadius: tokens.radius.card }}
|
|
102
92
|
>
|
|
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
|
-
|
|
93
|
+
<Text
|
|
94
|
+
accessibilityRole="header"
|
|
95
|
+
numberOfLines={1}
|
|
96
|
+
className={cn(caption, "px-4 pb-1 pt-3 font-instrument-sans-medium")}
|
|
97
|
+
style={[sizeStyle(caption), typeStyle("medium"), textStyle("muted", scheme)]}
|
|
98
|
+
>
|
|
99
|
+
{title}
|
|
100
|
+
</Text>
|
|
101
|
+
{actions.map((action, index) => {
|
|
102
|
+
const Icon = action.icon;
|
|
103
|
+
const color = action.destructive ? palette.danger[scheme] : tokens.color.foreground[scheme];
|
|
104
|
+
return (
|
|
105
|
+
<Pressable
|
|
106
|
+
key={action.key}
|
|
107
|
+
testID={`${testID}-action-${action.key}`}
|
|
108
|
+
accessibilityRole="button"
|
|
109
|
+
accessibilityLabel={action.label}
|
|
110
|
+
hitSlop={touchSlop(MIN_TOUCH_TARGET)}
|
|
111
|
+
onPress={() => {
|
|
112
|
+
onClose();
|
|
113
|
+
action.onPress();
|
|
114
|
+
}}
|
|
115
|
+
className="flex-row items-center gap-3 px-4"
|
|
116
|
+
style={[
|
|
117
|
+
{ minHeight },
|
|
118
|
+
index > 0 && {
|
|
119
|
+
borderTopWidth: StyleSheet.hairlineWidth,
|
|
120
|
+
borderTopColor: tokens.color.separator[scheme],
|
|
121
|
+
},
|
|
122
|
+
]}
|
|
133
123
|
>
|
|
134
|
-
{
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
124
|
+
{Icon ? <Icon size={largerText ? 22 : 20} color={color} /> : null}
|
|
125
|
+
<Text
|
|
126
|
+
numberOfLines={1}
|
|
127
|
+
className={cn(body, "min-w-0 flex-1 font-instrument-sans-medium")}
|
|
128
|
+
style={[sizeStyle(body), typeStyle("medium"), { color }]}
|
|
129
|
+
>
|
|
130
|
+
{action.label}
|
|
131
|
+
</Text>
|
|
132
|
+
</Pressable>
|
|
133
|
+
);
|
|
134
|
+
})}
|
|
135
|
+
</View>
|
|
136
|
+
<Pressable
|
|
137
|
+
testID={`${testID}-cancel`}
|
|
138
|
+
accessibilityRole="button"
|
|
139
|
+
accessibilityLabel={cancelLabel}
|
|
140
|
+
onPress={onClose}
|
|
141
|
+
hitSlop={touchSlop(MIN_TOUCH_TARGET)}
|
|
142
|
+
className="mt-2 items-center justify-center"
|
|
143
|
+
style={{
|
|
144
|
+
minHeight,
|
|
145
|
+
backgroundColor: tokens.color.surface[scheme],
|
|
146
|
+
borderRadius: tokens.radius.card,
|
|
147
|
+
}}
|
|
156
148
|
>
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
149
|
+
<Text
|
|
150
|
+
className={cn(body, "font-instrument-sans-semibold")}
|
|
151
|
+
style={[sizeStyle(body), typeStyle("semibold"), textStyle("foreground", scheme)]}
|
|
152
|
+
>
|
|
153
|
+
{cancelLabel}
|
|
154
|
+
</Text>
|
|
155
|
+
</Pressable>
|
|
156
|
+
</View>
|
|
157
|
+
</SheetDragView>
|
|
158
|
+
</OverlayModal>
|
|
159
|
+
</OverlayRoot>
|
|
163
160
|
);
|
|
164
161
|
}
|
|
165
|
-
|
|
166
|
-
const styles = StyleSheet.create({
|
|
167
|
-
anchor: { flex: 1, justifyContent: "flex-end" },
|
|
168
|
-
});
|
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;
|
|
@@ -15,10 +15,16 @@ import {
|
|
|
15
15
|
View,
|
|
16
16
|
type GestureResponderEvent,
|
|
17
17
|
} from "react-native";
|
|
18
|
-
import Animated
|
|
18
|
+
import Animated, {
|
|
19
|
+
runOnJS,
|
|
20
|
+
useAnimatedStyle,
|
|
21
|
+
useSharedValue,
|
|
22
|
+
withTiming,
|
|
23
|
+
} from "react-native-reanimated";
|
|
19
24
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
20
25
|
import { palette } from "../palette";
|
|
21
26
|
import { KeyboardAvoidingView, KeyboardLiftContext } from "./keyboard-parts";
|
|
27
|
+
import { DIALOG_ENTER, DIALOG_EXIT, PANEL_SCALE_FROM } from "../motion";
|
|
22
28
|
import { useReduceMotion } from "../useReduceMotion";
|
|
23
29
|
import { useScheme } from "../useScheme";
|
|
24
30
|
import {
|
|
@@ -113,7 +119,9 @@ export function OverlayClose(props: OverlayPressableProps & { part: string }) {
|
|
|
113
119
|
* RN `Modal` with a tinted backdrop, safe-area padding and keyboard avoidance
|
|
114
120
|
* (`KeyboardAvoidingView`: frame by frame with `KeyboardControllerProvider`).
|
|
115
121
|
* `placement="center"` centres the child (dialogs) inside the insets on every
|
|
116
|
-
* side
|
|
122
|
+
* side: the backdrop fades while the panel scales from 0.96 and fades in (200
|
|
123
|
+
* ms on open, 150 ms on close), and the `Modal` stays up until it is out of
|
|
124
|
+
* sight. `"bottom"` pins it to the bottom
|
|
117
125
|
* edge (sheets), which pad their own bottom inset: the sheet slides up on
|
|
118
126
|
* open and back down on close, however it closes, and may be dragged down to
|
|
119
127
|
* close (`SheetDragView`); the backdrop fades with the sheet's position. The
|
|
@@ -142,10 +150,11 @@ export function OverlayModal({
|
|
|
142
150
|
const { open, setOpen } = useOverlay(part);
|
|
143
151
|
const reduceMotion = useReduceMotion();
|
|
144
152
|
const slides = placement === "bottom" && !reduceMotion;
|
|
145
|
-
|
|
153
|
+
const scales = placement === "center" && !reduceMotion;
|
|
154
|
+
// Up while open, and after a close until a sliding sheet or a scaling dialog is out of sight.
|
|
146
155
|
const [shown, setShown] = useState(open);
|
|
147
156
|
if (open && !shown) setShown(true);
|
|
148
|
-
if (!open && shown && !slides) setShown(false);
|
|
157
|
+
if (!open && shown && !slides && !scales) setShown(false);
|
|
149
158
|
const close = () => setOpen(false);
|
|
150
159
|
const ModalRoot = useContext(SheetGestureContext)?.ModalRoot ?? Fragment;
|
|
151
160
|
|
|
@@ -153,7 +162,7 @@ export function OverlayModal({
|
|
|
153
162
|
<Modal
|
|
154
163
|
visible={shown}
|
|
155
164
|
transparent
|
|
156
|
-
animationType=
|
|
165
|
+
animationType="none"
|
|
157
166
|
statusBarTranslucent
|
|
158
167
|
onRequestClose={close}
|
|
159
168
|
onShow={onShow}
|
|
@@ -164,6 +173,7 @@ export function OverlayModal({
|
|
|
164
173
|
dismissible={dismissible}
|
|
165
174
|
open={open}
|
|
166
175
|
slides={slides}
|
|
176
|
+
scales={scales}
|
|
167
177
|
close={close}
|
|
168
178
|
onHidden={() => setShown(false)}
|
|
169
179
|
part={part}
|
|
@@ -182,6 +192,7 @@ function OverlayModalBody({
|
|
|
182
192
|
dismissible,
|
|
183
193
|
open,
|
|
184
194
|
slides,
|
|
195
|
+
scales,
|
|
185
196
|
close,
|
|
186
197
|
onHidden,
|
|
187
198
|
children,
|
|
@@ -191,6 +202,7 @@ function OverlayModalBody({
|
|
|
191
202
|
dismissible: boolean;
|
|
192
203
|
open: boolean;
|
|
193
204
|
slides: boolean;
|
|
205
|
+
scales: boolean;
|
|
194
206
|
close: () => void;
|
|
195
207
|
onHidden: () => void;
|
|
196
208
|
children: ReactNode;
|
|
@@ -198,7 +210,17 @@ function OverlayModalBody({
|
|
|
198
210
|
const scheme = useScheme();
|
|
199
211
|
const insets = useSafeAreaInsets();
|
|
200
212
|
const drag = useSheetDragState(close, slides);
|
|
201
|
-
const
|
|
213
|
+
const sheetBackdropStyle = useSheetBackdropStyle(drag);
|
|
214
|
+
// A centred dialog: one 0 to 1 `progress` drives the backdrop fade and the
|
|
215
|
+
// panel's scale and opacity. It mounts at 0 with each opening, and at 1
|
|
216
|
+
// under Reduce Motion, so there the dialog simply appears.
|
|
217
|
+
const progress = useSharedValue(scales ? 0 : 1);
|
|
218
|
+
const centreBackdropStyle = useAnimatedStyle(() => ({ opacity: progress.get() }));
|
|
219
|
+
const centrePanelStyle = useAnimatedStyle(() => ({
|
|
220
|
+
opacity: progress.get(),
|
|
221
|
+
transform: [{ scale: PANEL_SCALE_FROM + (1 - PANEL_SCALE_FROM) * progress.get() }],
|
|
222
|
+
}));
|
|
223
|
+
const backdropStyle = placement === "center" ? centreBackdropStyle : sheetBackdropStyle;
|
|
202
224
|
// The open state lives in React, the sheet's position on the UI thread:
|
|
203
225
|
// each change of `open` starts the slide that matches it. The slide in on
|
|
204
226
|
// opening starts from the sheet's first layout (`SheetDragView`).
|
|
@@ -208,6 +230,23 @@ function OverlayModalBody({
|
|
|
208
230
|
else slideSheetOut(drag, onHidden);
|
|
209
231
|
// Only a change of `open` starts a slide; `drag` and `onHidden` keep their meaning.
|
|
210
232
|
}, [open, slides]);
|
|
233
|
+
// The same for a centred dialog, from wherever `progress` is. Opened again
|
|
234
|
+
// while it leaves, it turns back at once, and the exit callback reports
|
|
235
|
+
// `finished` false, so the `Modal` stays up.
|
|
236
|
+
useEffect(() => {
|
|
237
|
+
if (!scales) return;
|
|
238
|
+
if (open) {
|
|
239
|
+
progress.set(withTiming(1, DIALOG_ENTER));
|
|
240
|
+
} else {
|
|
241
|
+
progress.set(
|
|
242
|
+
withTiming(0, DIALOG_EXIT, (finished) => {
|
|
243
|
+
"worklet";
|
|
244
|
+
if (finished) runOnJS(onHidden)();
|
|
245
|
+
}),
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
// As the slide: only a change of `open` starts it.
|
|
249
|
+
}, [open, scales]);
|
|
211
250
|
return (
|
|
212
251
|
<SheetDragProvider value={drag}>
|
|
213
252
|
{/* Android too: a modal window with `statusBarTranslucent` may not
|
|
@@ -231,7 +270,7 @@ function OverlayModalBody({
|
|
|
231
270
|
style={StyleSheet.absoluteFill}
|
|
232
271
|
/>
|
|
233
272
|
</Animated.View>
|
|
234
|
-
<View
|
|
273
|
+
<Animated.View
|
|
235
274
|
pointerEvents="box-none"
|
|
236
275
|
onAccessibilityEscape={close}
|
|
237
276
|
style={[
|
|
@@ -250,10 +289,11 @@ function OverlayModalBody({
|
|
|
250
289
|
paddingLeft: insets.left + 16,
|
|
251
290
|
paddingRight: insets.right + 16,
|
|
252
291
|
},
|
|
292
|
+
placement === "center" ? centrePanelStyle : null,
|
|
253
293
|
]}
|
|
254
294
|
>
|
|
255
295
|
{children}
|
|
256
|
-
</View>
|
|
296
|
+
</Animated.View>
|
|
257
297
|
</KeyboardLiftContext.Provider>
|
|
258
298
|
</KeyboardAvoidingView>
|
|
259
299
|
</SheetDragProvider>
|