rn-bottom-menu
Version:
React Native custom bottom menu
185 lines (183 loc) • 9.08 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BottomTabBarButton = exports.BottomBarButton = exports.defaultSpringConfig = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var react_native_reanimated_1 = __importStar(require("react-native-reanimated"));
var BottomMenu_styles_1 = require("./BottomMenu.styles");
exports.defaultSpringConfig = {
damping: 30,
mass: 0.7,
stiffness: 250,
};
exports.BottomBarButton = (0, react_1.memo)(function (_a) {
var isFocused = _a.isFocused, options = _a.options, onPress = _a.onPress, onLongPress = _a.onLongPress, inactiveTintColor = _a.inactiveTintColor, focusedButtonStyle = _a.focusedButtonStyle, springConfig = _a.springConfig;
var animationValueThreshold = (0, react_native_reanimated_1.useSharedValue)(0);
(0, react_1.useEffect)(function () {
if (isFocused) {
animationValueThreshold.value = (0, react_native_reanimated_1.withSpring)(0, springConfig || exports.defaultSpringConfig);
}
else {
animationValueThreshold.value = (0, react_native_reanimated_1.withSpring)(1, springConfig || exports.defaultSpringConfig);
}
}, [isFocused, animationValueThreshold, springConfig]);
var animatedStyles = (0, react_native_reanimated_1.useAnimatedStyle)(function () {
return {
opacity: animationValueThreshold.value,
transform: [
{
scale: animationValueThreshold.value,
},
],
};
});
var textAnimatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(function () {
return {
opacity: (0, react_native_reanimated_1.interpolate)(animationValueThreshold.value, [0.5, 1], [0, 1]),
};
});
var textAnimatedStyleNew = (0, react_native_reanimated_1.useAnimatedStyle)(function () {
return {
opacity: (0, react_native_reanimated_1.interpolate)(animationValueThreshold.value, [1, 1], [1, 0]),
};
});
return (<react_native_1.View style={BottomMenu_styles_1.style.wrapper}>
<react_native_reanimated_1.default.View style={animatedStyles}>
<react_native_1.TouchableOpacity accessibilityRole="button" accessibilityLabel={options.tabBarAccessibilityLabel}
// @ts-ignore
testID={options.tabBarTestID} onPress={onPress} style={[BottomMenu_styles_1.style.unfocusedButton, isFocused ? focusedButtonStyle : {}]} onLongPress={onLongPress}>
<react_native_1.View style={BottomMenu_styles_1.style.tabBarLabelWrapper}>
{options.tabBarIcon && !isFocused ? (options.tabBarIcon({
focused: isFocused,
color: inactiveTintColor || 'white',
size: 28,
})) : (<react_native_1.View />)}
{options.tabBarLabel && (<react_native_reanimated_1.default.Text style={[
{
marginTop: 2,
color: inactiveTintColor,
},
isFocused ? textAnimatedStyleNew : textAnimatedStyle,
options.tabBarLabelStyle,
]}>
{typeof options.tabBarLabel === 'function'
? options.tabBarLabel({
color: options.tabBarActiveTintColor || '', // pass the required properties
children: options.tabBarLabel ? options.tabBarLabel.toString() : '',
focused: false,
position: 'beside-icon'
})
: options.tabBarLabel}
</react_native_reanimated_1.default.Text>)}
</react_native_1.View>
</react_native_1.TouchableOpacity>
</react_native_reanimated_1.default.View>
</react_native_1.View>);
});
exports.BottomTabBarButton = (0, react_1.memo)(function (_a) {
var isFocused = _a.isFocused, options = _a.options, onPress = _a.onPress, onLongPress = _a.onLongPress, activeTintColor = _a.activeTintColor, springConfig = _a.springConfig, focusedButtonStyle = _a.focusedButtonStyle;
var animationValueThreshold = (0, react_native_reanimated_1.useSharedValue)(0);
(0, react_1.useEffect)(function () {
if (isFocused) {
animationValueThreshold.value = (0, react_native_reanimated_1.withSpring)(0, springConfig || exports.defaultSpringConfig);
}
else {
animationValueThreshold.value = (0, react_native_reanimated_1.withSpring)(1, springConfig || exports.defaultSpringConfig);
}
}, [isFocused, animationValueThreshold, springConfig]);
// [-18, 100]
var animatedStyles = (0, react_native_reanimated_1.useAnimatedStyle)(function () {
return {
transform: [
{
translateY: (0, react_native_reanimated_1.interpolate)(animationValueThreshold.value, [0, 1], [-35, 100]),
},
],
};
});
var animatedTextStyles = (0, react_native_reanimated_1.useAnimatedStyle)(function () {
return {
transform: [
{
translateY: (0, react_native_reanimated_1.interpolate)(animationValueThreshold.value, [0, 1], [55, 0]),
},
],
opacity: (0, react_native_reanimated_1.interpolate)(animationValueThreshold.value, [0, 1], [1, 0]),
color: isFocused ? '#F78E11' : 'white',
zIndex: 10,
};
});
return (<react_native_1.View style={BottomMenu_styles_1.style.wrapper}>
<react_native_reanimated_1.default.View style={[animatedStyles, BottomMenu_styles_1.style.focusedButton]}>
<react_native_1.TouchableOpacity accessibilityRole="button" accessibilityLabel={options.tabBarAccessibilityLabel}
// @ts-ignore
testID={options === null || options === void 0 ? void 0 : options.tabBarTestID} onPress={onPress} style={[
__assign(__assign({}, BottomMenu_styles_1.style.focusedButton), { backgroundColor: activeTintColor || 'white' }),
isFocused ? focusedButtonStyle : {},
]} onLongPress={onLongPress}>
{options.tabBarIcon
? options.tabBarIcon({
focused: isFocused,
color: 'white',
size: 28,
})
: null}
</react_native_1.TouchableOpacity>
{options.tabBarLabel && (<react_native_reanimated_1.default.Text style={[animatedTextStyles, options.tabBarLabelStyle]}>
{typeof options.tabBarLabel === 'function'
? options.tabBarLabel({
color: options.tabBarActiveTintColor || '', // pass the required properties
children: options.tabBarLabel ? options.tabBarLabel.toString() : '',
focused: false,
position: 'beside-icon'
})
: options.tabBarLabel}
</react_native_reanimated_1.default.Text>)}
</react_native_reanimated_1.default.View>
</react_native_1.View>);
});
exports.default = exports.BottomTabBarButton;
;