206 lines
7.8 KiB
JavaScript
206 lines
7.8 KiB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
import { getHeaderTitle, Header, SafeAreaProviderCompat, Screen } from '@react-navigation/elements';
|
|
import * as React from 'react';
|
|
import { Animated, Platform, StyleSheet } from 'react-native';
|
|
import { SafeAreaInsetsContext } from 'react-native-safe-area-context';
|
|
import { BottomTabBarHeightCallbackContext } from '../utils/BottomTabBarHeightCallbackContext';
|
|
import { BottomTabBarHeightContext } from '../utils/BottomTabBarHeightContext';
|
|
import { useAnimatedHashMap } from '../utils/useAnimatedHashMap';
|
|
import { BottomTabBar, getTabBarHeight } from './BottomTabBar';
|
|
import { MaybeScreen, MaybeScreenContainer } from './ScreenFallback';
|
|
var CompositeAnimation = Animated.CompositeAnimation;
|
|
const EPSILON = 1e-5;
|
|
const STATE_INACTIVE = 0;
|
|
const STATE_TRANSITIONING_OR_BELOW_TOP = 1;
|
|
const STATE_ON_TOP = 2;
|
|
const hasAnimation = options => {
|
|
const {
|
|
animationEnabled,
|
|
transitionSpec
|
|
} = options;
|
|
if (animationEnabled === false || !transitionSpec) {
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
export function BottomTabView(props) {
|
|
const {
|
|
tabBar = props => /*#__PURE__*/React.createElement(BottomTabBar, props),
|
|
state,
|
|
navigation,
|
|
descriptors,
|
|
safeAreaInsets,
|
|
detachInactiveScreens = Platform.OS === 'web' || Platform.OS === 'android' || Platform.OS === 'ios',
|
|
sceneContainerStyle
|
|
} = props;
|
|
const focusedRouteKey = state.routes[state.index].key;
|
|
|
|
/**
|
|
* List of loaded tabs, tabs will be loaded when navigated to.
|
|
*/
|
|
const [loaded, setLoaded] = React.useState([focusedRouteKey]);
|
|
if (!loaded.includes(focusedRouteKey)) {
|
|
// Set the current tab to be loaded if it was not loaded before
|
|
setLoaded([...loaded, focusedRouteKey]);
|
|
}
|
|
const tabAnims = useAnimatedHashMap(state);
|
|
React.useEffect(() => {
|
|
const animateToIndex = () => {
|
|
Animated.parallel(state.routes.map((route, index) => {
|
|
const {
|
|
options
|
|
} = descriptors[route.key];
|
|
const {
|
|
transitionSpec
|
|
} = options;
|
|
const animationEnabled = hasAnimation(options);
|
|
const toValue = index === state.index ? 0 : index >= state.index ? 1 : -1;
|
|
if (!animationEnabled || !transitionSpec) {
|
|
return Animated.timing(tabAnims[route.key], {
|
|
toValue,
|
|
duration: 0,
|
|
useNativeDriver: true
|
|
});
|
|
}
|
|
return Animated[transitionSpec.animation](tabAnims[route.key], {
|
|
...transitionSpec.config,
|
|
toValue,
|
|
useNativeDriver: true
|
|
});
|
|
}).filter(Boolean)).start();
|
|
};
|
|
animateToIndex();
|
|
}, [descriptors, state.index, state.routes, tabAnims]);
|
|
const dimensions = SafeAreaProviderCompat.initialMetrics.frame;
|
|
const [tabBarHeight, setTabBarHeight] = React.useState(() => getTabBarHeight({
|
|
state,
|
|
descriptors,
|
|
dimensions,
|
|
layout: {
|
|
width: dimensions.width,
|
|
height: 0
|
|
},
|
|
insets: {
|
|
...SafeAreaProviderCompat.initialMetrics.insets,
|
|
...props.safeAreaInsets
|
|
},
|
|
style: descriptors[state.routes[state.index].key].options.tabBarStyle
|
|
}));
|
|
const renderTabBar = () => {
|
|
return /*#__PURE__*/React.createElement(SafeAreaInsetsContext.Consumer, null, insets => tabBar({
|
|
state: state,
|
|
descriptors: descriptors,
|
|
navigation: navigation,
|
|
insets: {
|
|
top: (safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.top) ?? (insets === null || insets === void 0 ? void 0 : insets.top) ?? 0,
|
|
right: (safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.right) ?? (insets === null || insets === void 0 ? void 0 : insets.right) ?? 0,
|
|
bottom: (safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.bottom) ?? (insets === null || insets === void 0 ? void 0 : insets.bottom) ?? 0,
|
|
left: (safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.left) ?? (insets === null || insets === void 0 ? void 0 : insets.left) ?? 0
|
|
}
|
|
}));
|
|
};
|
|
const {
|
|
routes
|
|
} = state;
|
|
|
|
// If there is no animation, we only have 2 states: visible and invisible
|
|
const hasTwoStates = !routes.some(route => hasAnimation(descriptors[route.key].options));
|
|
const {
|
|
tabBarPosition = 'bottom'
|
|
} = descriptors[focusedRouteKey].options;
|
|
return /*#__PURE__*/React.createElement(SafeAreaProviderCompat, {
|
|
style: tabBarPosition === 'left' ? styles.left : tabBarPosition === 'right' ? styles.right : null
|
|
}, /*#__PURE__*/React.createElement(MaybeScreenContainer, {
|
|
enabled: detachInactiveScreens,
|
|
hasTwoStates: hasTwoStates,
|
|
style: styles.screens
|
|
}, routes.map((route, index) => {
|
|
const descriptor = descriptors[route.key];
|
|
const {
|
|
lazy = true,
|
|
unmountOnBlur,
|
|
sceneStyleInterpolator
|
|
} = descriptor.options;
|
|
const isFocused = state.index === index;
|
|
if (unmountOnBlur && !isFocused) {
|
|
return null;
|
|
}
|
|
if (lazy && !loaded.includes(route.key) && !isFocused) {
|
|
// Don't render a lazy screen if we've never navigated to it
|
|
return null;
|
|
}
|
|
const {
|
|
freezeOnBlur,
|
|
header = _ref => {
|
|
let {
|
|
layout,
|
|
options
|
|
} = _ref;
|
|
return /*#__PURE__*/React.createElement(Header, _extends({}, options, {
|
|
layout: layout,
|
|
title: getHeaderTitle(options, route.name)
|
|
}));
|
|
},
|
|
headerShown,
|
|
headerStatusBarHeight,
|
|
headerTransparent
|
|
} = descriptor.options;
|
|
const {
|
|
sceneStyle
|
|
} = (sceneStyleInterpolator === null || sceneStyleInterpolator === void 0 ? void 0 : sceneStyleInterpolator({
|
|
current: tabAnims[route.key]
|
|
})) ?? {};
|
|
const animationEnabled = hasAnimation(descriptor.options);
|
|
const activityState = isFocused ? STATE_ON_TOP // the screen is on top after the transition
|
|
: animationEnabled // is animation is not enabled, immediately move to inactive state
|
|
? tabAnims[route.key].interpolate({
|
|
inputRange: [0, 1 - EPSILON, 1],
|
|
outputRange: [STATE_TRANSITIONING_OR_BELOW_TOP,
|
|
// screen visible during transition
|
|
STATE_TRANSITIONING_OR_BELOW_TOP, STATE_INACTIVE // the screen is detached after transition
|
|
],
|
|
|
|
extrapolate: 'extend'
|
|
}) : STATE_INACTIVE;
|
|
return /*#__PURE__*/React.createElement(MaybeScreen, {
|
|
key: route.key,
|
|
style: [StyleSheet.absoluteFill, {
|
|
zIndex: isFocused ? 0 : -1
|
|
}],
|
|
active: activityState,
|
|
enabled: detachInactiveScreens,
|
|
freezeOnBlur: freezeOnBlur
|
|
}, /*#__PURE__*/React.createElement(BottomTabBarHeightContext.Provider, {
|
|
value: tabBarPosition === 'bottom' ? tabBarHeight : 0
|
|
}, /*#__PURE__*/React.createElement(Screen, {
|
|
focused: isFocused,
|
|
route: descriptor.route,
|
|
navigation: descriptor.navigation,
|
|
headerShown: headerShown,
|
|
headerStatusBarHeight: headerStatusBarHeight,
|
|
headerTransparent: headerTransparent,
|
|
header: header({
|
|
layout: dimensions,
|
|
route: descriptor.route,
|
|
navigation: descriptor.navigation,
|
|
options: descriptor.options
|
|
}),
|
|
style: [sceneContainerStyle, animationEnabled && sceneStyle]
|
|
}, descriptor.render())));
|
|
})), /*#__PURE__*/React.createElement(BottomTabBarHeightCallbackContext.Provider, {
|
|
value: setTabBarHeight
|
|
}, renderTabBar()));
|
|
}
|
|
const styles = StyleSheet.create({
|
|
left: {
|
|
flexDirection: 'row-reverse'
|
|
},
|
|
right: {
|
|
flexDirection: 'row'
|
|
},
|
|
screens: {
|
|
flex: 1,
|
|
overflow: 'hidden'
|
|
}
|
|
});
|
|
//# sourceMappingURL=BottomTabView.js.map
|