Skip to content

Commit

Permalink
Use fit-textarea to autosize textarea
Browse files Browse the repository at this point in the history
This seems to fix the annoying scrolling behaviour described in lektor#1038.

https://github.com/fregante/fit-textarea
  • Loading branch information
dairiki committed Jul 21, 2022
1 parent 7916596 commit 83f8bbf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
20 changes: 4 additions & 16 deletions frontend/js/widgets/MultiLineTextInputWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fitTextarea from "fit-textarea";
import React, { ChangeEvent, useCallback, useEffect, useRef } from "react";
import { getInputClass, WidgetProps } from "./types";

Expand All @@ -10,11 +11,9 @@ export function MultiLineTextInputWidget({
}: WidgetProps) {
const textarea = useRef<HTMLTextAreaElement | null>(null);

const recalculateSize = useCallback(() => {
const node = textarea.current;
if (node) {
node.style.height = "auto";
node.style.height = node.scrollHeight + "px";
useEffect(() => {
if (textarea?.current) {
fitTextarea.watch(textarea?.current);
}
}, []);

Expand All @@ -25,17 +24,6 @@ export function MultiLineTextInputWidget({
[onChangeProp]
);

useEffect(() => {
recalculateSize();
}, [recalculateSize, value]);

useEffect(() => {
window.addEventListener("resize", recalculateSize);
return () => {
window.removeEventListener("resize", recalculateSize);
};
}, [recalculateSize]);

return (
<div>
<textarea
Expand Down
13 changes: 13 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"eslint": "^8.4.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"fit-textarea": "^3.0.0",
"font-awesome": "^4.3.0",
"jsdom": "^19.0.0",
"mocha": "^9.0.3",
Expand Down

0 comments on commit 83f8bbf

Please sign in to comment.