[WEB-1072] fix: pages UI improvements (#4294)
* fix: outline alignment * fix: textarea auto-resize logic
This commit is contained in:
parent
f87bb95236
commit
ad27184a91
5 changed files with 25 additions and 26 deletions
|
|
@ -15,7 +15,7 @@ const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>((props, re
|
|||
// refs
|
||||
const textAreaRef = useRef<any>(ref);
|
||||
// auto re-size
|
||||
useAutoResizeTextArea(textAreaRef);
|
||||
useAutoResizeTextArea(textAreaRef, value);
|
||||
|
||||
return (
|
||||
<textarea
|
||||
|
|
|
|||
|
|
@ -1,24 +1,16 @@
|
|||
import { useEffect } from "react";
|
||||
import { useLayoutEffect } from "react";
|
||||
|
||||
export const useAutoResizeTextArea = (textAreaRef: React.RefObject<HTMLTextAreaElement>) => {
|
||||
useEffect(() => {
|
||||
export const useAutoResizeTextArea = (
|
||||
textAreaRef: React.RefObject<HTMLTextAreaElement>,
|
||||
value: string | number | readonly string[]
|
||||
) => {
|
||||
useLayoutEffect(() => {
|
||||
const textArea = textAreaRef.current;
|
||||
if (!textArea) return;
|
||||
|
||||
const resizeTextArea = () => {
|
||||
textArea.style.height = "auto";
|
||||
const computedHeight = textArea.scrollHeight + "px";
|
||||
textArea.style.height = computedHeight;
|
||||
};
|
||||
|
||||
const handleInput = () => resizeTextArea();
|
||||
|
||||
// resize on mount
|
||||
resizeTextArea();
|
||||
|
||||
textArea.addEventListener("input", handleInput);
|
||||
return () => {
|
||||
textArea.removeEventListener("input", handleInput);
|
||||
};
|
||||
}, [textAreaRef]);
|
||||
// We need to reset the height momentarily to get the correct scrollHeight for the textarea
|
||||
textArea.style.height = "0px";
|
||||
const scrollHeight = textArea.scrollHeight;
|
||||
textArea.style.height = scrollHeight + "px";
|
||||
}, [textAreaRef, value]);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue