UNPKG

choerodon-ui

Version:

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

28 lines (22 loc) 828 B
export default function measureTextWidth(text, style) { if (typeof window !== 'undefined') { var span = document.createElement('span'); span.style.cssText = 'position: absolute;top: -9999px;display: inline-block'; span.innerHTML = text.replace(/\s/g, ' '); if (style) { ['font', 'letterSpacing', 'wordSpacing'].forEach(function (property) { if (property in style) { span.style[property] = style[property]; } }); } document.body.appendChild(span); var _getComputedStyle = getComputedStyle(span), width = _getComputedStyle.width; var contentWidth = width && width !== 'auto' ? parseFloat(width) : span.offsetWidth; document.body.removeChild(span); return contentWidth; } return 0; } //# sourceMappingURL=measureTextWidth.js.map