Skip to content

Instantly share code, notes, and snippets.

@zechdc
Last active April 17, 2024 15:26
Show Gist options
  • Save zechdc/edc9797dcc50df1c9b54f3738a00e7df to your computer and use it in GitHub Desktop.
Save zechdc/edc9797dcc50df1c9b54f3738a00e7df to your computer and use it in GitHub Desktop.
React v18 Autosize Input
// Learn more: https://dev.to/zechdc/autosize-input-field-plain-js-and-react-examples-38kb
import { cn } from "~/lib/shadcnui";
interface InputAutosizeProps
extends React.InputHTMLAttributes<HTMLInputElement> {
value: string;
}
export default function InputAutosize({
className,
value,
...props
}: InputAutosizeProps) {
return (
<div className={cn("grid", className)}>
<span className="invisible" style={{ gridArea: " 1 / 1 " }}>
{!value && "\u00A0"}
{value.replace(/ /g, "\u00A0")}
</span>
<input
size={1}
style={{ gridArea: " 1 / 1 " }}
type="text"
value={value}
className={`border-none bg-transparent focus:outline-none`}
{...props}
/>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment