Skip to content

Commit

Permalink
do not need a resize observer in debouncedtextarea
Browse files Browse the repository at this point in the history
  • Loading branch information
srietkerk committed Mar 19, 2024
1 parent 0f07886 commit 6e6fe22
Showing 1 changed file with 1 addition and 23 deletions.
24 changes: 1 addition & 23 deletions teachertool/src/components/DebouncedTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const DebouncedTextarea: React.FC<DebouncedTextareaProps> = ({ intervalMs
const timerId = useRef<NodeJS.Timeout | undefined>(undefined);
const latestValue = useRef<string>("");
const textareaRef = useRef<HTMLTextAreaElement>(null);
const containerRef = useRef<HTMLDivElement>(null);

const sendChange = () => {
if (props.onChange) {
Expand All @@ -23,28 +22,11 @@ export const DebouncedTextarea: React.FC<DebouncedTextareaProps> = ({ intervalMs
// If the timer is pending and the component unmounts,
// clear the timer and fire the onChange event immediately.
useEffect(() => {
const resizeObserver = new ResizeObserver(() => {
window.requestAnimationFrame(() => {
if (containerRef.current) {
containerRef.current.style.height = "1px";
containerRef.current.style.height = `${25 + containerRef.current.scrollHeight}px`;
}
});
});

if (textareaRef.current) {
resizeObserver.observe(textareaRef.current);
}

return () => {
if (timerId.current) {
clearTimeout(timerId.current);
sendChange();
}

if (textareaRef.current) {
resizeObserver.unobserve(textareaRef.current);
}
};
}, []);

Expand All @@ -58,9 +40,5 @@ export const DebouncedTextarea: React.FC<DebouncedTextareaProps> = ({ intervalMs
timerId.current = setTimeout(sendChange, intervalMs);
};

return (
<div ref={containerRef}>
<Textarea {...props} autoResize={true} onChange={onChangeDebounce} resizeRef={textareaRef}/>
</div>
);
return <Textarea {...props} autoResize={true} onChange={onChangeDebounce} resizeRef={textareaRef}/>
};

0 comments on commit 6e6fe22

Please sign in to comment.