Shadcn Input for React and Tailwind
A field that allows users to enter text.
Installation
bunx --bun shadcn@latest add https://kit.dev/r/input.jsonpnpm dlx shadcn@latest add https://kit.dev/r/input.jsonnpx shadcn@latest add https://kit.dev/r/input.jsonyarn shadcn@latest add https://kit.dev/r/input.jsonInstall the following dependencies:
bun add @ark-ui/react tailwind-variantspnpm add @ark-ui/react tailwind-variantsnpm install @ark-ui/react tailwind-variantsyarn add @ark-ui/react tailwind-variantsCopy and paste the following code into your project.
"use client";
import { FieldInput } from "@ark-ui/react/field";
import type React from "react";
import { tv, type VariantProps } from "tailwind-variants";
import { cn } from "@/lib/utils";
export const inputVariants = tv({
base: [
"peer",
"w-full min-w-0",
"px-3",
"bg-transparent dark:bg-input/30",
"text-base md:text-sm",
"rounded-lg border border-input shadow-xs/5",
"placeholder:text-muted-foreground/64",
"file:inline-flex file:h-7 file:items-center file:border-0",
"file:font-medium file:text-foreground file:text-sm",
"transition-[color,box-shadow]",
"outline-none focus-visible:border-primary focus-visible:ring-[3px] focus-visible:ring-ring/32",
"aria-invalid:border-destructive aria-invalid:text-destructive aria-invalid:ring-[3px] aria-invalid:ring-destructive/24",
"data-invalid:border-destructive data-invalid:text-destructive data-invalid:ring-[3px] data-invalid:ring-destructive/24",
"dark:aria-invalid:border-destructive-foreground dark:aria-invalid:text-destructive-foreground dark:aria-invalid:ring-destructive-foreground/40",
"dark:data-invalid:border-destructive-foreground dark:data-invalid:text-destructive-foreground dark:data-invalid:ring-destructive-foreground/40",
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-64",
"motion-reduce:transition-none!",
],
variants: {
size: {
sm: ["h-7"],
md: ["h-8"],
lg: ["h-9"],
},
},
defaultVariants: {
size: "md",
},
});
export interface InputProps
extends Omit<React.ComponentProps<typeof FieldInput>, "size">,
VariantProps<typeof inputVariants> {}
export const Input = (props: InputProps) => {
const { size = "md", type = "text", className, ...rest } = props;
return (
<FieldInput
className={cn(inputVariants({ size }), className)}
data-size={size}
data-slot="input"
type={type}
{...rest}
/>
);
};Update the import paths to match your project setup.
Usage
import { Input } from "@/components/ui/input";<Input />Controlled
Control the value with value and onChange.
States
Disabled
Use the disabled prop to disable the input.
Invalid
Use the invalid prop to mark the input as invalid.
Sizes
Use the size prop to change the input height.
Small
Medium
Large
Examples
With Field
Use Field components to create an input with a label and a description.
Field Group
Use FieldGroup to show multiple Field blocks and to build forms.
File
Use the type="file" prop to create a file input.
Dedicated File Upload component
For more advanced file upload functionality, use the FileUpload component instead.
Inline
Use the orientation="horizontal" prop to create an inline input.
Grid
Badge
Use Badge in the label to highlight a recommended field.
Input Group
Use InputGroup to create an input group.
Button Group
Use ButtonGroup to create a button group.
API Reference
Input
Text input. Accepts all native input attributes.
| Prop | Type | Default |
|---|---|---|
size | "sm" | "md" | "lg" | "md" |
type | string | text |
className | string | - |
For a complete list of props, see the Ark UI documentation.