calcTextareaHeight.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * fork from element-ui
  3. */
  4. let hiddenTextarea;
  5. const HIDDEN_STYLE = `
  6. height:0 !important;
  7. visibility:hidden !important;
  8. overflow:hidden !important;
  9. position:absolute !important;
  10. z-index:-1000 !important;
  11. top:0 !important;
  12. right:0 !important
  13. `;
  14. const CONTEXT_STYLE = [
  15. "letter-spacing",
  16. "line-height",
  17. "padding-top",
  18. "padding-bottom",
  19. "font-family",
  20. "font-weight",
  21. "font-size",
  22. "text-rendering",
  23. "text-transform",
  24. "width",
  25. "text-indent",
  26. "padding-left",
  27. "padding-right",
  28. "border-width",
  29. "box-sizing",
  30. ];
  31. function calculateNodeStyling(targetElement) {
  32. const style = window.getComputedStyle(targetElement);
  33. const boxSizing = style.getPropertyValue("box-sizing");
  34. const paddingSize =
  35. parseFloat(style.getPropertyValue("padding-bottom")) +
  36. parseFloat(style.getPropertyValue("padding-top"));
  37. const borderSize =
  38. parseFloat(style.getPropertyValue("border-bottom-width")) +
  39. parseFloat(style.getPropertyValue("border-top-width"));
  40. const contextStyle = CONTEXT_STYLE.map(
  41. (name) => `${name}:${style.getPropertyValue(name)}`,
  42. ).join(";");
  43. return { contextStyle, paddingSize, borderSize, boxSizing };
  44. }
  45. export default function calcTextareaHeight(
  46. targetElement,
  47. minRows = 2,
  48. maxRows = 4,
  49. ) {
  50. if (!hiddenTextarea) {
  51. hiddenTextarea = document.createElement("textarea");
  52. document.body.appendChild(hiddenTextarea);
  53. }
  54. let { paddingSize, borderSize, boxSizing, contextStyle } =
  55. calculateNodeStyling(targetElement);
  56. hiddenTextarea.setAttribute("style", `${contextStyle};${HIDDEN_STYLE}`);
  57. hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
  58. let height = hiddenTextarea.scrollHeight;
  59. const result = {};
  60. if (boxSizing === "border-box") {
  61. height = height + borderSize;
  62. } else if (boxSizing === "content-box") {
  63. height = height - paddingSize;
  64. }
  65. hiddenTextarea.value = "";
  66. let singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  67. if (minRows !== null) {
  68. let minHeight = singleRowHeight * minRows;
  69. if (boxSizing === "border-box") {
  70. minHeight = minHeight + paddingSize + borderSize;
  71. }
  72. height = Math.max(minHeight, height);
  73. result.minHeight = `${minHeight}px`;
  74. }
  75. if (maxRows !== null) {
  76. let maxHeight = singleRowHeight * maxRows;
  77. if (boxSizing === "border-box") {
  78. maxHeight = maxHeight + paddingSize + borderSize;
  79. }
  80. height = Math.min(maxHeight, height);
  81. }
  82. result.height = `${height}px`;
  83. hiddenTextarea.parentNode &&
  84. hiddenTextarea.parentNode.removeChild(hiddenTextarea);
  85. hiddenTextarea = null;
  86. return result;
  87. }