UNPKG

tdesign-vue

Version:
71 lines (68 loc) 3.28 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ var hiddenTextarea; var HIDDEN_TEXTAREA_STYLE = "\n min-height:0 !important;\n max-height:none !important;\n height:0 !important;\n visibility:hidden !important;\n overflow-y:hidden !important;\n position:absolute !important;\n z-index:-1000 !important;\n top:0 !important;\n right:0 !important\n"; var SIZING_PROPS = ["letter-spacing", "line-height", "padding-top", "padding-bottom", "font-family", "font-weight", "font-size", "font-variant", "text-rendering", "text-transform", "width", "text-indent", "padding-left", "padding-right", "border-width", "box-sizing"]; function calculateNodeStyling(targetElement) { var style = window.getComputedStyle(targetElement); var boxSizing = style.getPropertyValue("box-sizing") || style.getPropertyValue("-moz-box-sizing") || style.getPropertyValue("-webkit-box-sizing"); var paddingSize = parseFloat(style.getPropertyValue("padding-bottom")) + parseFloat(style.getPropertyValue("padding-top")); var borderSize = parseFloat(style.getPropertyValue("border-bottom-width")) + parseFloat(style.getPropertyValue("border-top-width")); var sizingStyle = SIZING_PROPS.map(function (name) { return "".concat(name, ":").concat(style.getPropertyValue(name)); }).join(";"); return { sizingStyle: sizingStyle, paddingSize: paddingSize, borderSize: borderSize, boxSizing: boxSizing }; } function calcTextareaHeight(targetElement) { var minRows = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; var maxRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; if (!hiddenTextarea) { hiddenTextarea = document.createElement("textarea"); document.body.appendChild(hiddenTextarea); } var _calculateNodeStyling = calculateNodeStyling(targetElement), paddingSize = _calculateNodeStyling.paddingSize, borderSize = _calculateNodeStyling.borderSize, boxSizing = _calculateNodeStyling.boxSizing, sizingStyle = _calculateNodeStyling.sizingStyle; hiddenTextarea.setAttribute("style", "".concat(sizingStyle, ";").concat(HIDDEN_TEXTAREA_STYLE)); hiddenTextarea.value = targetElement.value || targetElement.placeholder || ""; var height = hiddenTextarea.scrollHeight; var result = {}; if (boxSizing === "border-box") { height += borderSize; } else if (boxSizing === "content-box") { height -= paddingSize; } hiddenTextarea.value = ""; var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize; if (minRows !== null) { var minHeight = singleRowHeight * minRows; if (boxSizing === "border-box") { minHeight = minHeight + paddingSize + borderSize; } height = Math.max(minHeight, height); result.minHeight = "".concat(minHeight, "px"); } if (maxRows !== null) { var maxHeight = singleRowHeight * maxRows; if (boxSizing === "border-box") { maxHeight = maxHeight + paddingSize + borderSize; } height = Math.min(maxHeight, height); } result.height = "".concat(height, "px"); hiddenTextarea.parentNode && hiddenTextarea.parentNode.removeChild(hiddenTextarea); hiddenTextarea = null; return result; } export { calcTextareaHeight as default }; //# sourceMappingURL=calcTextareaHeight.js.map