@vvedantb/native-ui 0.26.1 → 0.28.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.
@@ -0,0 +1,133 @@
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 };
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
+ const remove = (id: string) => {
38
+ if (!layers.some((item) => item.id === id)) return;
39
+ layers = layers.filter((item) => item.id !== id);
40
+ emit();
41
+ };
42
+
43
+ // Android back closes the newest layer, as a Modal's `onRequestClose`.
44
+ const onBack = () => {
45
+ const top = layers[layers.length - 1];
46
+ if (!top) return false;
47
+ top.onRequestClose();
48
+ return true;
49
+ };
50
+
51
+ /** Mounted: the host is there, and it answers Android back for its layers. */
52
+ const hostRef = (view: View | null) => {
53
+ if (view === null) return undefined;
54
+ hosts += 1;
55
+ const back = BackHandler.addEventListener("hardwareBackPress", onBack);
56
+ emit();
57
+ return () => {
58
+ hosts -= 1;
59
+ back.remove();
60
+ emit();
61
+ };
62
+ };
63
+
64
+ /**
65
+ * Wrap the app once (inside `NativeUiProvider`, around the navigator): the
66
+ * `WindowLayer`s draw over it, in the same window. While a layer shows, the
67
+ * app under it is hidden from TalkBack and VoiceOver, as under a Modal.
68
+ */
69
+ export function WindowLayerHost({ children }: { children: ReactNode }) {
70
+ const shown = useSyncExternalStore(subscribe, () => layers, () => EMPTY);
71
+ // The layers fade in and out as a Modal's `fade`; with Reduce Motion they appear at once.
72
+ const enter = FadeIn.duration(200).reduceMotion(ReduceMotion.System);
73
+ const exit = FadeOut.duration(150).reduceMotion(ReduceMotion.System);
74
+ return (
75
+ <View ref={hostRef} style={styles.fill}>
76
+ <View
77
+ style={styles.fill}
78
+ importantForAccessibility={shown.length > 0 ? "no-hide-descendants" : "auto"}
79
+ accessibilityElementsHidden={shown.length > 0}
80
+ >
81
+ {children}
82
+ </View>
83
+ {shown.map((layer) => (
84
+ <Animated.View key={layer.id} entering={enter} exiting={exit} style={StyleSheet.absoluteFill} pointerEvents="box-none">
85
+ {layer.node}
86
+ </Animated.View>
87
+ ))}
88
+ </View>
89
+ );
90
+ }
91
+
92
+ /**
93
+ * An overlay over the whole app, in its own window only when it must be: in
94
+ * `WindowLayerHost` when the app has one, else a transparent `Modal` (with
95
+ * `onDismiss` there). Render it only while the overlay shows. Android back
96
+ * calls `onRequestClose`. Its children keep the providers above the host, not
97
+ * those between the host and this element.
98
+ */
99
+ export function WindowLayer({
100
+ onRequestClose,
101
+ onDismiss,
102
+ children,
103
+ }: {
104
+ onRequestClose: () => void;
105
+ /** The fallback Modal's dismiss (iOS); a layer is gone as it unmounts. */
106
+ onDismiss?: () => void;
107
+ children: ReactNode;
108
+ }) {
109
+ const id = useId();
110
+ const reduceMotion = useReduceMotion();
111
+ const hosted = useSyncExternalStore(subscribe, () => hosts > 0, () => false);
112
+ // A portal has to hand the host its children after each render: an external
113
+ // store fed from an effect, the one sync React offers for it.
114
+ useLayoutEffect(() => {
115
+ if (hosted) upsert({ id, node: children, onRequestClose });
116
+ }, [hosted, id, children, onRequestClose]);
117
+ useLayoutEffect(() => () => remove(id), [id]);
118
+ if (hosted) return null;
119
+ return (
120
+ <Modal
121
+ visible
122
+ transparent
123
+ statusBarTranslucent
124
+ animationType={reduceMotion ? "none" : "fade"}
125
+ onRequestClose={onRequestClose}
126
+ onDismiss={onDismiss}
127
+ >
128
+ {children}
129
+ </Modal>
130
+ );
131
+ }
132
+
133
+ const styles = StyleSheet.create({ fill: { flex: 1 } });