calculateNodeHeight.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.calculateNodeStyling = calculateNodeStyling;
  6. exports.default = calculateAutoSizeStyle;
  7. // Thanks to https://github.com/andreypopp/react-textarea-autosize/
  8. /**
  9. * calculateNodeHeight(uiTextNode, useCache = false)
  10. */
  11. var HIDDEN_TEXTAREA_STYLE = "\n min-height:0 !important;\n max-height:none !important;\n height:0 !important;\n visibility:hidden !important;\n overflow:hidden !important;\n position:absolute !important;\n z-index:-1000 !important;\n top:0 !important;\n right:0 !important;\n pointer-events: none !important;\n";
  12. var SIZING_STYLE = ['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', 'word-break', 'white-space'];
  13. var computedStyleCache = {};
  14. var hiddenTextarea;
  15. function calculateNodeStyling(node) {
  16. var useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  17. var nodeRef = node.getAttribute('id') || node.getAttribute('data-reactid') || node.getAttribute('name');
  18. if (useCache && computedStyleCache[nodeRef]) {
  19. return computedStyleCache[nodeRef];
  20. }
  21. var style = window.getComputedStyle(node);
  22. var boxSizing = style.getPropertyValue('box-sizing') || style.getPropertyValue('-moz-box-sizing') || style.getPropertyValue('-webkit-box-sizing');
  23. var paddingSize = parseFloat(style.getPropertyValue('padding-bottom')) + parseFloat(style.getPropertyValue('padding-top'));
  24. var borderSize = parseFloat(style.getPropertyValue('border-bottom-width')) + parseFloat(style.getPropertyValue('border-top-width'));
  25. var sizingStyle = SIZING_STYLE.map(function (name) {
  26. return "".concat(name, ":").concat(style.getPropertyValue(name));
  27. }).join(';');
  28. var nodeInfo = {
  29. sizingStyle: sizingStyle,
  30. paddingSize: paddingSize,
  31. borderSize: borderSize,
  32. boxSizing: boxSizing
  33. };
  34. if (useCache && nodeRef) {
  35. computedStyleCache[nodeRef] = nodeInfo;
  36. }
  37. return nodeInfo;
  38. }
  39. function calculateAutoSizeStyle(uiTextNode) {
  40. var useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  41. var minRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  42. var maxRows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  43. if (!hiddenTextarea) {
  44. hiddenTextarea = document.createElement('textarea');
  45. hiddenTextarea.setAttribute('tab-index', '-1');
  46. hiddenTextarea.setAttribute('aria-hidden', 'true');
  47. // fix: A form field element should have an id or name attribute
  48. // A form field element has neither an id nor a name attribute. This might prevent the browser from correctly autofilling the form.
  49. // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea
  50. hiddenTextarea.setAttribute('name', 'hiddenTextarea');
  51. document.body.appendChild(hiddenTextarea);
  52. }
  53. // Fix wrap="off" issue
  54. // https://github.com/ant-design/ant-design/issues/6577
  55. if (uiTextNode.getAttribute('wrap')) {
  56. hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap'));
  57. } else {
  58. hiddenTextarea.removeAttribute('wrap');
  59. }
  60. // Copy all CSS properties that have an impact on the height of the content in
  61. // the textbox
  62. var _calculateNodeStyling = calculateNodeStyling(uiTextNode, useCache),
  63. paddingSize = _calculateNodeStyling.paddingSize,
  64. borderSize = _calculateNodeStyling.borderSize,
  65. boxSizing = _calculateNodeStyling.boxSizing,
  66. sizingStyle = _calculateNodeStyling.sizingStyle;
  67. // Need to have the overflow attribute to hide the scrollbar otherwise
  68. // text-lines will not calculated properly as the shadow will technically be
  69. // narrower for content
  70. hiddenTextarea.setAttribute('style', "".concat(sizingStyle, ";").concat(HIDDEN_TEXTAREA_STYLE));
  71. hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || '';
  72. var minHeight = undefined;
  73. var maxHeight = undefined;
  74. var overflowY;
  75. var height = hiddenTextarea.scrollHeight;
  76. if (boxSizing === 'border-box') {
  77. // border-box: add border, since height = content + padding + border
  78. height += borderSize;
  79. } else if (boxSizing === 'content-box') {
  80. // remove padding, since height = content
  81. height -= paddingSize;
  82. }
  83. if (minRows !== null || maxRows !== null) {
  84. // measure height of a textarea with a single row
  85. hiddenTextarea.value = ' ';
  86. var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  87. if (minRows !== null) {
  88. minHeight = singleRowHeight * minRows;
  89. if (boxSizing === 'border-box') {
  90. minHeight = minHeight + paddingSize + borderSize;
  91. }
  92. height = Math.max(minHeight, height);
  93. }
  94. if (maxRows !== null) {
  95. maxHeight = singleRowHeight * maxRows;
  96. if (boxSizing === 'border-box') {
  97. maxHeight = maxHeight + paddingSize + borderSize;
  98. }
  99. overflowY = height > maxHeight ? '' : 'hidden';
  100. height = Math.min(maxHeight, height);
  101. }
  102. }
  103. var style = {
  104. height: height,
  105. overflowY: overflowY,
  106. resize: 'none'
  107. };
  108. if (minHeight) {
  109. style.minHeight = minHeight;
  110. }
  111. if (maxHeight) {
  112. style.maxHeight = maxHeight;
  113. }
  114. return style;
  115. }