@antmjs/vantui
Version:
一套适用于Taro3及React的vantui组件库
157 lines • 6.57 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var _excluded = ["fixed", "placeholder", "border", "zIndex", "renderTitle", "homeUrl", "buttonColor", "title", "style", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { navigateBack, reLaunch, getCurrentPages } from '@tarojs/taro';
import { useState, useEffect, useCallback, useMemo } from 'react';
import { View } from '@tarojs/components';
import * as utils from '../wxs/utils';
import { Icon } from '../icon';
import { getWindowInfo, getMenuButtonBoundingClientRect } from '../common/utils';
import * as computed from './wxs';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
export function MiniNavBar(props) {
var _useState = useState({
height: 40,
fromTop: 44,
fromLeft: 7,
menuHeight: 32,
menuWidth: 87,
screenWidth: 375
}),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
var _props$fixed = props.fixed,
fixed = _props$fixed === void 0 ? true : _props$fixed,
_props$placeholder = props.placeholder,
placeholder = _props$placeholder === void 0 ? true : _props$placeholder,
_props$border = props.border,
border = _props$border === void 0 ? true : _props$border,
zIndex = props.zIndex,
renderTitle = props.renderTitle,
homeUrl = props.homeUrl,
_props$buttonColor = props.buttonColor,
buttonColor = _props$buttonColor === void 0 ? 'white' : _props$buttonColor,
title = props.title,
style = props.style,
className = props.className,
others = _objectWithoutProperties(props, _excluded);
var height = state.height,
fromTop = state.fromTop,
fromLeft = state.fromLeft,
menuHeight = state.menuHeight,
menuWidth = state.menuWidth,
screenWidth = state.screenWidth;
var handleGoBack = useCallback(function () {
navigateBack({
delta: 1
});
}, []);
var handleGoHome = useCallback(function () {
reLaunch({
url: homeUrl
});
}, [homeUrl]);
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
backButton = _useState4[0],
setBackButton = _useState4[1];
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
homeButton = _useState6[0],
setHomeButton = _useState6[1];
useEffect(function () {
var pages = getCurrentPages();
if (pages.length >= 1) {
var ins = pages[pages.length - 1];
var url = ins.route || ins.__route__ || ins['$taroPath'];
if (url !== homeUrl) {
setHomeButton(true);
}
if (pages.length > 1) {
setBackButton(true);
}
}
}, [homeUrl]);
useEffect(function () {
var sysInfo = getWindowInfo();
var menuInfo = getMenuButtonBoundingClientRect();
if (sysInfo && menuInfo) {
setState({
height: (menuInfo.top - sysInfo.statusBarHeight) * 2 + menuInfo.height,
fromTop: sysInfo.statusBarHeight,
fromLeft: sysInfo.screenWidth - menuInfo.right,
menuHeight: menuInfo.height,
menuWidth: menuInfo.width,
screenWidth: sysInfo.screenWidth
});
}
}, []);
var getMiniNavbarHeight = useMemo(function () {
return utils.style([computed.barStyle({
zIndex: zIndex,
fromTop: fromTop,
height: height,
fromLeft: fromLeft
}) + '; ' + style]);
}, [zIndex, fromTop, height, fromLeft, style]);
return /*#__PURE__*/_jsxs(_Fragment, {
children: [fixed && placeholder && /*#__PURE__*/_jsx(View, {
style: getMiniNavbarHeight
}), /*#__PURE__*/_jsx(View, _objectSpread(_objectSpread({
className: utils.bem('mini-nav-bar', {
fixed: fixed
}) + ' ' + (border ? 'van-hairline--bottom' : '') + " ".concat(className || ''),
style: getMiniNavbarHeight
}, others), {}, {
children: /*#__PURE__*/_jsxs(View, {
className: "van-mini-nav-bar__content",
children: [/*#__PURE__*/_jsxs(View, {
className: "van-mini-nav-bar__left",
style: {
left: "".concat(fromLeft, "px")
},
children: [backButton && /*#__PURE__*/_jsx(View, {
className: "van-mini-nav-bar__left-menu van-mini-nav-bar__left-menu-".concat(buttonColor),
onClick: handleGoBack,
style: {
width: "".concat(menuHeight, "px"),
height: "".concat(menuHeight, "px")
},
children: /*#__PURE__*/_jsx(Icon, {
name: "arrow-left",
size: 40
})
}), homeButton && /*#__PURE__*/_jsx(View, {
className: "van-mini-nav-bar__left-menu van-mini-nav-bar__left-menu-".concat(buttonColor),
onClick: handleGoHome,
style: {
width: "".concat(menuHeight, "px"),
height: "".concat(menuHeight, "px")
},
children: /*#__PURE__*/_jsx(Icon, {
name: "wap-home",
size: 40
})
})]
}), /*#__PURE__*/_jsx(View, {
className: "van-mini-nav-bar__title title-class van-ellipsis",
style: {
width: "".concat(screenWidth - menuWidth * 2 - fromLeft * 4, "px")
},
children: title ? /*#__PURE__*/_jsx(_Fragment, {
children: title
}) : renderTitle
}), /*#__PURE__*/_jsx(View, {
className: "van-mini-nav-bar__right"
})]
})
}))]
});
}
export default MiniNavBar;