zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
103 lines (96 loc) • 3.43 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import { useIsomorphicLayoutEffect } from '../shared/use-isomorphic-layout-effect';
import { classNames, getExtraAttrs, getSlots, emit } from '../shared/utils';
import { colorClasses } from '../shared/mixins';
import { useTheme } from '../shared/use-theme';
import { zmpready, zmp } from '../shared/zmp';
import { TabbarContext } from '../shared/tabbar-context';
var Toolbar = /*#__PURE__*/forwardRef(function (props, ref) {
var className = props.className,
id = props.id,
style = props.style,
tabbar = props.tabbar,
labels = props.labels,
scrollable = props.scrollable,
hidden = props.hidden,
noShadow = props.noShadow,
noHairline = props.noHairline,
noBorder = props.noBorder,
position = props.position,
topMd = props.topMd,
topIos = props.topIos,
topAurora = props.topAurora,
top = props.top,
bottomMd = props.bottomMd,
bottomIos = props.bottomIos,
bottomAurora = props.bottomAurora,
bottom = props.bottom,
_props$inner = props.inner,
inner = _props$inner === void 0 ? true : _props$inner;
var extraAttrs = getExtraAttrs(props);
var elRef = useRef(null);
var onHide = function onHide(toolbarEl) {
if (elRef.current !== toolbarEl) return;
emit(props, 'toolbarHide');
};
var onShow = function onShow(toolbarEl) {
if (elRef.current !== toolbarEl) return;
emit(props, 'toolbarShow');
};
var hide = function hide(animate) {
if (!zmp) return;
zmp.toolbar.hide(elRef.current, animate);
};
var show = function show(animate) {
if (!zmp) return;
zmp.toolbar.show(elRef.current, animate);
};
useImperativeHandle(ref, function () {
return {
el: elRef.current,
hide: hide,
show: show
};
});
useIsomorphicLayoutEffect(function () {
zmpready(function () {
if (tabbar && zmp && elRef.current) {
zmp.toolbar.setHighlight(elRef.current);
}
zmp.on('toolbarShow', onShow);
zmp.on('toolbarHide', onHide);
});
return function () {
if (!zmp) return;
zmp.off('toolbarShow', onShow);
zmp.off('toolbarHide', onHide);
};
});
var theme = useTheme();
var classes = classNames(className, 'toolbar', {
tabbar: tabbar,
'toolbar-bottom': theme && theme.md && bottomMd || theme && theme.ios && bottomIos || theme && theme.aurora && bottomAurora || bottom || position === 'bottom',
'toolbar-top': theme && theme.md && topMd || theme && theme.ios && topIos || theme && theme.aurora && topAurora || top || position === 'top',
'tabbar-labels': labels,
'tabbar-scrollable': scrollable,
'toolbar-hidden': hidden,
'no-shadow': noShadow,
'no-hairline': noHairline || noBorder
}, colorClasses(props));
var slots = getSlots(props);
return /*#__PURE__*/React.createElement("div", _extends({
id: id,
style: style,
className: classes,
ref: elRef
}, extraAttrs), /*#__PURE__*/React.createElement(TabbarContext.Provider, {
value: {
tabbarHasLabels: labels
}
}, slots['before-inner'], inner ? /*#__PURE__*/React.createElement("div", {
className: "toolbar-inner"
}, slots.default) : slots.default, slots['after-inner']));
});
Toolbar.displayName = 'zmp-toolbar';
export default Toolbar;