@thanhphi/unistyles-ui 0.2.6 → 0.2.7

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,151 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.UIModal = UIModal;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const react_1 = __importDefault(require("react"));
9
+ const react_native_1 = require("react-native");
10
+ const theme_1 = require("./theme");
11
+ function UIModal({ visible, onClose, onOpened, onClosed, position = "center", animationType = "slide", statusBarTranslucent, keyboardAvoiding = false, backdropColor, contentStyle, containerStyle, children, }) {
12
+ const { colors } = (0, theme_1.useUITheme)();
13
+ const Root = keyboardAvoiding && react_native_1.Platform.OS === "ios" ? react_native_1.KeyboardAvoidingView : react_native_1.View;
14
+ const backgroundColor = backdropColor ?? colors.backdrop;
15
+ const renderContent = (motion) => {
16
+ const Panel = motion ? react_native_1.Animated.View : react_native_1.View;
17
+ return ((0, jsx_runtime_1.jsxs)(Root, { behavior: Root === react_native_1.KeyboardAvoidingView ? "padding" : undefined, style: [
18
+ styles.root,
19
+ position === "bottom" && styles.bottom,
20
+ containerStyle,
21
+ !motion && { backgroundColor },
22
+ ], pointerEvents: visible ? "auto" : "none", children: [motion && ((0, jsx_runtime_1.jsx)(react_native_1.Animated.View, { pointerEvents: "none", style: [
23
+ react_native_1.StyleSheet.absoluteFill,
24
+ { backgroundColor, opacity: motion.progress },
25
+ ] })), onClose && ((0, jsx_runtime_1.jsx)(react_native_1.Pressable, { style: react_native_1.StyleSheet.absoluteFill, onPress: onClose, accessible: false })), (0, jsx_runtime_1.jsx)(Panel, { accessibilityViewIsModal: true, onLayout: motion?.onLayout, style: [
26
+ styles.panel,
27
+ position === "fullScreen" && styles.fullScreen,
28
+ contentStyle,
29
+ motion && {
30
+ opacity: motion.progress,
31
+ transform: [
32
+ {
33
+ translateY: motion.progress.interpolate({
34
+ inputRange: [0, 1],
35
+ outputRange: [motion.offset, 0],
36
+ }),
37
+ },
38
+ ],
39
+ },
40
+ ], children: children })] }));
41
+ };
42
+ if (onOpened || onClosed)
43
+ return ((0, jsx_runtime_1.jsx)(ModalTransition, { visible: visible, onClose: onClose, onOpened: onOpened, onClosed: onClosed, animationType: animationType, statusBarTranslucent: statusBarTranslucent, children: renderContent }));
44
+ return ((0, jsx_runtime_1.jsx)(react_native_1.Modal, { visible: visible, transparent: true, animationType: animationType, onRequestClose: onClose ?? ignoreModalClose, statusBarTranslucent: statusBarTranslucent, children: renderContent() }));
45
+ }
46
+ // Modal.onDismiss is iOS-only. Animate the panel and backdrop when callbacks
47
+ // are requested so Android can finish the exit before removing the modal.
48
+ function ModalTransition({ visible, onClose, statusBarTranslucent, onOpened, onClosed, animationType, children, }) {
49
+ const [presented, setPresented] = react_1.default.useState(visible);
50
+ const [shown, setShown] = react_1.default.useState(false);
51
+ const [panelHeight, setPanelHeight] = react_1.default.useState(0);
52
+ const [reduceMotion, setReduceMotion] = react_1.default.useState(false);
53
+ const [progress] = react_1.default.useState(() => new react_native_1.Animated.Value(0));
54
+ const request = react_1.default.useRef({ visible, onOpened, onClosed });
55
+ const dismissing = react_1.default.useRef(false);
56
+ const didOpen = react_1.default.useRef(false);
57
+ const entranceReady = shown && (animationType !== "slide" || panelHeight > 0);
58
+ const animate = animationType !== "none" && !reduceMotion;
59
+ react_1.default.useEffect(() => {
60
+ if (!presented)
61
+ return;
62
+ let active = true;
63
+ react_native_1.AccessibilityInfo.isReduceMotionEnabled().then((value) => {
64
+ if (active)
65
+ setReduceMotion(value);
66
+ });
67
+ const subscription = react_native_1.AccessibilityInfo.addEventListener("reduceMotionChanged", setReduceMotion);
68
+ return () => {
69
+ active = false;
70
+ subscription.remove();
71
+ };
72
+ }, [presented]);
73
+ react_1.default.useLayoutEffect(() => {
74
+ request.current = { visible, onOpened, onClosed };
75
+ }, [visible, onOpened, onClosed]);
76
+ const didDismiss = react_1.default.useCallback(() => {
77
+ if (!dismissing.current)
78
+ return;
79
+ dismissing.current = false;
80
+ setShown(false);
81
+ setPanelHeight(0);
82
+ if (request.current.visible)
83
+ setPresented(true);
84
+ else
85
+ request.current.onClosed?.();
86
+ }, []);
87
+ react_1.default.useEffect(() => {
88
+ if (react_native_1.Platform.OS !== "ios" && !presented)
89
+ didDismiss();
90
+ }, [didDismiss, presented]);
91
+ react_1.default.useEffect(() => {
92
+ if (dismissing.current)
93
+ return;
94
+ if (!presented) {
95
+ if (visible)
96
+ setPresented(true);
97
+ return;
98
+ }
99
+ if (visible && !entranceReady && animate)
100
+ return;
101
+ if (visible && !shown)
102
+ return;
103
+ if (!visible)
104
+ didOpen.current = false;
105
+ const complete = () => {
106
+ if (visible !== request.current.visible)
107
+ return;
108
+ if (visible) {
109
+ if (didOpen.current)
110
+ return;
111
+ didOpen.current = true;
112
+ request.current.onOpened?.();
113
+ }
114
+ else {
115
+ dismissing.current = true;
116
+ setPresented(false);
117
+ }
118
+ };
119
+ const target = visible ? 1 : 0;
120
+ if (!animate || !shown) {
121
+ progress.setValue(target);
122
+ complete();
123
+ return;
124
+ }
125
+ const animation = react_native_1.Animated.timing(progress, {
126
+ toValue: target,
127
+ duration: visible ? 220 : 200,
128
+ easing: react_native_1.Easing.out(react_native_1.Easing.cubic),
129
+ useNativeDriver: true,
130
+ });
131
+ animation.start(({ finished }) => {
132
+ if (finished)
133
+ complete();
134
+ });
135
+ return () => animation.stop();
136
+ }, [animate, entranceReady, presented, progress, shown, visible]);
137
+ return ((0, jsx_runtime_1.jsx)(react_native_1.Modal, { visible: presented, transparent: true, animationType: "none", onRequestClose: visible ? onClose ?? ignoreModalClose : ignoreModalClose, onShow: () => setShown(true), onDismiss: didDismiss, statusBarTranslucent: statusBarTranslucent, children: children(animate
138
+ ? {
139
+ progress,
140
+ offset: animationType === "slide" ? panelHeight : 0,
141
+ onLayout: (event) => setPanelHeight(event.nativeEvent.layout.height),
142
+ }
143
+ : undefined) }));
144
+ }
145
+ const ignoreModalClose = () => { };
146
+ const styles = react_native_1.StyleSheet.create({
147
+ root: { flex: 1, justifyContent: "center" },
148
+ bottom: { justifyContent: "flex-end" },
149
+ panel: { flexShrink: 1 },
150
+ fullScreen: { flex: 1 },
151
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thanhphi/unistyles-ui",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "React Native UI components with app-owned themes and Unistyles integration",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",