Skip to content

Text Input Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Add a text field to a form. name submits the text with the form, and a <label for> names the field.

--empty
Behavior
Clear button
An SVG in the clear-icon slot.
State
<form class="text-input-basic">
<label for="text-input-name">Full name</label>
<sv-text-input id="text-input-name" name="name" placeholder="Jane Doe"></sv-text-input>
</form>
<script type="application/x-typescript" module>
const form = document.querySelector<HTMLFormElement>(".text-input-basic")!;
form.addEventListener("submit", event => event.preventDefault());
</script>
<style>
.text-input-basic {
display: grid;
gap: 0.5em;
place-items: center;
label {
font-weight: 600;
}
.clear-icon {
inline-size: 1.25em;
fill: none;
stroke: currentcolor;
stroke-width: 2;
stroke-linecap: round;
}
}
</style>

Password

Let users show the password they type. type="password" adds a button that shows and hides the text.

Password
SVGs in the show-password-icon and hide-password-icon slots.
Clear button
<div class="text-input-password">
<label for="text-input-password">Password</label>
<sv-text-input id="text-input-password" type="password" value="correct horse"></sv-text-input>
</div>
<style>
.text-input-password {
display: grid;
gap: 0.5em;
place-items: center;
label {
font-weight: 600;
}
svg {
inline-size: 1.25em;
fill: none;
stroke: currentcolor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
}
</style>

Icon and Unit

Show an icon before the text, or a unit after it. The before and after slots place content inside the field, around the text.

slot="before"
kg slot="after"
<div class="text-input-icon-and-unit">
<div class="variant">
<label for="text-input-product-search">Search products</label>
<sv-text-input id="text-input-product-search" type="search" placeholder="Search products">
<!-- Search -->
<svg aria-hidden="true" slot="before" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<circle cx="11" cy="11" r="7"></circle>
<path d="m20 20-3.5-3.5"></path>
</svg>
</sv-text-input>
<span>slot="before"</span>
</div>
<div class="variant">
<label for="text-input-weight">Weight</label>
<sv-text-input
id="text-input-weight"
allowed-characters="[0-9]"
inputmode="numeric"
placeholder="0"
>
<span slot="after">kg</span>
</sv-text-input>
<span>slot="after"</span>
</div>
</div>
<style>
.text-input-icon-and-unit {
display: grid;
gap: 1.5em;
justify-items: center;
.variant {
display: grid;
gap: 0.4em;
justify-items: center;
label {
font-weight: 600;
}
> span {
font-size: 0.8em;
color: var(--sv-gray-3);
}
}
svg[slot="before"] {
inline-size: 1.25em;
margin-inline-start: 0.75em;
color: var(--sv-gray-3);
fill: none;
stroke: currentcolor;
stroke-width: 2;
stroke-linecap: round;
}
span[slot="after"] {
padding-inline-end: 0.75em;
color: var(--sv-gray-3);
}
}
</style>

Suggestions

Suggest full email addresses while the user types. The suggestions property sets the values that the browser offers in a dropdown, and the input handler rebuilds them from the name typed before the @.

<div class="text-input-suggestions">
<label for="text-input-email">Email</label>
<sv-text-input id="text-input-email" type="email" placeholder="name@example.com"></sv-text-input>
</div>
<script type="application/x-typescript" module>
import type { TextInput } from "@staticview/ui/text-input";
const recentEmails = ["jane.doe@gmail.com", "jane@work.example.com"];
const emailDomains = ["gmail.com", "outlook.com", "yahoo.com", "icloud.com"];
const emailField = document.querySelector<TextInput>(".text-input-suggestions sv-text-input")!;
const updateSuggestions = () => {
const [userName] = emailField.value.split("@");
emailField.suggestions = userName
? emailDomains.map(domain => `${userName}@${domain}`)
: recentEmails;
};
updateSuggestions();
emailField.addEventListener("input", updateSuggestions);
</script>
<style>
.text-input-suggestions {
display: grid;
gap: 0.5em;
place-items: center;
label {
font-weight: 600;
}
}
</style>

Search as You Type

Search while the user types, without a request on every key. The debouncedinput event fires once the user pauses for debounce-delay milliseconds, and loading shows a spinner while the request runs.

<div class="text-input-search-as-you-type">
<label for="text-input-fruit-search">Search fruits</label>
<sv-text-input
id="text-input-fruit-search"
type="search"
clearable
debounce-delay="400"
placeholder="Try a fruit"
></sv-text-input>
<output></output>
</div>
<script type="application/x-typescript" module>
import type { TextInput } from "@staticview/ui/text-input";
const fruits = [
"Apple",
"Apricot",
"Banana",
"Blueberry",
"Cherry",
"Grape",
"Lemon",
"Mango",
"Orange",
"Peach",
];
const searchField = document.querySelector<TextInput>(
".text-input-search-as-you-type sv-text-input"
)!;
const results = document.querySelector<HTMLOutputElement>(
".text-input-search-as-you-type output"
)!;
const getQuery = () => searchField.value.toLowerCase();
// Stands in for a request to your server
const fetchFruits = async (query: string) => {
await new Promise(resolve => setTimeout(resolve, 600));
return fruits.filter(fruit => fruit.toLowerCase().includes(query));
};
searchField.addEventListener("debouncedinput", async () => {
const query = getQuery();
if (!query) {
searchField.loading = false;
results.textContent = "";
return;
}
searchField.loading = true;
const matchingFruits = await fetchFruits(query);
if (query !== getQuery()) return;
searchField.loading = false;
results.textContent = matchingFruits.join(", ") || "No results";
});
</script>
<style>
.text-input-search-as-you-type {
display: grid;
gap: 0.75em;
justify-items: center;
label {
font-weight: 600;
}
output {
min-block-size: 1.5em;
color: var(--sv-gray-3);
}
}
</style>

Allowed Characters

Accept only digits in a verification code field. allowed-characters blocks typed or pasted text that does not match its regular expression.

<div class="text-input-allowed-characters">
<label for="text-input-verification-code">Verification code</label>
<sv-text-input
id="text-input-verification-code"
allowed-characters="[0-9]"
autocomplete="one-time-code"
inputmode="numeric"
maxlength="6"
placeholder="123456"
></sv-text-input>
</div>
<style>
.text-input-allowed-characters {
display: grid;
gap: 0.5em;
place-items: center;
label {
font-weight: 600;
}
}
</style>