UNPKG

4.81 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.calculateNodeStyling = calculateNodeStyling;
7exports.default = calculateNodeHeight;
8// Thanks to https://github.com/andreypopp/react-textarea-autosize/
9
10/**
11 * calculateNodeHeight(uiTextNode, useCache = false)
12 */
13var 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";
14var 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'];
15var computedStyleCache = {};
16var hiddenTextarea;
17
18function calculateNodeStyling(node) {
19 var useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
20 var nodeRef = node.getAttribute('id') || node.getAttribute('data-reactid') || node.getAttribute('name');
21
22 if (useCache && computedStyleCache[nodeRef]) {
23 return computedStyleCache[nodeRef];
24 }
25
26 var style = window.getComputedStyle(node);
27 var boxSizing = style.getPropertyValue('box-sizing') || style.getPropertyValue('-moz-box-sizing') || style.getPropertyValue('-webkit-box-sizing');
28 var paddingSize = parseFloat(style.getPropertyValue('padding-bottom')) + parseFloat(style.getPropertyValue('padding-top'));
29 var borderSize = parseFloat(style.getPropertyValue('border-bottom-width')) + parseFloat(style.getPropertyValue('border-top-width'));
30 var sizingStyle = SIZING_STYLE.map(function (name) {
31 return "".concat(name, ":").concat(style.getPropertyValue(name));
32 }).join(';');
33 var nodeInfo = {
34 sizingStyle: sizingStyle,
35 paddingSize: paddingSize,
36 borderSize: borderSize,
37 boxSizing: boxSizing
38 };
39
40 if (useCache && nodeRef) {
41 computedStyleCache[nodeRef] = nodeInfo;
42 }
43
44 return nodeInfo;
45}
46
47function calculateNodeHeight(uiTextNode) {
48 var useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
49 var minRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
50 var maxRows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
51
52 if (!hiddenTextarea) {
53 hiddenTextarea = document.createElement('textarea');
54 document.body.appendChild(hiddenTextarea);
55 } // Fix wrap="off" issue
56 // https://github.com/ant-design/ant-design/issues/6577
57
58
59 if (uiTextNode.getAttribute('wrap')) {
60 hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap'));
61 } else {
62 hiddenTextarea.removeAttribute('wrap');
63 } // Copy all CSS properties that have an impact on the height of the content in
64 // the textbox
65
66
67 var _calculateNodeStyling = calculateNodeStyling(uiTextNode, useCache),
68 paddingSize = _calculateNodeStyling.paddingSize,
69 borderSize = _calculateNodeStyling.borderSize,
70 boxSizing = _calculateNodeStyling.boxSizing,
71 sizingStyle = _calculateNodeStyling.sizingStyle; // Need to have the overflow attribute to hide the scrollbar otherwise
72 // text-lines will not calculated properly as the shadow will technically be
73 // narrower for content
74
75
76 hiddenTextarea.setAttribute('style', "".concat(sizingStyle, ";").concat(HIDDEN_TEXTAREA_STYLE));
77 hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || '';
78 var minHeight = Number.MIN_SAFE_INTEGER;
79 var maxHeight = Number.MAX_SAFE_INTEGER;
80 var height = hiddenTextarea.scrollHeight;
81 var overflowY;
82
83 if (boxSizing === 'border-box') {
84 // border-box: add border, since height = content + padding + border
85 height += borderSize;
86 } else if (boxSizing === 'content-box') {
87 // remove padding, since height = content
88 height -= paddingSize;
89 }
90
91 if (minRows !== null || maxRows !== null) {
92 // measure height of a textarea with a single row
93 hiddenTextarea.value = ' ';
94 var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
95
96 if (minRows !== null) {
97 minHeight = singleRowHeight * minRows;
98
99 if (boxSizing === 'border-box') {
100 minHeight = minHeight + paddingSize + borderSize;
101 }
102
103 height = Math.max(minHeight, height);
104 }
105
106 if (maxRows !== null) {
107 maxHeight = singleRowHeight * maxRows;
108
109 if (boxSizing === 'border-box') {
110 maxHeight = maxHeight + paddingSize + borderSize;
111 }
112
113 overflowY = height > maxHeight ? '' : 'hidden';
114 height = Math.min(maxHeight, height);
115 }
116 }
117
118 return {
119 height: "".concat(height, "px"),
120 minHeight: "".concat(minHeight, "px"),
121 maxHeight: "".concat(maxHeight, "px"),
122 overflowY: overflowY
123 };
124}
\No newline at end of file