-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
TextareaAutosize.tsx
215 lines (188 loc) · 5.79 KB
/
TextareaAutosize.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { TextareaAutosizeTypeMap } from ".";
import createComponentFactory from "./../createComponentFactory";
import createEffectWithCleaning from "@suid/system/createEffectWithCleaning";
import createRef from "@suid/system/createRef";
import { debounce, unstable_ownerWindow as ownerWindow } from "@suid/utils";
import {
createSignal,
createEffect,
on,
splitProps,
mergeProps,
JSX,
} from "solid-js";
const $ = createComponentFactory<TextareaAutosizeTypeMap>()({
name: "MuiTextareaAutosize",
selfPropNames: ["ref", "maxRows", "minRows"],
});
function getStyleValue(
computedStyle: CSSStyleDeclaration,
property: keyof CSSStyleDeclaration
) {
return parseInt(computedStyle[property] as string, 10) || 0;
}
const styles = {
shadow: {
// Visibility needed to hide the extra text area on iPads
visibility: "hidden",
// Remove from the content flow
position: "absolute",
// Ignore the scrollbar width
overflow: "hidden",
height: 0,
top: 0,
left: 0,
// Create a new layer, increase the isolation of the computed values
transform: "translateZ(0)",
} as JSX.CSSProperties,
};
const TextareaAutosize = $.defineComponent(function (props) {
const ref = createRef<HTMLTextAreaElement>(props);
const [, other] = splitProps(props, ["maxRows", "minRows", "style", "value"]);
const baseProps = mergeProps({ minRows: 1 }, props);
let shadowRef!: HTMLTextAreaElement;
const renders = { current: 0 };
const [state, setState] = createSignal<{
overflow?: boolean;
outerHeightStyle?: number;
}>({});
const syncHeight = () => {
const input = ref.current;
const containerWindow = ownerWindow(input);
const computedStyle = containerWindow.getComputedStyle(input);
// If input's width is shrunk and it's not visible, don't sync height.
if (computedStyle.width === "0px") {
return;
}
shadowRef.style.width = computedStyle.width;
shadowRef.value = input.value || props.placeholder || "x";
if (shadowRef.value.slice(-1) === "\n") {
// Certain fonts which overflow the line height will cause the textarea
// to report a different scrollHeight depending on whether the last line
// is empty. Make it non-empty to avoid this issue.
shadowRef.value += " ";
}
const boxSizing = computedStyle["boxSizing"];
const padding =
getStyleValue(computedStyle, "paddingBottom") +
getStyleValue(computedStyle, "paddingTop");
const border =
getStyleValue(computedStyle, "borderBottomWidth") +
getStyleValue(computedStyle, "borderTopWidth");
// The height of the inner content
const innerHeight = shadowRef.scrollHeight;
// Measure height of a textarea with a single row
shadowRef.value = "x";
const singleRowHeight = shadowRef.scrollHeight;
// The height of the outer content
let outerHeight = innerHeight;
if (baseProps.minRows) {
outerHeight = Math.max(
Number(baseProps.minRows) * singleRowHeight,
outerHeight
);
}
if (props.maxRows) {
outerHeight = Math.min(
Number(props.maxRows) * singleRowHeight,
outerHeight
);
}
outerHeight = Math.max(outerHeight, singleRowHeight);
// Take the box sizing into account for applying this value as a style.
const outerHeightStyle =
outerHeight + (boxSizing === "border-box" ? padding + border : 0);
const overflow = Math.abs(outerHeight - innerHeight) <= 1;
setState((prevState) => {
// Need a large enough difference to update the height.
// This prevents infinite rendering loop.
if (
renders.current < 20 &&
((outerHeightStyle > 0 &&
Math.abs((prevState.outerHeightStyle || 0) - outerHeightStyle) > 1) ||
prevState.overflow !== overflow)
) {
renders.current += 1;
return {
overflow,
outerHeightStyle,
};
}
if (process.env.NODE_ENV !== "production") {
if (renders.current === 20) {
console.error(
[
"MUI: Too many re-renders. The layout is unstable.",
"TextareaAutosize limits the number of renders to prevent an infinite loop.",
].join("\n")
);
}
}
return prevState;
});
};
createEffectWithCleaning(() => {
syncHeight();
const handleResize = debounce(() => {
renders.current = 0;
syncHeight();
});
const containerWindow = ownerWindow(ref.current);
containerWindow.addEventListener("resize", handleResize);
let resizeObserver: ResizeObserver | undefined;
if (typeof ResizeObserver !== "undefined") {
resizeObserver = new ResizeObserver(handleResize);
resizeObserver.observe(ref.current);
}
return () => {
handleResize.clear();
containerWindow.removeEventListener("resize", handleResize);
if (resizeObserver) {
resizeObserver.disconnect();
}
};
});
createEffect(
on(
() => [props.value],
() => {
renders.current = 0;
syncHeight();
}
)
);
const style1 = mergeProps(
{
get height() {
return `${state().outerHeightStyle}px`;
},
get overflow() {
return state().overflow ? "hidden" : null;
},
},
() => props.style
) as JSX.CSSProperties;
const style2 = mergeProps(styles.shadow, () => props.style, {
padding: 0,
}) as JSX.CSSProperties;
return (
<>
<textarea
ref={ref}
// Apply the rows prop to get a "correct" first SSR paint
rows={baseProps.minRows}
style={style1}
{...other}
/>
<textarea
aria-hidden
class={props.class}
readOnly
ref={shadowRef}
tabIndex={-1}
style={style2}
/>
</>
);
}, false);
export default TextareaAutosize;