@vvedantb/native-ui 0.24.0 → 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.
@@ -1,5 +1,5 @@
1
1
  import { useEffect, type ComponentType } from "react";
2
- import { Modal, Pressable, StyleSheet, Text, View } from "react-native";
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` (no bottom-sheet library) with one pressable row per action
43
- * and a Cancel row. It closes on the backdrop, Cancel, Android back and after
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
- <Modal
70
- visible={open}
71
- transparent
72
- animationType={reduceMotion ? "none" : "fade"}
73
- statusBarTranslucent
74
- onRequestClose={onClose}
70
+ <OverlayRoot
71
+ open={open}
72
+ onOpenChange={(next) => {
73
+ if (!next) onClose();
74
+ }}
75
75
  >
76
- <Pressable
77
- testID={`${testID}-backdrop`}
78
- accessibilityRole="button"
79
- accessibilityLabel="Close"
80
- onPress={onClose}
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
- className="overflow-hidden"
95
- style={{ backgroundColor: tokens.color.surface[scheme], borderRadius: tokens.radius.card }}
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
- <Text
98
- accessibilityRole="header"
99
- numberOfLines={1}
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
- {title}
104
- </Text>
105
- {actions.map((action, index) => {
106
- const Icon = action.icon;
107
- const color = action.destructive ? palette.danger[scheme] : tokens.color.foreground[scheme];
108
- return (
109
- <Pressable
110
- key={action.key}
111
- testID={`${testID}-action-${action.key}`}
112
- accessibilityRole="button"
113
- accessibilityLabel={action.label}
114
- hitSlop={touchSlop(MIN_TOUCH_TARGET)}
115
- onPress={() => {
116
- onClose();
117
- action.onPress();
118
- }}
119
- className="flex-row items-center gap-3 px-4"
120
- style={[
121
- { minHeight },
122
- index > 0 && {
123
- borderTopWidth: StyleSheet.hairlineWidth,
124
- borderTopColor: tokens.color.separator[scheme],
125
- },
126
- ]}
127
- >
128
- {Icon ? <Icon size={largerText ? 22 : 20} color={color} /> : null}
129
- <Text
130
- numberOfLines={1}
131
- className={cn(body, "min-w-0 flex-1 font-instrument-sans-medium")}
132
- style={[sizeStyle(body), typeStyle("medium"), { color }]}
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
- {action.label}
135
- </Text>
136
- </Pressable>
137
- );
138
- })}
139
- </View>
140
- <Pressable
141
- testID={`${testID}-cancel`}
142
- accessibilityRole="button"
143
- accessibilityLabel={cancelLabel}
144
- onPress={onClose}
145
- hitSlop={touchSlop(MIN_TOUCH_TARGET)}
146
- className="mt-2 items-center justify-center"
147
- style={{
148
- minHeight,
149
- backgroundColor: tokens.color.surface[scheme],
150
- borderRadius: tokens.radius.card,
151
- }}
152
- >
153
- <Text
154
- className={cn(body, "font-instrument-sans-semibold")}
155
- style={[sizeStyle(body), typeStyle("semibold"), textStyle("foreground", scheme)]}
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
- {cancelLabel}
158
- </Text>
159
- </Pressable>
160
- </View>
161
- </View>
162
- </Modal>
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;
@@ -0,0 +1,72 @@
1
+ import { cssInterop } from "nativewind";
2
+ import { useCallback, useEffect } from "react";
3
+ import { Pressable } from "react-native";
4
+ import Animated, { useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated";
5
+ import { EASE_OUT } from "./motion";
6
+ import { useReduceMotion } from "./useReduceMotion";
7
+
8
+ /**
9
+ * A `Pressable` whose `style` can take the style from `usePressScale`.
10
+ * NativeWind maps `className` only for components it knows, so it is told
11
+ * about this one, or the classes would be dropped.
12
+ */
13
+ export const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
14
+ cssInterop(AnimatedPressable, { className: "style" });
15
+
16
+ export type PressScaleOptions = {
17
+ /** Scale while pressed. Default 0.97. */
18
+ scale?: number;
19
+ /** Opacity while pressed, a light dim. Default 0.7, the dim these controls had before the scale. */
20
+ dim?: number;
21
+ /** Dim used instead under Reduce Motion. Default `dim`. */
22
+ reducedDim?: number;
23
+ /** Opacity at rest, such as 0.5 when disabled. The press dims from here. Default 1. */
24
+ restOpacity?: number;
25
+ /** Press-in duration in ms. Default 120. */
26
+ pressInMs?: number;
27
+ /** Release duration in ms. Default 160. */
28
+ pressOutMs?: number;
29
+ /** A disabled control lets go at once, so it cannot stay pressed. */
30
+ disabled?: boolean;
31
+ };
32
+
33
+ /**
34
+ * Press feedback for a small control: a scale and a light dim that ease in on
35
+ * press-in and ease back on release. Pass `onPressIn` and `onPressOut` to the
36
+ * `Pressable` and `style` to the animated view that should scale (an
37
+ * `AnimatedPressable`, or an `Animated.View` inside the `Pressable`). One
38
+ * shared value on the UI thread drives transform and opacity only. A new press
39
+ * or release starts from the current value, so a fast tap never sticks. With
40
+ * Reduce Motion nothing animates and nothing scales: the dim switches on and
41
+ * off at once.
42
+ */
43
+ export function usePressScale({
44
+ scale = 0.97,
45
+ dim = 0.7,
46
+ reducedDim = dim,
47
+ restOpacity = 1,
48
+ pressInMs = 120,
49
+ pressOutMs = 160,
50
+ disabled = false,
51
+ }: PressScaleOptions = {}) {
52
+ const reduceMotion = useReduceMotion();
53
+ const progress = useSharedValue(0);
54
+ const target = reduceMotion ? reducedDim : dim;
55
+ const onPressIn = useCallback(() => {
56
+ progress.set(reduceMotion ? 1 : withTiming(1, { duration: pressInMs, easing: EASE_OUT }));
57
+ }, [progress, reduceMotion, pressInMs]);
58
+ const onPressOut = useCallback(() => {
59
+ progress.set(reduceMotion ? 0 : withTiming(0, { duration: pressOutMs, easing: EASE_OUT }));
60
+ }, [progress, reduceMotion, pressOutMs]);
61
+ useEffect(() => {
62
+ if (disabled) progress.set(0);
63
+ }, [disabled, progress]);
64
+ const style = useAnimatedStyle(() => {
65
+ const p = progress.get();
66
+ return {
67
+ opacity: restOpacity * (1 - p * (1 - target)),
68
+ transform: [{ scale: reduceMotion ? 1 : 1 - p * (1 - scale) }],
69
+ };
70
+ });
71
+ return { style, onPressIn, onPressOut };
72
+ }
@@ -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>
@@ -5,6 +5,7 @@ import { IconCheck, IconChevronDown, IconChevronUp } from "@tabler/icons-react-n
5
5
  import * as React from "react";
6
6
  import { Platform, ScrollView, StyleSheet, Text, View } from "react-native";
7
7
  import { FadeIn, FadeOut } from "react-native-reanimated";
8
+ import { EASE_OUT } from "../motion";
8
9
  import { NativeOnlyAnimatedView } from "./native-only-animated-view";
9
10
  import { SelectHost } from "./select-host";
10
11
  import { TextClassContext } from "./text";
@@ -80,6 +81,10 @@ type SelectContentProps = React.ComponentPropsWithoutRef<
80
81
  portalHost?: string;
81
82
  };
82
83
 
84
+ /** The popover fades in and out on the strong ease-out. */
85
+ const SELECT_ENTER = FadeIn.duration(150).easing(EASE_OUT);
86
+ const SELECT_EXIT = FadeOut.duration(100).easing(EASE_OUT);
87
+
83
88
  const SelectContent = React.forwardRef<
84
89
  React.ElementRef<typeof SelectPrimitive.Content>,
85
90
  SelectContentProps
@@ -89,8 +94,8 @@ const SelectContent = React.forwardRef<
89
94
  <SelectPrimitive.Overlay style={overlayFill}>
90
95
  <TextClassContext.Provider value="text-popover-foreground">
91
96
  <NativeOnlyAnimatedView
92
- entering={FadeIn}
93
- exiting={FadeOut}
97
+ entering={SELECT_ENTER}
98
+ exiting={SELECT_EXIT}
94
99
  className="z-50"
95
100
  // Content is absolutely positioned, so a bare wrapper has zero height
96
101
  // and UI tests drop its children as invisible. Fill the overlay and