shineout
Version:
Shein 前端组件库
103 lines (88 loc) • 2.46 kB
JavaScript
import classnames from 'classnames';
import ReactDOM from 'react-dom';
import { tooltipClass } from './styles';
import getCommonContainer from '../utils/dom/popContainer';
var div = null;
var timer;
var arrow = null;
var inner = null;
var currentId;
var transStyle = function transStyle(value) {
if (value === void 0) {
value = '';
}
return typeof value === 'number' ? value + "px" : value;
};
export function hide() {
if (timer) clearTimeout(timer);
if (div) {
div.style.display = 'none';
div.className = '';
}
currentId = undefined;
}
function clickaway() {
hide();
document.removeEventListener('click', clickaway);
}
export var show = function show(props, id, innerStyle) {
var position = props.position,
_props$style = props.style,
style = _props$style === void 0 ? {} : _props$style,
tip = props.tip,
trigger = props.trigger,
animation = props.animation,
cn = props.className;
var container = getCommonContainer(); // create
if (!container.contains(div)) {
div = null;
arrow = null;
inner = null;
}
if (!div) {
div = document.createElement('div');
div.style.display = 'none';
container.appendChild(div);
}
if (!arrow) {
arrow = document.createElement('div');
arrow.className = tooltipClass('arrow');
div.appendChild(arrow);
}
if (!inner) {
inner = document.createElement('div');
inner.className = tooltipClass('inner');
div.appendChild(inner);
}
currentId = id;
div.style.cssText = 'display: none';
Object.keys(style).forEach(function (k) {
div.style[k] = transStyle(style[k]);
});
var className = tooltipClass('_', 'in', position, animation && 'animation'); // fix safari
timer = setTimeout(function () {
div.style.display = 'block';
div.className = classnames(className, cn);
}, 0);
ReactDOM.render(tip, inner);
inner.setAttribute('style', '');
if (innerStyle) {
Object.keys(innerStyle).forEach(function (k) {
inner.style[k] = transStyle(innerStyle[k]);
});
}
if (trigger === 'click') {
document.addEventListener('click', clickaway);
}
};
export var move = function move(id, pos) {
if (id === currentId && div) {
// eslint-disable-next-line no-return-assign
Object.keys(pos).map(function (key) {
return div.style[key] = transStyle(pos[key]);
});
}
};
export var isCurrent = function isCurrent(id) {
return id === currentId;
};