Components

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.

textfield.svelte
<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 the color with 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

textfield.svelte
<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 when error is true.
  • 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 with max for a limit.
  • max: number => Maximum character count.
textfield.svelte
<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.
textfield.svelte
<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
textfield.svelte
<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

propstypedescriptiondefault
ref $bindableHTMLElementBindable reference to the rendered DOM element.null
classstring | array | objectClasses of the field, with the rules of Svelte (clsx).
stylestringInline style of the field.
s-classstring | array | objectClasses of the field, merged with class.
s-styleobjectInline style as an object of property / value pairs.
value $bindablestring | numberInput value.
typeenum'text''email''password''number''search''tel''url'Input type.'text'
placeholderstringPlaceholder text.
variantenum'filled''tonal''outline''surface''text'Visual style.'filled'
sizeenum'xs''sm''md''lg''xl'Height, padding, font and icon size (in rem).'md'
densityenum'dense''compact''default''comfortable''spacious'Height and padding, from -4px to +4px.'default'
roundedenum0'xs''sm''md''lg''xl''full'Border radius.'md'
errorbooleanActivates error state.false
errorMessagestringMessage shown when error is true.
messagestringHelper text below the field.
messagePrefixstringLeading text in the message row.
messageSuffixstringTrailing text in the message row.
persistentMessagebooleanAlways shows the message, not only on focus.false
counterbooleanShows character counter.false
minnumberMinimum character count.
maxnumberMaximum character count.
clearablebooleanShows a clear button when the field has a value.false
clearLabelstringAccessible name of the clear button.'Clear'
persistentClearbooleanAlways shows the clear button.false
disabledbooleanDisables the input.false
readonlybooleanMakes the input read-only.false
hideSpinButtonsbooleanHides the spin buttons on type="number" inputs.false
prefixstringStatic text before the input value.
suffixstringStatic text after the input value.
idstringInput id attribute, generated when empty.
…attributesHTMLInputAttributesThe other attributes go on the <input> (name, required, oninput…).
colorstring | false | { base?, active? }Color of the field, its use depends on the variant.
prependSnippetContent outside the field, before it.
appendSnippetContent outside the field, after it.
prependInnerSnippetContent inside the field border, before the input.
appendInnerSnippetContent inside the field border, after the input.
elevationElevationStateenum'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.