choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
174 lines (145 loc) • 5.27 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getScroll = getScroll;
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _utils = require("./utils");
var _react = _interopRequireDefault(require("react"));
var _classnames2 = _interopRequireDefault(require("classnames"));
var isDev = process.env.NODE_ENV !== 'production';
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 = (0, _utils.isTransformSupported)(inkBarNodeStyle);
if (tabBarPosition === 'top' || tabBarPosition === 'bottom') {
var left = (0, _utils.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) {
(0, _utils.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 = (0, _utils.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) {
(0, _utils.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';
}
var _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 = (0, _classnames2["default"])((_classnames = {}, (0, _defineProperty2["default"])(_classnames, className, true), (0, _defineProperty2["default"])(_classnames, inkBarAnimated ? "".concat(className, "-animated") : "".concat(className, "-no-animated"), true), _classnames));
return _react["default"].createElement("div", {
style: styles.inkBar,
className: classes,
key: "inkBar",
ref: this.saveRef('inkBar')
});
}
};
exports["default"] = _default;
//# sourceMappingURL=InkTabBarMixin.js.map