@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.
@@ -15,10 +15,16 @@ import {
15
15
  View,
16
16
  type GestureResponderEvent,
17
17
  } from "react-native";
18
- import Animated from "react-native-reanimated";
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, and the `Modal` fades in and out. `"bottom"` pins it to the bottom
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
- // Up while open, and after a close until a sliding sheet is out of sight.
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={slides || reduceMotion ? "none" : "fade"}
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 backdropStyle = useSheetBackdropStyle(drag);
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>