TextInput

TextInput lets you create accessible text-based inputs like text, email, search and password with a simple API.


Really simple markup

TextInput has possibly the simplest markup you’ll ever for a form input field. The minimum you need is just name and label.

svelte
<TextInput name="name" label="First Name" />

Add hint text and error messages — even both!

Just use the hint and error properties to add them.

svelte
<TextInput
name="email"
type="email"
label="Email address"
hint="Use an email you use, kay?"
/>
<TextInput
name="email"
type="email"
label="Email address"
error="Email already taken"
/>
Hint only
Use an email you use, kay?
Error only
Email already taken
Both hint and error!
Use an email you use, kay?
Email already taken

Built with accessibility in mind

Both hint texts and error messages are linked to the <input> with aria-describedby.

The error message even has a live region attached so screen readers read error messages when they appear.

<TextInput
name="email"
type="email"
label="Email address"
hint="Use an email you use, kay?"
error="Email already taken"
/>
Hint: This will error.
Give this a try!

Add items before or after the input field easily

Just use the before or after slots. Easy peasy.

svelte
<!-- Before slot -->
<TextInput name="x-handle" label="X Username">
<span slot="before">@</span>
</TextInput>
<!-- After slot -->
<TextInput name="search" label="Search" placeholder="Find anything...">
<button slot="after">
<SVG icon="tabler:search" />
</button>
</TextInput>
Before slot
@
After slot

We get all the tiny details right too. Try it out: Focus on the search input, then focus the search button.

You’ll see a nice-looking outline in both cases — and the outline is never includes the divider!

Make elements float on the input field

This is easy too. Just add the Stack attribute and adjust the input field to create sufficient padding, so the input doesn’t get covered by the overlapping element.

<TextInput
Stack
fieldClass="p-4 pr-28"
type="email"
name="email"
label="Email"
placeholder="Your email address"
>
<button class="mr-2" slot="after"> Sign Up </button>
</TextInput>

Oh yeah, try focusing on these ones too! The focus details is done right as well.