@react-native-ama/bottom-sheet 2.0.0-beta.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/dist/components/BottomSheet.d.ts +41 -0
- package/dist/components/BottomSheet.js +290 -0
- package/dist/hooks/useBottomSheetGestureHandler.d.ts +14 -0
- package/dist/hooks/useBottomSheetGestureHandler.js +36 -0
- package/dist/hooks/useKeyboard.d.ts +5 -0
- package/dist/hooks/useKeyboard.js +87 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -0
- package/package.json +104 -0
- package/src/components/BottomSheet.test.tsx +482 -0
- package/src/components/BottomSheet.tsx +502 -0
- package/src/hooks/useBottomSheetGestureHandler.test.ts +158 -0
- package/src/hooks/useBottomSheetGestureHandler.ts +62 -0
- package/src/hooks/useKeyboard.test.ts +137 -0
- package/src/hooks/useKeyboard.ts +130 -0
- package/src/index.ts +6 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { ModalProps, ScrollViewProps, ViewStyle } from "react-native";
|
|
3
|
+
export type BottomSheetProps = {
|
|
4
|
+
animationDuration?: number;
|
|
5
|
+
autoCloseDelay?: number;
|
|
6
|
+
avoidKeyboard?: boolean;
|
|
7
|
+
bottomSheetStyle?: ViewStyle | ViewStyle[];
|
|
8
|
+
closeActionAccessibilityLabel: string;
|
|
9
|
+
closeDistance?: number;
|
|
10
|
+
footerComponent?: React.ReactElement;
|
|
11
|
+
handleComponent?: React.ReactElement | "none";
|
|
12
|
+
handleStyle?: ViewStyle | ViewStyle[];
|
|
13
|
+
headerComponent?: React.ReactElement;
|
|
14
|
+
maxHeight?: number;
|
|
15
|
+
minVelocityToClose?: number;
|
|
16
|
+
onBottomSheetHidden?: () => void;
|
|
17
|
+
onClose: () => void;
|
|
18
|
+
overlayOpacity?: number;
|
|
19
|
+
overlayStyle?: ViewStyle | ViewStyle[];
|
|
20
|
+
panGestureEnabled?: boolean;
|
|
21
|
+
persistent?: boolean;
|
|
22
|
+
hasScrollableContent?: boolean;
|
|
23
|
+
scrollViewProps?: Omit<ScrollViewWrapperProps, "testID" | "maxScrollViewHeight">;
|
|
24
|
+
testID?: string;
|
|
25
|
+
topInset: number;
|
|
26
|
+
visible: boolean;
|
|
27
|
+
ref?: React.RefObject<BottomSheetActions>;
|
|
28
|
+
shouldHandleKeyboardEvents?: boolean;
|
|
29
|
+
supportedOrientations?: ModalProps["supportedOrientations"];
|
|
30
|
+
isOverlayAccessible?: boolean;
|
|
31
|
+
};
|
|
32
|
+
export type BottomSheetActions = {
|
|
33
|
+
close: () => Promise<void>;
|
|
34
|
+
isVisible: () => boolean;
|
|
35
|
+
};
|
|
36
|
+
export declare const BottomSheet: React.ForwardRefExoticComponent<Omit<React.PropsWithChildren<BottomSheetProps>, "ref"> & React.RefAttributes<BottomSheetActions>>;
|
|
37
|
+
type ScrollViewWrapperProps = {
|
|
38
|
+
testID?: string;
|
|
39
|
+
maxScrollViewHeight: number;
|
|
40
|
+
} & ScrollViewProps;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
35
|
+
var t = {};
|
|
36
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
37
|
+
t[p] = s[p];
|
|
38
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
39
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
40
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
41
|
+
t[p[i]] = s[p[i]];
|
|
42
|
+
}
|
|
43
|
+
return t;
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.BottomSheet = void 0;
|
|
47
|
+
const React = __importStar(require("react"));
|
|
48
|
+
const react_native_1 = require("react-native");
|
|
49
|
+
const react_native_gesture_handler_1 = require("react-native-gesture-handler");
|
|
50
|
+
const react_native_reanimated_1 = __importStar(require("react-native-reanimated"));
|
|
51
|
+
const useBottomSheetGestureHandler_1 = require("../hooks/useBottomSheetGestureHandler");
|
|
52
|
+
const useKeyboard_1 = require("../hooks/useKeyboard");
|
|
53
|
+
const AnimaCore = require("@react-native-ama/core");
|
|
54
|
+
const isIOS = react_native_1.Platform.OS === "ios";
|
|
55
|
+
const SCREEN_HEIGHT = react_native_1.Dimensions.get("screen").height;
|
|
56
|
+
exports.BottomSheet = React.forwardRef(({ children, visible, onClose, handleStyle = {}, bottomSheetStyle = {}, overlayStyle, closeActionAccessibilityLabel, headerComponent, animationDuration = 300, closeDistance = 0.3, handleComponent, scrollViewProps, hasScrollableContent = true, persistent = false, autoCloseDelay, panGestureEnabled = true, testID, overlayOpacity = 1, footerComponent, avoidKeyboard, maxHeight: propMaxHeight, minVelocityToClose = 1000, topInset, onBottomSheetHidden, shouldHandleKeyboardEvents = true, supportedOrientations, isOverlayAccessible = true, }, ref) => {
|
|
57
|
+
var _a;
|
|
58
|
+
// This is used to let Reanimated animate the view out, before removing it from the tree.
|
|
59
|
+
const [shouldRenderContent, setShouldRenderContent] = React.useState(visible);
|
|
60
|
+
const [isModalVisible, setIsModalVisible] = React.useState(false);
|
|
61
|
+
const translateY = (0, react_native_reanimated_1.useSharedValue)(SCREEN_HEIGHT * 1.5);
|
|
62
|
+
const contentHeight = (0, react_native_reanimated_1.useSharedValue)(0);
|
|
63
|
+
const dragOpacity = (0, react_native_reanimated_1.useSharedValue)(0);
|
|
64
|
+
const onTimeout = ((_a = AnimaCore === null || AnimaCore === void 0 ? void 0 : AnimaCore.useTimedAction()) === null || _a === void 0 ? void 0 : _a.onTimeout) || setTimeout;
|
|
65
|
+
const isMounted = React.useRef(false);
|
|
66
|
+
const [headerHeight, setHeaderHeight] = React.useState(-1);
|
|
67
|
+
const [footerHeight, setFooterHeight] = React.useState(-1);
|
|
68
|
+
const [handleHeight, setHandleHeight] = React.useState(-1);
|
|
69
|
+
const promiseResolver = React.useRef(null);
|
|
70
|
+
const [maxScrollViewHeight, setMaxScrollViewHeight] = React.useState(0);
|
|
71
|
+
const { keyboardHeight, keyboardFinalHeight } = (0, useKeyboard_1.useKeyboard)(shouldHandleKeyboardEvents);
|
|
72
|
+
const shouldReduceMotion = (0, react_native_reanimated_1.useReducedMotion)();
|
|
73
|
+
const { height } = (0, react_native_1.useWindowDimensions)();
|
|
74
|
+
const maxHeight = propMaxHeight !== null && propMaxHeight !== void 0 ? propMaxHeight : height * 0.9;
|
|
75
|
+
const duration = shouldReduceMotion ? 0 : animationDuration;
|
|
76
|
+
React.useImperativeHandle(ref, () => ({
|
|
77
|
+
close: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
78
|
+
if (!shouldRenderContent) {
|
|
79
|
+
return Promise.resolve();
|
|
80
|
+
}
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
promiseResolver.current = resolve;
|
|
83
|
+
onClose();
|
|
84
|
+
});
|
|
85
|
+
}),
|
|
86
|
+
isVisible: () => {
|
|
87
|
+
return shouldRenderContent;
|
|
88
|
+
},
|
|
89
|
+
}));
|
|
90
|
+
React.useEffect(() => {
|
|
91
|
+
if (visible) {
|
|
92
|
+
dragOpacity.value = (0, react_native_reanimated_1.withTiming)(overlayOpacity, {
|
|
93
|
+
duration,
|
|
94
|
+
});
|
|
95
|
+
setIsModalVisible(true);
|
|
96
|
+
setShouldRenderContent(true);
|
|
97
|
+
if (autoCloseDelay) {
|
|
98
|
+
onTimeout(() => {
|
|
99
|
+
if (isMounted.current) {
|
|
100
|
+
onClose();
|
|
101
|
+
}
|
|
102
|
+
}, autoCloseDelay);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (isModalVisible) {
|
|
106
|
+
const finished = () => {
|
|
107
|
+
var _a;
|
|
108
|
+
setShouldRenderContent(false);
|
|
109
|
+
setIsModalVisible(false);
|
|
110
|
+
onBottomSheetHidden === null || onBottomSheetHidden === void 0 ? void 0 : onBottomSheetHidden();
|
|
111
|
+
(_a = promiseResolver.current) === null || _a === void 0 ? void 0 : _a.call(promiseResolver);
|
|
112
|
+
};
|
|
113
|
+
dragOpacity.value = (0, react_native_reanimated_1.withTiming)(0, {
|
|
114
|
+
duration,
|
|
115
|
+
});
|
|
116
|
+
translateY.value = (0, react_native_reanimated_1.withTiming)(contentHeight.value, {
|
|
117
|
+
duration,
|
|
118
|
+
}, () => {
|
|
119
|
+
(0, react_native_reanimated_1.runOnJS)(finished)();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}, [
|
|
123
|
+
duration,
|
|
124
|
+
autoCloseDelay,
|
|
125
|
+
isModalVisible,
|
|
126
|
+
onBottomSheetHidden,
|
|
127
|
+
onClose,
|
|
128
|
+
onTimeout,
|
|
129
|
+
translateY,
|
|
130
|
+
visible,
|
|
131
|
+
dragOpacity,
|
|
132
|
+
overlayOpacity,
|
|
133
|
+
contentHeight.value,
|
|
134
|
+
]);
|
|
135
|
+
React.useEffect(() => {
|
|
136
|
+
isMounted.current = true;
|
|
137
|
+
return () => {
|
|
138
|
+
isMounted.current = false;
|
|
139
|
+
};
|
|
140
|
+
}, []);
|
|
141
|
+
const dragStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
|
|
142
|
+
return {
|
|
143
|
+
opacity: dragOpacity.value,
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
const maxHeightValue = (0, react_native_reanimated_1.useDerivedValue)(() => {
|
|
147
|
+
return maxHeight - keyboardHeight.value;
|
|
148
|
+
}, [keyboardHeight, maxHeight]);
|
|
149
|
+
const animatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
|
|
150
|
+
const keyboard = isIOS ? keyboardHeight.value : 0;
|
|
151
|
+
return {
|
|
152
|
+
transform: [{ translateY: translateY.value - keyboard }],
|
|
153
|
+
maxHeight: maxHeightValue.value,
|
|
154
|
+
};
|
|
155
|
+
}, [maxHeightValue, translateY, keyboardHeight]);
|
|
156
|
+
(0, react_native_reanimated_1.useDerivedValue)(() => {
|
|
157
|
+
const maxScrollHeight = Math.ceil(maxHeight -
|
|
158
|
+
keyboardFinalHeight.value -
|
|
159
|
+
footerHeight -
|
|
160
|
+
headerHeight -
|
|
161
|
+
handleHeight -
|
|
162
|
+
topInset);
|
|
163
|
+
if (!Number.isNaN(maxScrollHeight) &&
|
|
164
|
+
maxScrollHeight !== maxScrollViewHeight) {
|
|
165
|
+
(0, react_native_reanimated_1.runOnJS)(setMaxScrollViewHeight)(maxScrollHeight);
|
|
166
|
+
}
|
|
167
|
+
}, [
|
|
168
|
+
footerHeight,
|
|
169
|
+
headerHeight,
|
|
170
|
+
handleHeight,
|
|
171
|
+
keyboardFinalHeight,
|
|
172
|
+
topInset,
|
|
173
|
+
maxHeight,
|
|
174
|
+
]);
|
|
175
|
+
const handleOnLayout = (event) => {
|
|
176
|
+
contentHeight.value = event.nativeEvent.layout.height;
|
|
177
|
+
if (translateY.value === 0) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
translateY.value = event.nativeEvent.layout.height;
|
|
181
|
+
translateY.value = (0, react_native_reanimated_1.withTiming)(0, { duration });
|
|
182
|
+
};
|
|
183
|
+
const maybeCloseBottomSheet = persistent ? undefined : onClose;
|
|
184
|
+
const Wrapper = avoidKeyboard && !shouldHandleKeyboardEvents
|
|
185
|
+
? BottomSheetKeyboardAvoidingView
|
|
186
|
+
: React.Fragment;
|
|
187
|
+
const opacity = [footerHeight, headerHeight, handleHeight].every((h) => h >= 0)
|
|
188
|
+
? 1
|
|
189
|
+
: 0;
|
|
190
|
+
const ContentWrapper = hasScrollableContent
|
|
191
|
+
? ScrollViewWrapper
|
|
192
|
+
: FragmentWrapper;
|
|
193
|
+
return (<react_native_1.Modal animationType="none" transparent={true} visible={isModalVisible} onRequestClose={onClose} ref={ref} testID={testID} supportedOrientations={supportedOrientations} statusBarTranslucent={true}>
|
|
194
|
+
<Wrapper>
|
|
195
|
+
{shouldRenderContent ? (<react_native_gesture_handler_1.GestureHandlerRootView style={{ flex: 1, opacity }}>
|
|
196
|
+
<react_native_reanimated_1.default.View style={[styles.overlay, overlayStyle, dragStyle]} testID={`${testID}-overlay-wrapper`}>
|
|
197
|
+
<react_native_1.Pressable style={styles.closeButton} accessibilityRole="button" onPress={maybeCloseBottomSheet} testID={`${testID}-overlay-button`} accessible={isOverlayAccessible && !persistent} importantForAccessibility={persistent ? "no-hide-descendants" : "yes"} accessibilityElementsHidden={persistent} onAccessibilityTap={maybeCloseBottomSheet} accessibilityLabel={closeActionAccessibilityLabel}/>
|
|
198
|
+
</react_native_reanimated_1.default.View>
|
|
199
|
+
<react_native_reanimated_1.default.View style={[
|
|
200
|
+
styles.contentWrapper,
|
|
201
|
+
styles.content,
|
|
202
|
+
bottomSheetStyle,
|
|
203
|
+
animatedStyle,
|
|
204
|
+
]} testID={`${testID}-wrapper`} onLayout={handleOnLayout}>
|
|
205
|
+
<GestureHandler translateY={translateY} closeDistance={closeDistance} contentHeight={contentHeight} onClose={onClose} testID={`${testID}-gesture-handler`} overlayOpacity={overlayOpacity} dragOpacity={dragOpacity} minVelocityToClose={minVelocityToClose} panGestureEnabled={panGestureEnabled}>
|
|
206
|
+
<react_native_1.View onLayout={(event) => {
|
|
207
|
+
setHandleHeight(event.nativeEvent.layout.height);
|
|
208
|
+
}}>
|
|
209
|
+
{handleComponent === "none"
|
|
210
|
+
? null
|
|
211
|
+
: handleComponent !== null && handleComponent !== void 0 ? handleComponent : (<react_native_1.View style={[styles.line, handleStyle]} testID={`${testID}-line`}/>)}
|
|
212
|
+
</react_native_1.View>
|
|
213
|
+
<react_native_1.View onLayout={(event) => {
|
|
214
|
+
setHeaderHeight(event.nativeEvent.layout.height);
|
|
215
|
+
}}>
|
|
216
|
+
{headerComponent}
|
|
217
|
+
</react_native_1.View>
|
|
218
|
+
<ContentWrapper {...scrollViewProps} testID={testID} maxScrollViewHeight={maxScrollViewHeight}>
|
|
219
|
+
{children}
|
|
220
|
+
</ContentWrapper>
|
|
221
|
+
<react_native_1.View onLayout={(event) => {
|
|
222
|
+
setFooterHeight(event.nativeEvent.layout.height);
|
|
223
|
+
}}>
|
|
224
|
+
{footerComponent}
|
|
225
|
+
</react_native_1.View>
|
|
226
|
+
</GestureHandler>
|
|
227
|
+
</react_native_reanimated_1.default.View>
|
|
228
|
+
</react_native_gesture_handler_1.GestureHandlerRootView>) : null}
|
|
229
|
+
</Wrapper>
|
|
230
|
+
</react_native_1.Modal>);
|
|
231
|
+
});
|
|
232
|
+
const BottomSheetKeyboardAvoidingView = ({ children, }) => {
|
|
233
|
+
return (<react_native_1.KeyboardAvoidingView behavior={react_native_1.Platform.OS === "ios" ? "padding" : undefined} pointerEvents="box-none" style={{ flex: 1 }}>
|
|
234
|
+
{children}
|
|
235
|
+
</react_native_1.KeyboardAvoidingView>);
|
|
236
|
+
};
|
|
237
|
+
const GestureHandler = ({ translateY, closeDistance, contentHeight, children, onClose, panGestureEnabled, testID, dragOpacity, overlayOpacity, minVelocityToClose, }) => {
|
|
238
|
+
const { gestureHandler } = (0, useBottomSheetGestureHandler_1.useBottomSheetGestureHandler)({
|
|
239
|
+
translateY,
|
|
240
|
+
closeDistance,
|
|
241
|
+
contentHeight,
|
|
242
|
+
onClose,
|
|
243
|
+
dragOpacity,
|
|
244
|
+
overlayOpacity,
|
|
245
|
+
minVelocityToClose,
|
|
246
|
+
});
|
|
247
|
+
return panGestureEnabled ? (<react_native_gesture_handler_1.GestureDetector gesture={gestureHandler}>
|
|
248
|
+
<react_native_reanimated_1.default.View testID={testID}>{children}</react_native_reanimated_1.default.View>
|
|
249
|
+
</react_native_gesture_handler_1.GestureDetector>) : (<react_native_reanimated_1.default.View testID={testID}>{children}</react_native_reanimated_1.default.View>);
|
|
250
|
+
};
|
|
251
|
+
const ScrollViewWrapper = (_a) => {
|
|
252
|
+
var { scrollEnabled = true, testID, maxScrollViewHeight, children, style } = _a, rest = __rest(_a, ["scrollEnabled", "testID", "maxScrollViewHeight", "children", "style"]);
|
|
253
|
+
return (<react_native_gesture_handler_1.ScrollView {...rest} style={[{ maxHeight: maxScrollViewHeight }, style]} scrollEnabled={scrollEnabled} testID={`${testID}-scrollview`}>
|
|
254
|
+
{children}
|
|
255
|
+
</react_native_gesture_handler_1.ScrollView>);
|
|
256
|
+
};
|
|
257
|
+
const FragmentWrapper = ({ children, }) => {
|
|
258
|
+
return <>{children}</>;
|
|
259
|
+
};
|
|
260
|
+
const styles = react_native_1.StyleSheet.create({
|
|
261
|
+
overlay: {
|
|
262
|
+
backgroundColor: "rgba(0, 0, 0, 0.5)",
|
|
263
|
+
flex: 1,
|
|
264
|
+
},
|
|
265
|
+
closeButton: {
|
|
266
|
+
flex: 1,
|
|
267
|
+
},
|
|
268
|
+
contentWrapper: {
|
|
269
|
+
flexDirection: "column",
|
|
270
|
+
flex: 1,
|
|
271
|
+
position: "absolute",
|
|
272
|
+
bottom: 0,
|
|
273
|
+
alignSelf: "flex-end",
|
|
274
|
+
width: "100%",
|
|
275
|
+
},
|
|
276
|
+
content: {
|
|
277
|
+
width: "100%",
|
|
278
|
+
backgroundColor: "#fff",
|
|
279
|
+
flex: 1,
|
|
280
|
+
},
|
|
281
|
+
line: {
|
|
282
|
+
width: 48,
|
|
283
|
+
height: 4,
|
|
284
|
+
backgroundColor: "grey",
|
|
285
|
+
alignSelf: "center",
|
|
286
|
+
marginBottom: 24,
|
|
287
|
+
borderRadius: 2,
|
|
288
|
+
marginTop: 12,
|
|
289
|
+
},
|
|
290
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SharedValue } from 'react-native-reanimated';
|
|
2
|
+
type UseBottomSheetGestureHandler = {
|
|
3
|
+
translateY: SharedValue<number>;
|
|
4
|
+
contentHeight: SharedValue<number>;
|
|
5
|
+
dragOpacity: SharedValue<number>;
|
|
6
|
+
closeDistance: number;
|
|
7
|
+
overlayOpacity: number;
|
|
8
|
+
onClose: () => void;
|
|
9
|
+
minVelocityToClose: number;
|
|
10
|
+
};
|
|
11
|
+
export declare const useBottomSheetGestureHandler: ({ translateY, closeDistance, contentHeight, onClose, dragOpacity, overlayOpacity, minVelocityToClose, }: UseBottomSheetGestureHandler) => {
|
|
12
|
+
gestureHandler: import("react-native-gesture-handler/lib/typescript/handlers/gestures/panGesture").PanGesture;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useBottomSheetGestureHandler = void 0;
|
|
4
|
+
const react_native_gesture_handler_1 = require("react-native-gesture-handler");
|
|
5
|
+
const react_native_reanimated_1 = require("react-native-reanimated");
|
|
6
|
+
const useBottomSheetGestureHandler = ({ translateY, closeDistance, contentHeight, onClose, dragOpacity, overlayOpacity, minVelocityToClose, }) => {
|
|
7
|
+
const shouldReduceMotion = (0, react_native_reanimated_1.useReducedMotion)();
|
|
8
|
+
const startY = (0, react_native_reanimated_1.useSharedValue)(0);
|
|
9
|
+
const gestureHandler = react_native_gesture_handler_1.Gesture.Pan()
|
|
10
|
+
.onStart(() => {
|
|
11
|
+
startY.value = translateY.value;
|
|
12
|
+
})
|
|
13
|
+
.onUpdate(event => {
|
|
14
|
+
translateY.value = Math.max(0, startY.value + event.translationY);
|
|
15
|
+
const distance = contentHeight.value - translateY.value;
|
|
16
|
+
const opacity = Math.min(distance / contentHeight.value, overlayOpacity);
|
|
17
|
+
dragOpacity.value = opacity;
|
|
18
|
+
})
|
|
19
|
+
.onEnd(event => {
|
|
20
|
+
const minimumDistanceToClose = contentHeight.value * closeDistance;
|
|
21
|
+
const shouldCloseBottomSheet = translateY.value >= minimumDistanceToClose ||
|
|
22
|
+
event.velocityY >= minVelocityToClose;
|
|
23
|
+
if (shouldCloseBottomSheet) {
|
|
24
|
+
(0, react_native_reanimated_1.runOnJS)(onClose)();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
translateY.value = (0, react_native_reanimated_1.withTiming)(0, {
|
|
28
|
+
duration: shouldReduceMotion ? 0 : 300,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
gestureHandler,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
exports.useBottomSheetGestureHandler = useBottomSheetGestureHandler;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const useKeyboard: (shouldHandleKeyboardEvents?: boolean) => {
|
|
2
|
+
keyboardHeight: import("react-native-reanimated").SharedValue<number>;
|
|
3
|
+
keyboardFinalHeight: import("react-native-reanimated").SharedValue<number>;
|
|
4
|
+
isKeyboardVisible: import("react-native-reanimated").SharedValue<boolean>;
|
|
5
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useKeyboard = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Based on: https://github.com/gorhom/react-native-bottom-sheet
|
|
6
|
+
*/
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
const react_native_1 = require("react-native");
|
|
9
|
+
const react_native_reanimated_1 = require("react-native-reanimated");
|
|
10
|
+
const KEYBOARD_EVENT_SHOW = react_native_1.Platform.select({
|
|
11
|
+
ios: 'keyboardWillShow',
|
|
12
|
+
default: 'keyboardDidShow',
|
|
13
|
+
});
|
|
14
|
+
const KEYBOARD_EVENT_HIDE = react_native_1.Platform.select({
|
|
15
|
+
ios: 'keyboardWillHide',
|
|
16
|
+
default: 'keyboardDidHide',
|
|
17
|
+
});
|
|
18
|
+
const useKeyboard = (shouldHandleKeyboardEvents = true) => {
|
|
19
|
+
const keyboardHeight = (0, react_native_reanimated_1.useSharedValue)(0);
|
|
20
|
+
const keyboardFinalHeight = (0, react_native_reanimated_1.useSharedValue)(0);
|
|
21
|
+
const isKeyboardVisible = (0, react_native_reanimated_1.useSharedValue)(false);
|
|
22
|
+
const handleKeyboardEvent = (0, react_1.useCallback)((isVisible, endCoordinatesHeight, duration, easing) => {
|
|
23
|
+
const finalHeight = isVisible ? endCoordinatesHeight : 0;
|
|
24
|
+
const animationConfig = getKeyboardAnimationConfigs(easing, duration);
|
|
25
|
+
keyboardFinalHeight.value = finalHeight;
|
|
26
|
+
keyboardHeight.value = (0, react_native_reanimated_1.withTiming)(isVisible ? finalHeight : 0, animationConfig);
|
|
27
|
+
isKeyboardVisible.value = isVisible;
|
|
28
|
+
},
|
|
29
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
30
|
+
[]);
|
|
31
|
+
(0, react_1.useEffect)(() => {
|
|
32
|
+
if (!shouldHandleKeyboardEvents) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const handleEvent = (isVisible) => {
|
|
36
|
+
return (event) => {
|
|
37
|
+
return handleKeyboardEvent(isVisible, event.endCoordinates.height, event.duration, event.easing);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const showSubscription = react_native_1.Keyboard.addListener(KEYBOARD_EVENT_SHOW, handleEvent(true));
|
|
41
|
+
const hideSubscription = react_native_1.Keyboard.addListener(KEYBOARD_EVENT_HIDE, handleEvent(false));
|
|
42
|
+
return () => {
|
|
43
|
+
showSubscription.remove();
|
|
44
|
+
hideSubscription.remove();
|
|
45
|
+
};
|
|
46
|
+
}, [handleKeyboardEvent, shouldHandleKeyboardEvents]);
|
|
47
|
+
return {
|
|
48
|
+
keyboardHeight,
|
|
49
|
+
keyboardFinalHeight,
|
|
50
|
+
isKeyboardVisible,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
exports.useKeyboard = useKeyboard;
|
|
54
|
+
const getKeyboardAnimationConfigs = (easing, duration) => {
|
|
55
|
+
switch (easing) {
|
|
56
|
+
case 'easeIn':
|
|
57
|
+
return {
|
|
58
|
+
easing: react_native_reanimated_1.Easing.in(react_native_reanimated_1.Easing.ease),
|
|
59
|
+
duration,
|
|
60
|
+
};
|
|
61
|
+
case 'easeOut':
|
|
62
|
+
return {
|
|
63
|
+
easing: react_native_reanimated_1.Easing.out(react_native_reanimated_1.Easing.ease),
|
|
64
|
+
duration,
|
|
65
|
+
};
|
|
66
|
+
case 'easeInEaseOut':
|
|
67
|
+
return {
|
|
68
|
+
easing: react_native_reanimated_1.Easing.inOut(react_native_reanimated_1.Easing.ease),
|
|
69
|
+
duration,
|
|
70
|
+
};
|
|
71
|
+
case 'linear':
|
|
72
|
+
return {
|
|
73
|
+
easing: react_native_reanimated_1.Easing.linear,
|
|
74
|
+
duration,
|
|
75
|
+
};
|
|
76
|
+
case 'keyboard':
|
|
77
|
+
return {
|
|
78
|
+
damping: 500,
|
|
79
|
+
stiffness: 1000,
|
|
80
|
+
mass: 3,
|
|
81
|
+
overshootClamping: true,
|
|
82
|
+
restDisplacementThreshold: 10,
|
|
83
|
+
restSpeedThreshold: 10,
|
|
84
|
+
duration,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useKeyboard = exports.useBottomSheetGestureHandler = exports.BottomSheet = void 0;
|
|
4
|
+
// Components
|
|
5
|
+
var BottomSheet_1 = require("./components/BottomSheet");
|
|
6
|
+
Object.defineProperty(exports, "BottomSheet", { enumerable: true, get: function () { return BottomSheet_1.BottomSheet; } });
|
|
7
|
+
// Hooks
|
|
8
|
+
var useBottomSheetGestureHandler_1 = require("./hooks/useBottomSheetGestureHandler");
|
|
9
|
+
Object.defineProperty(exports, "useBottomSheetGestureHandler", { enumerable: true, get: function () { return useBottomSheetGestureHandler_1.useBottomSheetGestureHandler; } });
|
|
10
|
+
var useKeyboard_1 = require("./hooks/useKeyboard");
|
|
11
|
+
Object.defineProperty(exports, "useKeyboard", { enumerable: true, get: function () { return useKeyboard_1.useKeyboard; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-native-ama/bottom-sheet",
|
|
3
|
+
"version": "2.0.0-beta.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"react-native": "./src/index.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./BottomSheet": {
|
|
12
|
+
"types": "./dist/components/BottomSheet.d.ts",
|
|
13
|
+
"default": "./dist/components/BottomSheet.js"
|
|
14
|
+
},
|
|
15
|
+
"./useBottomSheetGestureHandler": {
|
|
16
|
+
"types": "./dist/hooks/useBottomSheetGestureHandler.d.ts",
|
|
17
|
+
"default": "./dist/hooks/useBottomSheetGestureHandler.js"
|
|
18
|
+
},
|
|
19
|
+
"./useKeyboard": {
|
|
20
|
+
"types": "./dist/hooks/useKeyboard.d.ts",
|
|
21
|
+
"default": "./dist/hooks/useKeyboard.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "rm -rf dist && tsc -p ./tsconfig.build.json",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"test": "jest"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@react-native-ama/core": "~2.0.0-beta.0",
|
|
35
|
+
"react": "*",
|
|
36
|
+
"react-native": ">=0.62.0",
|
|
37
|
+
"react-native-gesture-handler": ">=2.0.0",
|
|
38
|
+
"react-native-reanimated": ">=2.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"@react-native-ama/core": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"react-native",
|
|
47
|
+
"accessibility"
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "https://github.com/FormidableLabs/react-native-ama",
|
|
52
|
+
"directory": "packages/bottom-sheet"
|
|
53
|
+
},
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/FormidableLabs/react-native-ama/issues"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://github.com/FormidableLabs/react-native-ama#readme",
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"provenance": true
|
|
60
|
+
},
|
|
61
|
+
"author": "",
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"jest": {
|
|
64
|
+
"preset": "react-native",
|
|
65
|
+
"setupFilesAfterEnv": [
|
|
66
|
+
"<rootDir>/jest.setup.js"
|
|
67
|
+
],
|
|
68
|
+
"modulePathIgnorePatterns": [
|
|
69
|
+
"<rootDir>/node_modules",
|
|
70
|
+
"<rootDir>/dist/"
|
|
71
|
+
],
|
|
72
|
+
"collectCoverageFrom": [
|
|
73
|
+
"src/**/*.{ts,tsx}",
|
|
74
|
+
"!src/index.ts"
|
|
75
|
+
],
|
|
76
|
+
"coverageThreshold": {
|
|
77
|
+
"global": {
|
|
78
|
+
"statements": 80,
|
|
79
|
+
"branches": 77,
|
|
80
|
+
"functions": 70,
|
|
81
|
+
"lines": 80
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"transformIgnorePatterns": [
|
|
85
|
+
"node_modules/(?!(react-native-reanimated|react-native-gesture-handler|expo|@expo)/)"
|
|
86
|
+
],
|
|
87
|
+
"transform": {
|
|
88
|
+
"\\.[jt]sx?$": "babel-jest"
|
|
89
|
+
},
|
|
90
|
+
"moduleDirectories": [
|
|
91
|
+
"node_modules",
|
|
92
|
+
"../../playground/node_modules"
|
|
93
|
+
],
|
|
94
|
+
"moduleNameMapper": {
|
|
95
|
+
"@react-native/js-polyfills/error-guard": "<rootDir>/../../jest-mocks/error-guard.js",
|
|
96
|
+
"^(\\.{1,2}/)*ama\\.config\\.json$": "<rootDir>/../../jest-mocks/ama-config.js",
|
|
97
|
+
"^(\\.{1,2}/)*ReactNativeAmaModule$": "<rootDir>/../../jest-mocks/ReactNativeAmaModule.js",
|
|
98
|
+
"^(\\.{1,2}/)*ReactNativeAmaView(\\.web)?$": "<rootDir>/../../jest-mocks/ReactNativeAmaModule.js",
|
|
99
|
+
"^@react-native-ama/core$": "<rootDir>/../../jest-mocks/ama-core.js",
|
|
100
|
+
"^@react-native-ama/core/(.*)$": "<rootDir>/../../packages/core/$1"
|
|
101
|
+
},
|
|
102
|
+
"verbose": true
|
|
103
|
+
}
|
|
104
|
+
}
|