UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

158 lines (137 loc) 4.85 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { isTransformSupported, setTransform, getLeft, getTop } from './utils'; import React from 'react'; import classnames from 'classnames'; var isDev = process.env.NODE_ENV !== 'production'; export function getScroll(w, top) { var ret = w["page".concat(top ? 'Y' : 'X', "Offset")]; var method = "scroll".concat(top ? 'Top' : 'Left'); if (typeof ret !== 'number') { var d = w.document; // ie6,7,8 standard mode ret = d.documentElement[method]; if (typeof ret !== 'number') { // quirks mode ret = d.body[method]; } } return ret; } function offset(elem) { var box; var x; var y; var doc = elem.ownerDocument; var body = doc.body; var docElem = doc && doc.documentElement; box = elem.getBoundingClientRect(); x = box.left; y = box.top; x -= docElem.clientLeft || body.clientLeft || 0; y -= docElem.clientTop || body.clientTop || 0; var w = doc.defaultView || doc.parentWindow; x += getScroll(w); y += getScroll(w, true); return { left: x, top: y }; } function _componentDidUpdate(component, init) { var styles = component.props.styles; var rootNode = component.root; var wrapNode = component.nav || rootNode; var inkBarNode = component.inkBar; var activeTab = component.activeTab; var inkBarNodeStyle = inkBarNode.style; var tabBarPosition = component.props.tabBarPosition; if (init) { // prevent mount animation inkBarNodeStyle.display = 'none'; } if (activeTab) { var tabNode = activeTab; var transformSupported = isTransformSupported(inkBarNodeStyle); if (tabBarPosition === 'top' || tabBarPosition === 'bottom') { var left = getLeft(tabNode, wrapNode); var width = tabNode.offsetWidth; // If tabNode'width width equal to wrapNode'width when tabBarPosition is top or bottom // It means no css working, then ink bar should not have width until css is loaded if (width === rootNode.offsetWidth) { width = 0; } else if (styles.inkBar && styles.inkBar.width !== undefined) { width = parseFloat(styles.inkBar.width, 10); if (width) { left = left + (tabNode.offsetWidth - width) / 2; } } // use 3d gpu to optimize render if (transformSupported) { setTransform(inkBarNodeStyle, "translate3d(".concat(left, "px,0,0)")); inkBarNodeStyle.width = "".concat(width, "px"); inkBarNodeStyle.height = ''; } else { inkBarNodeStyle.left = "".concat(left, "px"); inkBarNodeStyle.top = ''; inkBarNodeStyle.bottom = ''; inkBarNodeStyle.right = "".concat(wrapNode.offsetWidth - left - width, "px"); } } else { var top = getTop(tabNode, wrapNode); var height = tabNode.offsetHeight; if (styles.inkBar && styles.inkBar.height !== undefined) { height = parseFloat(styles.inkBar.height, 10); if (height) { top = top + (tabNode.offsetHeight - height) / 2; } } if (transformSupported) { setTransform(inkBarNodeStyle, "translate3d(0,".concat(top, "px,0)")); inkBarNodeStyle.height = "".concat(height, "px"); inkBarNodeStyle.width = ''; } else { inkBarNodeStyle.left = ''; inkBarNodeStyle.right = ''; inkBarNodeStyle.top = "".concat(top, "px"); inkBarNodeStyle.bottom = "".concat(wrapNode.offsetHeight - top - height, "px"); } } } inkBarNodeStyle.display = activeTab ? 'block' : 'none'; } export default { getDefaultProps: function getDefaultProps() { return { inkBarAnimated: true }; }, componentDidUpdate: function componentDidUpdate() { _componentDidUpdate(this); }, componentDidMount: function componentDidMount() { var _this = this; if (isDev) { this.timeout = setTimeout(function () { _componentDidUpdate(_this, true); }, 0); } else { _componentDidUpdate(this, true); } }, componentWillUnmount: function componentWillUnmount() { clearTimeout(this.timeout); }, getInkBarNode: function getInkBarNode() { var _classnames; var _this$props = this.props, prefixCls = _this$props.prefixCls, styles = _this$props.styles, inkBarAnimated = _this$props.inkBarAnimated; var className = "".concat(prefixCls, "-ink-bar"); var classes = classnames((_classnames = {}, _defineProperty(_classnames, className, true), _defineProperty(_classnames, inkBarAnimated ? "".concat(className, "-animated") : "".concat(className, "-no-animated"), true), _classnames)); return React.createElement("div", { style: styles.inkBar, className: classes, key: "inkBar", ref: this.saveRef('inkBar') }); } }; //# sourceMappingURL=InkTabBarMixin.js.map