react-native-collapsible-tab-view
Version:
Collapsible tab view component for React Native
98 lines (95 loc) • 3.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ScrollView = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactNativeReanimated = _interopRequireDefault(require("react-native-reanimated"));
var _hooks = require("./hooks");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* Used as a memo to prevent rerendering too often when the context changes.
* See: https://github.com/facebook/react/issues/15156#issuecomment-474590693
*/const ScrollViewMemo = /*#__PURE__*/_react.default.memo(/*#__PURE__*/_react.default.forwardRef((props, passRef) => {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.ScrollView, {
// @ts-expect-error reanimated types are broken on ref
ref: passRef,
...props
});
}));
/**
* Use like a regular ScrollView.
*/
const ScrollView = exports.ScrollView = /*#__PURE__*/_react.default.forwardRef(({
contentContainerStyle,
style,
onContentSizeChange,
children,
refreshControl,
...rest
}, passRef) => {
const name = (0, _hooks.useTabNameContext)();
const ref = (0, _hooks.useSharedAnimatedRef)(passRef);
const {
setRef,
contentInset
} = (0, _hooks.useTabsContext)();
const {
style: _style,
contentContainerStyle: _contentContainerStyle,
progressViewOffset
} = (0, _hooks.useCollapsibleStyle)();
const {
scrollHandler,
enable
} = (0, _hooks.useScrollHandlerY)(name);
const onLayout = (0, _hooks.useAfterMountEffect)(rest.onLayout, () => {
'worklet';
// we enable the scroll event after mounting
// otherwise we get an `onScroll` call with the initial scroll position which can break things
enable(true);
});
_react.default.useEffect(() => {
setRef(name, ref);
}, [name, ref, setRef]);
const scrollContentSizeChange = (0, _hooks.useUpdateScrollViewContentSize)({
name
});
const scrollContentSizeChangeHandlers = (0, _hooks.useChainCallback)(_react.default.useMemo(() => [scrollContentSizeChange, onContentSizeChange], [onContentSizeChange, scrollContentSizeChange]));
const memoRefreshControl = _react.default.useMemo(() => refreshControl && /*#__PURE__*/_react.default.cloneElement(refreshControl, {
progressViewOffset,
...refreshControl.props
}), [progressViewOffset, refreshControl]);
const memoContentInset = _react.default.useMemo(() => ({
top: contentInset
}), [contentInset]);
const memoContentOffset = _react.default.useMemo(() => ({
x: 0,
y: -contentInset
}), [contentInset]);
const memoContentContainerStyle = _react.default.useMemo(() => [_contentContainerStyle,
// TODO: investigate types
contentContainerStyle], [_contentContainerStyle, contentContainerStyle]);
const memoStyle = _react.default.useMemo(() => [_style, style], [_style, style]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(ScrollViewMemo, {
...rest,
onLayout: onLayout,
ref: ref,
bouncesZoom: false,
style: memoStyle,
contentContainerStyle: memoContentContainerStyle,
onScroll: scrollHandler,
onContentSizeChange: scrollContentSizeChangeHandlers,
scrollEventThrottle: 16,
contentInset: memoContentInset,
contentOffset: memoContentOffset,
automaticallyAdjustContentInsets: false,
refreshControl: memoRefreshControl
// workaround for: https://github.com/software-mansion/react-native-reanimated/issues/2735
,
onMomentumScrollEnd: () => {},
children: children
});
});
//# sourceMappingURL=ScrollView.js.map