import {
DetailedHTMLProps,
TextareaHTMLAttributes,
forwardRef,
useEffect,
useImperativeHandle,
useRef,
} from "react";
type AreaProps = DetailedHTMLProps<
TextareaHTMLAttributes,
HTMLTextAreaElement
>;
const ExpandingTextarea = forwardRef(function ExpandingTextarea(
props: AreaProps | { enterKeyHint: string },
ref
) {
const { value } = props as AreaProps;
const textAreaRef = useRef(null);
useImperativeHandle(ref, () => {
return {
focus() {
textAreaRef.current?.focus();
},
};
}, []);
useEffect(() => {
const node = textAreaRef.current;
if (!node) {
return;
}
// Collapse.
node.style.height = "";
if (value && node.scrollHeight > node.clientHeight) {
// Expand.
node.style.height = `${node.scrollHeight + 2}px`;
}
}, [value]);
return (
);
});
export default ExpandingTextarea;