@gorhom/bottom-sheet
Version:
A performant interactive bottom sheet with fully configurable options 🚀
108 lines (88 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));
var _reactNativeGestureHandler = require("react-native-gesture-handler");
var _hooks = require("../../hooks");
var _constants = require("./constants");
var _styles = require("./styles");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const BottomSheetBackdropComponent = ({
animatedIndex,
opacity = _constants.DEFAULT_OPACITY,
appearsOnIndex = _constants.DEFAULT_APPEARS_ON_INDEX,
disappearsOnIndex = _constants.DEFAULT_DISAPPEARS_ON_INDEX,
enableTouchThrough = _constants.DEFAULT_ENABLE_TOUCH_THROUGH,
pressBehavior = _constants.DEFAULT_PRESS_BEHAVIOR,
style,
children
}) => {
//#region hooks
const {
snapToIndex,
close
} = (0, _hooks.useBottomSheet)(); //#endregion
//#region variables
const containerRef = (0, _react.useRef)(null);
const pointerEvents = enableTouchThrough ? 'none' : 'auto'; //#endregion
//#region callbacks
const handleOnPress = (0, _react.useCallback)(() => {
if (pressBehavior === 'close') {
close();
} else if (pressBehavior === 'collapse') {
snapToIndex(disappearsOnIndex);
} else if (typeof pressBehavior === 'number') {
snapToIndex(pressBehavior);
}
}, [snapToIndex, close, disappearsOnIndex, pressBehavior]);
const handleContainerTouchability = (0, _react.useCallback)(shouldDisableTouchability => {
if (!containerRef.current) {
return;
} // @ts-ignore
containerRef.current.setNativeProps({
pointerEvents: shouldDisableTouchability ? 'none' : 'auto'
});
}, []); //#endregion
//#region tap gesture
const gestureHandler = (0, _reactNativeReanimated.useAnimatedGestureHandler)({
onFinish: () => {
(0, _reactNativeReanimated.runOnJS)(handleOnPress)();
}
}, [handleOnPress]); //#endregion
//#region styles
const containerAnimatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({
opacity: (0, _reactNativeReanimated.interpolate)(animatedIndex.value, [-1, disappearsOnIndex, appearsOnIndex], [0, 0, opacity], _reactNativeReanimated.Extrapolate.CLAMP),
flex: 1
}));
const containerStyle = (0, _react.useMemo)(() => [_styles.styles.container, style, containerAnimatedStyle], [style, containerAnimatedStyle]); //#endregion
//#region effects
(0, _reactNativeReanimated.useAnimatedReaction)(() => animatedIndex.value <= disappearsOnIndex, (shouldDisableTouchability, previous) => {
if (shouldDisableTouchability === previous) {
return;
}
(0, _reactNativeReanimated.runOnJS)(handleContainerTouchability)(shouldDisableTouchability);
}, [disappearsOnIndex]); //#endregion
return pressBehavior !== 'none' ? /*#__PURE__*/_react.default.createElement(_reactNativeGestureHandler.TapGestureHandler, {
onGestureEvent: gestureHandler
}, /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, {
ref: containerRef,
style: containerStyle,
accessible: true,
accessibilityRole: "button",
accessibilityLabel: "Bottom Sheet backdrop",
accessibilityHint: `Tap to ${typeof pressBehavior === 'string' ? pressBehavior : 'move'} the Bottom Sheet`
}, children)) : /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, {
ref: containerRef,
pointerEvents: pointerEvents,
style: containerStyle
}, children);
};
const BottomSheetBackdrop = /*#__PURE__*/(0, _react.memo)(BottomSheetBackdropComponent);
BottomSheetBackdrop.displayName = 'BottomSheetBackdrop';
var _default = BottomSheetBackdrop;
exports.default = _default;
//# sourceMappingURL=BottomSheetBackdrop.js.map