Components
npx shadcn@latest add https://21st.dev/r/originui/textareaLoading preview...
"use client";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { ChangeEvent, useId, useRef } from "react";
function Component() {
const id = useId();
const textareaRef = useRef<HTMLTextAreaElement>(null);
const defaultRows = 1;
const maxRows = undefined;
const handleInput = (e: ChangeEvent<HTMLTextAreaElement>) => {
const textarea = e.target;
textarea.style.height = "auto";
const style = window.getComputedStyle(textarea);
const borderHeight = parseInt(style.borderTopWidth) + parseInt(style.borderBottomWidth);
const paddingHeight = parseInt(style.paddingTop) + parseInt(style.paddingBottom);
const lineHeight = parseInt(style.lineHeight);
const maxHeight = maxRows ? lineHeight * maxRows + borderHeight + paddingHeight : Infinity;
const newHeight = Math.min(textarea.scrollHeight + borderHeight, maxHeight);
textarea.style.height = `${newHeight}px`;
};
return (
<div className="space-y-2 min-w-[300px]">
<Label htmlFor={id}>Autogrowing textarea</Label>
<Textarea
id={id}
placeholder="Leave a comment"
ref={textareaRef}
onChange={handleInput}
rows={defaultRows}
className="min-h-[none] resize-none"
/>
</div>
);
}
export { Component };
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...