Textfield component
The kit:textfield component provides a versatile input solution for capturing user data. It supports various input types, validation states, and enhanced features like character counting and clearable functionality. Designed for i increase accessibility and user experience in mind in your interphase.
Instead of reinventing textfield logic, Lapikit Textfield enables you to focus on behavior and data flow.
<script lang="ts">
let name = $state('');
</script>
<div style="max-width: 360px; display: flex; flex-direction: column; gap: 1rem;">
<kit:textfield bind:value={name} placeholder="Full name" />
<kit:textfield placeholder="Email address" type="email" />
<kit:textfield placeholder="Password" type="password" />
</div>
kit:textfield is a Field with a native <input>: the field slots prepend/append (outside the box) and prependInner/appendInner (inside the box). The value prop is bindable.
The attributes of an input (required, pattern, oninput, onchange, aria-label…) go on the <input>, like on a native input. class, style, s-class and s-style stay on the field.
Examples of textfield
Variants
Visual variants match different design systems and UI contexts.
filled (default): surface background (
--kit-color-surface-2), or thecolorwith a contrasted text.tonal: light tint of the color, text in the color.
outline: transparent background with a border.
surface: very light tint of the color with a soft border.
text: transparent background with a line under the input.
variant:
'filled' | 'tonal' | 'outline' | 'surface' | 'text'='filled'color:
string | false | { base?, active? }=undefined
<div style="max-width: 360px; display: flex; flex-direction: column; gap: 1rem;">
{#each ['filled', 'tonal', 'outline', 'surface', 'text'] as variant (variant)}
<kit:textfield {variant} color="accent" placeholder={variant} />
{/each}
</div>
message and error
Show helper text or validation errors below the field. The message is visible on focus unless persistentMessage is set.
- message:
string=> Helper text shown below the field. - errorMessage:
string=> Message shown whenerroristrue. - error:
boolean=> Activates the error state (red border + message). - persistentMessage:
boolean=> Always shows the message (not only on focus). - counter:
boolean=> Shows the character count. Use withmaxfor a limit. - max:
number=> Maximum character count.
<script lang="ts">
let email = $state('');
let hasError = $derived(email.length > 0 && !email.includes('@'));
</script>
<div style="max-width: 360px; display: flex; flex-direction: column; gap: 1rem;">
<kit:textfield
bind:value={email}
placeholder="Email address"
type="email"
message="We'll never share your email."
persistentMessage
error={hasError}
errorMessage="Please enter a valid email address."
/>
<kit:textfield
placeholder="Username"
message="3–20 characters"
max={20}
counter
persistentMessage
/>
</div>
prependInner and appendInner
Place icons or buttons inside the field border using the prependInner and appendInner snippets.
- prependInner:
Snippet | undefined=undefined=> Content inside the field, before the input. - appendInner:
Snippet | undefined=undefined=> Content inside the field, after the input. - prepend:
Snippet | undefined=undefined=> Content outside the field, before it. - append:
Snippet | undefined=undefined=> Content outside the field, after it.
<script lang="ts">
import { Search, Eye, EyeOff } from 'lucide-svelte';
let showPassword = $state(false);
</script>
<div style="max-width: 360px; display: flex; flex-direction: column; gap: 1rem;">
<kit:textfield placeholder="Search...">
{#snippet prependInner()}
<kit:icon><Search /></kit:icon>
{/snippet}
</kit:textfield>
<kit:textfield placeholder="Password" type={showPassword ? 'text' : 'password'}>
{#snippet appendInner()}
<button
type="button"
onclick={() => (showPassword = !showPassword)}
style="display:inline-flex; background:none; border:none; cursor:pointer; color:inherit; padding:0;"
>
<kit:icon>
{#if showPassword}<Eye />{:else}<EyeOff />{/if}
</kit:icon>
</button>
{/snippet}
</kit:textfield>
</div>
clearable
Adds a clear button that appears when the field has a value.
- clearable:
boolean=false - persistentClear:
boolean=false
<script lang="ts">
let value = $state('Hello Lapikit');
</script>
<div style="max-width: 360px;">
<kit:textfield bind:value placeholder="Type something..." clearable />
</div>
Without preprocess Lapikit
If you aren’t using the Lapikit preprocessor in svelte.config.js, import the components directly:
import { Textfield } from 'lapikit/components';API Reference
kit:textfield
| props | type | description | default |
|---|---|---|---|
| ref $bindable | HTMLElement | Bindable reference to the rendered DOM element. | null |
| class | string | array | object | Classes of the field, with the rules of Svelte (clsx). | |
| style | string | Inline style of the field. | |
| s-class | string | array | object | Classes of the field, merged with class. | |
| s-style | object | Inline style as an object of property / value pairs. | |
| value $bindable | string | number | Input value. | |
| type | enum'text''email''password''number''search''tel''url' | Input type. | 'text' |
| placeholder | string | Placeholder text. | |
| variant | enum'filled''tonal''outline''surface''text' | Visual style. | 'filled' |
| size | enum'xs''sm''md''lg''xl' | Height, padding, font and icon size (in rem). | 'md' |
| density | enum'dense''compact''default''comfortable''spacious' | Height and padding, from -4px to +4px. | 'default' |
| rounded | enum0'xs''sm''md''lg''xl''full' | Border radius. | 'md' |
| error | boolean | Activates error state. | false |
| errorMessage | string | Message shown when error is true. | |
| message | string | Helper text below the field. | |
| messagePrefix | string | Leading text in the message row. | |
| messageSuffix | string | Trailing text in the message row. | |
| persistentMessage | boolean | Always shows the message, not only on focus. | false |
| counter | boolean | Shows character counter. | false |
| min | number | Minimum character count. | |
| max | number | Maximum character count. | |
| clearable | boolean | Shows a clear button when the field has a value. | false |
| clearLabel | string | Accessible name of the clear button. | 'Clear' |
| persistentClear | boolean | Always shows the clear button. | false |
| disabled | boolean | Disables the input. | false |
| readonly | boolean | Makes the input read-only. | false |
| hideSpinButtons | boolean | Hides the spin buttons on type="number" inputs. | false |
| prefix | string | Static text before the input value. | |
| suffix | string | Static text after the input value. | |
| id | string | Input id attribute, generated when empty. | |
| …attributes | HTMLInputAttributes | The other attributes go on the <input> (name, required, oninput…). | |
| color | string | false | { base?, active? } | Color of the field, its use depends on the variant. | |
| prepend | Snippet | Content outside the field, before it. | |
| append | Snippet | Content outside the field, after it. | |
| prependInner | Snippet | Content inside the field border, before the input. | |
| appendInner | Snippet | Content inside the field border, after the input. | |
| elevation | ElevationStateenum'0''1''2''3''4''5' | Add shadow effect. |
kit:textfield works great with Form validation and can be enhanced with Icons and Buttons using snippets.
