stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
155 lines • 7.54 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AttachmentPicker = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeReanimated = require("react-native-reanimated");
var _bottomSheet = require("@gorhom/bottom-sheet");
var _dayjs = _interopRequireDefault(require("dayjs"));
var _duration = _interopRequireDefault(require("dayjs/plugin/duration"));
var _AttachmentPickerContext = require("../../contexts/attachmentPickerContext/AttachmentPickerContext");
var _ComponentsContext = require("../../contexts/componentsContext/ComponentsContext");
var _ThemeContext = require("../../contexts/themeContext/ThemeContext");
var _hooks = require("../../hooks");
var _BottomSheet = require("../BottomSheetCompatibility/BottomSheet");
var _jsxRuntime = require("react/jsx-runtime");
var _jsxFileName = "/home/runner/work/stream-chat-react-native/stream-chat-react-native/package/src/components/AttachmentPicker/AttachmentPicker.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
_dayjs.default.extend(_duration.default);
var SPRING_CONFIG = {
damping: 80,
overshootClamping: true,
restDisplacementThreshold: 0.1,
restSpeedThreshold: 0.1,
stiffness: 500,
duration: 200
};
var AttachmentPicker = () => {
var _useAttachmentPickerC = (0, _AttachmentPickerContext.useAttachmentPickerContext)(),
closePicker = _useAttachmentPickerC.closePicker,
attachmentPickerStore = _useAttachmentPickerC.attachmentPickerStore,
attachmentPickerBottomSheetHeight = _useAttachmentPickerC.attachmentPickerBottomSheetHeight,
ref = _useAttachmentPickerC.bottomSheetRef,
bottomInset = _useAttachmentPickerC.bottomInset,
topInset = _useAttachmentPickerC.topInset,
disableAttachmentPicker = _useAttachmentPickerC.disableAttachmentPicker;
var _useComponentsContext = (0, _ComponentsContext.useComponentsContext)(),
AttachmentPickerContent = _useComponentsContext.AttachmentPickerContent,
AttachmentPickerSelectionBar = _useComponentsContext.AttachmentPickerSelectionBar;
var _useTheme = (0, _ThemeContext.useTheme)(),
semantics = _useTheme.theme.semantics;
var _useWindowDimensions = (0, _reactNative.useWindowDimensions)(),
windowHeight = _useWindowDimensions.height;
var _useState = (0, _react.useState)(-1),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
currentIndex = _useState2[0],
setCurrentIndexInternal = _useState2[1];
var currentIndexRef = (0, _react.useRef)(currentIndex);
var setCurrentIndex = (0, _hooks.useStableCallback)((_, toIndex) => {
setCurrentIndexInternal(toIndex);
currentIndexRef.current = toIndex;
});
(0, _react.useEffect)(() => {
var backAction = () => {
if (currentIndexRef.current === -1) {
return false;
}
if (attachmentPickerStore.state.getLatestValue().selectedPicker) {
attachmentPickerStore.setSelectedPicker(undefined);
closePicker();
return true;
}
return false;
};
var backHandler = _reactNative.BackHandler.addEventListener('hardwareBackPress', backAction);
return () => backHandler.remove();
}, [attachmentPickerStore, closePicker]);
(0, _react.useEffect)(() => {
var onKeyboardOpenHandler = () => {
if (attachmentPickerStore.state.getLatestValue().selectedPicker) {
attachmentPickerStore.setSelectedPicker(undefined);
}
closePicker();
};
var keyboardShowEvent = _reactNative.Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow';
var keyboardSubscription = _reactNative.Keyboard.addListener(keyboardShowEvent, onKeyboardOpenHandler);
return () => {
keyboardSubscription.remove();
};
}, [attachmentPickerStore, closePicker]);
(0, _react.useEffect)(() => {
if (currentIndex < 0) {
attachmentPickerStore.setSelectedPicker(undefined);
}
}, [currentIndex, attachmentPickerStore]);
var selectionBarRef = (0, _react.useRef)(null);
var initialSnapPoint = attachmentPickerBottomSheetHeight;
var pickerTopInset = Math.max(0, windowHeight - topInset - attachmentPickerBottomSheetHeight - bottomInset);
var snapPoints = (0, _react.useMemo)(() => [initialSnapPoint], [initialSnapPoint]);
var onAttachmentPickerSelectionBarLayout = (0, _hooks.useStableCallback)(e => {
selectionBarRef.current = e.nativeEvent.layout.height;
});
var animationConfigs = (0, _bottomSheet.useBottomSheetSpringConfigs)(SPRING_CONFIG);
var backgroundStyle = (0, _react.useMemo)(() => ({
backgroundColor: semantics.backgroundCoreElevation1,
borderTopWidth: 0,
elevation: _reactNative.Platform.OS === 'android' ? 0 : undefined,
shadowOpacity: _reactNative.Platform.OS === 'android' ? 0 : undefined
}), [semantics.backgroundCoreElevation1]);
var animatedIndex = (0, _reactNativeReanimated.useSharedValue)(currentIndex);
var reactToIndex = (0, _hooks.useStableCallback)(index => {
if (index === -1) {
attachmentPickerStore.setSelectedPicker(undefined);
}
if (index === 0) {
attachmentPickerStore.setSelectedPicker('images');
}
});
(0, _reactNativeReanimated.useAnimatedReaction)(() => animatedIndex.value, (index, previousIndex) => {
if ((index === 0 || index === -1) && index !== previousIndex) {
(0, _reactNativeReanimated.runOnJS)(reactToIndex)(index);
}
});
return (0, _jsxRuntime.jsxs)(_BottomSheet.BottomSheet, {
accessible: false,
accessibilityLabel: null,
accessibilityRole: null,
android_keyboardInputMode: "adjustResize",
backgroundComponent: AttachmentPickerBackground,
backgroundStyle: backgroundStyle,
bottomInset: bottomInset,
enablePanDownToClose: false,
enableContentPanningGesture: false,
enableDynamicSizing: false,
handleComponent: RenderNull,
index: currentIndex,
onAnimate: setCurrentIndex,
animatedIndex: animatedIndex,
ref: ref,
snapPoints: snapPoints,
topInset: pickerTopInset,
animationConfigs: animationConfigs,
children: [(0, _jsxRuntime.jsx)(_reactNative.View, {
onLayout: onAttachmentPickerSelectionBarLayout,
children: (0, _jsxRuntime.jsx)(AttachmentPickerSelectionBar, {})
}), !disableAttachmentPicker ? (0, _jsxRuntime.jsx)(AttachmentPickerContent, {
height: attachmentPickerBottomSheetHeight - (selectionBarRef?.current ?? 0)
}) : null]
});
};
exports.AttachmentPicker = AttachmentPicker;
var RenderNull = () => null;
var AttachmentPickerBackground = ({
pointerEvents,
style
}) => (0, _jsxRuntime.jsx)(_reactNative.View, {
accessible: false,
importantForAccessibility: "no",
pointerEvents: pointerEvents,
style: style
});
AttachmentPicker.displayName = 'AttachmentPicker';
//# sourceMappingURL=AttachmentPicker.js.map