zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
84 lines (73 loc) • 2.56 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import { classNames, getExtraAttrs, getSlots, emit } from '../shared/utils';
import { colorClasses } from '../shared/mixins';
import { useTooltip } from '../shared/use-tooltip';
var Fab = /*#__PURE__*/forwardRef(function (props, ref) {
var className = props.className,
id = props.id,
style = props.style,
morphTo = props.morphTo,
href = props.href,
target = props.target,
text = props.text,
_props$position = props.position,
position = _props$position === void 0 ? 'right-bottom' : _props$position;
var extraAttrs = getExtraAttrs(props);
var elRef = useRef(null);
var onClick = function onClick(e) {
emit(props, 'click', e);
};
useImperativeHandle(ref, function () {
return {
el: elRef.current
};
});
useTooltip(elRef, props);
var hrefComputed = href;
if (hrefComputed === true) hrefComputed = '#';
if (hrefComputed === false) hrefComputed = undefined; // no href attribute
var linkChildren = [];
var rootChildren = [];
var _getSlots = getSlots(props),
linkSlots = _getSlots.link,
defaultSlots = _getSlots.default,
rootSlots = _getSlots.root,
textSlots = _getSlots.text;
if (defaultSlots) {
for (var i = 0; i < defaultSlots.length; i += 1) {
var child = defaultSlots[i];
var isRoot = void 0;
var tag = child.type && (child.type.displayName || child.type.name);
if (tag === 'FabButtons' || tag === 'zmp-fab-buttons') isRoot = true;
if (isRoot) rootChildren.push(child);else linkChildren.push(child);
}
}
var textEl;
if (text || textSlots && textSlots.length) {
textEl = /*#__PURE__*/React.createElement("div", {
className: "fab-text"
}, text, textSlots);
}
var linkEl;
if (linkChildren.length || linkSlots && linkSlots.length || textEl) {
linkEl = /*#__PURE__*/React.createElement("a", {
target: target,
href: hrefComputed,
onClick: onClick
}, linkChildren, textEl, linkSlots);
}
var classes = classNames(className, 'fab', "fab-" + position, {
'fab-morph': morphTo,
'fab-extended': typeof textEl !== 'undefined'
}, colorClasses(props));
return /*#__PURE__*/React.createElement("div", _extends({
id: id,
style: style,
className: classes,
"data-morph-to": morphTo,
ref: elRef
}, extraAttrs), linkEl, rootChildren, rootSlots);
});
Fab.displayName = 'zmp-fab';
export default Fab;