calculateNodeHeight.js 5.1 KB

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