Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Input for Svelte UI SDK

A one-line text input for Svelte on UI SDK, for names, emails, searches and files, labeled with Field and styled for disabled and invalid states.

On this page9 sections

Input is a one-line text box for a name, an email address, a search term, a password or a file picked with the browser's own button. It renders a native <input>, so type, name, required and autofill work the way the browser defines them.

When the answer runs to more than one line, use a Textarea. When an icon, a unit or a button belongs inside the box, build it with Input Group, and give every field a label with Field.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Input anatomy: input, placeholder, value, caret

Usage

<script lang="ts">
import {
  Field, FieldDescription, FieldLabel,
} from "@/components/ui/field";
import { Input } from "@/components/ui/input";
</script>

FieldLabel names the input and FieldDescription adds a hint under it. Field links the label to the input by itself, so the label's for and the input's matching id below are optional on UI SDK.

<Field class="max-w-xs">
  <FieldLabel for="api-key">API Key</FieldLabel>
  <Input id="api-key" placeholder="sk-..." type="password" />
  <FieldDescription>
    Your API key is encrypted and stored securely.
  </FieldDescription>
</Field>

Accessibility

Input renders a native <input>, so typing, selection, autofill and the phone keyboard that type picks all come from the browser, with no keys of its own. What it can't supply is a name. Inside Field, FieldLabel is linked to the input with no ids to write, invalid on Field sets aria-invalid on it, and FieldDescription and FieldError are read out as its description. Where no label shows on screen, set aria-label, because a placeholder disappears as soon as someone types.

Demos

Basic

A lone input with "Enter text" as its placeholder and nothing around it. Use it where something else on screen already says what goes in the box, such as a search field under a page heading, and give it an aria-label so a screen reader has a name too. A field people fill in on a form wants a visible label, as in Field.

Preview width

Field

Username, with its label above the box and a line under it about picking a unique name. Field links the label to the input, so clicking Username focuses the box. This is the shape to start from for any form field. Leave the description out when the label says enough.

Preview width

Field group

A name field, then an email field with a note about where updates go, then Reset and Submit side by side. FieldGroup stacks the fields with one even gap, and the button row is a Field with orientation set to horizontal. The group sits in a <form>, so Reset clears both fields and Submit fires the form's submit handler, which here only calls preventDefault.

Preview width

Disabled

The email box is disabled, so it can't be focused or edited, Tab skips it, and its value stays out of the form data. disabled on Field disables the input inside it and fades the label. Use it for a value this screen shows but doesn't let people change.

Preview width

Invalid

The field is marked invalid with invalid on Field and aria-invalid on the input, which turns the box's border and ring to the destructive color and tells a screen reader the value is wrong. The note under it only says the field has errors. In a real form, say what to fix in a FieldError that shows while the value is wrong.

Preview width

File

A Picture field whose input has type set to file, so the box holds the browser's own file button and the name of the file picked. Clicking the Picture label opens the picker too. Use it for one upload in a settings form. When people should drag files in or see a list of what they added, use File Upload.

Preview width

Inline

A search box and a Search button on one row, laid side by side by orientation set to horizontal on Field. There is no label on screen, so the button and the placeholder tell people what the box is for. Use it in a toolbar or above a table. When the two should read as one joined control, use the Button group layout.

Preview width

Grid

First name and last name side by side, with the FieldGroup turned into a two-column grid. The columns stay at two on every screen width, so keep this layout to short values such as names, or add a breakpoint that stacks them on a phone.

Preview width

Required

The label ends in a red asterisk and the input is required, so a form around it won't submit while the box is empty. required on Field reaches the input, and FieldRequiredIndicator draws the asterisk with aria-hidden, so a screen reader reads the label without it. required is what tells assistive technology the field must be filled in.

Preview width

Badge

Webhook URL, with a Beta Badge at the end of the label row and an input whose type is url. The badge sits inside the label, so a screen reader hears "Webhook URL Beta" as the field's name. Use it to flag a new or experimental setting when a sentence of description would be too much.

Preview width

Input group

The Website URL field shows https:// before the text and an info icon after it, inside one border. That is Input Group, with InputGroupInput as the part people type in and an InputGroupAddon on each side. Use it when a protocol, a unit or an icon belongs to the value, instead of positioning an icon over a plain Input.

Preview width

Button group

A Search label over a text box and a Search button joined edge to edge, with the corners between them squared off, so they read as one control. Button Group does the joining. Use it when the button acts on this field alone, such as a search or a coupon code. A prefix or an icon that belongs inside the box goes in an input group instead.

Preview width

Form

A whole contact form: name, email, a phone number beside a country Select, an address, then Cancel and Submit. One FieldGroup inside a <form> spaces every field, and a plain two-column grid holds the phone row. Name is required, and phone and email set type to tel and email, so phones show a matching keyboard.

Preview width

RTL

The API key field from the top of the page, in Arabic, with dir set to rtl on Field and on the input. The label, the box and the note line up from the right edge, and typing starts there too. Use it when one field is in Arabic or Hebrew on a left-to-right page. When the whole page reads right to left, set dir on the document instead.

Preview width

FAQ

How do I add a label to an Input in Svelte?

Wrap the Input and a FieldLabel in Field, as the Field demo does. Field gives the input an id and points the label at it, so clicking the label focuses the box and a screen reader reads the label as its name.

When the design has no visible label, such as a search box next to a magnifying glass, set aria-label on the Input. A placeholder is no substitute, because it disappears once someone starts typing.

How do I control an Input's value in Svelte?

Bind a variable with bind:value, or pass value and update it in onValueChange, which receives the new text first and the change details, with a cancel(), second. Without value, the Input keeps its own text, and defaultValue sets the text it starts with.

Control it when something reacts while people type, such as a live search or a character count. A value you only need on submit can stay uncontrolled and be read from the form.

How do I show a validation error under an Input?

Set invalid on Field and put the message in a FieldError inside it. FieldError shows only while the field is invalid, and Field adds aria-invalid to the input and names the error in its aria-describedby, so a screen reader reads the message when the box takes focus. Without invalid, the browser's own checks, such as required, mark the field invalid after a failed submit, and a FieldError with no text of its own shows the browser's message.

Say what to fix, such as "Enter an email address with an @", rather than "Invalid input". The Invalid demo shows the styling.

How do I get an Input's value when a form is submitted?

Give the Input a name. It renders a real <input>, so the browser adds its value to the form's data under that name, and your submit handler reads it with FormData. Before it submits, the browser also checks required, pattern and types such as email, and points people to the first field that fails.

A disabled Input is left out of the data, so make a value people may not edit read-only instead when it still has to be sent.

Should a locked Input be disabled or read-only?

Make it read-only when people should still see, select and copy the value, such as an account ID or an API key. A read-only Input stays in the tab order and its value is submitted. A disabled one can't be focused, Tab skips it, and its value is left out of the form data.

Either one can go on Field instead of the Input, and Field passes it to the Input or Textarea inside. A disabled Field also marks every part with data-disabled, which fades the label, while readOnly changes the input alone.

How do I change the height or width of an Input?

Set size to sm, md or lg for an input 28, 32 or 36 pixels tall, with md as the default. The input sets data-size to the same value, for any styles of your own that should follow it.

The width comes from the container. Input is w-full, so it fills whatever holds it. Most demos cap it with max-w-xs on the Field, and the Basic demo, which has no Field, puts the class on the Input itself.

How do I put an icon or a button inside an Input?

Use Input Group: swap the Input for an InputGroupInput inside an InputGroup, then put the icon, the text or an InputGroupButton in an InputGroupAddon. An addon sits at the start of the box until you set its align: inline-end moves it to the end, and block-start or block-end gives it a full-width row above or below. The group draws one border and one focus ring around all of it, as the Input group demo shows.

For a button beside the box rather than inside it, join the two with Button Group, as in the Button group demo.

How do I make a file picker with Input?

Set type to file, as the File demo does, and add accept to narrow the picker, such as image/* for pictures or .pdf for documents. Add multiple to let people choose more than one file. Read the chosen files from the input's files list when it changes, or from FormData on submit under the input's name.

accept only filters the picker, so check the type and size again before you upload. When people should drag files in, or see and remove what they added, use File Upload.

API Reference

Input

<input> data-slot="input"

PropType
value?string | number

The value, for a controlled field. A number is written as its text, as a native input does. Two-way with bind:value.

Type
string | number
defaultValue?string | number

The value it starts with. A form reset returns here.

Type
string | number
onValueChange?(value: string, details: ChangeDetails<InputValueChangeReason>) → void

Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.

Type
(value: string, details: ChangeDetails<InputValueChangeReason>) → void
Parameters

value – string

details – ChangeDetails<InputValueChangeReason>

Returns
void
controller?InputController

Reads and writes the field from anywhere. Create one with createInputController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables the field.

readOnly?boolean

Makes the field read-only.

required?boolean

Makes the field required.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field is disabled, by its own prop or its Field.

data-filled?string

Present while the field holds any text.

data-readonly?string

Present when the field is read-only.

data-required?string

Present when the field is required.