Overview
Get Started
Input
A native input element that automatically works with Field for labeling and validation.
import { Input } from "@/components/ui/input";
export function InputDemo() {
return <Input type="email" placeholder="Email" className="max-w-sm" />;
}Installation
import { cva, type VariantProps } from "class-variance-authority";
import { Input as InputPrimitive } from "@base-ui/react/input";
import { cn } from "@/lib/cn";
const inputSizeVariants = cva("", {
variants: {
size: {
xs: "h-6 rounded-sm px-2 text-xs focus-visible:ring-2",
sm: "h-7 rounded-md px-2 text-[0.8125rem] focus-visible:ring-2",
default: "h-8 rounded-lg px-2.5 py-1",
lg: "h-9",
xl: "h-10 px-3 text-base",
},
},
defaultVariants: {
size: "default",
},
});
type InputSize = VariantProps<typeof inputSizeVariants>["size"] | number;
type InputProps = Omit<React.ComponentProps<typeof InputPrimitive>, "size"> & {
size?: InputSize;
};
function Input({ className, size, ...props }: InputProps) {
return (
<InputPrimitive
data-slot="input"
size={typeof size === "number" ? size : undefined}
className={cn(
"h-8 w-full rounded-lg border border-input bg-transparent px-2.5 py-1 text-sm shadow-xs outline-none transition-[color,box-shadow] selection:bg-primary selection:text-primary-foreground placeholder:text-muted-foreground disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 dark:bg-input/30",
"focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50",
"aria-invalid:border-danger aria-invalid:ring-danger/30",
typeof size === "string" && inputSizeVariants({ size }),
props.type === "search" &&
"[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none",
props.type === "file" &&
"py-0 ps-0 file:me-3 file:h-full file:cursor-pointer file:border-0 file:bg-muted file:px-3 file:font-medium file:text-muted-foreground",
className
)}
{...props}
/>
);
}
export { Input, type InputProps };Usage
import { Input } from "@/components/ui/input";<Input />Examples
Sizes
import { Input } from "@/components/ui/input";
export function InputDemo() {
return (
<div className="flex flex-col gap-4">
<Input placeholder="Name" size="xs" />
<Input placeholder="Name" size="sm" />
<Input placeholder="Name" size="default" />
<Input placeholder="Name" size="lg" />
<Input placeholder="Name" size="xl" />
</div>
);
}File Input
import { Input } from "@/components/ui/input";
export function InputDemo() {
return (
<div className="flex flex-col gap-2">
<label htmlFor="picture">Picture</label>
<Input id="picture" type="file" />
</div>
);
}Disabled
import { Input } from "@/components/ui/input";
export function InputDemo() {
return <Input placeholder="Enter your name" disabled className="max-w-sm" />;
}API Reference
Input Props
Component type props
Prop
Type
Default
Expand prop details
The size of the input component.
Type
'xs' | 'sm' | 'default' | 'lg' | 'xl'Default
'default'