import React, { FC, useState, useRef } from "react"; import styled from "styled-components"; import CSS from "csstype"; import { color, outline, typography, borderRadius, padding, } from "../shared/styles"; export interface AutoResizingTextAreaProps { style?: CSS.Properties; id: string; value: string; onChange?: (e: React.ChangeEvent) => void; placeholder?: string; minHeight?: string; maxHeight?: string; } const AutoResizingTextArea: FC = ({ style, id, value, onChange, placeholder = "Tell me your secrets", minHeight = "21.6rem", maxHeight = "60rem", }) => { const textAreaRef = useRef(null); const [divHeight, setDivHeight] = useState(minHeight); const handleChange = (e: React.ChangeEvent) => { const newOrMaxHeight = textAreaRef.current!.scrollHeight < parseInt(maxHeight) ? `${textAreaRef.current!.scrollHeight}px` : maxHeight; setDivHeight(newOrMaxHeight); if (onChange) { onChange(e); } }; return ( ); }; export default AutoResizingTextArea; interface DivProps { height: string; } const StyledDiv = styled.div` height: ${(props) => props.height}; width: 100%; `; const StyledTextArea = styled.textarea` border-radius: ${borderRadius.medium}; border: none; padding: ${padding.medium}; width: 100%; height: 100%; color: ${color.darkblue}; font-size: ${typography.size.medium}; font-family: ${typography.family.primary}; font-weight: ${typography.weight.regular}; appearance: none; &::placeholder { color: ${color.darkblue}; } &:focus, &:focus-visible { outline: ${outline.regular}; } `;