@vvedantb/native-ui 0.27.0 → 0.29.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/README.md +3 -3
- package/dist/DropdownMenu.jsx +26 -12
- package/dist/ai/conversation-context.js +2 -0
- package/dist/ai/conversation.jsx +31 -7
- package/dist/ai/message-context-menu.jsx +111 -113
- package/dist/entries/primitives.js +2 -0
- package/dist/media/ImageGallery.jsx +12 -15
- package/dist/primitives/overlay.jsx +31 -17
- package/dist/primitives/window-layer.jsx +230 -0
- package/package.json +1 -1
- package/src/DropdownMenu.tsx +13 -12
- package/src/ai/conversation-context.ts +3 -0
- package/src/ai/conversation.tsx +21 -0
- package/src/ai/message-context-menu.tsx +11 -10
- package/src/entries/primitives.ts +3 -0
- package/src/media/ImageGallery.tsx +6 -5
- package/src/primitives/overlay.tsx +18 -18
- package/src/primitives/window-layer.tsx +159 -0
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
type ReactNode,
|
|
10
10
|
} from "react";
|
|
11
11
|
import {
|
|
12
|
-
Modal,
|
|
13
12
|
Pressable,
|
|
14
13
|
StyleSheet,
|
|
15
14
|
View,
|
|
@@ -36,6 +35,7 @@ import {
|
|
|
36
35
|
useSheetDragState,
|
|
37
36
|
} from "./sheet-drag";
|
|
38
37
|
import { useControllableState } from "./use-controllable-state";
|
|
38
|
+
import { WindowLayer } from "./window-layer";
|
|
39
39
|
|
|
40
40
|
type OverlayContextValue = {
|
|
41
41
|
open: boolean;
|
|
@@ -116,22 +116,23 @@ export function OverlayClose(props: OverlayPressableProps & { part: string }) {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
|
-
*
|
|
119
|
+
* A `WindowLayer` (in the app's window under `WindowLayerHost`, so a chat
|
|
120
|
+
* under it keeps its place; else a `Modal`) with a tinted backdrop, safe-area padding and keyboard avoidance
|
|
120
121
|
* (`KeyboardAvoidingView`: frame by frame with `KeyboardControllerProvider`).
|
|
121
122
|
* `placement="center"` centres the child (dialogs) inside the insets on every
|
|
122
123
|
* 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
|
|
124
|
+
* ms on open, 150 ms on close), and the layer stays up until it is out of
|
|
124
125
|
* sight. `"bottom"` pins it to the bottom
|
|
125
126
|
* edge (sheets), which pad their own bottom inset: the sheet slides up on
|
|
126
127
|
* open and back down on close, however it closes, and may be dragged down to
|
|
127
128
|
* close (`SheetDragView`); the backdrop fades with the sheet's position. The
|
|
128
|
-
*
|
|
129
|
+
* layer stays up until the sheet is out of sight. The backdrop closes the
|
|
129
130
|
* overlay only when `dismissible`; Android back and the VoiceOver escape
|
|
130
131
|
* gesture always close it, as Escape does on the web. With Reduce Motion on,
|
|
131
132
|
* overlays appear and leave in place. With the Gesture Handler parts
|
|
132
133
|
* (`GestureHandlerProvider`), the content sits in its own root view: on
|
|
133
134
|
* Android a `Modal` is a separate root, and without one there every gesture
|
|
134
|
-
* in it silently does nothing.
|
|
135
|
+
* in it silently does nothing (the fallback `Modal`).
|
|
135
136
|
*/
|
|
136
137
|
export function OverlayModal({
|
|
137
138
|
part,
|
|
@@ -147,7 +148,8 @@ export function OverlayModal({
|
|
|
147
148
|
onShow?: () => void;
|
|
148
149
|
children: ReactNode;
|
|
149
150
|
}) {
|
|
150
|
-
const
|
|
151
|
+
const overlay = useOverlay(part);
|
|
152
|
+
const { open, setOpen } = overlay;
|
|
151
153
|
const reduceMotion = useReduceMotion();
|
|
152
154
|
const slides = placement === "bottom" && !reduceMotion;
|
|
153
155
|
const scales = placement === "center" && !reduceMotion;
|
|
@@ -158,15 +160,12 @@ export function OverlayModal({
|
|
|
158
160
|
const close = () => setOpen(false);
|
|
159
161
|
const ModalRoot = useContext(SheetGestureContext)?.ModalRoot ?? Fragment;
|
|
160
162
|
|
|
163
|
+
if (!shown) return null;
|
|
161
164
|
return (
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
statusBarTranslucent
|
|
167
|
-
onRequestClose={close}
|
|
168
|
-
onShow={onShow}
|
|
169
|
-
>
|
|
165
|
+
// The layer runs its own motion, so no fade; a layer loses the context
|
|
166
|
+
// between it and the host, so the open state is provided again.
|
|
167
|
+
<WindowLayer onRequestClose={close} onShow={onShow} fade={false}>
|
|
168
|
+
<OverlayContext.Provider value={overlay}>
|
|
170
169
|
<ModalRoot>
|
|
171
170
|
<OverlayModalBody
|
|
172
171
|
placement={placement}
|
|
@@ -181,11 +180,12 @@ export function OverlayModal({
|
|
|
181
180
|
{children}
|
|
182
181
|
</OverlayModalBody>
|
|
183
182
|
</ModalRoot>
|
|
184
|
-
|
|
183
|
+
</OverlayContext.Provider>
|
|
184
|
+
</WindowLayer>
|
|
185
185
|
);
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
/** The
|
|
188
|
+
/** The layer's content: mounted with each opening, so a sheet starts below the screen (or at rest). */
|
|
189
189
|
function OverlayModalBody({
|
|
190
190
|
part,
|
|
191
191
|
placement,
|
|
@@ -232,7 +232,7 @@ function OverlayModalBody({
|
|
|
232
232
|
}, [open, slides]);
|
|
233
233
|
// The same for a centred dialog, from wherever `progress` is. Opened again
|
|
234
234
|
// while it leaves, it turns back at once, and the exit callback reports
|
|
235
|
-
// `finished` false, so the
|
|
235
|
+
// `finished` false, so the layer stays up.
|
|
236
236
|
useEffect(() => {
|
|
237
237
|
if (!scales) return;
|
|
238
238
|
if (open) {
|
|
@@ -254,7 +254,7 @@ function OverlayModalBody({
|
|
|
254
254
|
is the measured overlap, so it drops to 0 where the window does
|
|
255
255
|
resize. Frame by frame with `KeyboardControllerProvider`. */}
|
|
256
256
|
<KeyboardAvoidingView style={styles.fill}>
|
|
257
|
-
{/*
|
|
257
|
+
{/* Over the app, not in the dock it opened from: the dock's lift is not for it. */}
|
|
258
258
|
<KeyboardLiftContext.Provider value={false}>
|
|
259
259
|
<Animated.View
|
|
260
260
|
pointerEvents="box-none"
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useId, useLayoutEffect, useSyncExternalStore, type ReactNode } from "react";
|
|
2
|
+
import { BackHandler, Modal, StyleSheet, View } from "react-native";
|
|
3
|
+
import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";
|
|
4
|
+
import { useReduceMotion } from "../useReduceMotion";
|
|
5
|
+
|
|
6
|
+
// Overlays drawn in the app's own window. A React Native `Modal` is a window
|
|
7
|
+
// of its own on Android: it takes the focus as it opens and hands it back as
|
|
8
|
+
// it closes, and each time `react-native-keyboard-controller`'s chat list
|
|
9
|
+
// scrolls to its newest message. A layer in `WindowLayerHost` is a view over
|
|
10
|
+
// the app instead, so the focus never moves. Without a host, `WindowLayer`
|
|
11
|
+
// falls back to a `Modal`, so an app that mounts none keeps working.
|
|
12
|
+
|
|
13
|
+
type Layer = { id: string; node: ReactNode; onRequestClose: () => void; onShow?: () => void; fade: boolean };
|
|
14
|
+
|
|
15
|
+
const EMPTY: readonly Layer[] = [];
|
|
16
|
+
let layers: readonly Layer[] = EMPTY;
|
|
17
|
+
let hosts = 0;
|
|
18
|
+
const listeners = new Set<() => void>();
|
|
19
|
+
|
|
20
|
+
const emit = () => {
|
|
21
|
+
for (const listener of listeners) listener();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const subscribe = (listener: () => void) => {
|
|
25
|
+
listeners.add(listener);
|
|
26
|
+
return () => {
|
|
27
|
+
listeners.delete(listener);
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const upsert = (layer: Layer) => {
|
|
32
|
+
const at = layers.findIndex((item) => item.id === layer.id);
|
|
33
|
+
layers = at === -1 ? [...layers, layer] : layers.map((item, index) => (index === at ? layer : item));
|
|
34
|
+
emit();
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Layers that have called `onShow` once, as a Modal does once per opening.
|
|
38
|
+
const announced = new Set<string>();
|
|
39
|
+
|
|
40
|
+
const showOnce = (layer: Layer) => {
|
|
41
|
+
if (announced.has(layer.id)) return;
|
|
42
|
+
announced.add(layer.id);
|
|
43
|
+
layer.onShow?.();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const remove = (id: string) => {
|
|
47
|
+
announced.delete(id);
|
|
48
|
+
if (!layers.some((item) => item.id === id)) return;
|
|
49
|
+
layers = layers.filter((item) => item.id !== id);
|
|
50
|
+
emit();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Android back closes the newest layer, as a Modal's `onRequestClose`.
|
|
54
|
+
const onBack = () => {
|
|
55
|
+
const top = layers[layers.length - 1];
|
|
56
|
+
if (!top) return false;
|
|
57
|
+
top.onRequestClose();
|
|
58
|
+
return true;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Mounted: the host is there, and it answers Android back for its layers. */
|
|
62
|
+
const hostRef = (view: View | null) => {
|
|
63
|
+
if (view === null) return undefined;
|
|
64
|
+
hosts += 1;
|
|
65
|
+
const back = BackHandler.addEventListener("hardwareBackPress", onBack);
|
|
66
|
+
emit();
|
|
67
|
+
return () => {
|
|
68
|
+
hosts -= 1;
|
|
69
|
+
back.remove();
|
|
70
|
+
emit();
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Wrap the app once (inside `NativeUiProvider`, around the navigator): the
|
|
76
|
+
* `WindowLayer`s draw over it, in the same window. While a layer shows, the
|
|
77
|
+
* app under it is hidden from TalkBack and VoiceOver, as under a Modal.
|
|
78
|
+
*/
|
|
79
|
+
export function WindowLayerHost({ children }: { children: ReactNode }) {
|
|
80
|
+
const shown = useSyncExternalStore(subscribe, () => layers, () => EMPTY);
|
|
81
|
+
// The layers fade in and out as a Modal's `fade`, unless they move on their
|
|
82
|
+
// own (`fade={false}`); with Reduce Motion they appear at once.
|
|
83
|
+
const enter = FadeIn.duration(200).reduceMotion(ReduceMotion.System);
|
|
84
|
+
const exit = FadeOut.duration(150).reduceMotion(ReduceMotion.System);
|
|
85
|
+
return (
|
|
86
|
+
<View ref={hostRef} style={styles.fill}>
|
|
87
|
+
<View
|
|
88
|
+
style={styles.fill}
|
|
89
|
+
importantForAccessibility={shown.length > 0 ? "no-hide-descendants" : "auto"}
|
|
90
|
+
accessibilityElementsHidden={shown.length > 0}
|
|
91
|
+
>
|
|
92
|
+
{children}
|
|
93
|
+
</View>
|
|
94
|
+
{shown.map((layer) => (
|
|
95
|
+
<Animated.View
|
|
96
|
+
key={layer.id}
|
|
97
|
+
entering={layer.fade ? enter : undefined}
|
|
98
|
+
exiting={layer.fade ? exit : undefined}
|
|
99
|
+
onLayout={() => showOnce(layer)}
|
|
100
|
+
style={StyleSheet.absoluteFill}
|
|
101
|
+
pointerEvents="box-none"
|
|
102
|
+
>
|
|
103
|
+
{layer.node}
|
|
104
|
+
</Animated.View>
|
|
105
|
+
))}
|
|
106
|
+
</View>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* An overlay over the whole app, in its own window only when it must be: in
|
|
112
|
+
* `WindowLayerHost` when the app has one, else a transparent `Modal` (with
|
|
113
|
+
* `onDismiss` there). Render it only while the overlay shows. Android back
|
|
114
|
+
* calls `onRequestClose`. Its children keep the providers above the host, not
|
|
115
|
+
* those between the host and this element: an overlay with its own context
|
|
116
|
+
* (a Dialog's open state, a menu's) provides it again inside.
|
|
117
|
+
*/
|
|
118
|
+
export function WindowLayer({
|
|
119
|
+
onRequestClose,
|
|
120
|
+
onDismiss,
|
|
121
|
+
onShow,
|
|
122
|
+
fade = true,
|
|
123
|
+
children,
|
|
124
|
+
}: {
|
|
125
|
+
onRequestClose: () => void;
|
|
126
|
+
/** The fallback Modal's dismiss (iOS); a layer is gone as it unmounts. */
|
|
127
|
+
onDismiss?: () => void;
|
|
128
|
+
/** Once the layer is on screen, as a Modal's `onShow`. */
|
|
129
|
+
onShow?: () => void;
|
|
130
|
+
/** Fade in and out. `false` for an overlay that runs its own motion. Default `true`. */
|
|
131
|
+
fade?: boolean;
|
|
132
|
+
children: ReactNode;
|
|
133
|
+
}) {
|
|
134
|
+
const id = useId();
|
|
135
|
+
const reduceMotion = useReduceMotion();
|
|
136
|
+
const hosted = useSyncExternalStore(subscribe, () => hosts > 0, () => false);
|
|
137
|
+
// A portal has to hand the host its children after each render: an external
|
|
138
|
+
// store fed from an effect, the one sync React offers for it.
|
|
139
|
+
useLayoutEffect(() => {
|
|
140
|
+
if (hosted) upsert({ id, node: children, onRequestClose, onShow, fade });
|
|
141
|
+
}, [hosted, id, children, onRequestClose, onShow, fade]);
|
|
142
|
+
useLayoutEffect(() => () => remove(id), [id]);
|
|
143
|
+
if (hosted) return null;
|
|
144
|
+
return (
|
|
145
|
+
<Modal
|
|
146
|
+
visible
|
|
147
|
+
transparent
|
|
148
|
+
statusBarTranslucent
|
|
149
|
+
animationType={fade && !reduceMotion ? "fade" : "none"}
|
|
150
|
+
onRequestClose={onRequestClose}
|
|
151
|
+
onDismiss={onDismiss}
|
|
152
|
+
onShow={onShow}
|
|
153
|
+
>
|
|
154
|
+
{children}
|
|
155
|
+
</Modal>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const styles = StyleSheet.create({ fill: { flex: 1 } });
|